AutoSlugField for user and topic #7

This commit is contained in:
nitely 2014-05-22 15:53:51 -03:00
parent 12e14617d9
commit 8da9796f86
9 changed files with 45 additions and 18 deletions

View File

@ -1,9 +1,6 @@
#-*- coding: utf-8 -*-
from django import forms
from django.conf import settings
from django.utils import timezone
from django.utils.text import slugify
from django.utils.translation import ugettext as _
from ..utils.forms import NestedModelChoiceField
@ -44,6 +41,5 @@ class TopicForm(forms.ModelForm):
def save(self, commit=True):
if not self.instance.pk:
self.instance.user = self.user
self.instance.slug = slugify(self.cleaned_data["title"])
return super(TopicForm, self).save(commit)

View File

@ -2,7 +2,6 @@
from django import forms
from django.utils.translation import ugettext as _
from django.utils.text import slugify
from django.contrib.auth import get_user_model
from django.conf import settings
@ -30,7 +29,6 @@ class TopicForPrivateForm(forms.ModelForm):
if not self.instance.pk:
self.instance.user = self.user
self.instance.category = Category.objects.get(pk=settings.ST_TOPIC_PRIVATE_CATEGORY_PK)
self.instance.slug = slugify(self.cleaned_data["title"])
return super(TopicForPrivateForm, self).save(commit)

View File

@ -3,7 +3,6 @@
from django import forms
from django.utils.translation import ugettext as _
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.utils.text import slugify
from django.contrib.auth import get_user_model
@ -39,7 +38,6 @@ class RegistrationForm(UserCreationForm):
raise forms.ValidationError(_("The username is taken."))
def save(self, commit=True):
self.instance.slug = slugify(self.cleaned_data["username"])
self.instance.is_active = False
return super(RegistrationForm, self).save(commit)

View File

@ -10,7 +10,8 @@ from django.utils import timezone
from ..signals.comment import comment_posted
from spirit.signals.topic import topic_viewed
from ..managers.topic import TopicManager
from spirit.managers.topic import TopicManager
from spirit.utils.models import AutoSlugField
class Topic(models.Model):
@ -19,7 +20,7 @@ class Topic(models.Model):
category = models.ForeignKey('spirit.Category', verbose_name=_("category"))
title = models.CharField(_("title"), max_length=75)
slug = models.SlugField(db_index=False, blank=True)
slug = AutoSlugField(populate_from="title", db_index=False, blank=True)
date = models.DateTimeField(_("date"), auto_now_add=True)
last_active = models.DateTimeField(_("last active"), auto_now_add=True)
is_pinned = models.BooleanField(_("pinned"), default=False)

View File

@ -10,12 +10,13 @@ from django.core import validators
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from ..utils.timezone import TIMEZONE_CHOICES
from spirit.utils.timezone import TIMEZONE_CHOICES
from spirit.utils.models import AutoSlugField
class AbstractForumUser(models.Model):
slug = models.SlugField(db_index=False, blank=True)
slug = AutoSlugField(populate_from="username", db_index=False, blank=True)
location = models.CharField(_("location"), max_length=75, blank=True)
last_seen = models.DateTimeField(_("last seen"), auto_now=True)
last_ip = models.GenericIPAddressField(_("last ip"), blank=True, null=True)

View File

@ -39,11 +39,29 @@ class TopicViewTest(TestCase):
topic = Topic.objects.last()
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
# ratelimit
response = self.client.post(reverse('spirit:topic-publish'),
form_data)
self.assertEqual(response.status_code, 200)
# get
response = self.client.get(reverse('spirit:topic-publish'))
self.assertEqual(response.status_code, 200)
def test_topic_publish_long_title(self):
"""
POST, create topic with long title
"""
utils.login(self)
category = utils.create_category()
title = "a" * 75
form_data = {'comment': 'foo', 'title': title, 'category': category.pk}
response = self.client.post(reverse('spirit:topic-publish'),
form_data)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(Topic.objects.all()), 1)
def test_topic_publish_in_category(self):
"""
POST, create topic in category
@ -56,6 +74,7 @@ class TopicViewTest(TestCase):
topic = Topic.objects.last()
expected_url = topic.get_absolute_url()
self.assertRedirects(response, expected_url, status_code=302)
# ratelimit
response = self.client.post(reverse('spirit:topic-publish', kwargs={'category_id': category.pk, }),
form_data)
@ -153,7 +172,8 @@ class TopicViewTest(TestCase):
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk, }))
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk, 'slug': topic.slug}))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['topic'], topic)
def test_topic_detail_view_signals(self):
@ -168,7 +188,8 @@ class TopicViewTest(TestCase):
category = utils.create_category()
topic = utils.create_topic(category=category, user=self.user)
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk, }))
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk, 'slug': topic.slug}))
self.assertEqual(response.status_code, 200)
self.assertSequenceEqual(self._viewed, [response.context['request'], repr(topic)])
def test_topic_detail_view_invalid_slug(self):
@ -179,7 +200,18 @@ class TopicViewTest(TestCase):
category = utils.create_category()
topic = utils.create_topic(category=category)
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk,
'slug': 'bar'}))
'slug': 'bar'}))
self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
def test_topic_detail_view_no_slug(self):
"""
no slug
"""
utils.login(self)
category = utils.create_category()
topic = utils.create_topic(category=category)
response = self.client.get(reverse('spirit:topic-detail', kwargs={'pk': topic.pk,
'slug': ''}))
self.assertRedirects(response, topic.get_absolute_url(), status_code=301)
def test_topic_active_view(self):

View File

@ -99,7 +99,8 @@ class TopicPrivateViewTest(TestCase):
"""
utils.login(self)
private = utils.create_private_topic(user=self.user)
response = self.client.get(reverse('spirit:private-detail', kwargs={'topic_id': private.topic.pk, }))
response = self.client.get(reverse('spirit:private-detail', kwargs={'topic_id': private.topic.pk,
'slug': private.topic.slug}))
self.assertEqual(response.context['topic'], private.topic)
def test_private_access_create(self):

View File

@ -35,9 +35,9 @@ class NestedModelChoiceField(forms.ModelChoiceField):
self.choices = choices
def label_from_instance(self, obj):
level_indicator = ""
level_indicator = u""
if getattr(obj, self.parent_field):
level_indicator = "--- "
level_indicator = u"--- "
return mark_safe(level_indicator + conditional_escape(smart_text(getattr(obj, self.label_field))))