2008-01-28 13:27:39 +08:00
|
|
|
import datetime
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.comments.managers import CommentManager
|
|
|
|
from django.contrib.contenttypes import generic
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.sites.models import Site
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.db import models
|
Removed oldforms, validators, and related code:
* Removed `Manipulator`, `AutomaticManipulator`, and related classes.
* Removed oldforms specific bits from model fields:
* Removed `validator_list` and `core` arguments from constructors.
* Removed the methods:
* `get_manipulator_field_names`
* `get_manipulator_field_objs`
* `get_manipulator_fields`
* `get_manipulator_new_data`
* `prepare_field_objs_and_params`
* `get_follow`
* Renamed `flatten_data` method to `value_to_string` for better alignment with its use by the serialization framework, which was the only remaining code using `flatten_data`.
* Removed oldforms methods from `django.db.models.Options` class: `get_followed_related_objects`, `get_data_holders`, `get_follow`, and `has_field_type`.
* Removed oldforms-admin specific options from `django.db.models.fields.related` classes: `num_in_admin`, `min_num_in_admin`, `max_num_in_admin`, `num_extra_on_change`, and `edit_inline`.
* Serialization framework
* `Serializer.get_string_value` now calls the model fields' renamed `value_to_string` methods.
* Removed a special-casing of `models.DateTimeField` in `core.serializers.base.Serializer.get_string_value` that's handled by `django.db.models.fields.DateTimeField.value_to_string`.
* Removed `django.core.validators`:
* Moved `ValidationError` exception to `django.core.exceptions`.
* For the couple places that were using validators, brought over the necessary code to maintain the same functionality.
* Introduced a SlugField form field for validation and to compliment the SlugField model field (refs #8040).
* Removed an oldforms-style model creation hack (refs #2160).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8616 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-27 15:19:44 +08:00
|
|
|
from django.core import urlresolvers
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH',3000)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
class BaseCommentAbstractModel(models.Model):
|
|
|
|
"""
|
|
|
|
An abstract base class that any custom comment models probably should
|
|
|
|
subclass.
|
|
|
|
"""
|
2008-09-02 01:56:22 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
# Content-object field
|
2008-09-02 01:56:22 +08:00
|
|
|
content_type = models.ForeignKey(ContentType,
|
|
|
|
related_name="content_type_set_for_%(class)s")
|
2008-08-26 06:14:22 +08:00
|
|
|
object_pk = models.TextField(_('object ID'))
|
|
|
|
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
# Metadata about the comment
|
|
|
|
site = models.ForeignKey(Site)
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
2008-08-26 06:14:22 +08:00
|
|
|
abstract = True
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
def get_content_object_url(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-27 15:04:58 +08:00
|
|
|
Get a URL suitable for redirecting to the content object.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-26 06:14:22 +08:00
|
|
|
return urlresolvers.reverse(
|
2008-08-27 15:04:58 +08:00
|
|
|
"comments-url-redirect",
|
2008-08-26 06:14:22 +08:00
|
|
|
args=(self.content_type_id, self.object_pk)
|
|
|
|
)
|
|
|
|
|
|
|
|
class Comment(BaseCommentAbstractModel):
|
|
|
|
"""
|
|
|
|
A user comment about some object.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Who posted this comment? If ``user`` is set then it was an authenticated
|
2008-09-04 02:44:19 +08:00
|
|
|
# user; otherwise at least user_name should have been set and the comment
|
2008-08-26 06:14:22 +08:00
|
|
|
# was posted by a non-authenticated user.
|
|
|
|
user = models.ForeignKey(User, blank=True, null=True, related_name="%(class)s_comments")
|
|
|
|
user_name = models.CharField(_("user's name"), max_length=50, blank=True)
|
|
|
|
user_email = models.EmailField(_("user's email address"), blank=True)
|
|
|
|
user_url = models.URLField(_("user's URL"), blank=True)
|
|
|
|
|
|
|
|
comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)
|
|
|
|
|
|
|
|
# Metadata about the comment
|
|
|
|
submit_date = models.DateTimeField(_('date/time submitted'), default=None)
|
|
|
|
ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)
|
|
|
|
is_public = models.BooleanField(_('is public'), default=True,
|
|
|
|
help_text=_('Uncheck this box to make the comment effectively ' \
|
|
|
|
'disappear from the site.'))
|
|
|
|
is_removed = models.BooleanField(_('is removed'), default=False,
|
|
|
|
help_text=_('Check this box if the comment is inappropriate. ' \
|
|
|
|
'A "This comment has been removed" message will ' \
|
|
|
|
'be displayed instead.'))
|
|
|
|
|
|
|
|
# Manager
|
|
|
|
objects = CommentManager()
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
2008-08-26 06:14:22 +08:00
|
|
|
db_table = "django_comments"
|
|
|
|
ordering = ('submit_date',)
|
|
|
|
permissions = [("can_moderate", "Can moderate comments")]
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2007-11-30 02:15:31 +08:00
|
|
|
def __unicode__(self):
|
2008-08-26 06:14:22 +08:00
|
|
|
return "%s: %s..." % (self.name, self.comment[:50])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-29 03:28:17 +08:00
|
|
|
def save(self, force_insert=False, force_update=False):
|
2008-08-26 06:14:22 +08:00
|
|
|
if self.submit_date is None:
|
|
|
|
self.submit_date = datetime.datetime.now()
|
2008-08-29 03:28:17 +08:00
|
|
|
super(Comment, self).save(force_insert, force_update)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
def _get_userinfo(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-26 06:14:22 +08:00
|
|
|
Get a dictionary that pulls together information about the poster
|
|
|
|
safely for both authenticated and non-authenticated comments.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
This dict will have ``name``, ``email``, and ``url`` fields.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-26 06:14:22 +08:00
|
|
|
if not hasattr(self, "_userinfo"):
|
|
|
|
self._userinfo = {
|
|
|
|
"name" : self.user_name,
|
|
|
|
"email" : self.user_email,
|
|
|
|
"url" : self.user_url
|
|
|
|
}
|
|
|
|
if self.user_id:
|
|
|
|
u = self.user
|
|
|
|
if u.email:
|
|
|
|
self._userinfo["email"] = u.email
|
|
|
|
|
|
|
|
# If the user has a full name, use that for the user name.
|
|
|
|
# However, a given user_name overrides the raw user.username,
|
|
|
|
# so only use that if this comment has no associated name.
|
|
|
|
if u.get_full_name():
|
|
|
|
self._userinfo["name"] = self.user.get_full_name()
|
|
|
|
elif not self.user_name:
|
|
|
|
self._userinfo["name"] = u.username
|
|
|
|
return self._userinfo
|
|
|
|
userinfo = property(_get_userinfo, doc=_get_userinfo.__doc__)
|
|
|
|
|
|
|
|
def _get_name(self):
|
|
|
|
return self.userinfo["name"]
|
|
|
|
def _set_name(self, val):
|
|
|
|
if self.user_id:
|
|
|
|
raise AttributeError(_("This comment was posted by an authenticated "\
|
|
|
|
"user and thus the name is read-only."))
|
|
|
|
self.user_name = val
|
|
|
|
name = property(_get_name, _set_name, doc="The name of the user who posted this comment")
|
|
|
|
|
|
|
|
def _get_email(self):
|
|
|
|
return self.userinfo["email"]
|
|
|
|
def _set_email(self, val):
|
|
|
|
if self.user_id:
|
|
|
|
raise AttributeError(_("This comment was posted by an authenticated "\
|
|
|
|
"user and thus the email is read-only."))
|
|
|
|
self.user_email = val
|
|
|
|
email = property(_get_email, _set_email, doc="The email of the user who posted this comment")
|
|
|
|
|
|
|
|
def _get_url(self):
|
|
|
|
return self.userinfo["url"]
|
|
|
|
def _set_url(self, val):
|
|
|
|
self.user_url = val
|
|
|
|
url = property(_get_url, _set_url, doc="The URL given by the user who posted this comment")
|
|
|
|
|
|
|
|
def get_absolute_url(self, anchor_pattern="#c%(id)s"):
|
|
|
|
return self.get_content_object_url() + (anchor_pattern % self.__dict__)
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
def get_as_text(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-26 06:14:22 +08:00
|
|
|
Return this comment as plain text. Useful for emails.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-08-26 06:14:22 +08:00
|
|
|
d = {
|
|
|
|
'user': self.user,
|
|
|
|
'date': self.submit_date,
|
|
|
|
'comment': self.comment,
|
|
|
|
'domain': self.site.domain,
|
|
|
|
'url': self.get_absolute_url()
|
|
|
|
}
|
|
|
|
return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d
|
|
|
|
|
|
|
|
class CommentFlag(models.Model):
|
|
|
|
"""
|
|
|
|
Records a flag on a comment. This is intentionally flexible; right now, a
|
|
|
|
flag could be:
|
|
|
|
|
|
|
|
* A "removal suggestion" -- where a user suggests a comment for (potential) removal.
|
|
|
|
|
|
|
|
* A "moderator deletion" -- used when a moderator deletes a comment.
|
|
|
|
|
|
|
|
You can (ab)use this model to add other flags, if needed. However, by
|
|
|
|
design users are only allowed to flag a comment with a given flag once;
|
|
|
|
if you want rating look elsewhere.
|
|
|
|
"""
|
|
|
|
user = models.ForeignKey(User, related_name="comment_flags")
|
|
|
|
comment = models.ForeignKey(Comment, related_name="flags")
|
|
|
|
flag = models.CharField(max_length=30, db_index=True)
|
|
|
|
flag_date = models.DateTimeField(default=None)
|
|
|
|
|
|
|
|
# Constants for flag types
|
|
|
|
SUGGEST_REMOVAL = "removal suggestion"
|
|
|
|
MODERATOR_DELETION = "moderator deletion"
|
|
|
|
MODERATOR_APPROVAL = "moderator approval"
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Meta:
|
2008-08-26 06:14:22 +08:00
|
|
|
db_table = 'django_comment_flags'
|
|
|
|
unique_together = [('user', 'comment', 'flag')]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-11-30 02:15:31 +08:00
|
|
|
def __unicode__(self):
|
2008-08-26 06:14:22 +08:00
|
|
|
return "%s flag of comment ID %s by %s" % \
|
|
|
|
(self.flag, self.comment_id, self.user.username)
|
2008-01-28 13:27:39 +08:00
|
|
|
|
2008-08-29 03:28:17 +08:00
|
|
|
def save(self, force_insert=False, force_update=False):
|
2008-08-26 06:14:22 +08:00
|
|
|
if self.flag_date is None:
|
|
|
|
self.flag_date = datetime.datetime.now()
|
2008-08-29 03:28:17 +08:00
|
|
|
super(CommentFlag, self).save(force_insert, force_update)
|