Browse Source

thread has_poll flag

Rafał Pitoń 8 years ago
parent
commit
90a2965fc1

+ 6 - 0
misago/threads/api/threadpoll.py

@@ -79,6 +79,9 @@ class ViewSet(viewsets.ViewSet):
             for choice in instance.choices:
                 choice['selected'] = False
 
+            thread.has_poll = True
+            thread.save()
+
             return Response(PollSerializer(instance).data)
         else:
             return Response(serializer.errors, status=400)
@@ -110,6 +113,9 @@ class ViewSet(viewsets.ViewSet):
 
         thread.poll.delete()
 
+        thread.has_poll = False
+        thread.save()
+
         return Response({
             'can_start_poll': can_start_poll(request.user, thread)
         })

+ 1 - 1
misago/threads/migrations/0001_initial.py

@@ -77,6 +77,7 @@ class Migration(migrations.Migration):
                 ('slug', models.CharField(max_length=255)),
                 ('replies', models.PositiveIntegerField(default=0, db_index=True)),
                 ('has_events', models.BooleanField(default=False)),
+                ('has_poll', models.BooleanField(default=False)),
                 ('has_reported_posts', models.BooleanField(default=False)),
                 ('has_open_reports', models.BooleanField(default=False)),
                 ('has_unapproved_posts', models.BooleanField(default=False)),
@@ -89,7 +90,6 @@ class Migration(migrations.Migration):
                 ('last_poster_name', models.CharField(max_length=255, null=True, blank=True)),
                 ('last_poster_slug', models.CharField(max_length=255, null=True, blank=True)),
                 ('weight', models.PositiveIntegerField(default=0)),
-                ('is_poll', models.BooleanField(default=False)),
                 ('is_unapproved', models.BooleanField(default=False, db_index=True)),
                 ('is_hidden', models.BooleanField(default=False)),
                 ('is_closed', models.BooleanField(default=False)),

+ 7 - 1
misago/threads/models/thread.py

@@ -1,3 +1,4 @@
+from django.core.exceptions import ObjectDoesNotExist
 from django.db import models, transaction
 from django.utils.encoding import python_2_unicode_compatible
 from django.utils.translation import ugettext_lazy as _
@@ -35,6 +36,7 @@ class Thread(models.Model):
     replies = models.PositiveIntegerField(default=0, db_index=True)
 
     has_events = models.BooleanField(default=False)
+    has_poll = models.BooleanField(default=False)
     has_reported_posts = models.BooleanField(default=False)
     has_open_reports = models.BooleanField(default=False)
     has_unapproved_posts = models.BooleanField(default=False)
@@ -79,7 +81,6 @@ class Thread(models.Model):
 
     weight = models.PositiveIntegerField(default=THREAD_WEIGHT_DEFAULT)
 
-    is_poll = models.BooleanField(default=False)
     is_unapproved = models.BooleanField(default=False, db_index=True)
     is_hidden = models.BooleanField(default=False)
     is_closed = models.BooleanField(default=False)
@@ -124,6 +125,11 @@ class Thread(models.Model):
         move_thread.send(sender=self)
 
     def synchronize(self):
+        try:
+            self.has_poll = bool(self.poll)
+        except ObjectDoesNotExist:
+            self.has_poll = False
+
         self.replies = self.post_set.filter(is_event=False, is_unapproved=False).count()
         if self.replies > 0:
             self.replies -= 1

+ 1 - 0
misago/threads/serializers/thread.py

@@ -171,6 +171,7 @@ class ThreadsListSerializer(ThreadSerializer):
             'category',
             'title',
             'replies',
+            'has_poll',
             'has_unapproved_posts',
             'started_on',
             'last_post_on',

+ 16 - 1
misago/threads/tests/test_thread_model.py

@@ -6,7 +6,7 @@ from django.utils import timezone
 
 from misago.categories.models import Category
 
-from ..models import Post, Thread, ThreadParticipant
+from ..models import Poll, Post, Thread, ThreadParticipant
 
 
 class ThreadModelTests(TestCase):
@@ -215,6 +215,21 @@ class ThreadModelTests(TestCase):
         self.assertFalse(self.thread.last_post_is_event)
         self.assertFalse(self.thread.has_events)
 
+        # has poll flag
+        self.assertFalse(self.thread.has_poll)
+
+        Poll.objects.create(
+            thread=self.thread,
+            category=self.category,
+            poster_name='test',
+            poster_slug='test',
+            poster_ip='127.0.0.1',
+            choices=[]
+        )
+
+        self.thread.synchronize()
+        self.assertTrue(self.thread.has_poll)
+
     def test_set_first_post(self):
         """set_first_post sets first post and poster data on thread"""
         User = get_user_model()

+ 4 - 0
misago/threads/tests/test_thread_pollcreate_api.py

@@ -306,3 +306,7 @@ class ThreadPollCreateTests(ThreadPollApiTestCase):
 
         self.assertEqual(len(poll.choices), 3)
         self.assertEqual(len(set([c['hash'] for c in poll.choices])), 3)
+
+        # api set poll flag on thread to true
+        thread = Thread.objects.get(pk=self.thread.pk)
+        self.assertTrue(thread.has_poll)

+ 9 - 1
misago/threads/tests/test_thread_polldelete_api.py

@@ -3,7 +3,7 @@ from datetime import timedelta
 from django.urls import reverse
 from django.utils import timezone
 
-from ..models import Poll, PollVote
+from ..models import Poll, PollVote, Thread
 from .test_thread_poll_api import ThreadPollApiTestCase
 
 
@@ -155,6 +155,10 @@ class ThreadPollDeleteTests(ThreadPollApiTestCase):
         self.assertEqual(Poll.objects.count(), 0)
         self.assertEqual(PollVote.objects.count(), 0)
 
+        # api set poll flag on thread to False
+        thread = Thread.objects.get(pk=self.thread.pk)
+        self.assertFalse(thread.has_poll)
+
     def test_other_user_poll_delete(self):
         """api deletes other user's poll and associated votes, even if its over"""
         self.override_acl({
@@ -173,3 +177,7 @@ class ThreadPollDeleteTests(ThreadPollApiTestCase):
 
         self.assertEqual(Poll.objects.count(), 0)
         self.assertEqual(PollVote.objects.count(), 0)
+
+        # api set poll flag on thread to False
+        thread = Thread.objects.get(pk=self.thread.pk)
+        self.assertFalse(thread.has_poll)