Commit e2d764da2cc5785743d09de1c3f7a79322e41474

Authored by Claude Paroz
2 parents b8a4f8d1 fdd1be60

Merge pull request #66 from jaddison/master

General improvements
1 1 from importlib import import_module
2 2
  3 +from django.apps import apps
3 4 from django.conf import settings
4 5 from django.core import urlresolvers
5 6 from django.core.exceptions import ImproperlyConfigured
... ... @@ -14,9 +15,9 @@ def get_comment_app():
14 15 """
15 16 # Make sure the app's in INSTALLED_APPS
16 17 comments_app = get_comment_app_name()
17   - if comments_app not in settings.INSTALLED_APPS:
  18 + if not apps.is_installed(comments_app):
18 19 raise ImproperlyConfigured(
19   - "The COMMENTS_APP (%r) must be in INSTALLED_APPS" % settings.COMMENTS_APP
  20 + "The COMMENTS_APP (%r) must be in INSTALLED_APPS" % comments_app
20 21 )
21 22
22 23 # Try to import the package
... ...
... ... @@ -9,7 +9,8 @@ from django.utils.encoding import force_text
9 9 from django.utils.text import get_text_list
10 10 from django.utils import timezone
11 11 from django.utils.translation import pgettext_lazy, ungettext, ugettext, ugettext_lazy as _
12   -from django_comments.models import Comment
  12 +
  13 +from . import get_model
13 14
14 15 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
15 16 DEFAULT_COMMENTS_TIMEOUT = getattr(settings, 'COMMENTS_TIMEOUT', (2 * 60 * 60)) # 2h
... ... @@ -127,7 +128,7 @@ class CommentDetailsForm(CommentSecurityForm):
127 128 comment apps should override this, get_comment_create_data, and perhaps
128 129 check_for_duplicate_comment to provide custom comment models.
129 130 """
130   - return Comment
  131 + return get_model()
131 132
132 133 def get_comment_create_data(self):
133 134 """
... ...
... ... @@ -43,7 +43,7 @@ class BaseCommentAbstractModel(models.Model):
43 43
44 44
45 45 @python_2_unicode_compatible
46   -class Comment(BaseCommentAbstractModel):
  46 +class CommentAbstractModel(BaseCommentAbstractModel):
47 47 """
48 48 A user comment about some object.
49 49 """
... ... @@ -76,7 +76,7 @@ class Comment(BaseCommentAbstractModel):
76 76 objects = CommentManager()
77 77
78 78 class Meta:
79   - db_table = "django_comments"
  79 + abstract = True
80 80 ordering = ('submit_date',)
81 81 permissions = [("can_moderate", "Can moderate comments")]
82 82 verbose_name = _('comment')
... ... @@ -88,7 +88,7 @@ class Comment(BaseCommentAbstractModel):
88 88 def save(self, *args, **kwargs):
89 89 if self.submit_date is None:
90 90 self.submit_date = timezone.now()
91   - super(Comment, self).save(*args, **kwargs)
  91 + super(CommentAbstractModel, self).save(*args, **kwargs)
92 92
93 93 def _get_userinfo(self):
94 94 """
... ... @@ -167,6 +167,11 @@ class Comment(BaseCommentAbstractModel):
167 167 return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d
168 168
169 169
  170 +class Comment(CommentAbstractModel):
  171 + class Meta(CommentAbstractModel.Meta):
  172 + db_table = "django_comments"
  173 +
  174 +
170 175 @python_2_unicode_compatible
171 176 class CommentFlag(models.Model):
172 177 """
... ...
... ... @@ -61,21 +61,20 @@ the ``my_comment_app`` directory::
61 61 In the ``models.py`` we'll define a ``CommentWithTitle`` model::
62 62
63 63 from django.db import models
64   - from django_comments.models import Comment
  64 + from django_comments.models import CommentAbstractModel
65 65
66   - class CommentWithTitle(Comment):
  66 + class CommentWithTitle(CommentAbstractModel):
67 67 title = models.CharField(max_length=300)
68 68
69 69 Most custom comment models will subclass the
70   -:class:`~django_comments.models.Comment` model. However,
  70 +:class:`~django_comments.models.CommentAbstractModel` model. However,
71 71 if you want to substantially remove or change the fields available in the
72   -:class:`~django_comments.models.Comment` model, but don't want to
  72 +:class:`~django_comments.models.CommentAbstractModel` model, but don't want to
73 73 rewrite the templates, you could try subclassing from
74 74 ``BaseCommentAbstractModel``.
75 75
76 76 Next, we'll define a custom comment form in ``forms.py``. This is a little more
77 77 tricky: we have to both create a form and override
78   -``CommentForm.get_comment_model()`` and
79 78 ``CommentForm.get_comment_create_data()`` to return deal with our custom title
80 79 field::
81 80
... ... @@ -86,10 +85,6 @@ field::
86 85 class CommentFormWithTitle(CommentForm):
87 86 title = forms.CharField(max_length=300)
88 87
89   - def get_comment_model(self):
90   - # Use our custom comment model instead of the default one.
91   - return CommentWithTitle
92   -
93 88 def get_comment_create_data(self):
94 89 # Use the data of the superclass, and add in the title field
95 90 data = super(CommentFormWithTitle, self).get_comment_create_data()
... ...
Please register or login to post a comment