Skip to content
Open
14 changes: 14 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions ella_flatcomments/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
50 changes: 50 additions & 0 deletions ella_flatcomments/models.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this returning a string? what possible use case is there? Return a datetime and use regular methods for rendering it as text

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the utility of returning this as a string. I'm also unable to find any other code that consumes the 'get_remaining_edit_time' function. I'll see if I can track down the original author and figure out the reasoning

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case just remove it, we can add it later if we find we need it - the less code the better :)

'''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

5 changes: 4 additions & 1 deletion ella_flatcomments/urls.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 7 additions & 0 deletions ella_flatcomments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down