From 0bc849ff89bc7cbc4e9133df297de3542240a21a Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 14 Jul 2005 18:20:03 +0000 Subject: Created django.contrib and moved comments into it git-svn-id: http://code.djangoproject.com/svn/django/trunk@28 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/urls/comments.py | 12 - django/contrib/__init__.py | 0 django/contrib/comments/__init__.py | 0 django/contrib/comments/models/__init__.py | 1 + django/contrib/comments/models/comments.py | 281 ++++++++++++++++++ django/contrib/comments/templatetags/__init__.py | 0 django/contrib/comments/templatetags/comments.py | 331 +++++++++++++++++++++ django/contrib/comments/urls/__init__.py | 0 django/contrib/comments/urls/comments.py | 12 + django/contrib/comments/views/__init__.py | 0 django/contrib/comments/views/comments.py | 347 +++++++++++++++++++++++ django/contrib/comments/views/karma.py | 34 +++ django/contrib/comments/views/userflags.py | 82 ++++++ django/models/__init__.py | 2 +- django/models/comments.py | 281 ------------------ django/templatetags/comments.py | 331 --------------------- django/views/comments/__init__.py | 0 django/views/comments/comments.py | 347 ----------------------- django/views/comments/karma.py | 34 --- django/views/comments/userflags.py | 82 ------ 20 files changed, 1089 insertions(+), 1088 deletions(-) delete mode 100644 django/conf/urls/comments.py create mode 100644 django/contrib/__init__.py create mode 100644 django/contrib/comments/__init__.py create mode 100644 django/contrib/comments/models/__init__.py create mode 100644 django/contrib/comments/models/comments.py create mode 100644 django/contrib/comments/templatetags/__init__.py create mode 100644 django/contrib/comments/templatetags/comments.py create mode 100644 django/contrib/comments/urls/__init__.py create mode 100644 django/contrib/comments/urls/comments.py create mode 100644 django/contrib/comments/views/__init__.py create mode 100644 django/contrib/comments/views/comments.py create mode 100644 django/contrib/comments/views/karma.py create mode 100644 django/contrib/comments/views/userflags.py delete mode 100644 django/models/comments.py delete mode 100644 django/templatetags/comments.py delete mode 100644 django/views/comments/__init__.py delete mode 100644 django/views/comments/comments.py delete mode 100644 django/views/comments/karma.py delete mode 100644 django/views/comments/userflags.py diff --git a/django/conf/urls/comments.py b/django/conf/urls/comments.py deleted file mode 100644 index f117e8924a..0000000000 --- a/django/conf/urls/comments.py +++ /dev/null @@ -1,12 +0,0 @@ -from django.conf.urls.defaults import * - -urlpatterns = patterns('django.views', - (r'^post/$', 'comments.comments.post_comment'), - (r'^postfree/$', 'comments.comments.post_free_comment'), - (r'^posted/$', 'comments.comments.comment_was_posted'), - (r'^karma/vote/(?P\d+)/(?Pup|down)/$', 'comments.karma.vote'), - (r'^flag/(?P\d+)/$', 'comments.userflags.flag'), - (r'^flag/(?P\d+)/done/$', 'comments.userflags.flag_done'), - (r'^delete/(?P\d+)/$', 'comments.userflags.delete'), - (r'^delete/(?P\d+)/done/$', 'comments.userflags.delete_done'), -) diff --git a/django/contrib/__init__.py b/django/contrib/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/comments/__init__.py b/django/contrib/comments/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/comments/models/__init__.py b/django/contrib/comments/models/__init__.py new file mode 100644 index 0000000000..2bf3cc96cc --- /dev/null +++ b/django/contrib/comments/models/__init__.py @@ -0,0 +1 @@ +__all__ = ['comments'] diff --git a/django/contrib/comments/models/comments.py b/django/contrib/comments/models/comments.py new file mode 100644 index 0000000000..61988595db --- /dev/null +++ b/django/contrib/comments/models/comments.py @@ -0,0 +1,281 @@ +from django.core import meta +from django.models import auth, core + +class Comment(meta.Model): + db_table = 'comments' + fields = ( + meta.ForeignKey(auth.User, raw_id_admin=True), + meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), + meta.IntegerField('object_id', 'object ID'), + meta.CharField('headline', 'headline', maxlength=255, blank=True), + meta.TextField('comment', 'comment', maxlength=3000), + meta.PositiveSmallIntegerField('rating1', 'rating #1', blank=True, null=True), + meta.PositiveSmallIntegerField('rating2', 'rating #2', blank=True, null=True), + meta.PositiveSmallIntegerField('rating3', 'rating #3', blank=True, null=True), + meta.PositiveSmallIntegerField('rating4', 'rating #4', blank=True, null=True), + meta.PositiveSmallIntegerField('rating5', 'rating #5', blank=True, null=True), + meta.PositiveSmallIntegerField('rating6', 'rating #6', blank=True, null=True), + meta.PositiveSmallIntegerField('rating7', 'rating #7', blank=True, null=True), + meta.PositiveSmallIntegerField('rating8', 'rating #8', blank=True, null=True), + # This field designates whether to use this row's ratings in + # aggregate functions (summaries). We need this because people are + # allowed to post multiple review on the same thing, but the system + # will only use the latest one (with valid_rating=True) in tallying + # the reviews. + meta.BooleanField('valid_rating', 'is valid rating'), + meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), + meta.BooleanField('is_public', 'is public'), + meta.IPAddressField('ip_address', 'IP address', blank=True, null=True), + meta.BooleanField('is_removed', 'is removed', + help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.'), + meta.ForeignKey(core.Site), + ) + module_constants = { + # used as shared secret between comment form and comment-posting script + 'COMMENT_SALT': 'ijw2f3_MRS_PIGGY_LOVES_KERMIT_avo#*5vv0(23j)(*', + + # min. and max. allowed dimensions for photo resizing (in pixels) + 'MIN_PHOTO_DIMENSION': 5, + 'MAX_PHOTO_DIMENSION': 1000, + + # option codes for comment-form hidden fields + 'PHOTOS_REQUIRED': 'pr', + 'PHOTOS_OPTIONAL': 'pa', + 'RATINGS_REQUIRED': 'rr', + 'RATINGS_OPTIONAL': 'ra', + 'IS_PUBLIC': 'ip', + } + ordering = (('submit_date', 'DESC'),) + admin = meta.Admin( + fields = ( + (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), + ('Content', {'fields': ('user_id', 'headline', 'comment')}), + ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), + ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), + ), + list_display = ('user_id', 'submit_date', 'content_type_id', 'get_content_object'), + list_filter = ('submit_date',), + date_hierarchy = 'submit_date', + search_fields = ('comment', 'user__username'), + ) + + def __repr__(self): + return "%s: %s..." % (self.get_user().username, self.comment[:100]) + + def get_absolute_url(self): + return self.get_content_object().get_absolute_url() + "#c" + str(self.id) + + def get_crossdomain_url(self): + return "/r/%d/%d/" % (self.content_type_id, self.object_id) + + def get_flag_url(self): + return "/comments/flag/%s/" % self.id + + def get_deletion_url(self): + return "/comments/delete/%s/" % self.id + + def get_content_object(self): + """ + Returns the object that this comment is a comment on. Returns None if + the object no longer exists. + """ + from django.core.exceptions import ObjectDoesNotExist + try: + return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) + except ObjectDoesNotExist: + return None + + get_content_object.short_description = 'Content object' + + def _fill_karma_cache(self): + "Helper function that populates good/bad karma caches" + good, bad = 0, 0 + for k in self.get_karmascore_list(): + if k.score == -1: + bad +=1 + elif k.score == 1: + good +=1 + self._karma_total_good, self._karma_total_bad = good, bad + + def get_good_karma_total(self): + if not hasattr(self, "_karma_total_good"): + self._fill_karma_cache() + return self._karma_total_good + + def get_bad_karma_total(self): + if not hasattr(self, "_karma_total_bad"): + self._fill_karma_cache() + return self._karma_total_bad + + def get_karma_total(self): + if not hasattr(self, "_karma_total_good") or not hasattr(self, "_karma_total_bad"): + self._fill_karma_cache() + return self._karma_total_good + self._karma_total_bad + + def get_as_text(self): + return 'Posted by %s at %s\n\n%s\n\nhttp://%s%s' % \ + (self.get_user().username, self.submit_date, + self.comment, self.get_site().domain, self.get_absolute_url()) + + def _module_get_security_hash(options, photo_options, rating_options, target): + """ + Returns the MD5 hash of the given options (a comma-separated string such as + 'pa,ra') and target (something like 'lcom.eventtimes:5157'). Used to + validate that submitted form options have not been tampered-with. + """ + import md5 + return md5.new(options + photo_options + rating_options + target + COMMENT_SALT).hexdigest() + + def _module_get_rating_options(rating_string): + """ + Given a rating_string, this returns a tuple of (rating_range, options). + >>> s = "scale:1-10|First_category|Second_category" + >>> get_rating_options(s) + ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ['First category', 'Second category']) + """ + rating_range, options = rating_string.split('|', 1) + rating_range = range(int(rating_range[6:].split('-')[0]), int(rating_range[6:].split('-')[1])+1) + choices = [c.replace('_', ' ') for c in options.split('|')] + return rating_range, choices + + def _module_get_list_with_karma(**kwargs): + """ + Returns a list of Comment objects matching the given lookup terms, with + _karma_total_good and _karma_total_bad filled. + """ + kwargs.setdefault('select', {}) + kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=1' + kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=-1' + return get_list(**kwargs) + + def _module_user_is_moderator(user): + from django.conf.settings import COMMENTS_MODERATORS_GROUP + if user.is_superuser: + return True + for g in user.get_group_list(): + if g.id == COMMENTS_MODERATORS_GROUP: + return True + return False + +class FreeComment(meta.Model): + "A FreeComment is a comment by a non-registered user" + db_table = 'comments_free' + fields = ( + meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), + meta.IntegerField('object_id', 'object ID'), + meta.TextField('comment', 'comment', maxlength=3000), + meta.CharField('person_name', "person's name", maxlength=50), + meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), + meta.BooleanField('is_public', 'is public'), + meta.IPAddressField('ip_address', 'IP address'), + # TODO: Change this to is_removed, like Comment + meta.BooleanField('approved', 'approved by staff'), + meta.ForeignKey(core.Site), + ) + ordering = (('submit_date', 'DESC'),) + admin = meta.Admin( + fields = ( + (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), + ('Content', {'fields': ('person_name', 'comment')}), + ('Meta', {'fields': ('submit_date', 'is_public', 'ip_address', 'approved')}), + ), + list_display = ('person_name', 'submit_date', 'content_type_id', 'get_content_object'), + list_filter = ('submit_date',), + date_hierarchy = 'submit_date', + search_fields = ('comment', 'person_name'), + ) + + def __repr__(self): + return "%s: %s..." % (self.person_name, self.comment[:100]) + + def get_content_object(self): + """ + Returns the object that this comment is a comment on. Returns None if + the object no longer exists. + """ + from django.core.exceptions import ObjectDoesNotExist + try: + return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) + except ObjectDoesNotExist: + return None + + get_content_object.short_description = 'Content object' + +class KarmaScore(meta.Model): + module_name = 'karma' + fields = ( + meta.ForeignKey(auth.User), + meta.ForeignKey(Comment), + meta.SmallIntegerField('score', 'score', db_index=True), + meta.DateTimeField('scored_date', 'date scored', auto_now=True), + ) + unique_together = (('user_id', 'comment_id'),) + module_constants = { + # what users get if they don't have any karma + 'DEFAULT_KARMA': 5, + 'KARMA_NEEDED_BEFORE_DISPLAYED': 3, + } + + def __repr__(self): + return "%d rating by %s" % (self.score, self.get_user()) + + def _module_vote(user_id, comment_id, score): + try: + karma = get_object(comment_id__exact=comment_id, user_id__exact=user_id) + except KarmaScoreDoesNotExist: + karma = KarmaScore(None, user_id, comment_id, score, datetime.datetime.now()) + karma.save() + else: + karma.score = score + karma.scored_date = datetime.datetime.now() + karma.save() + + def _module_get_pretty_score(score): + """ + Given a score between -1 and 1 (inclusive), returns the same score on a + scale between 1 and 10 (inclusive), as an integer. + """ + if score is None: + return DEFAULT_KARMA + return int(round((4.5 * score) + 5.5)) + +class UserFlag(meta.Model): + db_table = 'comments_user_flags' + fields = ( + meta.ForeignKey(auth.User), + meta.ForeignKey(Comment), + meta.DateTimeField('flag_date', 'date flagged', auto_now_add=True), + ) + unique_together = (('user_id', 'comment_id'),) + + def __repr__(self): + return "Flag by %r" % self.get_user() + + def _module_flag(comment, user): + """ + Flags the given comment by the given user. If the comment has already + been flagged by the user, or it was a comment posted by the user, + nothing happens. + """ + if int(comment.user_id) == int(user.id): + return # A user can't flag his own comment. Fail silently. + try: + f = get_object(user_id__exact=user.id, comment_id__exact=comment.id) + except UserFlagDoesNotExist: + from django.core.mail import mail_managers + f = UserFlag(None, user.id, comment.id, None) + message = 'This comment was flagged by %s:\n\n%s' % (user.username, comment.get_as_text()) + mail_managers('Comment flagged', message, fail_silently=True) + f.save() + +class ModeratorDeletion(meta.Model): + db_table = 'comments_moderator_deletions' + fields = ( + meta.ForeignKey(auth.User, verbose_name='moderator'), + meta.ForeignKey(Comment), + meta.DateTimeField('deletion_date', 'date deleted', auto_now_add=True), + ) + unique_together = (('user_id', 'comment_id'),) + + def __repr__(self): + return "Moderator deletion by %r" % self.get_user() diff --git a/django/contrib/comments/templatetags/__init__.py b/django/contrib/comments/templatetags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py new file mode 100644 index 0000000000..adfd3cedac --- /dev/null +++ b/django/contrib/comments/templatetags/comments.py @@ -0,0 +1,331 @@ +"Custom template tags for user comments" + +from django.core import template +from django.core.exceptions import ObjectDoesNotExist +from django.models.comments import comments, freecomments +from django.models.core import contenttypes +import re + +COMMENT_FORM = ''' +{% if display_form %} +
+ +{% if user.is_anonymous %} +

Username:
Password: (Forgotten your password?)

+{% else %} +

Username: {{ user.username }} (Log out)

+{% endif %} + +{% if ratings_optional or ratings_required %} +

Ratings ({% if ratings_required %}Required{% else %}Optional{% endif %}):

+ +{% for value in rating_range %}{% endfor %} +{% for rating in rating_choices %} +{% for value in rating_range %}{% endfor %} +{% endfor %} +
 {{ value }}
{{ rating }}
+ +{% endif %} + +{% if photos_optional or photos_required %} +

Post a photo ({% if photos_required %}Required{% else %}Optional{% endif %}):

+ +{% endif %} + +

Comment:

+ + + + +

+
+{% endif %} +''' + +FREE_COMMENT_FORM = ''' +{% if display_form %} +
+

Your name:

+

Comment:

+ + + +

+
+{% endif %} +''' + +class CommentFormNode(template.Node): + def __init__(self, content_type, obj_id_lookup_var, obj_id, free, + photos_optional=False, photos_required=False, photo_options='', + ratings_optional=False, ratings_required=False, rating_options='', + is_public=True): + self.content_type = content_type + self.obj_id_lookup_var, self.obj_id, self.free = obj_id_lookup_var, obj_id, free + self.photos_optional, self.photos_required = photos_optional, photos_required + self.ratings_optional, self.ratings_required = ratings_optional, ratings_required + self.photo_options, self.rating_options = photo_options, rating_options + self.is_public = is_public + + def render(self, context): + from django.utils.text import normalize_newlines + import base64 + context.push() + if self.obj_id_lookup_var is not None: + try: + self.obj_id = template.resolve_variable(self.obj_id_lookup_var, context) + except template.VariableDoesNotExist: + return '' + # Validate that this object ID is valid for this content-type. + # We only have to do this validation if obj_id_lookup_var is provided, + # because do_comment_form() validates hard-coded object IDs. + try: + self.content_type.get_object_for_this_type(id__exact=self.obj_id) + except ObjectDoesNotExist: + context['display_form'] = False + else: + context['display_form'] = True + context['target'] = '%s:%s' % (self.content_type.id, self.obj_id) + options = [] + for var, abbr in (('photos_required', comments.PHOTOS_REQUIRED), + ('photos_optional', comments.PHOTOS_OPTIONAL), + ('ratings_required', comments.RATINGS_REQUIRED), + ('ratings_optional', comments.RATINGS_OPTIONAL), + ('is_public', comments.IS_PUBLIC)): + context[var] = getattr(self, var) + if getattr(self, var): + options.append(abbr) + context['options'] = ','.join(options) + if self.free: + context['hash'] = comments.get_security_hash(context['options'], '', '', context['target']) + default_form = FREE_COMMENT_FORM + else: + context['photo_options'] = self.photo_options + context['rating_options'] = normalize_newlines(base64.encodestring(self.rating_options).strip()) + if self.rating_options: + context['rating_range'], context['rating_choices'] = comments.get_rating_options(self.rating_options) + context['hash'] = comments.get_security_hash(context['options'], context['photo_options'], context['rating_options'], context['target']) + default_form = COMMENT_FORM + output = template.Template(default_form).render(context) + context.pop() + return output + +class CommentCountNode(template.Node): + def __init__(self, package, module, context_var_name, obj_id, var_name, free): + self.package, self.module = package, module + self.context_var_name, self.obj_id = context_var_name, obj_id + self.var_name, self.free = var_name, free + + def render(self, context): + from django.conf.settings import SITE_ID + get_count_function = self.free and freecomments.get_count or comments.get_count + if self.context_var_name is not None: + self.obj_id = template.resolve_variable(self.context_var_name, context) + comment_count = get_count_function(object_id__exact=self.obj_id, + content_type__package__label__exact=self.package, + content_type__python_module_name__exact=self.module, site_id__exact=SITE_ID) + context[self.var_name] = comment_count + return '' + +class CommentListNode(template.Node): + def __init__(self, package, module, context_var_name, obj_id, var_name, free): + self.package, self.module = package, module + self.context_var_name, self.obj_id = context_var_name, obj_id + self.var_name, self.free = var_name, free + + def render(self, context): + from django.conf.settings import COMMENTS_BANNED_USERS_GROUP, SITE_ID + get_list_function = self.free and freecomments.get_list or comments.get_list_with_karma + if self.context_var_name is not None: + try: + self.obj_id = template.resolve_variable(self.context_var_name, context) + except template.VariableDoesNotExist: + return '' + kwargs = { + 'object_id__exact': self.obj_id, + 'content_type__package__label__exact': self.package, + 'content_type__python_module_name__exact': self.module, + 'site_id__exact': SITE_ID, + 'select_related': True, + 'order_by': (('submit_date', 'ASC'),), + } + if not self.free and COMMENTS_BANNED_USERS_GROUP: + kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_users_groups WHERE group_id = %s)' % COMMENTS_BANNED_USERS_GROUP} + comment_list = get_list_function(**kwargs) + + if not self.free: + if context.has_key('user') and not context['user'].is_anonymous(): + user_id = context['user'].id + context['user_can_moderate_comments'] = comments.user_is_moderator(context['user']) + else: + user_id = None + context['user_can_moderate_comments'] = False + # Only display comments by banned users to those users themselves. + if COMMENTS_BANNED_USERS_GROUP: + comment_list = [c for c in comment_list if not c.is_hidden or (user_id == c.user_id)] + + context[self.var_name] = comment_list + return '' + +class DoCommentForm: + """ + Displays a comment form for the given params. Syntax: + {% comment_form for [pkg].[py_module_name] [context_var_containing_obj_id] with [list of options] %} + Example usage: + {% comment_form for lcom.eventtimes event.id with is_public yes photos_optional thumbs,200,400 ratings_optional scale:1-5|first_option|second_option %} + [context_var_containing_obj_id] can be a hard-coded integer or a variable containing the ID. + """ + def __init__(self, free, tag_name): + self.free, self.tag_name = free, tag_name + + def __call__(self, parser, token): + tokens = token.contents.split() + if len(tokens) < 4: + raise template.TemplateSyntaxError, "'%s' tag requires at least 3 arguments" % self.tag_name + if tokens[1] != 'for': + raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name + try: + package, module = tokens[2].split('.') + except ValueError: # unpack list of wrong size + raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name + try: + content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) + except contenttypes.ContentTypeDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) + obj_id_lookup_var, obj_id = None, None + if tokens[3].isdigit(): + obj_id = tokens[3] + try: # ensure the object ID is valid + content_type.get_object_for_this_type(id__exact=obj_id) + except ObjectDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) + else: + obj_id_lookup_var = tokens[3] + kwargs = {} + if len(tokens) > 4: + if tokens[4] != 'with': + raise template.TemplateSyntaxError, "Fourth argument in '%s' tag must be 'with'" % self.tag_name + for option, args in zip(tokens[5::2], tokens[6::2]): + if option in ('photos_optional', 'photos_required') and not self.free: + # VALIDATION ############################################## + option_list = args.split(',') + if len(option_list) % 3 != 0: + raise template.TemplateSyntaxError, "Incorrect number of comma-separated arguments to '%s' tag" % self.tag_name + for opt in option_list[::3]: + if not opt.isalnum(): + raise template.TemplateSyntaxError, "Invalid photo directory name in '%s' tag: '%s'" % (self.tag_name, opt) + for opt in option_list[1::3] + option_list[2::3]: + if not opt.isdigit() or not (comments.MIN_PHOTO_DIMENSION <= int(opt) <= comments.MAX_PHOTO_DIMENSION): + raise template.TemplateSyntaxError, "Invalid photo dimension in '%s' tag: '%s'. Only values between %s and %s are allowed." % (self.tag_name, opt, comments.MIN_PHOTO_DIMENSION, comments.MAX_PHOTO_DIMENSION) + # VALIDATION ENDS ######################################### + kwargs[option] = True + kwargs['photo_options'] = args + elif option in ('ratings_optional', 'ratings_required') and not self.free: + # VALIDATION ############################################## + if 2 < len(args.split('|')) > 9: + raise template.TemplateSyntaxError, "Incorrect number of '%s' options in '%s' tag. Use between 2 and 8." % (option, self.tag_name) + if re.match('^scale:\d+\-\d+\:$', args.split('|')[0]): + raise template.TemplateSyntaxError, "Invalid 'scale' in '%s' tag's '%s' options" % (self.tag_name, option) + # VALIDATION ENDS ######################################### + kwargs[option] = True + kwargs['rating_options'] = args + elif option in ('is_public'): + kwargs[option] = (args == 'true') + else: + raise template.TemplateSyntaxError, "'%s' tag got invalid parameter '%s'" % (self.tag_name, option) + return CommentFormNode(content_type, obj_id_lookup_var, obj_id, self.free, **kwargs) + +class DoCommentCount: + """ + Gets comment count for the given params and populates the template context + with a variable containing that value, whose name is defined by the 'as' + clause. Syntax: + {% get_comment_count for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname] %} + Example usage: + {% get_comment_count for lcom.eventtimes event.id as comment_count %} + Note: [context_var_containing_obj_id] can also be a hard-coded integer, like this: + {% get_comment_count for lcom.eventtimes 23 as comment_count %} + """ + def __init__(self, free, tag_name): + self.free, self.tag_name = free, tag_name + + def __call__(self, parser, token): + tokens = token.contents.split() + # Now tokens is a list like this: + # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] + if len(tokens) != 6: + raise template.TemplateSyntaxError, "%s block tag requires 5 arguments" % self.tag_name + if tokens[1] != 'for': + raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name + try: + package, module = tokens[2].split('.') + except ValueError: # unpack list of wrong size + raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name + try: + content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) + except contenttypes.ContentTypeDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) + var_name, obj_id = None, None + if tokens[3].isdigit(): + obj_id = tokens[3] + try: # ensure the object ID is valid + content_type.get_object_for_this_type(id__exact=obj_id) + except ObjectDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) + else: + var_name = tokens[3] + if tokens[4] != 'as': + raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name + return CommentCountNode(package, module, var_name, obj_id, tokens[5], self.free) + +class DoGetCommentList: + """ + Gets comments for the given params and populates the template context with + a special comment_package variable, whose name is defined by the 'as' + clause. Syntax: + {% get_comment_list for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname] %} + Example usage: + {% get_comment_list for lcom.eventtimes event.id as comment_list %} + Note: [context_var_containing_obj_id] can also be a hard-coded integer, like this: + {% get_comment_list for lcom.eventtimes 23 as comment_list %} + """ + def __init__(self, free, tag_name): + self.free, self.tag_name = free, tag_name + + def __call__(self, parser, token): + tokens = token.contents.split() + # Now tokens is a list like this: + # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] + if len(tokens) != 6: + raise template.TemplateSyntaxError, "%s block tag requires 5 arguments" % self.tag_name + if tokens[1] != 'for': + raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name + try: + package, module = tokens[2].split('.') + except ValueError: # unpack list of wrong size + raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name + try: + content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) + except contenttypes.ContentTypeDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) + var_name, obj_id = None, None + if tokens[3].isdigit(): + obj_id = tokens[3] + try: # ensure the object ID is valid + content_type.get_object_for_this_type(id__exact=obj_id) + except ObjectDoesNotExist: + raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) + else: + var_name = tokens[3] + if tokens[4] != 'as': + raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name + return CommentListNode(package, module, var_name, obj_id, tokens[5], self.free) + +# registration comments +template.register_tag('get_comment_list', DoGetCommentList(free=False, tag_name='get_comment_list')) +template.register_tag('comment_form', DoCommentForm(free=False, tag_name='comment_form')) +template.register_tag('get_comment_count', DoCommentCount(free=False, tag_name='get_comment_count')) +# free comments +template.register_tag('get_free_comment_list', DoGetCommentList(free=True, tag_name='get_free_comment_list')) +template.register_tag('free_comment_form', DoCommentForm(free=True, tag_name='free_comment_form')) +template.register_tag('get_free_comment_count', DoCommentCount(free=True, tag_name='get_free_comment_count')) diff --git a/django/contrib/comments/urls/__init__.py b/django/contrib/comments/urls/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/comments/urls/comments.py b/django/contrib/comments/urls/comments.py new file mode 100644 index 0000000000..f117e8924a --- /dev/null +++ b/django/contrib/comments/urls/comments.py @@ -0,0 +1,12 @@ +from django.conf.urls.defaults import * + +urlpatterns = patterns('django.views', + (r'^post/$', 'comments.comments.post_comment'), + (r'^postfree/$', 'comments.comments.post_free_comment'), + (r'^posted/$', 'comments.comments.comment_was_posted'), + (r'^karma/vote/(?P\d+)/(?Pup|down)/$', 'comments.karma.vote'), + (r'^flag/(?P\d+)/$', 'comments.userflags.flag'), + (r'^flag/(?P\d+)/done/$', 'comments.userflags.flag_done'), + (r'^delete/(?P\d+)/$', 'comments.userflags.delete'), + (r'^delete/(?P\d+)/done/$', 'comments.userflags.delete_done'), +) diff --git a/django/contrib/comments/views/__init__.py b/django/contrib/comments/views/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/comments/views/comments.py b/django/contrib/comments/views/comments.py new file mode 100644 index 0000000000..ffad921ad3 --- /dev/null +++ b/django/contrib/comments/views/comments.py @@ -0,0 +1,347 @@ +from django.core import formfields, template_loader, validators +from django.core.mail import mail_admins, mail_managers +from django.core.exceptions import Http404, ObjectDoesNotExist +from django.core.extensions import CMSContext as Context +from django.models.auth import sessions +from django.models.comments import comments, freecomments +from django.models.core import contenttypes +from django.parts.auth.formfields import AuthenticationForm +from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect +from django.utils.text import normalize_newlines +from django.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID +import base64, datetime + +COMMENTS_PER_PAGE = 20 + +class PublicCommentManipulator(AuthenticationForm): + "Manipulator that handles public registered comments" + def __init__(self, user, ratings_required, ratings_range, num_rating_choices): + AuthenticationForm.__init__(self) + self.ratings_range, self.num_rating_choices = ratings_range, num_rating_choices + choices = [(c, c) for c in ratings_range] + def get_validator_list(rating_num): + if rating_num <= num_rating_choices: + return [validators.RequiredIfOtherFieldsGiven(['rating%d' % i for i in range(1, 9) if i != rating_num], "This rating is required because you've entered at least one other rating.")] + else: + return [] + self.fields.extend([ + formfields.LargeTextField(field_name="comment", maxlength=3000, is_required=True, + validator_list=[self.hasNoProfanities]), + formfields.RadioSelectField(field_name="rating1", choices=choices, + is_required=ratings_required and num_rating_choices > 0, + validator_list=get_validator_list(1), + ), + formfields.RadioSelectField(field_name="rating2", choices=choices, + is_required=ratings_required and num_rating_choices > 1, + validator_list=get_validator_list(2), + ), + formfields.RadioSelectField(field_name="rating3", choices=choices, + is_required=ratings_required and num_rating_choices > 2, + validator_list=get_validator_list(3), + ), + formfields.RadioSelectField(field_name="rating4", choices=choices, + is_required=ratings_required and num_rating_choices > 3, + validator_list=get_validator_list(4), + ), + formfields.RadioSelectField(field_name="rating5", choices=choices, + is_required=ratings_required and num_rating_choices > 4, + validator_list=get_validator_list(5), + ), + formfields.RadioSelectField(field_name="rating6", choices=choices, + is_required=ratings_required and num_rating_choices > 5, + validator_list=get_validator_list(6), + ), + formfields.RadioSelectField(field_name="rating7", choices=choices, + is_required=ratings_required and num_rating_choices > 6, + validator_list=get_validator_list(7), + ), + formfields.RadioSelectField(field_name="rating8", choices=choices, + is_required=ratings_required and num_rating_choices > 7, + validator_list=get_validator_list(8), + ), + ]) + if not user.is_anonymous(): + self["username"].is_required = False + self["username"].validator_list = [] + self["password"].is_required = False + self["password"].validator_list = [] + self.user_cache = user + + def hasNoProfanities(self, field_data, all_data): + if COMMENTS_ALLOW_PROFANITIES: + return + return validators.hasNoProfanities(field_data, all_data) + + def get_comment(self, new_data): + "Helper function" + return comments.Comment(None, self.get_user_id(), new_data["content_type_id"], + new_data["object_id"], new_data.get("headline", "").strip(), + new_data["comment"].strip(), new_data.get("rating1", None), + new_data.get("rating2", None), new_data.get("rating3", None), + new_data.get("rating4", None), new_data.get("rating5", None), + new_data.get("rating6", None), new_data.get("rating7", None), + new_data.get("rating8", None), new_data.get("rating1", None) is not None, + datetime.datetime.now(), new_data["is_public"], new_data["ip_address"], False, SITE_ID) + + def save(self, new_data): + today = datetime.date.today() + c = self.get_comment(new_data) + for old in comments.get_list(content_type_id__exact=new_data["content_type_id"], + object_id__exact=new_data["object_id"], user_id__exact=self.get_user_id()): + # Check that this comment isn't duplicate. (Sometimes people post + # comments twice by mistake.) If it is, fail silently by pretending + # the comment was posted successfully. + if old.submit_date.date() == today and old.comment == c.comment \ + and old.rating1 == c.rating1 and old.rating2 == c.rating2 \ + and old.rating3 == c.rating3 and old.rating4 == c.rating4 \ + and old.rating5 == c.rating5 and old.rating6 == c.rating6 \ + and old.rating7 == c.rating7 and old.rating8 == c.rating8: + return old + # If the user is leaving a rating, invalidate all old ratings. + if c.rating1 is not None: + old.valid_rating = False + old.save() + c.save() + # If the commentor has posted fewer than COMMENTS_FIRST_FEW comments, + # send the comment to the managers. + if self.user_cache.get_comments_comment_count() <= COMMENTS_FIRST_FEW: + message = 'This comment was posted by a user who has posted fewer than %s comments:\n\n%s' % \ + (COMMENTS_FIRST_FEW, c.get_as_text()) + mail_managers("Comment posted by rookie user", message) + if COMMENTS_SKETCHY_USERS_GROUP and COMMENTS_SKETCHY_USERS_GROUP in [g.id for g in self.user_cache.get_group_list()]: + message = 'This comment was posted by a sketchy user:\n\n%s' % c.get_as_text() + mail_managers("Comment posted by sketchy user (%s)" % self.user_cache.username, c.get_as_text()) + return c + +class PublicFreeCommentManipulator(formfields.Manipulator): + "Manipulator that handles public free (unregistered) comments" + def __init__(self): + self.fields = ( + formfields.TextField(field_name="person_name", maxlength=50, is_required=True, + validator_list=[self.hasNoProfanities]), + formfields.LargeTextField(field_name="comment", maxlength=3000, is_required=True, + validator_list=[self.hasNoProfanities]), + ) + + def hasNoProfanities(self, field_data, all_data): + if COMMENTS_ALLOW_PROFANITIES: + return + return validators.hasNoProfanities(field_data, all_data) + + def get_comment(self, new_data): + "Helper function" + return freecomments.FreeComment(None, new_data["content_type_id"], + new_data["object_id"], new_data["comment"].strip(), + new_data["person_name"].strip(), datetime.datetime.now(), new_data["is_public"], + new_data["ip_address"], False, SITE_ID) + + def save(self, new_data): + today = datetime.date.today() + c = self.get_comment(new_data) + # Check that this comment isn't duplicate. (Sometimes people post + # comments twice by mistake.) If it is, fail silently by pretending + # the comment was posted successfully. + for old_comment in freecomments.get_list(content_type_id__exact=new_data["content_type_id"], + object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"], + submit_date__year=today.year, submit_date__month=today.month, + submit_date__day=today.day): + if old_comment.comment == c.comment: + return old_comment + c.save() + return c + +def post_comment(request): + """ + Post a comment + + Redirects to the `comments.comments.comment_was_posted` view upon success. + + Templates: `comment_preview` + Context: + comment + the comment being posted + comment_form + the comment form + options + comment options + target + comment target + hash + security hash (must be included in a posted form to succesfully + post a comment). + rating_options + comment ratings options + ratings_optional + are ratings optional? + ratings_required + are ratings required? + rating_range + range of ratings + rating_choices + choice of ratings + """ + if not request.POST: + raise Http404, "Only POSTs are allowed" + try: + options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] + except KeyError: + raise Http404, "One or more of the required fields wasn't submitted" + photo_options = request.POST.get('photo_options', '') + rating_options = normalize_newlines(request.POST.get('rating_options', '')) + if comments.get_security_hash(options, photo_options, rating_options, target) != security_hash: + raise Http404, "Somebody tampered with the comment form (security violation)" + # Now we can be assured the data is valid. + if rating_options: + rating_range, rating_choices = comments.get_rating_options(base64.decodestring(rating_options)) + else: + rating_range, rating_choices = [], [] + content_type_id, object_id = target.split(':') # target is something like '52:5157' + try: + obj = contenttypes.get_object(id__exact=content_type_id).get_object_for_this_type(id__exact=object_id) + except ObjectDoesNotExist: + raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" + option_list = options.split(',') # options is something like 'pa,ra' + new_data = request.POST.copy() + new_data['content_type_id'] = content_type_id + new_data['object_id'] = object_id + new_data['ip_address'] = request.META['REMOTE_ADDR'] + new_data['is_public'] = comments.IS_PUBLIC in option_list + response = HttpResponse() + manipulator = PublicCommentManipulator(request.user, + ratings_required=comments.RATINGS_REQUIRED in option_list, + ratings_range=rating_range, + num_rating_choices=len(rating_choices)) + errors = manipulator.get_validation_errors(new_data) + # If user gave correct username/password and wasn't already logged in, log them in + # so they don't have to enter a username/password again. + if manipulator.get_user() and new_data.has_key('password') and manipulator.get_user().check_password(new_data['password']): + sessions.start_web_session(manipulator.get_user_id(), request, response) + if errors or request.POST.has_key('preview'): + class CommentFormWrapper(formfields.FormWrapper): + def __init__(self, manipulator, new_data, errors, rating_choices): + formfields.FormWrapper.__init__(self, manipulator, new_data, errors) + self.rating_choices = rating_choices + def ratings(self): + field_list = [self['rating%d' % (i+1)] for i in range(len(rating_choices))] + for i, f in enumerate(field_list): + f.choice = rating_choices[i] + return field_list + comment = errors and '' or manipulator.get_comment(new_data) + comment_form = CommentFormWrapper(manipulator, new_data, errors, rating_choices) + t = template_loader.get_template('comments/preview') + c = Context(request, { + 'comment': comment, + 'comment_form': comment_form, + 'options': options, + 'target': target, + 'hash': security_hash, + 'rating_options': rating_options, + 'ratings_optional': comments.RATINGS_OPTIONAL in option_list, + 'ratings_required': comments.RATINGS_REQUIRED in option_list, + 'rating_range': rating_range, + 'rating_choices': rating_choices, + }) + elif request.POST.has_key('post'): + # If the IP is banned, mail the admins, do NOT save the comment, and + # serve up the "Thanks for posting" page as if the comment WAS posted. + if request.META['REMOTE_ADDR'] in BANNED_IPS: + mail_admins("Banned IP attempted to post comment", str(request.POST) + "\n\n" + str(request.META)) + else: + manipulator.do_html2python(new_data) + comment = manipulator.save(new_data) + return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) + else: + raise Http404, "The comment form didn't provide either 'preview' or 'post'" + response.write(t.render(c)) + return response + +def post_free_comment(request): + """ + Post a free comment (not requiring a log in) + + Redirects to `comments.comments.comment_was_posted` view on success. + + Templates: `comment_free_preview` + Context: + comment + comment being posted + comment_form + comment form object + options + comment options + target + comment target + hash + security hash (must be included in a posted form to succesfully + post a comment). + """ + if not request.POST: + raise Http404, "Only POSTs are allowed" + try: + options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] + except KeyError: + raise Http404, "One or more of the required fields wasn't submitted" + if comments.get_security_hash(options, '', '', target) != security_hash: + raise Http404, "Somebody tampered with the comment form (security violation)" + content_type_id, object_id = target.split(':') # target is something like '52:5157' + content_type = contenttypes.get_object(id__exact=content_type_id) + try: + obj = content_type.get_object_for_this_type(id__exact=object_id) + except ObjectDoesNotExist: + raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" + option_list = options.split(',') + new_data = request.POST.copy() + new_data['content_type_id'] = content_type_id + new_data['object_id'] = object_id + new_data['ip_address'] = request.META['REMOTE_ADDR'] + new_data['is_public'] = comments.IS_PUBLIC in option_list + response = HttpResponse() + manipulator = PublicFreeCommentManipulator() + errors = manipulator.get_validation_errors(new_data) + if errors or request.POST.has_key('preview'): + comment = errors and '' or manipulator.get_comment(new_data) + t = template_loader.get_template('comments/free_preview') + c = Context(request, { + 'comment': comment, + 'comment_form': formfields.FormWrapper(manipulator, new_data, errors), + 'options': options, + 'target': target, + 'hash': security_hash, + }) + elif request.POST.has_key('post'): + # If the IP is banned, mail the admins, do NOT save the comment, and + # serve up the "Thanks for posting" page as if the comment WAS posted. + if request.META['REMOTE_ADDR'] in BANNED_IPS: + from django.core.mail import mail_admins + mail_admins("Practical joker", str(request.POST) + "\n\n" + str(request.META)) + else: + manipulator.do_html2python(new_data) + comment = manipulator.save(new_data) + return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) + else: + raise Http404, "The comment form didn't provide either 'preview' or 'post'" + response.write(t.render(c)) + return response + +def comment_was_posted(request): + """ + Display "comment was posted" success page + + Templates: `comment_posted` + Context: + object + The object the comment was posted on + """ + obj = None + if request.GET.has_key('c'): + content_type_id, object_id = request.GET['c'].split(':') + try: + content_type = contenttypes.get_object(id__exact=content_type_id) + obj = content_type.get_object_for_this_type(id__exact=object_id) + except ObjectDoesNotExist: + pass + t = template_loader.get_template('comments/posted') + c = Context(request, { + 'object': obj, + }) + return HttpResponse(t.render(c)) diff --git a/django/contrib/comments/views/karma.py b/django/contrib/comments/views/karma.py new file mode 100644 index 0000000000..84f0ca852d --- /dev/null +++ b/django/contrib/comments/views/karma.py @@ -0,0 +1,34 @@ +from django.core import template_loader +from django.core.extensions import CMSContext as Context +from django.core.exceptions import Http404 +from django.models.comments import comments, karma +from django.utils.httpwrappers import HttpResponse + +def vote(request, comment_id, vote): + """ + Rate a comment (+1 or -1) + + Templates: `karma_vote_accepted` + Context: + comment + `comments.comments` object being rated + """ + rating = {'up': 1, 'down': -1}.get(vote, False) + if not rating: + raise Http404, "Invalid vote" + if request.user.is_anonymous(): + raise Http404, "Anonymous users cannot vote" + try: + comment = comments.get_object(id__exact=comment_id) + except comments.CommentDoesNotExist: + raise Http404, "Invalid comment ID" + if comment.user_id == request.user.id: + raise Http404, "No voting for yourself" + karma.vote(request.user.id, comment_id, rating) + # Reload comment to ensure we have up to date karma count + comment = comments.get_object(id__exact=comment_id) + t = template_loader.get_template('comments/karma_vote_accepted') + c = Context(request, { + 'comment': comment + }) + return HttpResponse(t.render(c)) diff --git a/django/contrib/comments/views/userflags.py b/django/contrib/comments/views/userflags.py new file mode 100644 index 0000000000..051ecb19fe --- /dev/null +++ b/django/contrib/comments/views/userflags.py @@ -0,0 +1,82 @@ +from django.core import template_loader +from django.core.extensions import CMSContext as Context +from django.core.exceptions import Http404 +from django.models.comments import comments, moderatordeletions, userflags +from django.views.decorators.auth import login_required +from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect +from django.conf.settings import SITE_ID + +def flag(request, comment_id): + """ + Flags a comment. Confirmation on GET, action on POST. + + Templates: `comments/flag_verify`, `comments/flag_done` + Context: + comment + the flagged `comments.comments` object + """ + try: + comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) + except comments.CommentDoesNotExist: + raise Http404 + if request.POST: + userflags.flag(comment, request.user) + return HttpResponseRedirect('%sdone/' % request.path) + t = template_loader.get_template('comments/flag_verify') + c = Context(request, { + 'comment': comment, + }) + return HttpResponse(t.render(c)) +flag = login_required(flag) + +def flag_done(request, comment_id): + try: + comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) + except comments.CommentDoesNotExist: + raise Http404 + t = template_loader.get_template('comments/flag_done') + c = Context(request, { + 'comment': comment, + }) + return HttpResponse(t.render(c)) + +def delete(request, comment_id): + """ + Deletes a comment. Confirmation on GET, action on POST. + + Templates: `comments/delete_verify`, `comments/delete_done` + Context: + comment + the flagged `comments.comments` object + """ + try: + comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) + except comments.CommentDoesNotExist: + raise Http404 + if not comments.user_is_moderator(request.user): + raise Http404 + if request.POST: + # If the comment has already been removed, silently fail. + if not comment.is_removed: + comment.is_removed = True + comment.save() + m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None) + m.save() + return HttpResponseRedirect('%sdone/' % request.path) + t = template_loader.get_template('comments/delete_verify') + c = Context(request, { + 'comment': comment, + }) + return HttpResponse(t.render(c)) +delete = login_required(delete) + +def delete_done(request, comment_id): + try: + comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) + except comments.CommentDoesNotExist: + raise Http404 + t = template_loader.get_template('comments/delete_done') + c = Context(request, { + 'comment': comment, + }) + return HttpResponse(t.render(c)) diff --git a/django/models/__init__.py b/django/models/__init__.py index 63cedc9af1..b4a1d75617 100644 --- a/django/models/__init__.py +++ b/django/models/__init__.py @@ -1,6 +1,6 @@ from django.core import meta -__all__ = ['auth', 'comments', 'core'] +__all__ = ['auth', 'core'] # Alter this package's __path__ variable so that calling code can import models # from "django.models" even though the model code doesn't physically live diff --git a/django/models/comments.py b/django/models/comments.py deleted file mode 100644 index 61988595db..0000000000 --- a/django/models/comments.py +++ /dev/null @@ -1,281 +0,0 @@ -from django.core import meta -from django.models import auth, core - -class Comment(meta.Model): - db_table = 'comments' - fields = ( - meta.ForeignKey(auth.User, raw_id_admin=True), - meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), - meta.IntegerField('object_id', 'object ID'), - meta.CharField('headline', 'headline', maxlength=255, blank=True), - meta.TextField('comment', 'comment', maxlength=3000), - meta.PositiveSmallIntegerField('rating1', 'rating #1', blank=True, null=True), - meta.PositiveSmallIntegerField('rating2', 'rating #2', blank=True, null=True), - meta.PositiveSmallIntegerField('rating3', 'rating #3', blank=True, null=True), - meta.PositiveSmallIntegerField('rating4', 'rating #4', blank=True, null=True), - meta.PositiveSmallIntegerField('rating5', 'rating #5', blank=True, null=True), - meta.PositiveSmallIntegerField('rating6', 'rating #6', blank=True, null=True), - meta.PositiveSmallIntegerField('rating7', 'rating #7', blank=True, null=True), - meta.PositiveSmallIntegerField('rating8', 'rating #8', blank=True, null=True), - # This field designates whether to use this row's ratings in - # aggregate functions (summaries). We need this because people are - # allowed to post multiple review on the same thing, but the system - # will only use the latest one (with valid_rating=True) in tallying - # the reviews. - meta.BooleanField('valid_rating', 'is valid rating'), - meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), - meta.BooleanField('is_public', 'is public'), - meta.IPAddressField('ip_address', 'IP address', blank=True, null=True), - meta.BooleanField('is_removed', 'is removed', - help_text='Check this box if the comment is inappropriate. A "This comment has been removed" message will be displayed instead.'), - meta.ForeignKey(core.Site), - ) - module_constants = { - # used as shared secret between comment form and comment-posting script - 'COMMENT_SALT': 'ijw2f3_MRS_PIGGY_LOVES_KERMIT_avo#*5vv0(23j)(*', - - # min. and max. allowed dimensions for photo resizing (in pixels) - 'MIN_PHOTO_DIMENSION': 5, - 'MAX_PHOTO_DIMENSION': 1000, - - # option codes for comment-form hidden fields - 'PHOTOS_REQUIRED': 'pr', - 'PHOTOS_OPTIONAL': 'pa', - 'RATINGS_REQUIRED': 'rr', - 'RATINGS_OPTIONAL': 'ra', - 'IS_PUBLIC': 'ip', - } - ordering = (('submit_date', 'DESC'),) - admin = meta.Admin( - fields = ( - (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), - ('Content', {'fields': ('user_id', 'headline', 'comment')}), - ('Ratings', {'fields': ('rating1', 'rating2', 'rating3', 'rating4', 'rating5', 'rating6', 'rating7', 'rating8', 'valid_rating')}), - ('Meta', {'fields': ('is_public', 'is_removed', 'ip_address')}), - ), - list_display = ('user_id', 'submit_date', 'content_type_id', 'get_content_object'), - list_filter = ('submit_date',), - date_hierarchy = 'submit_date', - search_fields = ('comment', 'user__username'), - ) - - def __repr__(self): - return "%s: %s..." % (self.get_user().username, self.comment[:100]) - - def get_absolute_url(self): - return self.get_content_object().get_absolute_url() + "#c" + str(self.id) - - def get_crossdomain_url(self): - return "/r/%d/%d/" % (self.content_type_id, self.object_id) - - def get_flag_url(self): - return "/comments/flag/%s/" % self.id - - def get_deletion_url(self): - return "/comments/delete/%s/" % self.id - - def get_content_object(self): - """ - Returns the object that this comment is a comment on. Returns None if - the object no longer exists. - """ - from django.core.exceptions import ObjectDoesNotExist - try: - return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) - except ObjectDoesNotExist: - return None - - get_content_object.short_description = 'Content object' - - def _fill_karma_cache(self): - "Helper function that populates good/bad karma caches" - good, bad = 0, 0 - for k in self.get_karmascore_list(): - if k.score == -1: - bad +=1 - elif k.score == 1: - good +=1 - self._karma_total_good, self._karma_total_bad = good, bad - - def get_good_karma_total(self): - if not hasattr(self, "_karma_total_good"): - self._fill_karma_cache() - return self._karma_total_good - - def get_bad_karma_total(self): - if not hasattr(self, "_karma_total_bad"): - self._fill_karma_cache() - return self._karma_total_bad - - def get_karma_total(self): - if not hasattr(self, "_karma_total_good") or not hasattr(self, "_karma_total_bad"): - self._fill_karma_cache() - return self._karma_total_good + self._karma_total_bad - - def get_as_text(self): - return 'Posted by %s at %s\n\n%s\n\nhttp://%s%s' % \ - (self.get_user().username, self.submit_date, - self.comment, self.get_site().domain, self.get_absolute_url()) - - def _module_get_security_hash(options, photo_options, rating_options, target): - """ - Returns the MD5 hash of the given options (a comma-separated string such as - 'pa,ra') and target (something like 'lcom.eventtimes:5157'). Used to - validate that submitted form options have not been tampered-with. - """ - import md5 - return md5.new(options + photo_options + rating_options + target + COMMENT_SALT).hexdigest() - - def _module_get_rating_options(rating_string): - """ - Given a rating_string, this returns a tuple of (rating_range, options). - >>> s = "scale:1-10|First_category|Second_category" - >>> get_rating_options(s) - ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], ['First category', 'Second category']) - """ - rating_range, options = rating_string.split('|', 1) - rating_range = range(int(rating_range[6:].split('-')[0]), int(rating_range[6:].split('-')[1])+1) - choices = [c.replace('_', ' ') for c in options.split('|')] - return rating_range, choices - - def _module_get_list_with_karma(**kwargs): - """ - Returns a list of Comment objects matching the given lookup terms, with - _karma_total_good and _karma_total_bad filled. - """ - kwargs.setdefault('select', {}) - kwargs['select']['_karma_total_good'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=1' - kwargs['select']['_karma_total_bad'] = 'SELECT COUNT(*) FROM comments_karma WHERE comments_karma.comment_id=comments.id AND score=-1' - return get_list(**kwargs) - - def _module_user_is_moderator(user): - from django.conf.settings import COMMENTS_MODERATORS_GROUP - if user.is_superuser: - return True - for g in user.get_group_list(): - if g.id == COMMENTS_MODERATORS_GROUP: - return True - return False - -class FreeComment(meta.Model): - "A FreeComment is a comment by a non-registered user" - db_table = 'comments_free' - fields = ( - meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type'), - meta.IntegerField('object_id', 'object ID'), - meta.TextField('comment', 'comment', maxlength=3000), - meta.CharField('person_name', "person's name", maxlength=50), - meta.DateTimeField('submit_date', 'date/time submitted', auto_now_add=True), - meta.BooleanField('is_public', 'is public'), - meta.IPAddressField('ip_address', 'IP address'), - # TODO: Change this to is_removed, like Comment - meta.BooleanField('approved', 'approved by staff'), - meta.ForeignKey(core.Site), - ) - ordering = (('submit_date', 'DESC'),) - admin = meta.Admin( - fields = ( - (None, {'fields': ('content_type_id', 'object_id', 'site_id')}), - ('Content', {'fields': ('person_name', 'comment')}), - ('Meta', {'fields': ('submit_date', 'is_public', 'ip_address', 'approved')}), - ), - list_display = ('person_name', 'submit_date', 'content_type_id', 'get_content_object'), - list_filter = ('submit_date',), - date_hierarchy = 'submit_date', - search_fields = ('comment', 'person_name'), - ) - - def __repr__(self): - return "%s: %s..." % (self.person_name, self.comment[:100]) - - def get_content_object(self): - """ - Returns the object that this comment is a comment on. Returns None if - the object no longer exists. - """ - from django.core.exceptions import ObjectDoesNotExist - try: - return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) - except ObjectDoesNotExist: - return None - - get_content_object.short_description = 'Content object' - -class KarmaScore(meta.Model): - module_name = 'karma' - fields = ( - meta.ForeignKey(auth.User), - meta.ForeignKey(Comment), - meta.SmallIntegerField('score', 'score', db_index=True), - meta.DateTimeField('scored_date', 'date scored', auto_now=True), - ) - unique_together = (('user_id', 'comment_id'),) - module_constants = { - # what users get if they don't have any karma - 'DEFAULT_KARMA': 5, - 'KARMA_NEEDED_BEFORE_DISPLAYED': 3, - } - - def __repr__(self): - return "%d rating by %s" % (self.score, self.get_user()) - - def _module_vote(user_id, comment_id, score): - try: - karma = get_object(comment_id__exact=comment_id, user_id__exact=user_id) - except KarmaScoreDoesNotExist: - karma = KarmaScore(None, user_id, comment_id, score, datetime.datetime.now()) - karma.save() - else: - karma.score = score - karma.scored_date = datetime.datetime.now() - karma.save() - - def _module_get_pretty_score(score): - """ - Given a score between -1 and 1 (inclusive), returns the same score on a - scale between 1 and 10 (inclusive), as an integer. - """ - if score is None: - return DEFAULT_KARMA - return int(round((4.5 * score) + 5.5)) - -class UserFlag(meta.Model): - db_table = 'comments_user_flags' - fields = ( - meta.ForeignKey(auth.User), - meta.ForeignKey(Comment), - meta.DateTimeField('flag_date', 'date flagged', auto_now_add=True), - ) - unique_together = (('user_id', 'comment_id'),) - - def __repr__(self): - return "Flag by %r" % self.get_user() - - def _module_flag(comment, user): - """ - Flags the given comment by the given user. If the comment has already - been flagged by the user, or it was a comment posted by the user, - nothing happens. - """ - if int(comment.user_id) == int(user.id): - return # A user can't flag his own comment. Fail silently. - try: - f = get_object(user_id__exact=user.id, comment_id__exact=comment.id) - except UserFlagDoesNotExist: - from django.core.mail import mail_managers - f = UserFlag(None, user.id, comment.id, None) - message = 'This comment was flagged by %s:\n\n%s' % (user.username, comment.get_as_text()) - mail_managers('Comment flagged', message, fail_silently=True) - f.save() - -class ModeratorDeletion(meta.Model): - db_table = 'comments_moderator_deletions' - fields = ( - meta.ForeignKey(auth.User, verbose_name='moderator'), - meta.ForeignKey(Comment), - meta.DateTimeField('deletion_date', 'date deleted', auto_now_add=True), - ) - unique_together = (('user_id', 'comment_id'),) - - def __repr__(self): - return "Moderator deletion by %r" % self.get_user() diff --git a/django/templatetags/comments.py b/django/templatetags/comments.py deleted file mode 100644 index adfd3cedac..0000000000 --- a/django/templatetags/comments.py +++ /dev/null @@ -1,331 +0,0 @@ -"Custom template tags for user comments" - -from django.core import template -from django.core.exceptions import ObjectDoesNotExist -from django.models.comments import comments, freecomments -from django.models.core import contenttypes -import re - -COMMENT_FORM = ''' -{% if display_form %} -
- -{% if user.is_anonymous %} -

Username:
Password: (Forgotten your password?)

-{% else %} -

Username: {{ user.username }} (Log out)

-{% endif %} - -{% if ratings_optional or ratings_required %} -

Ratings ({% if ratings_required %}Required{% else %}Optional{% endif %}):

- -{% for value in rating_range %}{% endfor %} -{% for rating in rating_choices %} -{% for value in rating_range %}{% endfor %} -{% endfor %} -
 {{ value }}
{{ rating }}
- -{% endif %} - -{% if photos_optional or photos_required %} -

Post a photo ({% if photos_required %}Required{% else %}Optional{% endif %}):

- -{% endif %} - -

Comment:

- - - - -

-
-{% endif %} -''' - -FREE_COMMENT_FORM = ''' -{% if display_form %} -
-

Your name:

-

Comment:

- - - -

-
-{% endif %} -''' - -class CommentFormNode(template.Node): - def __init__(self, content_type, obj_id_lookup_var, obj_id, free, - photos_optional=False, photos_required=False, photo_options='', - ratings_optional=False, ratings_required=False, rating_options='', - is_public=True): - self.content_type = content_type - self.obj_id_lookup_var, self.obj_id, self.free = obj_id_lookup_var, obj_id, free - self.photos_optional, self.photos_required = photos_optional, photos_required - self.ratings_optional, self.ratings_required = ratings_optional, ratings_required - self.photo_options, self.rating_options = photo_options, rating_options - self.is_public = is_public - - def render(self, context): - from django.utils.text import normalize_newlines - import base64 - context.push() - if self.obj_id_lookup_var is not None: - try: - self.obj_id = template.resolve_variable(self.obj_id_lookup_var, context) - except template.VariableDoesNotExist: - return '' - # Validate that this object ID is valid for this content-type. - # We only have to do this validation if obj_id_lookup_var is provided, - # because do_comment_form() validates hard-coded object IDs. - try: - self.content_type.get_object_for_this_type(id__exact=self.obj_id) - except ObjectDoesNotExist: - context['display_form'] = False - else: - context['display_form'] = True - context['target'] = '%s:%s' % (self.content_type.id, self.obj_id) - options = [] - for var, abbr in (('photos_required', comments.PHOTOS_REQUIRED), - ('photos_optional', comments.PHOTOS_OPTIONAL), - ('ratings_required', comments.RATINGS_REQUIRED), - ('ratings_optional', comments.RATINGS_OPTIONAL), - ('is_public', comments.IS_PUBLIC)): - context[var] = getattr(self, var) - if getattr(self, var): - options.append(abbr) - context['options'] = ','.join(options) - if self.free: - context['hash'] = comments.get_security_hash(context['options'], '', '', context['target']) - default_form = FREE_COMMENT_FORM - else: - context['photo_options'] = self.photo_options - context['rating_options'] = normalize_newlines(base64.encodestring(self.rating_options).strip()) - if self.rating_options: - context['rating_range'], context['rating_choices'] = comments.get_rating_options(self.rating_options) - context['hash'] = comments.get_security_hash(context['options'], context['photo_options'], context['rating_options'], context['target']) - default_form = COMMENT_FORM - output = template.Template(default_form).render(context) - context.pop() - return output - -class CommentCountNode(template.Node): - def __init__(self, package, module, context_var_name, obj_id, var_name, free): - self.package, self.module = package, module - self.context_var_name, self.obj_id = context_var_name, obj_id - self.var_name, self.free = var_name, free - - def render(self, context): - from django.conf.settings import SITE_ID - get_count_function = self.free and freecomments.get_count or comments.get_count - if self.context_var_name is not None: - self.obj_id = template.resolve_variable(self.context_var_name, context) - comment_count = get_count_function(object_id__exact=self.obj_id, - content_type__package__label__exact=self.package, - content_type__python_module_name__exact=self.module, site_id__exact=SITE_ID) - context[self.var_name] = comment_count - return '' - -class CommentListNode(template.Node): - def __init__(self, package, module, context_var_name, obj_id, var_name, free): - self.package, self.module = package, module - self.context_var_name, self.obj_id = context_var_name, obj_id - self.var_name, self.free = var_name, free - - def render(self, context): - from django.conf.settings import COMMENTS_BANNED_USERS_GROUP, SITE_ID - get_list_function = self.free and freecomments.get_list or comments.get_list_with_karma - if self.context_var_name is not None: - try: - self.obj_id = template.resolve_variable(self.context_var_name, context) - except template.VariableDoesNotExist: - return '' - kwargs = { - 'object_id__exact': self.obj_id, - 'content_type__package__label__exact': self.package, - 'content_type__python_module_name__exact': self.module, - 'site_id__exact': SITE_ID, - 'select_related': True, - 'order_by': (('submit_date', 'ASC'),), - } - if not self.free and COMMENTS_BANNED_USERS_GROUP: - kwargs['select'] = {'is_hidden': 'user_id IN (SELECT user_id FROM auth_users_groups WHERE group_id = %s)' % COMMENTS_BANNED_USERS_GROUP} - comment_list = get_list_function(**kwargs) - - if not self.free: - if context.has_key('user') and not context['user'].is_anonymous(): - user_id = context['user'].id - context['user_can_moderate_comments'] = comments.user_is_moderator(context['user']) - else: - user_id = None - context['user_can_moderate_comments'] = False - # Only display comments by banned users to those users themselves. - if COMMENTS_BANNED_USERS_GROUP: - comment_list = [c for c in comment_list if not c.is_hidden or (user_id == c.user_id)] - - context[self.var_name] = comment_list - return '' - -class DoCommentForm: - """ - Displays a comment form for the given params. Syntax: - {% comment_form for [pkg].[py_module_name] [context_var_containing_obj_id] with [list of options] %} - Example usage: - {% comment_form for lcom.eventtimes event.id with is_public yes photos_optional thumbs,200,400 ratings_optional scale:1-5|first_option|second_option %} - [context_var_containing_obj_id] can be a hard-coded integer or a variable containing the ID. - """ - def __init__(self, free, tag_name): - self.free, self.tag_name = free, tag_name - - def __call__(self, parser, token): - tokens = token.contents.split() - if len(tokens) < 4: - raise template.TemplateSyntaxError, "'%s' tag requires at least 3 arguments" % self.tag_name - if tokens[1] != 'for': - raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name - try: - package, module = tokens[2].split('.') - except ValueError: # unpack list of wrong size - raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name - try: - content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) - except contenttypes.ContentTypeDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) - obj_id_lookup_var, obj_id = None, None - if tokens[3].isdigit(): - obj_id = tokens[3] - try: # ensure the object ID is valid - content_type.get_object_for_this_type(id__exact=obj_id) - except ObjectDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) - else: - obj_id_lookup_var = tokens[3] - kwargs = {} - if len(tokens) > 4: - if tokens[4] != 'with': - raise template.TemplateSyntaxError, "Fourth argument in '%s' tag must be 'with'" % self.tag_name - for option, args in zip(tokens[5::2], tokens[6::2]): - if option in ('photos_optional', 'photos_required') and not self.free: - # VALIDATION ############################################## - option_list = args.split(',') - if len(option_list) % 3 != 0: - raise template.TemplateSyntaxError, "Incorrect number of comma-separated arguments to '%s' tag" % self.tag_name - for opt in option_list[::3]: - if not opt.isalnum(): - raise template.TemplateSyntaxError, "Invalid photo directory name in '%s' tag: '%s'" % (self.tag_name, opt) - for opt in option_list[1::3] + option_list[2::3]: - if not opt.isdigit() or not (comments.MIN_PHOTO_DIMENSION <= int(opt) <= comments.MAX_PHOTO_DIMENSION): - raise template.TemplateSyntaxError, "Invalid photo dimension in '%s' tag: '%s'. Only values between %s and %s are allowed." % (self.tag_name, opt, comments.MIN_PHOTO_DIMENSION, comments.MAX_PHOTO_DIMENSION) - # VALIDATION ENDS ######################################### - kwargs[option] = True - kwargs['photo_options'] = args - elif option in ('ratings_optional', 'ratings_required') and not self.free: - # VALIDATION ############################################## - if 2 < len(args.split('|')) > 9: - raise template.TemplateSyntaxError, "Incorrect number of '%s' options in '%s' tag. Use between 2 and 8." % (option, self.tag_name) - if re.match('^scale:\d+\-\d+\:$', args.split('|')[0]): - raise template.TemplateSyntaxError, "Invalid 'scale' in '%s' tag's '%s' options" % (self.tag_name, option) - # VALIDATION ENDS ######################################### - kwargs[option] = True - kwargs['rating_options'] = args - elif option in ('is_public'): - kwargs[option] = (args == 'true') - else: - raise template.TemplateSyntaxError, "'%s' tag got invalid parameter '%s'" % (self.tag_name, option) - return CommentFormNode(content_type, obj_id_lookup_var, obj_id, self.free, **kwargs) - -class DoCommentCount: - """ - Gets comment count for the given params and populates the template context - with a variable containing that value, whose name is defined by the 'as' - clause. Syntax: - {% get_comment_count for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname] %} - Example usage: - {% get_comment_count for lcom.eventtimes event.id as comment_count %} - Note: [context_var_containing_obj_id] can also be a hard-coded integer, like this: - {% get_comment_count for lcom.eventtimes 23 as comment_count %} - """ - def __init__(self, free, tag_name): - self.free, self.tag_name = free, tag_name - - def __call__(self, parser, token): - tokens = token.contents.split() - # Now tokens is a list like this: - # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] - if len(tokens) != 6: - raise template.TemplateSyntaxError, "%s block tag requires 5 arguments" % self.tag_name - if tokens[1] != 'for': - raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name - try: - package, module = tokens[2].split('.') - except ValueError: # unpack list of wrong size - raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name - try: - content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) - except contenttypes.ContentTypeDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) - var_name, obj_id = None, None - if tokens[3].isdigit(): - obj_id = tokens[3] - try: # ensure the object ID is valid - content_type.get_object_for_this_type(id__exact=obj_id) - except ObjectDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) - else: - var_name = tokens[3] - if tokens[4] != 'as': - raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name - return CommentCountNode(package, module, var_name, obj_id, tokens[5], self.free) - -class DoGetCommentList: - """ - Gets comments for the given params and populates the template context with - a special comment_package variable, whose name is defined by the 'as' - clause. Syntax: - {% get_comment_list for [pkg].[py_module_name] [context_var_containing_obj_id] as [varname] %} - Example usage: - {% get_comment_list for lcom.eventtimes event.id as comment_list %} - Note: [context_var_containing_obj_id] can also be a hard-coded integer, like this: - {% get_comment_list for lcom.eventtimes 23 as comment_list %} - """ - def __init__(self, free, tag_name): - self.free, self.tag_name = free, tag_name - - def __call__(self, parser, token): - tokens = token.contents.split() - # Now tokens is a list like this: - # ['get_comment_list', 'for', 'lcom.eventtimes', 'event.id', 'as', 'comment_list'] - if len(tokens) != 6: - raise template.TemplateSyntaxError, "%s block tag requires 5 arguments" % self.tag_name - if tokens[1] != 'for': - raise template.TemplateSyntaxError, "Second argument in '%s' tag must be 'for'" % self.tag_name - try: - package, module = tokens[2].split('.') - except ValueError: # unpack list of wrong size - raise template.TemplateSyntaxError, "Third argument in '%s' tag must be in the format 'package.module'" % self.tag_name - try: - content_type = contenttypes.get_object(package__label__exact=package, python_module_name__exact=module) - except contenttypes.ContentTypeDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag has invalid content-type '%s.%s'" % (self.tag_name, package, module) - var_name, obj_id = None, None - if tokens[3].isdigit(): - obj_id = tokens[3] - try: # ensure the object ID is valid - content_type.get_object_for_this_type(id__exact=obj_id) - except ObjectDoesNotExist: - raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) - else: - var_name = tokens[3] - if tokens[4] != 'as': - raise template.TemplateSyntaxError, "Fourth argument in '%s' must be 'as'" % self.tag_name - return CommentListNode(package, module, var_name, obj_id, tokens[5], self.free) - -# registration comments -template.register_tag('get_comment_list', DoGetCommentList(free=False, tag_name='get_comment_list')) -template.register_tag('comment_form', DoCommentForm(free=False, tag_name='comment_form')) -template.register_tag('get_comment_count', DoCommentCount(free=False, tag_name='get_comment_count')) -# free comments -template.register_tag('get_free_comment_list', DoGetCommentList(free=True, tag_name='get_free_comment_list')) -template.register_tag('free_comment_form', DoCommentForm(free=True, tag_name='free_comment_form')) -template.register_tag('get_free_comment_count', DoCommentCount(free=True, tag_name='get_free_comment_count')) diff --git a/django/views/comments/__init__.py b/django/views/comments/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/django/views/comments/comments.py b/django/views/comments/comments.py deleted file mode 100644 index ffad921ad3..0000000000 --- a/django/views/comments/comments.py +++ /dev/null @@ -1,347 +0,0 @@ -from django.core import formfields, template_loader, validators -from django.core.mail import mail_admins, mail_managers -from django.core.exceptions import Http404, ObjectDoesNotExist -from django.core.extensions import CMSContext as Context -from django.models.auth import sessions -from django.models.comments import comments, freecomments -from django.models.core import contenttypes -from django.parts.auth.formfields import AuthenticationForm -from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect -from django.utils.text import normalize_newlines -from django.conf.settings import BANNED_IPS, COMMENTS_ALLOW_PROFANITIES, COMMENTS_SKETCHY_USERS_GROUP, COMMENTS_FIRST_FEW, SITE_ID -import base64, datetime - -COMMENTS_PER_PAGE = 20 - -class PublicCommentManipulator(AuthenticationForm): - "Manipulator that handles public registered comments" - def __init__(self, user, ratings_required, ratings_range, num_rating_choices): - AuthenticationForm.__init__(self) - self.ratings_range, self.num_rating_choices = ratings_range, num_rating_choices - choices = [(c, c) for c in ratings_range] - def get_validator_list(rating_num): - if rating_num <= num_rating_choices: - return [validators.RequiredIfOtherFieldsGiven(['rating%d' % i for i in range(1, 9) if i != rating_num], "This rating is required because you've entered at least one other rating.")] - else: - return [] - self.fields.extend([ - formfields.LargeTextField(field_name="comment", maxlength=3000, is_required=True, - validator_list=[self.hasNoProfanities]), - formfields.RadioSelectField(field_name="rating1", choices=choices, - is_required=ratings_required and num_rating_choices > 0, - validator_list=get_validator_list(1), - ), - formfields.RadioSelectField(field_name="rating2", choices=choices, - is_required=ratings_required and num_rating_choices > 1, - validator_list=get_validator_list(2), - ), - formfields.RadioSelectField(field_name="rating3", choices=choices, - is_required=ratings_required and num_rating_choices > 2, - validator_list=get_validator_list(3), - ), - formfields.RadioSelectField(field_name="rating4", choices=choices, - is_required=ratings_required and num_rating_choices > 3, - validator_list=get_validator_list(4), - ), - formfields.RadioSelectField(field_name="rating5", choices=choices, - is_required=ratings_required and num_rating_choices > 4, - validator_list=get_validator_list(5), - ), - formfields.RadioSelectField(field_name="rating6", choices=choices, - is_required=ratings_required and num_rating_choices > 5, - validator_list=get_validator_list(6), - ), - formfields.RadioSelectField(field_name="rating7", choices=choices, - is_required=ratings_required and num_rating_choices > 6, - validator_list=get_validator_list(7), - ), - formfields.RadioSelectField(field_name="rating8", choices=choices, - is_required=ratings_required and num_rating_choices > 7, - validator_list=get_validator_list(8), - ), - ]) - if not user.is_anonymous(): - self["username"].is_required = False - self["username"].validator_list = [] - self["password"].is_required = False - self["password"].validator_list = [] - self.user_cache = user - - def hasNoProfanities(self, field_data, all_data): - if COMMENTS_ALLOW_PROFANITIES: - return - return validators.hasNoProfanities(field_data, all_data) - - def get_comment(self, new_data): - "Helper function" - return comments.Comment(None, self.get_user_id(), new_data["content_type_id"], - new_data["object_id"], new_data.get("headline", "").strip(), - new_data["comment"].strip(), new_data.get("rating1", None), - new_data.get("rating2", None), new_data.get("rating3", None), - new_data.get("rating4", None), new_data.get("rating5", None), - new_data.get("rating6", None), new_data.get("rating7", None), - new_data.get("rating8", None), new_data.get("rating1", None) is not None, - datetime.datetime.now(), new_data["is_public"], new_data["ip_address"], False, SITE_ID) - - def save(self, new_data): - today = datetime.date.today() - c = self.get_comment(new_data) - for old in comments.get_list(content_type_id__exact=new_data["content_type_id"], - object_id__exact=new_data["object_id"], user_id__exact=self.get_user_id()): - # Check that this comment isn't duplicate. (Sometimes people post - # comments twice by mistake.) If it is, fail silently by pretending - # the comment was posted successfully. - if old.submit_date.date() == today and old.comment == c.comment \ - and old.rating1 == c.rating1 and old.rating2 == c.rating2 \ - and old.rating3 == c.rating3 and old.rating4 == c.rating4 \ - and old.rating5 == c.rating5 and old.rating6 == c.rating6 \ - and old.rating7 == c.rating7 and old.rating8 == c.rating8: - return old - # If the user is leaving a rating, invalidate all old ratings. - if c.rating1 is not None: - old.valid_rating = False - old.save() - c.save() - # If the commentor has posted fewer than COMMENTS_FIRST_FEW comments, - # send the comment to the managers. - if self.user_cache.get_comments_comment_count() <= COMMENTS_FIRST_FEW: - message = 'This comment was posted by a user who has posted fewer than %s comments:\n\n%s' % \ - (COMMENTS_FIRST_FEW, c.get_as_text()) - mail_managers("Comment posted by rookie user", message) - if COMMENTS_SKETCHY_USERS_GROUP and COMMENTS_SKETCHY_USERS_GROUP in [g.id for g in self.user_cache.get_group_list()]: - message = 'This comment was posted by a sketchy user:\n\n%s' % c.get_as_text() - mail_managers("Comment posted by sketchy user (%s)" % self.user_cache.username, c.get_as_text()) - return c - -class PublicFreeCommentManipulator(formfields.Manipulator): - "Manipulator that handles public free (unregistered) comments" - def __init__(self): - self.fields = ( - formfields.TextField(field_name="person_name", maxlength=50, is_required=True, - validator_list=[self.hasNoProfanities]), - formfields.LargeTextField(field_name="comment", maxlength=3000, is_required=True, - validator_list=[self.hasNoProfanities]), - ) - - def hasNoProfanities(self, field_data, all_data): - if COMMENTS_ALLOW_PROFANITIES: - return - return validators.hasNoProfanities(field_data, all_data) - - def get_comment(self, new_data): - "Helper function" - return freecomments.FreeComment(None, new_data["content_type_id"], - new_data["object_id"], new_data["comment"].strip(), - new_data["person_name"].strip(), datetime.datetime.now(), new_data["is_public"], - new_data["ip_address"], False, SITE_ID) - - def save(self, new_data): - today = datetime.date.today() - c = self.get_comment(new_data) - # Check that this comment isn't duplicate. (Sometimes people post - # comments twice by mistake.) If it is, fail silently by pretending - # the comment was posted successfully. - for old_comment in freecomments.get_list(content_type_id__exact=new_data["content_type_id"], - object_id__exact=new_data["object_id"], person_name__exact=new_data["person_name"], - submit_date__year=today.year, submit_date__month=today.month, - submit_date__day=today.day): - if old_comment.comment == c.comment: - return old_comment - c.save() - return c - -def post_comment(request): - """ - Post a comment - - Redirects to the `comments.comments.comment_was_posted` view upon success. - - Templates: `comment_preview` - Context: - comment - the comment being posted - comment_form - the comment form - options - comment options - target - comment target - hash - security hash (must be included in a posted form to succesfully - post a comment). - rating_options - comment ratings options - ratings_optional - are ratings optional? - ratings_required - are ratings required? - rating_range - range of ratings - rating_choices - choice of ratings - """ - if not request.POST: - raise Http404, "Only POSTs are allowed" - try: - options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] - except KeyError: - raise Http404, "One or more of the required fields wasn't submitted" - photo_options = request.POST.get('photo_options', '') - rating_options = normalize_newlines(request.POST.get('rating_options', '')) - if comments.get_security_hash(options, photo_options, rating_options, target) != security_hash: - raise Http404, "Somebody tampered with the comment form (security violation)" - # Now we can be assured the data is valid. - if rating_options: - rating_range, rating_choices = comments.get_rating_options(base64.decodestring(rating_options)) - else: - rating_range, rating_choices = [], [] - content_type_id, object_id = target.split(':') # target is something like '52:5157' - try: - obj = contenttypes.get_object(id__exact=content_type_id).get_object_for_this_type(id__exact=object_id) - except ObjectDoesNotExist: - raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" - option_list = options.split(',') # options is something like 'pa,ra' - new_data = request.POST.copy() - new_data['content_type_id'] = content_type_id - new_data['object_id'] = object_id - new_data['ip_address'] = request.META['REMOTE_ADDR'] - new_data['is_public'] = comments.IS_PUBLIC in option_list - response = HttpResponse() - manipulator = PublicCommentManipulator(request.user, - ratings_required=comments.RATINGS_REQUIRED in option_list, - ratings_range=rating_range, - num_rating_choices=len(rating_choices)) - errors = manipulator.get_validation_errors(new_data) - # If user gave correct username/password and wasn't already logged in, log them in - # so they don't have to enter a username/password again. - if manipulator.get_user() and new_data.has_key('password') and manipulator.get_user().check_password(new_data['password']): - sessions.start_web_session(manipulator.get_user_id(), request, response) - if errors or request.POST.has_key('preview'): - class CommentFormWrapper(formfields.FormWrapper): - def __init__(self, manipulator, new_data, errors, rating_choices): - formfields.FormWrapper.__init__(self, manipulator, new_data, errors) - self.rating_choices = rating_choices - def ratings(self): - field_list = [self['rating%d' % (i+1)] for i in range(len(rating_choices))] - for i, f in enumerate(field_list): - f.choice = rating_choices[i] - return field_list - comment = errors and '' or manipulator.get_comment(new_data) - comment_form = CommentFormWrapper(manipulator, new_data, errors, rating_choices) - t = template_loader.get_template('comments/preview') - c = Context(request, { - 'comment': comment, - 'comment_form': comment_form, - 'options': options, - 'target': target, - 'hash': security_hash, - 'rating_options': rating_options, - 'ratings_optional': comments.RATINGS_OPTIONAL in option_list, - 'ratings_required': comments.RATINGS_REQUIRED in option_list, - 'rating_range': rating_range, - 'rating_choices': rating_choices, - }) - elif request.POST.has_key('post'): - # If the IP is banned, mail the admins, do NOT save the comment, and - # serve up the "Thanks for posting" page as if the comment WAS posted. - if request.META['REMOTE_ADDR'] in BANNED_IPS: - mail_admins("Banned IP attempted to post comment", str(request.POST) + "\n\n" + str(request.META)) - else: - manipulator.do_html2python(new_data) - comment = manipulator.save(new_data) - return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) - else: - raise Http404, "The comment form didn't provide either 'preview' or 'post'" - response.write(t.render(c)) - return response - -def post_free_comment(request): - """ - Post a free comment (not requiring a log in) - - Redirects to `comments.comments.comment_was_posted` view on success. - - Templates: `comment_free_preview` - Context: - comment - comment being posted - comment_form - comment form object - options - comment options - target - comment target - hash - security hash (must be included in a posted form to succesfully - post a comment). - """ - if not request.POST: - raise Http404, "Only POSTs are allowed" - try: - options, target, security_hash = request.POST['options'], request.POST['target'], request.POST['gonzo'] - except KeyError: - raise Http404, "One or more of the required fields wasn't submitted" - if comments.get_security_hash(options, '', '', target) != security_hash: - raise Http404, "Somebody tampered with the comment form (security violation)" - content_type_id, object_id = target.split(':') # target is something like '52:5157' - content_type = contenttypes.get_object(id__exact=content_type_id) - try: - obj = content_type.get_object_for_this_type(id__exact=object_id) - except ObjectDoesNotExist: - raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" - option_list = options.split(',') - new_data = request.POST.copy() - new_data['content_type_id'] = content_type_id - new_data['object_id'] = object_id - new_data['ip_address'] = request.META['REMOTE_ADDR'] - new_data['is_public'] = comments.IS_PUBLIC in option_list - response = HttpResponse() - manipulator = PublicFreeCommentManipulator() - errors = manipulator.get_validation_errors(new_data) - if errors or request.POST.has_key('preview'): - comment = errors and '' or manipulator.get_comment(new_data) - t = template_loader.get_template('comments/free_preview') - c = Context(request, { - 'comment': comment, - 'comment_form': formfields.FormWrapper(manipulator, new_data, errors), - 'options': options, - 'target': target, - 'hash': security_hash, - }) - elif request.POST.has_key('post'): - # If the IP is banned, mail the admins, do NOT save the comment, and - # serve up the "Thanks for posting" page as if the comment WAS posted. - if request.META['REMOTE_ADDR'] in BANNED_IPS: - from django.core.mail import mail_admins - mail_admins("Practical joker", str(request.POST) + "\n\n" + str(request.META)) - else: - manipulator.do_html2python(new_data) - comment = manipulator.save(new_data) - return HttpResponseRedirect("/comments/posted/?c=%s:%s" % (content_type_id, object_id)) - else: - raise Http404, "The comment form didn't provide either 'preview' or 'post'" - response.write(t.render(c)) - return response - -def comment_was_posted(request): - """ - Display "comment was posted" success page - - Templates: `comment_posted` - Context: - object - The object the comment was posted on - """ - obj = None - if request.GET.has_key('c'): - content_type_id, object_id = request.GET['c'].split(':') - try: - content_type = contenttypes.get_object(id__exact=content_type_id) - obj = content_type.get_object_for_this_type(id__exact=object_id) - except ObjectDoesNotExist: - pass - t = template_loader.get_template('comments/posted') - c = Context(request, { - 'object': obj, - }) - return HttpResponse(t.render(c)) diff --git a/django/views/comments/karma.py b/django/views/comments/karma.py deleted file mode 100644 index 84f0ca852d..0000000000 --- a/django/views/comments/karma.py +++ /dev/null @@ -1,34 +0,0 @@ -from django.core import template_loader -from django.core.extensions import CMSContext as Context -from django.core.exceptions import Http404 -from django.models.comments import comments, karma -from django.utils.httpwrappers import HttpResponse - -def vote(request, comment_id, vote): - """ - Rate a comment (+1 or -1) - - Templates: `karma_vote_accepted` - Context: - comment - `comments.comments` object being rated - """ - rating = {'up': 1, 'down': -1}.get(vote, False) - if not rating: - raise Http404, "Invalid vote" - if request.user.is_anonymous(): - raise Http404, "Anonymous users cannot vote" - try: - comment = comments.get_object(id__exact=comment_id) - except comments.CommentDoesNotExist: - raise Http404, "Invalid comment ID" - if comment.user_id == request.user.id: - raise Http404, "No voting for yourself" - karma.vote(request.user.id, comment_id, rating) - # Reload comment to ensure we have up to date karma count - comment = comments.get_object(id__exact=comment_id) - t = template_loader.get_template('comments/karma_vote_accepted') - c = Context(request, { - 'comment': comment - }) - return HttpResponse(t.render(c)) diff --git a/django/views/comments/userflags.py b/django/views/comments/userflags.py deleted file mode 100644 index 051ecb19fe..0000000000 --- a/django/views/comments/userflags.py +++ /dev/null @@ -1,82 +0,0 @@ -from django.core import template_loader -from django.core.extensions import CMSContext as Context -from django.core.exceptions import Http404 -from django.models.comments import comments, moderatordeletions, userflags -from django.views.decorators.auth import login_required -from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect -from django.conf.settings import SITE_ID - -def flag(request, comment_id): - """ - Flags a comment. Confirmation on GET, action on POST. - - Templates: `comments/flag_verify`, `comments/flag_done` - Context: - comment - the flagged `comments.comments` object - """ - try: - comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) - except comments.CommentDoesNotExist: - raise Http404 - if request.POST: - userflags.flag(comment, request.user) - return HttpResponseRedirect('%sdone/' % request.path) - t = template_loader.get_template('comments/flag_verify') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) -flag = login_required(flag) - -def flag_done(request, comment_id): - try: - comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) - except comments.CommentDoesNotExist: - raise Http404 - t = template_loader.get_template('comments/flag_done') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) - -def delete(request, comment_id): - """ - Deletes a comment. Confirmation on GET, action on POST. - - Templates: `comments/delete_verify`, `comments/delete_done` - Context: - comment - the flagged `comments.comments` object - """ - try: - comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) - except comments.CommentDoesNotExist: - raise Http404 - if not comments.user_is_moderator(request.user): - raise Http404 - if request.POST: - # If the comment has already been removed, silently fail. - if not comment.is_removed: - comment.is_removed = True - comment.save() - m = moderatordeletions.ModeratorDeletion(None, request.user.id, comment.id, None) - m.save() - return HttpResponseRedirect('%sdone/' % request.path) - t = template_loader.get_template('comments/delete_verify') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) -delete = login_required(delete) - -def delete_done(request, comment_id): - try: - comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) - except comments.CommentDoesNotExist: - raise Http404 - t = template_loader.get_template('comments/delete_done') - c = Context(request, { - 'comment': comment, - }) - return HttpResponse(t.render(c)) -- cgit v1.3