Skip to content

Commit

Permalink
Added rudimentary post locking.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurora0000 committed Jun 5, 2015
1 parent c67b26f commit d98c1b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
18 changes: 18 additions & 0 deletions forums/migrations/0024_post_is_locked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):
dependencies = [
('forums', '0023_auto_20150531_1225'),
]

operations = [
migrations.AddField(
model_name='post',
name='is_locked',
field=models.NullBooleanField(),
),
]
5 changes: 5 additions & 0 deletions forums/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ class Post(models.Model):
# Tags are stored in their own table, the IDs of those are here.
tag_ids = models.ManyToManyField(Tag, related_name='posts', blank=True)

is_locked = models.NullBooleanField(blank=True)

# END Topics only fields #
# These fields apply to replies only #

reply_to = models.ForeignKey('Post', related_name='replies', blank=True, null=True, editable=False)

# END Replies only fields #

def was_edited(self):
return True if self.last_edit_date != self.post_date else False

Expand Down
3 changes: 2 additions & 1 deletion forums/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class Meta:
model = Post
fields = ('id', 'title', 'tag_ids', 'author', 'author_name',
'contents', 'post_date', 'last_edit_date', 'reply_count',
'was_edited', 'avatar_url', 'contents_marked_up')
'was_edited', 'avatar_url', 'contents_marked_up',
'is_locked')

read_only_fields = ('replies',)

Expand Down
7 changes: 5 additions & 2 deletions forums/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class TopicList(generics.ListCreateAPIView):
throttle_classes = (StandardThrottle,)

def perform_create(self, serializer):
serializer.save(author=self.request.user, is_topic=True)
serializer.save(author=self.request.user, is_topic=True, is_locked=False)


class TopicListReverse(generics.ListAPIView):
Expand Down Expand Up @@ -120,7 +120,10 @@ class ReplyList(generics.ListCreateAPIView):
throttle_classes = (StandardThrottle,)

def perform_create(self, serializer):
serializer.save(author=self.request.user, reply_to=Post.objects.get(id=self.kwargs['reply_to'], is_topic=True))
post = Post.objects.get(id=self.kwargs['reply_to'], is_topic=True)
if post.is_locked:
return
serializer.save(author=self.request.user, reply_to=post)

def get_queryset(self):
return Post.objects.all().filter(is_topic=False, reply_to=self.kwargs['reply_to'])
Expand Down

0 comments on commit d98c1b8

Please sign in to comment.