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 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 ac7b40c..344ad0c 100644 --- a/ella_flatcomments/models.py +++ b/ella_flatcomments/models.py @@ -1,5 +1,7 @@ +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 +19,10 @@ redis = Redis(**comments_settings.REDIS) +EDIT_TIMER_ENABLED = getattr(comments_settings, 'EDIT_TIMER_ENABLED', False) +EDIT_TIMER_MINUTES = getattr(comments_settings, 'EDIT_TIMER_MINUTES', 15) + + class CommentList(object): @classmethod def for_object(cls, content_object, reversed=False): @@ -128,6 +134,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) @@ -158,7 +171,13 @@ 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): + # 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) def moderate(self, user=None, commit=True): @@ -178,3 +197,34 @@ def save(self, **kwargs): if self.submit_date is None: 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 the creation time. + ''' + age = timezone.now() - 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 = timezone.now() - 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/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 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