Пример #1
0
    def action_move(self, request, thread):
        form = MoveThreadForm(acl=request.user.acl, forum=self.forum)

        if request.method == "POST" and 'submit' in request.POST:
            form = MoveThreadForm(
                request.POST, acl=request.user.acl, forum=self.forum)
            if form.is_valid():
                new_forum = form.cleaned_data['new_forum']

                with atomic():
                    self.forum.lock()
                    moderation.move_thread(request.user, thread, new_forum)
                    self.forum.synchronize()
                    self.forum.save()
                    new_forum.synchronize()
                    new_forum.save()

                message = _('Thread was moved to "%(forum)s".')
                messages.success(request, message % {
                    'forum': new_forum.name
                })

                return None # trigger thread refresh

        if request.is_ajax():
            template = self.move_thread_modal_template
        else:
            template = self.move_thread_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'path': get_forum_path(self.forum),
            'thread': thread
        })
Пример #2
0
    def action_merge(self, request, threads):
        if len(threads) == 1:
            message = _("You have to select at least two threads to merge.")
            raise moderation.ModerationError(message)

        form = MergeThreadsForm()

        if 'submit' in request.POST:
            form = MergeThreadsForm(request.POST)
            if form.is_valid():
                with atomic():
                    merged_thread = Thread()
                    merged_thread.forum = self.forum
                    merged_thread.set_title(
                        form.cleaned_data['merged_thread_title'])
                    merged_thread.starter_name = "-"
                    merged_thread.starter_slug = "-"
                    merged_thread.last_poster_name = "-"
                    merged_thread.last_poster_slug = "-"
                    merged_thread.started_on = timezone.now()
                    merged_thread.last_post_on = timezone.now()
                    merged_thread.is_pinned = max(t.is_pinned for t in threads)
                    merged_thread.is_closed = max(t.is_closed for t in threads)
                    merged_thread.save()

                    for thread in threads:
                        moderation.merge_thread(
                            request.user, merged_thread, thread)

                    merged_thread.synchronize()
                    merged_thread.save()

                    self.forum.lock()
                    self.forum.synchronize()
                    self.forum.save()

                changed_threads = len(threads)
                message = ungettext(
                    '%(changed)d thread was merged into "%(thread)s".',
                    '%(changed)d threads were merged into "%(thread)s".',
                changed_threads)
                messages.success(request, message % {
                    'changed': changed_threads,
                    'thread': merged_thread.title
                })

                return None # trigger threads list refresh

        if request.is_ajax():
            template = self.merge_threads_modal_template
        else:
            template = self.merge_threads_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'path': get_forum_path(self.forum),
            'threads': threads
        })
Пример #3
0
    def dispatch(self, request, *args, **kwargs):
        relations = ['forum', 'starter', 'last_poster', 'first_post']
        thread = self.fetch_thread(request, select_related=relations, **kwargs)
        forum = thread.forum

        self.check_forum_permissions(request, forum)
        self.check_thread_permissions(request, thread)

        validate_slug(thread, kwargs['thread_slug'])

        threadstracker.make_read_aware(request.user, thread)

        thread_actions = self.ThreadActions(user=request.user, thread=thread)
        posts_actions = self.PostsActions(user=request.user, thread=thread)

        if request.method == 'POST':
            if thread_actions.query_key in request.POST:
                response = thread_actions.handle_post(request, thread)
                if response:
                    return response
            if posts_actions.query_key in request.POST:
                queryset = self.get_posts_queryset(request.user, forum, thread)
                response = posts_actions.handle_post(request, queryset)
                if response:
                    return response

        page, posts = self.get_posts(request.user, forum, thread, kwargs)
        make_posts_reports_aware(request.user, thread, posts)

        threadstracker.make_posts_read_aware(request.user, thread, posts)
        threadstracker.read_thread(request.user, thread, posts[-1])

        try:
            allow_reply_thread(request.user, thread)
            thread_reply_message = None
        except PermissionDenied as e:
            thread_reply_message = unicode(e)

        return self.render(request, {
            'link_name': request.resolver_match.view_name,
            'links_params': {
                'thread_id': thread.id, 'thread_slug': thread.slug
            },

            'forum': forum,
            'path': get_forum_path(forum),

            'thread': thread,
            'thread_actions': thread_actions,
            'thread_reply_message': thread_reply_message,

            'posts': posts,
            'posts_actions': posts_actions,
            'selected_posts': posts_actions.get_selected_ids(),

            'paginator': page.paginator,
            'page': page,
        })
Пример #4
0
def category(request, forum):
    allow_browse_forum(request.user, forum)
    if forum.level == 1:
        return dj_redirect(forum.get_absolute_url())
    forums = get_forums_list(request.user, forum)

    return render(request, 'misago/forums/category.html', {
        'category': forum,
        'forums': forums,
        'path': get_forum_path(forum),
    })
Пример #5
0
    def dispatch(self, request, *args, **kwargs):
        forum = self.get_forum(request, **kwargs)
        validate_slug(forum, kwargs['forum_slug'])

        forum.labels = Label.objects.get_forum_labels(forum)

        if forum.lft + 1 < forum.rght:
            forum.subforums = get_forums_list(request.user, forum)
        else:
            forum.subforums = []

        page_number = kwargs.pop('page', None)
        cleaned_kwargs = self.clean_kwargs(request, kwargs)

        link_name = request.resolver_match.view_name

        sorting = self.Sorting(link_name, cleaned_kwargs)
        cleaned_kwargs = sorting.clean_kwargs(cleaned_kwargs)

        filtering = self.Filtering(forum, link_name, cleaned_kwargs)
        cleaned_kwargs = filtering.clean_kwargs(cleaned_kwargs)

        if cleaned_kwargs != kwargs:
            return redirect(link_name, **cleaned_kwargs)

        threads = self.Threads(request.user, forum)
        sorting.sort(threads)
        filtering.filter(threads)

        actions = self.Actions(user=request.user, forum=forum)
        if request.method == 'POST':
            response = actions.handle_post(request, threads.get_queryset())
            if response:
                return response

        return self.render(request, {
            'link_name': link_name,
            'links_params': cleaned_kwargs,

            'forum': forum,
            'path': get_forum_path(forum),

            'threads': threads.list(page_number),
            'threads_count': threads.count(),
            'page': threads.page,
            'paginator': threads.paginator,

            'threads_actions': actions,
            'selected_threads': actions.get_selected_ids(),

            'sorting': sorting,
            'filtering': filtering,
        })
Пример #6
0
    def action_move(self, request, posts):
        if posts[0].id == self.thread.first_post_id:
            message = _("You can't move thread's first post.")
            raise moderation.ModerationError(message)

        form = MovePostsForm(user=request.user, thread=self.thread)

        if 'submit' in request.POST or 'follow' in request.POST:
            form = MovePostsForm(request.POST,
                                 user=request.user,
                                 thread=self.thread)
            if form.is_valid():
                for post in posts:
                    post.move(form.new_thread)
                    post.save()

                form.new_thread.lock()
                form.new_thread.synchronize()
                form.new_thread.save()

                if form.new_thread.forum != self.forum:
                    form.new_thread.forum.lock()
                    form.new_thread.forum.synchronize()
                    form.new_thread.forum.save()

                changed_posts = len(posts)
                message = ungettext(
                    '%(changed)d post was moved to "%(thread)s".',
                    '%(changed)d posts were moved to "%(thread)s".',
                changed_posts)
                messages.success(request, message % {
                    'changed': changed_posts,
                    'thread': form.new_thread.title
                })

                if 'follow' in request.POST:
                    return redirect(form.new_thread.get_absolute_url())
                else:
                    return None # trigger thread refresh

        if request.is_ajax():
            template = self.move_posts_modal_template
        else:
            template = self.move_posts_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'thread': self.thread,
            'path': get_forum_path(self.forum),

            'posts': posts
        })
Пример #7
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)
        mode, forum, thread, post = mode_context

        if not request.is_ajax():
            response = render(request, 'misago/errorpages/wrong_way.html')
            response.status_code = 405
            return response

        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post)

        if request.method == 'POST':
            if 'submit' in request.POST:
                if formset.is_valid():
                    try:
                        formset.save()
                        messages.success(request, _("New thread was posted."))
                        return JsonResponse({
                            'thread_url': thread.get_absolute_url()
                        })
                    except PostingInterrupt as e:
                        return JsonResponse({'interrupt': e.message})
                else:
                    return JsonResponse({'errors': formset.errors})

            if 'preview' in request.POST:
                formset.update()
                return JsonResponse({'preview': formset.post.parsed})

        return self.render(request, {
            'mode': mode,
            'formset': formset,
            'forms': formset.get_forms_list(),
            'main_forms': formset.get_main_forms(),
            'supporting_forms': formset.get_supporting_forms(),
            'forum': forum,
            'path': get_forum_path(forum),
            'thread': thread,
            'post': post,
            'api_url': request.path
        })
Пример #8
0
    def action_move(self, request, threads):
        form = MoveThreadsForm(acl=request.user.acl, forum=self.forum)

        if 'submit' in request.POST:
            form = MoveThreadsForm(
                request.POST, acl=request.user.acl, forum=self.forum)
            if form.is_valid():
                new_forum = form.cleaned_data['new_forum']
                with atomic():

                    for thread in threads:
                        moderation.move_thread(request.user, thread, new_forum)

                    self.forum.lock()
                    new_forum.lock()

                    self.forum.synchronize()
                    self.forum.save()
                    new_forum.synchronize()
                    new_forum.save()

                changed_threads = len(threads)
                message = ungettext(
                    '%(changed)d thread was moved to "%(forum)s".',
                    '%(changed)d threads were moved to "%(forum)s".',
                changed_threads)
                messages.success(request, message % {
                    'changed': changed_threads,
                    'forum': new_forum.name
                })

                return None # trigger threads list refresh

        if request.is_ajax():
            template = self.move_threads_modal_template
        else:
            template = self.move_threads_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'path': get_forum_path(self.forum),
            'threads': threads
        })
Пример #9
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)
        mode, forum, thread, post = mode_context

        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post)

        if request.method == 'POST':
            if 'submit' in request.POST:
                if formset.is_valid():
                    try:
                        formset.save()
                        return self.handle_submit(request, formset)
                    except PostingInterrupt as e:
                        return JsonResponse({'interrupt': e.message})
                else:
                    return JsonResponse({'errors': formset.errors})

            if 'preview' in request.POST:
                formset.update()
                return JsonResponse({'preview': formset.post.parsed})

        return self.render(
            request, {
                'mode': mode,
                'formset': formset,
                'forms': formset.get_forms_list(),
                'main_forms': formset.get_main_forms(),
                'supporting_forms': formset.get_supporting_forms(),
                'forum': forum,
                'path': get_forum_path(forum),
                'thread': thread,
                'post': post,
                'api_url': request.path
            })
Пример #10
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)
        mode, forum, thread, post = mode_context

        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post)

        if request.method == 'POST':
            if 'submit' in request.POST:
                if formset.is_valid():
                    try:
                        formset.save()
                        return self.handle_submit(request, formset)
                    except PostingInterrupt as e:
                        return JsonResponse({'interrupt': e.message})
                else:
                    return JsonResponse({'errors': formset.errors})

            if 'preview' in request.POST:
                formset.update()
                return JsonResponse({'preview': formset.post.parsed})

        return self.render(request, {
            'mode': mode,
            'formset': formset,
            'forms': formset.get_forms_list(),
            'main_forms': formset.get_main_forms(),
            'supporting_forms': formset.get_supporting_forms(),
            'forum': forum,
            'path': get_forum_path(forum),
            'thread': thread,
            'post': post,
            'api_url': request.path
        })
Пример #11
0
    def dispatch(self, request, *args, **kwargs):
        relations = ['forum', 'starter', 'last_poster', 'first_post']
        thread = self.fetch_thread(request, select_related=relations, **kwargs)
        forum = thread.forum

        self.check_forum_permissions(request, forum)
        self.check_thread_permissions(request, thread)

        threadstracker.make_read_aware(request.user, thread)

        page, posts = self.get_posts(request.user, forum, thread, kwargs)
        threadstracker.make_posts_read_aware(thread, posts)
        threadstracker.read_thread(request.user, thread, posts[-1])

        return self.render(request, {
            'forum': forum,
            'path': get_forum_path(forum),
            'thread': thread,
            'posts': posts,
            'page': page,
            'paginator': page.paginator,
        })
Пример #12
0
    def dispatch(self, request, *args, **kwargs):
        forum = self.get_forum(request, **kwargs)
        forum.labels = Label.objects.get_forum_labels(forum)

        if forum.lft + 1 < forum.rght:
            forum.subforums = get_forums_list(request.user, forum)
        else:
            forum.subforums = []

        page_number = kwargs.pop('page', None)
        cleaned_kwargs = self.clean_kwargs(request, kwargs)

        sorting = self.Sorting(self.link_name, cleaned_kwargs)
        cleaned_kwargs = sorting.clean_kwargs(cleaned_kwargs)

        filtering = self.Filtering(forum, self.link_name, cleaned_kwargs)
        cleaned_kwargs = filtering.clean_kwargs(cleaned_kwargs)

        if cleaned_kwargs != kwargs:
            return redirect('misago:forum', **cleaned_kwargs)

        threads = self.Threads(request.user, forum)
        sorting.sort(threads)
        filtering.filter(threads)

        return self.render(request, {
            'link_name': self.link_name,
            'links_params': cleaned_kwargs,

            'forum': forum,
            'path': get_forum_path(forum),

            'threads': threads.list(page_number),
            'page': threads.page,
            'paginator': threads.paginator,

            'sorting': sorting,
            'filtering': filtering,
        })
Пример #13
0
    def dispatch(self, request, *args, **kwargs):
        relations = ['forum', 'starter', 'last_poster', 'first_post']
        thread = self.fetch_thread(request, select_related=relations, **kwargs)
        forum = thread.forum

        self.check_forum_permissions(request, forum)
        self.check_thread_permissions(request, thread)

        threadstracker.make_read_aware(request.user, thread)

        page, posts = self.get_posts(request.user, forum, thread, kwargs)
        threadstracker.make_posts_read_aware(request.user, thread, posts)
        threadstracker.read_thread(request.user, thread, posts[-1])

        return self.render(request, {
            'forum': forum,
            'path': get_forum_path(forum),
            'thread': thread,
            'posts': posts,
            'page': page,
            'paginator': page.paginator,
        })
Пример #14
0
    def action_move(self, request, thread):
        form = MoveThreadForm(acl=request.user.acl, forum=self.forum)

        if 'submit' in request.POST:
            form = MoveThreadForm(request.POST,
                                  acl=request.user.acl,
                                  forum=self.forum)
            if form.is_valid():
                new_forum = form.cleaned_data['new_forum']

                with atomic():
                    moderation.move_thread(request.user, thread, new_forum)

                    self.forum.lock()
                    new_forum.lock()

                    self.forum.synchronize()
                    self.forum.save()
                    new_forum.synchronize()
                    new_forum.save()

                message = _('Thread was moved to "%(forum)s".')
                messages.success(request, message % {'forum': new_forum.name})

                return None  # trigger thread refresh

        if request.is_ajax():
            template = self.move_thread_modal_template
        else:
            template = self.move_thread_full_template

        return render(
            request, template, {
                'form': form,
                'forum': self.forum,
                'path': get_forum_path(self.forum),
                'thread': thread
            })
Пример #15
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)

        mode, forum, thread, post, quote = mode_context
        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post,
                                quote=quote)

        if request.method == 'POST':
            if 'submit' in request.POST and formset.is_valid():
                try:
                    formset.save()
                    return redirect(thread.get_absolute_url())
                except PostingInterrupt as e:
                    messages.error(request, e.message)
            else:
                formset.update()

        return self.render(
            request, {
                'mode': mode,
                'formset': formset,
                'forms': formset.get_forms_list(),
                'main_forms': formset.get_main_forms(),
                'supporting_forms': formset.get_supporting_forms(),
                'forum': forum,
                'path': get_forum_path(forum),
                'thread': thread,
                'post': post,
                'quote': quote,
            })
Пример #16
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)

        mode, forum, thread, post, quote = mode_context
        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post,
                                quote=quote)

        if request.method == 'POST':
            if 'submit' in request.POST and formset.is_valid():
                try:
                    formset.save()
                    return redirect(thread.get_absolute_url())
                except PostingInterrupt as e:
                    messages.error(request, e.message)
            else:
                formset.update()

        return self.render(request, {
            'mode': mode,
            'formset': formset,
            'forms': formset.get_forms_list(),
            'main_forms': formset.get_main_forms(),
            'supporting_forms': formset.get_supporting_forms(),
            'forum': forum,
            'path': get_forum_path(forum),
            'thread': thread,
            'post': post,
            'quote': quote,
        })
Пример #17
0
    def action_merge(self, request, threads):
        if len(threads) == 1:
            message = _("You have to select at least two threads to merge.")
            raise moderation.ModerationError(message)

        form = MergeThreadsForm()

        if request.method == "POST" and 'submit' in request.POST:
            form = MergeThreadsForm(request.POST)
            if form.is_valid():
                thread_title = form.cleaned_data['merged_thread_title']

                with atomic():
                    merged_thread = Thread()
                    merged_thread.forum = self.forum
                    merged_thread.set_title(
                        form.cleaned_data['merged_thread_title'])
                    merged_thread.starter_name = "-"
                    merged_thread.starter_slug = "-"
                    merged_thread.last_poster_name = "-"
                    merged_thread.last_poster_slug = "-"
                    merged_thread.started_on = timezone.now()
                    merged_thread.last_post_on = timezone.now()
                    merged_thread.is_pinned = max(t.is_pinned for t in threads)
                    merged_thread.is_closed = max(t.is_closed for t in threads)
                    merged_thread.save()

                    for thread in threads:
                        moderation.merge_thread(
                            request.user, merged_thread, thread)

                    merged_thread.synchronize()
                    merged_thread.save()

                with atomic():
                    self.forum.synchronize()
                    self.forum.save()

                changed_threads = len(threads)
                message = ungettext(
                    '%(changed)d thread was merged into "%(thread)s".',
                    '%(changed)d threads were merged into "%(thread)s".',
                changed_threads)
                messages.success(request, message % {
                    'changed': changed_threads,
                    'thread': merged_thread.title
                })

                return None # trigger threads list refresh

        if request.is_ajax():
            template = self.merge_threads_modal_template
        else:
            template = self.merge_threads_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'path': get_forum_path(self.forum),
            'threads': threads
        })
Пример #18
0
 def test_get_forum_path(self):
     """get_forums_list returns all children of root nodes"""
     for node in get_forums_list(self.user):
         parent_nodes = len(get_forum_path(node))
         self.assertEqual(parent_nodes, node.level)
Пример #19
0
    def real_dispatch(self, request, *args, **kwargs):
        mode_context = self.find_mode(request, *args, **kwargs)
        self.allow_mode(request.user, *mode_context)
        mode, forum, thread, post = mode_context

        if not request.is_ajax():
            response = render(request, 'misago/errorpages/wrong_way.html')
            response.status_code = 405
            return response

        forum.labels = Label.objects.get_forum_labels(forum)
        formset = EditorFormset(request=request,
                                mode=mode,
                                user=request.user,
                                forum=forum,
                                thread=thread,
                                post=post)

        if request.method == 'POST':
            if 'submit' in request.POST:
                if formset.is_valid():
                    try:
                        formset.save()

                        if mode == EDIT:
                            message = _("Changes saved.")
                        else:
                            if mode == START:
                                message = _("New thread was posted.")
                            if mode == REPLY:
                                message = _("Your reply was posted.")
                            messages.success(request, message)

                        return JsonResponse({
                            'message':
                            message,
                            'post_url':
                            goto.post(request.user, thread, post),
                            'parsed':
                            post.parsed,
                            'original':
                            post.original,
                            'title':
                            thread.title,
                            'title_escaped':
                            html.escape(thread.title),
                        })
                    except PostingInterrupt as e:
                        return JsonResponse({'interrupt': e.message})
                else:
                    return JsonResponse({'errors': formset.errors})

            if 'preview' in request.POST:
                formset.update()
                return JsonResponse({'preview': formset.post.parsed})

        return self.render(
            request, {
                'mode': mode,
                'formset': formset,
                'forms': formset.get_forms_list(),
                'main_forms': formset.get_main_forms(),
                'supporting_forms': formset.get_supporting_forms(),
                'forum': forum,
                'path': get_forum_path(forum),
                'thread': thread,
                'post': post,
                'api_url': request.path
            })
Пример #20
0
    def action_split(self, request, posts):
        if posts[0].id == self.thread.first_post_id:
            message = _("You can't split thread's first post.")
            raise moderation.ModerationError(message)

        form = SplitThreadForm(acl=request.user.acl)

        if 'submit' in request.POST or 'follow' in request.POST:
            form = SplitThreadForm(request.POST, acl=request.user.acl)
            if form.is_valid():
                split_thread = Thread()
                split_thread.forum = form.cleaned_data['forum']
                split_thread.set_title(
                    form.cleaned_data['thread_title'])
                split_thread.starter_name = "-"
                split_thread.starter_slug = "-"
                split_thread.last_poster_name = "-"
                split_thread.last_poster_slug = "-"
                split_thread.started_on = timezone.now()
                split_thread.last_post_on = timezone.now()
                split_thread.save()

                for post in posts:
                    post.move(split_thread)
                    post.save()

                split_thread.synchronize()
                split_thread.save()

                if split_thread.forum != self.forum:
                    split_thread.forum.lock()
                    split_thread.forum.synchronize()
                    split_thread.forum.save()

                changed_posts = len(posts)
                message = ungettext(
                    '%(changed)d post was split to "%(thread)s".',
                    '%(changed)d posts were split to "%(thread)s".',
                changed_posts)
                messages.success(request, message % {
                    'changed': changed_posts,
                    'thread': split_thread.title
                })

                if 'follow' in request.POST:
                    return redirect(split_thread.get_absolute_url())
                else:
                    return None # trigger thread refresh

        if request.is_ajax():
            template = self.split_thread_modal_template
        else:
            template = self.split_thread_full_template

        return render(request, template, {
            'form': form,
            'forum': self.forum,
            'thread': self.thread,
            'path': get_forum_path(self.forum),

            'posts': posts
        })