From 1afbc049a0028faaaff1ab6280f22fddbf74528c Mon Sep 17 00:00:00 2001 From: Luis Artola Date: Thu, 22 Aug 2013 13:49:44 -0700 Subject: [PATCH 1/8] Added edit timer to FlatComments. By default, users have 15 minutes from the moment the post was created to make any changes. After that, the edit is not allowed. Moderators can always edit regardless of this time constraint. --- ella_flatcomments/models.py | 32 ++++++++++++++++++++++++++++++++ ella_flatcomments/views.py | 7 +++++++ 2 files changed, 39 insertions(+) diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index ac7b40c..0cc738c 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -1,5 +1,8 @@ +from datetime import datetime, timedelta + from redis import Redis +from django.conf import settings from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site @@ -17,6 +20,9 @@ redis = Redis(**comments_settings.REDIS) +EDIT_TIMER_MINUTES = getattr(settings, 'EDIT_TIMER_MINUTES', 15) + + class CommentList(object): @classmethod def for_object(cls, content_object, reversed=False): @@ -178,3 +184,29 @@ def save(self, **kwargs): if self.submit_date is None: self.submit_date = timezone.now() super(FlatComment, self).save(**kwargs) + + def is_edit_timer_expired(self): + '''is_edit_timer_expired() -> bool + Check whether the comment is still within the allowed edit time + from the creation time. + ''' + age = datetime.now(self.submit_date.tzinfo) - self.submit_date + if age >= timedelta(minutes=EDIT_TIMER_MINUTES): + return True + return False + + def get_remaining_edit_time(self): + '''get_remaining_edit_time() -> str + Returns the remaining edit time from comment creation. The returned + string is formatted as HH:MM:SS, e.g. 0:01:23 + ''' + age = datetime.now(self.submit_date.tzinfo) - self.submit_date + edit_time = timedelta(minutes=EDIT_TIMER_MINUTES) + if age >= edit_time: + return '0:00:00' + seconds = edit_time.total_seconds() - age.total_seconds() + remaining = timedelta(seconds=seconds) + text = str(remaining) + text = text.split('.')[0] + return text + diff --git a/ella_flatcomments/views.py b/ella_flatcomments/views.py index 5312205..c9cf803 100644 --- a/ella_flatcomments/views.py +++ b/ella_flatcomments/views.py @@ -73,6 +73,13 @@ def post_comment(request, context, comment_id=None): if comment.user != request.user and not comments_settings.IS_MODERATOR_FUNC(request.user): return HttpResponseForbidden("You cannot edit other people's comments.") + # Don't allow users to edit a comment after the allowed edit time has expired + if comment.user == request.user and not comments_settings.IS_MODERATOR_FUNC(request.user) and comment.is_edit_timer_expired(): + if request.is_ajax(): + context.update({'comment': comment}) + return TemplateResponse(request, get_template('comment_detail_async.html', context['object']), context) + return HttpResponseForbidden("The allowed time to edit the comment has expired.") + data, files = None, None if request.method == 'POST': data, files = request.POST, request.FILES From 0fd58d1034de0078c93b75b335a2699ca7d66cec Mon Sep 17 00:00:00 2001 From: Luis Artola Date: Tue, 1 Oct 2013 16:29:11 -0700 Subject: [PATCH 2/8] Checking whether edit timer is enabled in settings or not. --- ella_flatcomments/models.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index 0cc738c..debce9f 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -20,6 +20,7 @@ redis = Redis(**comments_settings.REDIS) +EDIT_TIMER_ENABLED = getattr(settings, 'EDIT_TIMER_ENABLED', False) EDIT_TIMER_MINUTES = getattr(settings, 'EDIT_TIMER_MINUTES', 15) @@ -185,6 +186,11 @@ def save(self, **kwargs): self.submit_date = timezone.now() super(FlatComment, self).save(**kwargs) + def has_edit_timer(self): + '''has_edit_timer() -> bool + ''' + return EDIT_TIMER_ENABLED + def is_edit_timer_expired(self): '''is_edit_timer_expired() -> bool Check whether the comment is still within the allowed edit time From 2e0135526e5dc6506b7fe032a26368c8fa0d0f1b Mon Sep 17 00:00:00 2001 From: Luis Artola Date: Tue, 14 Jan 2014 12:56:48 -0800 Subject: [PATCH 3/8] Accounting for modules deprecated in Django 1.6, trying to be backwards compatible wherever possible. --- ella_flatcomments/urls.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ella_flatcomments/urls.py b/ella_flatcomments/urls.py index 7075dd6..894d27f 100644 --- a/ella_flatcomments/urls.py +++ b/ella_flatcomments/urls.py @@ -1,6 +1,9 @@ from django.template.defaultfilters import slugify from django.utils.translation import ugettext as _ -from django.conf.urls.defaults import patterns, url +try: + from django.conf.urls import patterns, url +except: + from django.conf.urls.defaults import patterns, url from ella_flatcomments.views import list_comments, post_comment, comment_detail, moderate_comment, lock_comments, unlock_comments From 6897ddcd61fb05188ba41edd83a26edddd674124 Mon Sep 17 00:00:00 2001 From: colony005 Date: Tue, 29 Jul 2014 09:09:05 -0700 Subject: [PATCH 4/8] Create circle.yml --- circle.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 circle.yml diff --git a/circle.yml b/circle.yml new file mode 100644 index 0000000..4fcf64e --- /dev/null +++ b/circle.yml @@ -0,0 +1,14 @@ +checkout: + post: + - git submodule sync + - git submodule update --init + - git clone git@github.com:WhiskeyMedia/scout.git scout.git +dependencies: + override: + + cache_directories: + - venv/src + - ~/.pip/cache +test: + override: +# - python setup.py test From fba7b91576808716f89e4d65795f88fec8fc1414 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Wed, 17 Dec 2014 11:59:08 -0800 Subject: [PATCH 5/8] adding comment reinstate methods --- ella_flatcomments/models.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index debce9f..6129451 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -135,6 +135,13 @@ def moderate_comment(self, comment, user=None, commit=True): redis.lrem(self._key, comment.id) comment_was_moderated.send(FlatComment, comment=comment, user=user) + def reinstate_comment(self, comment, request=None): + # comments are removed from the redis cache when moderated + # to be visible again, they must be reinstated as well + redis.lrem(self._key, comment.id) + redis.lpush(self._key, comment.id) + return True, None + def locked(self): return redis.sismember(comments_settings.LOCKED_KEY, self._id) @@ -165,6 +172,10 @@ def _comment_list(self, reversed=False): self.__comment_list = CommentList(self.content_type, self.object_id, reversed) return self.__comment_list + def reinstate(self, request=None): + # re instate this comment for a user who has been unbanned + return self._comment_list().reinstate_comment(self, request) + def post(self, request=None): return self._comment_list().post_comment(self, request) From ce623b2bf757d456801b115114620fbf5c9439f0 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 18 Dec 2014 09:21:33 -0800 Subject: [PATCH 6/8] added submit_date to unsaved FlatComments in order to make it possible to check for comment flooding --- ella_flatcomments/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index 6129451..74e94e2 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -1,5 +1,5 @@ from datetime import datetime, timedelta - +from ella.utils.timezone import now from redis import Redis from django.conf import settings @@ -177,6 +177,9 @@ def reinstate(self, request=None): return self._comment_list().reinstate_comment(self, request) def post(self, request=None): + # update TESTED-442 + # must provide temporary submit_date to check if the comment is flooding + self.submit_date = now() return self._comment_list().post_comment(self, request) def moderate(self, user=None, commit=True): From 4910ef2d29fdc4a23aaa80a80745ffc673e25be4 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 18 Dec 2014 09:29:43 -0800 Subject: [PATCH 7/8] cleaning up, removing redundant import to ella timezone utils --- ella_flatcomments/models.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index 74e94e2..cbe5cbb 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -1,5 +1,4 @@ from datetime import datetime, timedelta -from ella.utils.timezone import now from redis import Redis from django.conf import settings @@ -179,7 +178,7 @@ def reinstate(self, request=None): def post(self, request=None): # update TESTED-442 # must provide temporary submit_date to check if the comment is flooding - self.submit_date = now() + self.submit_date = timezone.now() return self._comment_list().post_comment(self, request) def moderate(self, user=None, commit=True): From f37879d737792533ff2623b6627ad1a9f3f73be2 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 18 Dec 2014 10:25:43 -0800 Subject: [PATCH 8/8] using ella timezone utils, shifting comment edit settings to ella flatcomment confs --- ella_flatcomments/conf.py | 3 +++ ella_flatcomments/models.py | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ella_flatcomments/conf.py b/ella_flatcomments/conf.py index bbe0b42..b083fb4 100644 --- a/ella_flatcomments/conf.py +++ b/ella_flatcomments/conf.py @@ -17,4 +17,7 @@ IS_MODERATOR_FUNC = lambda u: u.is_staff +EDIT_TIMER_ENABLED = False +EDIT_TIMER_MINUTES = 15 + comments_settings = Settings('ella_flatcomments.conf', 'COMMENTS') diff --git a/ella_flatcomments/models.py b/ella_flatcomments/models.py index cbe5cbb..344ad0c 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -19,8 +19,8 @@ redis = Redis(**comments_settings.REDIS) -EDIT_TIMER_ENABLED = getattr(settings, 'EDIT_TIMER_ENABLED', False) -EDIT_TIMER_MINUTES = getattr(settings, 'EDIT_TIMER_MINUTES', 15) +EDIT_TIMER_ENABLED = getattr(comments_settings, 'EDIT_TIMER_ENABLED', False) +EDIT_TIMER_MINUTES = getattr(comments_settings, 'EDIT_TIMER_MINUTES', 15) class CommentList(object): @@ -176,7 +176,6 @@ def reinstate(self, request=None): return self._comment_list().reinstate_comment(self, request) def post(self, request=None): - # update TESTED-442 # must provide temporary submit_date to check if the comment is flooding self.submit_date = timezone.now() return self._comment_list().post_comment(self, request) @@ -209,7 +208,7 @@ def is_edit_timer_expired(self): Check whether the comment is still within the allowed edit time from the creation time. ''' - age = datetime.now(self.submit_date.tzinfo) - self.submit_date + age = timezone.now() - self.submit_date if age >= timedelta(minutes=EDIT_TIMER_MINUTES): return True return False @@ -219,7 +218,7 @@ def get_remaining_edit_time(self): Returns the remaining edit time from comment creation. The returned string is formatted as HH:MM:SS, e.g. 0:01:23 ''' - age = datetime.now(self.submit_date.tzinfo) - self.submit_date + age = timezone.now() - self.submit_date edit_time = timedelta(minutes=EDIT_TIMER_MINUTES) if age >= edit_time: return '0:00:00'