def test_direct_child_thread_from_parent(self): """thread in direct child category is handled""" thread = testutils.post_thread(category=self.category_e) add_categories_to_threads(self.root, self.categories, [thread]) self.assertEqual(thread.top_category, self.category_e) self.assertEqual(thread.category, self.category_e)
def test_child_thread_from_elsewhere(self): """thread in child category is handled""" thread = testutils.post_thread(category=self.category_d) add_categories_to_threads(self.category_f, self.categories, [thread]) self.assertEqual(thread.top_category, self.category_a) self.assertEqual(thread.category, self.category_d)
def test_root_thread_from_elsewhere(self): """thread in root category is handled""" thread = testutils.post_thread(category=self.root) add_categories_to_threads(self.category_e, self.categories, [thread]) self.assertIsNone(thread.top_category) self.assertEqual(thread.category, self.root)
def test_direct_child_thread_from_parent(self): """thread in direct child category is handled""" thread = testutils.post_thread(category=self.category_e) add_categories_to_threads(self.root, self.categories, [thread]) self.assertEqual(thread.top_category, self.category_e) self.assertEqual(thread.category, self.category_e)
def test_root_thread_from_elsewhere(self): """thread in root category is handled""" thread = testutils.post_thread(category=self.root) add_categories_to_threads(self.category_e, self.categories, [thread]) self.assertIsNone(thread.top_category) self.assertEqual(thread.category, self.root)
def test_child_thread_from_elsewhere(self): """thread in child category is handled""" thread = testutils.post_thread(category=self.category_d) add_categories_to_threads(self.category_f, self.categories, [thread]) self.assertEqual(thread.top_category, self.category_a) self.assertEqual(thread.category, self.category_d)
def patch_top_category(request, thread, value): category_pk = get_int_or_404(value) root_category = get_object_or_404( Category.objects.all_categories(include_root=True), pk=category_pk ) categories = list(Category.objects.all_categories().filter( id__in=request.user.acl['visible_categories'] )) add_categories_to_threads(root_category, categories, [thread]) return {'top_category': CategorySerializer(thread.top_category).data}
def patch_top_category(request, thread, value): category_pk = get_int_or_404(value) root_category = get_object_or_404( Category.objects.all_categories(include_root=True), pk=category_pk ) categories = list(Category.objects.all_categories().filter( id__in=request.user.acl['visible_categories'] )) add_categories_to_threads(root_category, categories, [thread]) return {'top_category': CategorySerializer(thread.top_category).data}
def __call__(self, request): try: page = int(request.query_params.get('page', 0)) except ValueError: raise Http404() list_type = request.query_params.get('list') or 'all' if list_type not in LIST_TYPES: raise Http404() category = self.get_category(request) self.allow_see_list(request, category, list_type) subcategories = self.get_subcategories(request, category) categories = [category] + subcategories queryset = self.get_queryset( request, categories, list_type).order_by('-last_post_on') page = paginate(queryset, page, 24, 6, allow_explicit_first_page=True) response_dict = pagination_dict(page, include_page_range=False) if list_type in ('new', 'unread'): """we already know all threads on list are unread""" threadstracker.make_unread(page.object_list) else: threadstracker.make_threads_read_aware( request.user, page.object_list) add_categories_to_threads(categories, page.object_list) visible_subcategories = [] for thread in page.object_list: if (thread.top_category and thread.top_category not in visible_subcategories): visible_subcategories.append(thread.top_category.pk) if self.serialize_subcategories: response_dict['subcategories'] = [] for subcategory in subcategories: if subcategory.pk in visible_subcategories: response_dict['subcategories'].append(subcategory.pk) add_acl(request.user, page.object_list) return Response(dict( results=ThreadListSerializer(page.object_list, many=True).data, **response_dict))
def merge_threads(user, validated_data, threads): new_thread = Thread( category=validated_data['category'], weight=validated_data.get('weight', 0), is_closed=validated_data.get('is_closed', False), started_on=threads[0].started_on, last_post_on=threads[0].last_post_on, ) new_thread.set_title(validated_data['title']) new_thread.save() categories = [] for thread in threads: categories.append(thread.category) new_thread.merge(thread) thread.delete() new_thread.synchronize() new_thread.save() if new_thread.category not in categories: categories.append(new_thread.category) for category in categories: category.synchronize() category.save() # set extra attrs on thread for UI new_thread.is_read = False new_thread.subscription = None # add top category to thread if validated_data.get('top_category'): categories = list(Category.objects.all_categories().filter( id__in=user.acl['visible_categories'] )) add_categories_to_threads( validated_data['top_category'], categories, [new_thread]) else: new_thread.top_category = None new_thread.save() add_acl(user, new_thread) return new_thread
def merge_threads(user, validated_data, threads): new_thread = Thread( category=validated_data['category'], weight=validated_data.get('weight', 0), is_closed=validated_data.get('is_closed', False), started_on=threads[0].started_on, last_post_on=threads[0].last_post_on, ) new_thread.set_title(validated_data['title']) new_thread.save() categories = [] for thread in threads: categories.append(thread.category) new_thread.merge(thread) thread.delete() new_thread.synchronize() new_thread.save() if new_thread.category not in categories: categories.append(new_thread.category) for category in categories: category.synchronize() category.save() # set extra attrs on thread for UI new_thread.is_read = False new_thread.subscription = None # add top category to thread if validated_data.get('top_category'): categories = list(Category.objects.all_categories().filter( id__in=user.acl['visible_categories'])) add_categories_to_threads(validated_data['top_category'], categories, [new_thread]) else: new_thread.top_category = None new_thread.save() add_acl(user, new_thread) return new_thread
def get(self, request, **kwargs): try: page = int(request.GET.get('page', 0)) if page == 1: page = None except ValueError: raise Http404() list_type = kwargs['list_type'] categories = self.get_categories(request) category = self.get_category(request, categories, **kwargs) self.allow_see_list(request, category, list_type) subcategories = self.get_subcategories(category, categories) queryset = self.get_queryset(request, categories, list_type) threads_categories = [category] + subcategories rest_queryset = self.get_rest_queryset(queryset, threads_categories) page = paginate(rest_queryset, page, 24, 6) paginator = pagination_dict(page, include_page_range=False) if page.number > 1: threads = list(page.object_list) else: pinned_threads = self.get_pinned_threads( queryset, threads_categories) threads = list(pinned_threads) + list(page.object_list) if list_type in ('new', 'unread'): """we already know all threads on list are unread""" threadstracker.make_unread(threads) else: threadstracker.make_threads_read_aware( request.user, threads) add_categories_to_threads(category, categories, threads) visible_subcategories = [] for thread in threads: if (thread.top_category and thread.category in threads_categories and thread.top_category.pk not in visible_subcategories): visible_subcategories.append(thread.top_category.pk) category.subcategories = [] for subcategory in subcategories: if subcategory.pk in visible_subcategories: category.subcategories.append(subcategory) add_acl(request.user, threads) make_subscription_aware(request.user, threads) request.frontend_context.update({ 'THREADS': dict( results=ThreadListSerializer(threads, many=True).data, subcategories=[c.pk for c in category.subcategories], **paginator), 'CATEGORIES': IndexCategorySerializer(categories, many=True).data, }) if categories[0].special_role: request.frontend_context['CATEGORIES'][0]['special_role'] = True self.set_frontend_context(request) return render(request, self.template_name, dict( category=category, list_type=list_type, list_name=LISTS_NAMES[list_type], threads=threads, paginator=paginator, count=paginator['count'], **self.get_extra_context(request) ))
def __call__(self, request): try: page = int(request.query_params.get('page', 0)) except ValueError: raise Http404() list_type = request.query_params.get('list') or 'all' if list_type not in LIST_TYPES: raise Http404() categories = self.get_categories(request) category = self.get_category(request, categories) self.allow_see_list(request, category, list_type) subcategories = self.get_subcategories(category, categories) queryset = self.get_queryset(request, categories, list_type) threads_categories = [category] + subcategories rest_queryset = self.get_rest_queryset( category, queryset, threads_categories) page = paginate(rest_queryset, page, 24, 6, allow_explicit_first_page=True ) response_dict = pagination_dict(page, include_page_range=False) if page.number > 1: threads = list(page.object_list) else: pinned_threads = self.get_pinned_threads( category, queryset, threads_categories) threads = list(pinned_threads) + list(page.object_list) if list_type in ('new', 'unread'): """we already know all threads on list are unread""" threadstracker.make_unread(threads) else: threadstracker.make_threads_read_aware( request.user, threads) add_categories_to_threads(category, categories, threads) visible_subcategories = [] for thread in threads: if (thread.top_category and thread.category in threads_categories and thread.top_category not in visible_subcategories): visible_subcategories.append(thread.top_category.pk) if self.serialize_subcategories: response_dict['subcategories'] = [] for subcategory in subcategories: if subcategory.pk in visible_subcategories: response_dict['subcategories'].append(subcategory.pk) add_acl(request.user, threads) make_subscription_aware(request.user, threads) return Response(dict( results=ThreadListSerializer(threads, many=True).data, **response_dict))
def get(self, request, **kwargs): try: page = int(request.GET.get('page', 0)) if page == 1: page = None except ValueError: raise Http404() list_type = kwargs['list_type'] categories = self.get_categories(request) category = self.get_category(request, categories, **kwargs) self.allow_see_list(request, category, list_type) subcategories = self.get_subcategories(category, categories) queryset = self.get_queryset(request, categories, list_type) threads_categories = [category] + subcategories rest_queryset = self.get_rest_queryset(queryset, threads_categories) page = paginate(rest_queryset, page, 24, 6) paginator = pagination_dict(page, include_page_range=False) if page.number > 1: threads = list(page.object_list) else: pinned_threads = self.get_pinned_threads(queryset, threads_categories) threads = list(pinned_threads) + list(page.object_list) if list_type in ('new', 'unread'): """we already know all threads on list are unread""" threadstracker.make_unread(threads) else: threadstracker.make_threads_read_aware(request.user, threads) add_categories_to_threads(category, categories, threads) visible_subcategories = [] for thread in threads: if (thread.top_category and thread.category in threads_categories and thread.top_category.pk not in visible_subcategories): visible_subcategories.append(thread.top_category.pk) category.subcategories = [] for subcategory in subcategories: if subcategory.pk in visible_subcategories: category.subcategories.append(subcategory) add_acl(request.user, threads) make_subscription_aware(request.user, threads) request.frontend_context.update({ 'THREADS': dict(results=ThreadListSerializer(threads, many=True).data, subcategories=[c.pk for c in category.subcategories], **paginator), 'CATEGORIES': IndexCategorySerializer(categories, many=True).data, }) if categories[0].special_role: request.frontend_context['CATEGORIES'][0]['special_role'] = True self.set_frontend_context(request) return render( request, self.template_name, dict(category=category, list_type=list_type, list_name=LISTS_NAMES[list_type], threads=threads, paginator=paginator, count=paginator['count'], **self.get_extra_context(request)))