Browse Source

serialize participants on private threads

Rafał Pitoń 8 years ago
parent
commit
b8a1868a55

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

@@ -1,4 +1,5 @@
 from .moderation import *
 from .moderation import *
+from .threadparticipant import *
 from .thread import *
 from .thread import *
 from .post import *
 from .post import *
 from .postedit import *
 from .postedit import *

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

@@ -6,10 +6,12 @@ from misago.categories.serializers import BasicCategorySerializer
 
 
 from ..models import Thread
 from ..models import Thread
 from .poll import PollSerializer
 from .poll import PollSerializer
+from .threadparticipant import ThreadParticipantSerializer
 
 
 
 
 __all__ = [
 __all__ = [
     'ThreadSerializer',
     'ThreadSerializer',
+    'PrivateThreadSerializer',
     'ThreadsListSerializer',
     'ThreadsListSerializer',
 ]
 ]
 
 
@@ -118,6 +120,42 @@ class ThreadSerializer(serializers.ModelSerializer):
             return None
             return None
 
 
 
 
+class PrivateThreadSerializer(ThreadSerializer):
+    participants = serializers.SerializerMethodField()
+
+    class Meta:
+        model = Thread
+        fields = (
+            'id',
+            'category',
+            'title',
+            'replies',
+            'has_unapproved_posts',
+            'started_on',
+            'last_post_on',
+            'last_post',
+            'last_poster_name',
+            'is_unapproved',
+            'is_hidden',
+            'is_closed',
+            'weight',
+
+            'acl',
+            'is_new',
+            'is_read',
+            'participants',
+            'path',
+            'poll',
+            'subscription',
+
+            'api',
+            'url',
+        )
+
+    def get_participants(self, obj):
+        return ThreadParticipantSerializer(obj.participants_list, many=True).data
+
+
 class ThreadsListSerializer(ThreadSerializer):
 class ThreadsListSerializer(ThreadSerializer):
     category = serializers.PrimaryKeyRelatedField(read_only=True)
     category = serializers.PrimaryKeyRelatedField(read_only=True)
     last_post = serializers.PrimaryKeyRelatedField(read_only=True)
     last_post = serializers.PrimaryKeyRelatedField(read_only=True)

+ 36 - 0
misago/threads/serializers/threadparticipant.py

@@ -0,0 +1,36 @@
+from rest_framework import serializers
+
+from ..models import ThreadParticipant
+
+
+__all__ = ['ThreadParticipantSerializer']
+
+
+class ThreadParticipantSerializer(serializers.ModelSerializer):
+    id = serializers.SerializerMethodField()
+    username = serializers.SerializerMethodField()
+    avatar_hash = serializers.SerializerMethodField()
+
+    url = serializers.SerializerMethodField()
+
+    class Meta:
+        model = ThreadParticipant
+        fields = (
+            'id',
+            'username',
+            'avatar_hash',
+            'url',
+            'is_owner'
+        )
+
+    def get_id(self, obj):
+        return obj.user.id
+
+    def get_username(self, obj):
+        return obj.user.username
+
+    def get_avatar_hash(self, obj):
+        return obj.user.avatar_hash
+
+    def get_url(self, obj):
+        return obj.user.get_absolute_url()

+ 31 - 3
misago/threads/tests/test_privatethreads_api.py

@@ -119,14 +119,38 @@ class PrivateThreadsApiGetTests(PrivateThreadsTestCase):
         ThreadParticipant.objects.set_owner(self.thread, self.user)
         ThreadParticipant.objects.set_owner(self.thread, self.user)
 
 
         response = self.client.get(self.api_url)
         response = self.client.get(self.api_url)
-        self.assertContains(response, self.thread.title)
+        self.assertEqual(response.status_code, 200)
+
+        response_json = response.json()
+        self.assertEqual(response_json['title'], self.thread.title)
+        self.assertEqual(response_json['participants'], [
+            {
+                'id': self.user.id,
+                'username': self.user.username,
+                'avatar_hash': self.user.avatar_hash,
+                'url': self.user.get_absolute_url(),
+                'is_owner': True
+            }
+        ])
 
 
     def test_can_see_participant(self):
     def test_can_see_participant(self):
         """user can see thread he is participant of"""
         """user can see thread he is participant of"""
         ThreadParticipant.objects.add_participants(self.thread, [self.user])
         ThreadParticipant.objects.add_participants(self.thread, [self.user])
 
 
         response = self.client.get(self.api_url)
         response = self.client.get(self.api_url)
-        self.assertContains(response, self.thread.title)
+        self.assertEqual(response.status_code, 200)
+
+        response_json = response.json()
+        self.assertEqual(response_json['title'], self.thread.title)
+        self.assertEqual(response_json['participants'], [
+            {
+                'id': self.user.id,
+                'username': self.user.username,
+                'avatar_hash': self.user.avatar_hash,
+                'url': self.user.get_absolute_url(),
+                'is_owner': False
+            }
+        ])
 
 
     def test_mod_can_see_reported(self):
     def test_mod_can_see_reported(self):
         """moderator can see private thread that has reports"""
         """moderator can see private thread that has reports"""
@@ -138,4 +162,8 @@ class PrivateThreadsApiGetTests(PrivateThreadsTestCase):
         self.thread.save()
         self.thread.save()
 
 
         response = self.client.get(self.api_url)
         response = self.client.get(self.api_url)
-        self.assertContains(response, self.thread.title)
+        self.assertEqual(response.status_code, 200)
+
+        response_json = response.json()
+        self.assertEqual(response_json['title'], self.thread.title)
+        self.assertEqual(response_json['participants'], [])

+ 9 - 3
misago/threads/viewmodels/thread.py

@@ -11,7 +11,7 @@ from ..models import Poll, Thread
 from ..participants import make_participants_aware
 from ..participants import make_participants_aware
 from ..permissions.privatethreads import allow_use_private_threads, allow_see_private_thread
 from ..permissions.privatethreads import allow_use_private_threads, allow_see_private_thread
 from ..permissions.threads import allow_see_thread
 from ..permissions.threads import allow_see_thread
-from ..serializers import ThreadSerializer
+from ..serializers import PrivateThreadSerializer, ThreadSerializer
 from ..subscriptions import make_subscription_aware
 from ..subscriptions import make_subscription_aware
 from ..threadtypes import trees_map
 from ..threadtypes import trees_map
 
 
@@ -79,10 +79,10 @@ class ViewModel(BaseViewModel):
         return thread_path
         return thread_path
 
 
     def get_root_name(self):
     def get_root_name(self):
-        raise NotImplementedError('Thread view model has to implement get_root_name()')
+        raise NotImplementedError("Thread view model has to implement get_root_name()")
 
 
     def get_frontend_context(self):
     def get_frontend_context(self):
-        return ThreadSerializer(self._model).data
+        raise NotImplementedError("Thread view model has to implement get_frontend_context()")
 
 
     def get_template_context(self):
     def get_template_context(self):
         return {
         return {
@@ -114,6 +114,9 @@ class ForumThread(ViewModel):
     def get_root_name(self):
     def get_root_name(self):
         return _("Threads")
         return _("Threads")
 
 
+    def get_frontend_context(self):
+        return ThreadSerializer(self._model).data
+
 
 
 class PrivateThread(ViewModel):
 class PrivateThread(ViewModel):
     def get_thread(self, request, pk, slug=None, select_for_update=False):
     def get_thread(self, request, pk, slug=None, select_for_update=False):
@@ -140,3 +143,6 @@ class PrivateThread(ViewModel):
 
 
     def get_root_name(self):
     def get_root_name(self):
         return _("Private threads")
         return _("Private threads")
+
+    def get_frontend_context(self):
+        return PrivateThreadSerializer(self._model).data