Browse Source

small cleanup on viewmodels

Rafał Pitoń 8 years ago
parent
commit
bb448e4934

+ 1 - 1
misago/threads/api/threadendpoints/merge.py

@@ -38,7 +38,7 @@ def thread_merge_endpoint(request, thread, viewmodel):
         return Response({'detail': _("You can't merge thread with itself.")}, status=400)
 
     try:
-        other_thread = viewmodel(request, other_thread_id, select_for_update=True).thread
+        other_thread = viewmodel(request, other_thread_id, select_for_update=True).model
     except PermissionDenied as e:
         return Response({
             'detail': e.args[0]

+ 11 - 11
misago/threads/api/threadposts.py

@@ -71,12 +71,12 @@ class ViewSet(viewsets.ViewSet):
     @list_route(methods=['post'], url_path='merge')
     @transaction.atomic
     def merge(self, request, thread_pk):
-        thread = self.get_thread_for_update(request, thread_pk).thread
+        thread = self.get_thread_for_update(request, thread_pk).model
         return posts_merge_endpoint(request, thread)
 
     @transaction.atomic
     def create(self, request, thread_pk):
-        thread = self.get_thread_for_update(request, thread_pk).thread
+        thread = self.get_thread_for_update(request, thread_pk).model
         allow_reply_thread(request.user, thread)
 
         post = Post(thread=thread, category=thread.category)
@@ -108,14 +108,14 @@ class ViewSet(viewsets.ViewSet):
     @transaction.atomic
     def update(self, request, thread_pk, pk):
         thread = self.get_thread_for_update(request, thread_pk)
-        post = self.get_post_for_update(request, thread, pk).post
+        post = self.get_post_for_update(request, thread, pk).model
 
         allow_edit_post(request.user, post)
 
         posting = PostingEndpoint(
             request,
             PostingEndpoint.EDIT,
-            thread=thread.thread,
+            thread=thread.model,
             post=post
         )
 
@@ -140,7 +140,7 @@ class ViewSet(viewsets.ViewSet):
     @transaction.atomic
     def partial_update(self, request, thread_pk, pk):
         thread = self.get_thread_for_update(request, thread_pk)
-        post = self.get_post_for_update(request, thread, pk).post
+        post = self.get_post_for_update(request, thread, pk).model
 
         if post.is_event:
             return event_patch_endpoint(request, post)
@@ -150,7 +150,7 @@ class ViewSet(viewsets.ViewSet):
     @transaction.atomic
     def delete(self, request, thread_pk, pk):
         thread = self.get_thread_for_update(request, thread_pk)
-        post = self.get_post_for_update(request, thread, pk).post
+        post = self.get_post_for_update(request, thread, pk).model
 
         if post.is_event:
             allow_delete_event(request.user, post)
@@ -159,8 +159,8 @@ class ViewSet(viewsets.ViewSet):
 
         moderation.delete_post(request.user, post)
 
-        thread.thread.synchronize()
-        thread.thread.save()
+        thread.model.synchronize()
+        thread.model.save()
 
         thread.category.synchronize()
         thread.category.save()
@@ -170,7 +170,7 @@ class ViewSet(viewsets.ViewSet):
     @detail_route(methods=['get'], url_path='editor')
     def post_editor(self, request, thread_pk, pk):
         thread = self.thread(request, get_int_or_404(thread_pk))
-        post = self.post(request, thread, get_int_or_404(pk)).post
+        post = self.post(request, thread, get_int_or_404(pk)).model
 
         allow_edit_post(request.user, post)
 
@@ -186,10 +186,10 @@ class ViewSet(viewsets.ViewSet):
     @list_route(methods=['get'], url_path='editor')
     def reply_editor(self, request, thread_pk):
         thread = self.thread(request, get_int_or_404(thread_pk))
-        allow_reply_thread(request.user, thread.thread)
+        allow_reply_thread(request.user, thread.model)
 
         if 'reply' in request.query_params:
-            reply_to = self.post(request, thread, get_int_or_404(request.query_params['reply'])).post
+            reply_to = self.post(request, thread, get_int_or_404(request.query_params['reply'])).model
 
             if reply_to.is_event:
                 raise PermissionDenied(_("You can't reply to events."))

+ 3 - 3
misago/threads/api/threads.py

@@ -56,12 +56,12 @@ class ViewSet(viewsets.ViewSet):
 
     @transaction.atomic
     def partial_update(self, request, pk):
-        thread = self.get_thread_for_update(request, pk).thread
+        thread = self.get_thread_for_update(request, pk).model
         return thread_patch_endpoint(request, thread)
 
     @transaction.atomic
     def destroy(self, request, pk):
-        thread = self.get_thread_for_update(request, pk).thread
+        thread = self.get_thread_for_update(request, pk).model
 
         if thread.acl.get('can_hide') == 2:
             moderation.delete_thread(request.user, thread)
@@ -101,7 +101,7 @@ class ThreadViewSet(ViewSet):
     @detail_route(methods=['post'], url_path='merge')
     @transaction.atomic
     def thread_merge(self, request, pk):
-        thread = self.get_thread_for_update(request, pk).thread
+        thread = self.get_thread_for_update(request, pk).model
         return thread_merge_endpoint(request, thread, self.thread)
 
     @list_route(methods=['post'], url_path='merge')

+ 25 - 9
misago/threads/viewmodels/category.py

@@ -9,12 +9,28 @@ from misago.core.shortcuts import validate_slug
 
 class ViewModel(object):
     def __init__(self, request, **kwargs):
-        self.categories = self.get_categories(request)
-        map(lambda c: add_acl(request.user, c), self.categories)
+        self._categories = self.get_categories(request)
+        add_acl(request.user, self._categories)
 
-        self.category = self.get_category(request, self.categories, **kwargs)
-        self.subcategories = list(filter(self.category.has_child, self.categories))
-        self.children = list(filter(lambda s: s.parent_id == self.category.pk, self.subcategories))
+        self._model = self.get_category(request, self._categories, **kwargs)
+        self._subcategories = list(filter(self._model.has_child, self._categories))
+        self._children = list(filter(lambda s: s.parent_id == self._model.pk, self._subcategories))
+
+    @property
+    def model(self):
+        return self._model
+
+    @property
+    def categories(self):
+        return self._categories
+
+    @property
+    def subcategories(self):
+        return self._subcategories
+
+    @property
+    def children(self):
+        return self._children
 
     def get_categories(self, request):
         raise NotImplementedError('Category view model has to implement get_categories(request)')
@@ -24,13 +40,13 @@ class ViewModel(object):
 
     def get_frontend_context(self):
         return {
-            'CATEGORIES': BasicCategorySerializer(self.categories, many=True).data
+            'CATEGORIES': BasicCategorySerializer(self._categories, many=True).data
         }
 
     def get_template_context(self):
         return {
-            'category': self.category,
-            'subcategories': self.children
+            'category': self._model,
+            'subcategories': self._children
         }
 
 
@@ -45,7 +61,7 @@ class ThreadsRootCategory(ViewModel):
 class ThreadsCategory(ThreadsRootCategory):
     @property
     def level(self):
-        return self.category.level
+        return self._model.level
 
     def get_category(self, request, categories, **kwargs):
         for category in categories:

+ 19 - 5
misago/threads/viewmodels/post.py

@@ -7,14 +7,28 @@ from ..permissions.threads import exclude_invisible_posts
 
 class ViewModel(object):
     def __init__(self, request, thread, pk, select_for_update=False):
-        post = self.get_post(request, thread, pk, select_for_update)
+        model = self.get_post(request, thread, pk, select_for_update)
 
-        add_acl(request.user, post)
+        add_acl(request.user, model)
 
-        self.post = post
+        self._model = model
+        self._thread = model.thread
+        self._category = model.category
+
+    @property
+    def model(self):
+        return self._model
+
+    @property
+    def thread(self):
+        return self._thread
+
+    @property
+    def category(self):
+        return self._category
 
     def get_post(self, request, thread, pk, select_for_update=False):
-        queryset = self.get_queryset(request, thread.thread)
+        queryset = self.get_queryset(request, thread.model)
         if select_for_update:
             queryset = queryset.select_for_update()
         else:
@@ -26,7 +40,7 @@ class ViewModel(object):
 
         post = get_object_or_404(queryset, pk=pk)
 
-        post.thread = thread.thread
+        post.thread = thread.model
         post.category = thread.category
 
         return post

+ 3 - 3
misago/threads/viewmodels/posts.py

@@ -11,7 +11,7 @@ from ..serializers import PostSerializer
 
 class ViewModel(object):
     def __init__(self, request, thread, page):
-        posts_queryset = self.get_queryset(request, thread.thread)
+        posts_queryset = self.get_queryset(request, thread.model)
 
         list_page = paginate(posts_queryset, page, settings.MISAGO_POSTS_PER_PAGE, settings.MISAGO_POSTS_TAIL)
         paginator = pagination_dict(list_page, include_page_range=False)
@@ -21,14 +21,14 @@ class ViewModel(object):
 
         for post in posts:
             post.category = thread.category
-            post.thread = thread.thread
+            post.thread = thread.model
 
             if post.poster and post.poster not in posters:
                 posters.append(post.poster)
 
         add_acl(request.user, posts)
 
-        make_posts_read_aware(request.user, thread.thread, posts)
+        make_posts_read_aware(request.user, thread.model, posts)
         make_users_status_aware(request.user, posters)
 
         self.posts = posts

+ 25 - 13
misago/threads/viewmodels/thread.py

@@ -17,21 +17,33 @@ BASE_RELATIONS = ('category', 'starter', 'starter__rank', 'starter__ban_cache',
 
 class ViewModel(object):
     def __init__(self, request, pk, slug=None, read_aware=False, subscription_aware=False, select_for_update=False):
-        thread = self.get_thread(request, pk, slug, select_for_update)
+        model = self.get_thread(request, pk, slug, select_for_update)
 
-        thread.path = self.get_thread_path(thread.category)
+        model.path = self.get_thread_path(model.category)
 
-        add_acl(request.user, thread.category)
-        add_acl(request.user, thread)
+        add_acl(request.user, model.category)
+        add_acl(request.user, model)
 
         if read_aware:
-            make_read_aware(request.user, thread)
+            make_read_aware(request.user, model)
         if subscription_aware:
-            make_subscription_aware(request.user, thread)
+            make_subscription_aware(request.user, model)
 
-        self.thread = thread
-        self.category = thread.category
-        self.path = thread.path
+        self._model = model
+        self._category = model.category
+        self._path = model.path
+
+    @property
+    def model(self):
+        return self._model
+
+    @property
+    def category(self):
+        return self._category
+
+    @property
+    def path(self):
+        return self._path
 
     def get_thread(self, request, pk, slug=None, select_for_update=False):
         raise NotImplementedError('Thread view model has to implement get_thread(request, pk, slug=None)')
@@ -56,13 +68,13 @@ class ViewModel(object):
         raise NotImplementedError('Thread view model has to implement get_root_name()')
 
     def get_frontend_context(self):
-        return ThreadSerializer(self.thread).data
+        return ThreadSerializer(self._model).data
 
     def get_template_context(self):
         return {
-            'thread': self.thread,
-            'category': self.category,
-            'breadcrumbs': self.path
+            'thread': self._model,
+            'category': self._category,
+            'breadcrumbs': self._path
         }
 
 

+ 4 - 4
misago/threads/viewmodels/threads.py

@@ -42,9 +42,9 @@ class ViewModel(object):
         self.allow_see_list(request, category, list_type)
 
         base_queryset = self.get_base_queryset(request, category.categories, list_type)
-        threads_categories = [category.category] + category.subcategories
+        threads_categories = [category.model] + category.subcategories
 
-        threads_queryset = self.get_remaining_threads_queryset(base_queryset, category.category, threads_categories)
+        threads_queryset = self.get_remaining_threads_queryset(base_queryset, category.model, threads_categories)
 
         list_page = paginate(threads_queryset, page, settings.MISAGO_THREADS_PER_PAGE, settings.MISAGO_THREADS_TAIL)
         paginator = pagination_dict(list_page, include_page_range=False)
@@ -52,7 +52,7 @@ class ViewModel(object):
         if list_page.number > 1:
             threads = list(list_page.object_list)
         else:
-            pinned_threads = list(self.get_pinned_threads(base_queryset, category.category, threads_categories))
+            pinned_threads = list(self.get_pinned_threads(base_queryset, category.model, threads_categories))
             threads = list(pinned_threads) + list(list_page.object_list)
 
         if list_type in ('new', 'unread'):
@@ -61,7 +61,7 @@ class ViewModel(object):
         else:
             threadstracker.make_threads_read_aware(request.user, threads)
 
-        add_categories_to_threads(category.category, category.categories, threads)
+        add_categories_to_threads(category.model, category.categories, threads)
         add_acl(request.user, threads)
         make_subscription_aware(request.user, threads)
 

+ 1 - 1
misago/threads/views/goto.py

@@ -16,7 +16,7 @@ class GotoView(View):
     read_aware=False
 
     def get(self, request, pk, slug, **kwargs):
-        thread = self.get_thread(request, pk, slug).thread
+        thread = self.get_thread(request, pk, slug).model
         self.test_permissions(request, thread)
 
         posts_queryset = exclude_invisible_posts(request.user, thread.category, thread.post_set)