Example #1
0
    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_items(self.root, self.categories, [thread])

        self.assertEqual(thread.top_category, self.category_e)
        self.assertEqual(thread.category, self.category_e)
Example #2
0
    def test_child_thread_from_elsewhere(self):
        """thread in child category is handled"""
        thread = testutils.post_thread(category=self.category_d)
        add_categories_to_items(self.category_f, self.categories, [thread])

        self.assertEqual(thread.top_category, self.category_a)
        self.assertEqual(thread.category, self.category_d)
Example #3
0
    def test_root_thread_from_elsewhere(self):
        """thread in root category is handled"""
        thread = testutils.post_thread(category=self.root)
        add_categories_to_items(self.category_e, self.categories, [thread])

        self.assertIsNone(thread.top_category)
        self.assertEqual(thread.category, self.root)
Example #4
0
def merge_threads(request, validated_data, threads, poll):
    new_thread = Thread(category=validated_data['category'],
                        started_on=threads[0].started_on,
                        last_post_on=threads[0].last_post_on)

    new_thread.set_title(validated_data['title'])
    new_thread.save()

    if poll:
        poll.move(new_thread)

    categories = []
    for thread in threads:
        categories.append(thread.category)
        new_thread.merge(thread)
        thread.delete()

        record_event(request,
                     new_thread,
                     'merged', {
                         'merged_thread': thread.title,
                     },
                     commit=False)

    new_thread.synchronize()
    new_thread.save()

    if validated_data.get('weight') == Thread.WEIGHT_GLOBAL:
        moderation.pin_thread_globally(request, new_thread)
    elif validated_data.get('weight'):
        moderation.pin_thread_locally(request, new_thread)
    if validated_data.get('is_hidden', False):
        moderation.hide_thread(request, new_thread)
    if validated_data.get('is_closed', False):
        moderation.close_thread(request, new_thread)

    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=request.user.acl['visible_categories']))
        add_categories_to_items(validated_data['top_category'], categories,
                                [new_thread])
    else:
        new_thread.top_category = None

    add_acl(request.user, new_thread)
    return new_thread
Example #5
0
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_items(root_category, categories, [thread])
    return {'top_category': CategorySerializer(thread.top_category).data}
Example #6
0
    def __init__(self, request, profile, page=0):
        root_category = ThreadsRootCategory(request)
        threads_categories = [root_category.unwrap()
                              ] + root_category.subcategories

        threads_queryset = self.get_threads_queryset(request,
                                                     threads_categories,
                                                     profile)
        posts_queryset = self.get_posts_queryset(
            request.user, profile,
            threads_queryset).filter(is_hidden=False,
                                     is_unapproved=False).order_by('-pk')

        list_page = paginate(posts_queryset, page,
                             settings.MISAGO_POSTS_PER_PAGE,
                             settings.MISAGO_POSTS_TAIL)
        paginator = pagination_dict(list_page, include_page_range=False)

        posts = list(list_page.object_list)

        posters = []
        threads = []

        for post in posts:
            threads.append(post.thread)

            if post.poster:
                posters.append(post.poster)

        add_categories_to_items(root_category.unwrap(), threads_categories,
                                posts + threads)

        add_acl(request.user, threads)
        add_acl(request.user, posts)

        threadstracker.make_threads_read_aware(request.user, threads)
        for post in posts:
            threadstracker.make_posts_read_aware(request.user, post.thread,
                                                 [post])

        add_likes_to_posts(request.user, posts)

        make_users_status_aware(request.user, posters)

        self._user = request.user

        self.posts = posts
        self.paginator = paginator
Example #7
0
    def __init__(self, request, profile, page=0):
        root_category = ThreadsRootCategory(request)
        threads_categories = [root_category.unwrap()] + root_category.subcategories

        threads_queryset = self.get_threads_queryset(
            request, threads_categories, profile)
        posts_queryset = self.get_posts_queryset(
            request.user, profile, threads_queryset
            ).filter(
                is_event=False,
                is_hidden=False,
                is_unapproved=False
            ).order_by('-pk')

        list_page = paginate(
            posts_queryset, page, settings.MISAGO_POSTS_PER_PAGE, settings.MISAGO_POSTS_TAIL)
        paginator = pagination_dict(list_page, include_page_range=False)

        posts = list(list_page.object_list)

        posters = []
        threads = []

        for post in posts:
            threads.append(post.thread)

            if post.poster:
                posters.append(post.poster)

        add_categories_to_items(
            root_category.unwrap(), threads_categories, posts + threads)

        add_acl(request.user, threads)
        add_acl(request.user, posts)

        threadstracker.make_threads_read_aware(request.user, threads)
        for post in posts:
            threadstracker.make_posts_read_aware(request.user, post.thread, [post])

        add_likes_to_posts(request.user, posts)

        make_users_status_aware(request.user, posters)

        self._user = request.user

        self.posts = posts
        self.paginator = paginator
Example #8
0
    def __init__(self, request, category, list_type, page):
        self.allow_see_list(request, category, list_type)

        category_model = category.unwrap()

        base_queryset = self.get_base_queryset(request, category.categories,
                                               list_type)
        base_queryset = base_queryset.select_related('starter', 'last_poster')

        threads_categories = [category_model] + category.subcategories

        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)

        if list_page.number > 1:
            threads = list(list_page.object_list)
        else:
            pinned_threads = list(
                self.get_pinned_threads(base_queryset, category_model,
                                        threads_categories))
            threads = list(pinned_threads) + list(list_page.object_list)

        add_categories_to_items(category_model, category.categories, threads)
        add_acl(request.user, threads)
        make_subscription_aware(request.user, threads)

        if list_type in ('new', 'unread'):
            # we already know all threads on list are unread
            for thread in threads:
                thread.is_read = False
                thread.is_new = True
        else:
            threadstracker.make_read_aware(request.user, threads)

        self.filter_threads(request, threads)

        # set state on object for easy access from hooks
        self.category = category
        self.threads = threads
        self.list_type = list_type
        self.paginator = paginator
    def __init__(self, request, category, list_type, page):
        self.allow_see_list(request, category, list_type)

        category_model = category.unwrap()

        base_queryset = self.get_base_queryset(request, category.categories, list_type)
        base_queryset = base_queryset.select_related('starter', 'last_poster')

        threads_categories = [category_model] + category.subcategories

        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)

        if list_page.number > 1:
            threads = list(list_page.object_list)
        else:
            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'):
            # 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_items(category_model, category.categories, threads)

        add_acl(request.user, threads)
        make_subscription_aware(request.user, threads)

        self.filter_threads(request, threads)

        # set state on object for easy access from hooks
        self.category = category
        self.threads = threads
        self.list_type = list_type
        self.paginator = paginator
Example #10
0
    def __init__(self, request, profile, page=0):
        root_category = ThreadsRootCategory(request)
        threads_categories = [root_category.unwrap()
                              ] + root_category.subcategories

        threads_queryset = self.get_threads_queryset(request,
                                                     threads_categories,
                                                     profile)

        posts_queryset = self.get_posts_queryset(request.user, profile,
                                                 threads_queryset).filter(
                                                     is_event=False,
                                                     is_hidden=False,
                                                     is_unapproved=False,
                                                 ).order_by('-id')

        list_page = paginate(posts_queryset, page,
                             settings.MISAGO_POSTS_PER_PAGE,
                             settings.MISAGO_POSTS_TAIL)
        paginator = pagination_dict(list_page)

        posts = list(list_page.object_list)
        threads = []

        for post in posts:
            threads.append(post.thread)

        add_categories_to_items(root_category.unwrap(), threads_categories,
                                posts + threads)

        add_acl(request.user, threads)
        add_acl(request.user, posts)

        self._user = request.user

        self.posts = posts
        self.paginator = paginator
    def test_root_thread_from_root(self):
        """thread in root category is handled"""
        thread = testutils.post_thread(category=self.root)
        add_categories_to_items(self.root, self.categories, [thread])

        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_items(self.category_f, self.categories, [thread])

        self.assertEqual(thread.category, self.category_d)
    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_items(self.root, self.categories, [thread])

        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_items(self.category_e, self.categories, [thread])

        self.assertEqual(thread.category, self.root)