Example #1
0
    def validate_other_thread(self, data):
        request = self.context['request']
        thread = self.context['thread']
        viewmodel = self.context['viewmodel']

        other_thread_id = get_thread_id_from_url(request, data)
        if not other_thread_id:
            raise ValidationError(_("This is not a valid thread link."))
        if other_thread_id == thread.pk:
            raise ValidationError(_("You can't merge thread with itself."))

        try:
            other_thread = viewmodel(request, other_thread_id).unwrap()
            allow_merge_thread(request.user, other_thread, otherthread=True)
        except PermissionDenied as e:
            raise serializers.ValidationError(e)
        except Http404:
            raise ValidationError(
                _(
                    "The thread you have entered link to doesn't "
                    "exist or you don't have permission to see it."
                )
            )

        if not can_reply_thread(request.user, other_thread):
            raise ValidationError(_("You can't merge this thread into thread you can't reply."))

        return other_thread
Example #2
0
def thread_merge_endpoint(request, thread, viewmodel):
    if not thread.acl['can_merge']:
        raise PermissionDenied(
            _("You don't have permission to merge this thread with others."))

    other_thread_id = get_thread_id_from_url(
        request, request.data.get('thread_url', None))
    if not other_thread_id:
        return Response({'detail': _("This is not a valid thread link.")},
                        status=400)
    if other_thread_id == thread.pk:
        return Response({'detail': _("You can't merge thread with itself.")},
                        status=400)

    try:
        other_thread = viewmodel(request,
                                 other_thread_id,
                                 select_for_update=True).unwrap()
        if not can_reply_thread(request.user, other_thread):
            raise PermissionDenied(
                _("You can't merge this thread into thread you can't reply."))
        if not other_thread.acl['can_merge']:
            raise PermissionDenied(
                _("You don't have permission to merge this thread with current one."
                  ))
    except PermissionDenied as e:
        return Response({'detail': e.args[0]}, status=400)
    except Http404:
        return Response(
            {
                'detail':
                _("The thread you have entered link to doesn't exist or you don't have permission to see it."
                  )
            },
            status=400)

    polls_handler = PollMergeHandler([thread, other_thread])
    if len(polls_handler.polls) == 1:
        poll = polls_handler.polls[0]
        poll.move(other_thread)
    elif polls_handler.is_merge_conflict():
        if 'poll' in request.data:
            polls_handler.set_resolution(request.data.get('poll'))
            if polls_handler.is_valid():
                poll = polls_handler.get_resolution()
                if poll and poll.thread_id != other_thread.id:
                    other_thread.poll.delete()
                    poll.move(other_thread)
                elif not poll:
                    other_thread.poll.delete()
            else:
                return Response({'detail': _("Invalid choice.")}, status=400)
        else:
            return Response(
                {'polls': polls_handler.get_available_resolutions()},
                status=400)

    moderation.merge_thread(request, other_thread, thread)

    other_thread.synchronize()
    other_thread.save()

    other_thread.category.synchronize()
    other_thread.category.save()

    if thread.category != other_thread.category:
        thread.category.synchronize()
        thread.category.save()

    return Response({
        'id': other_thread.pk,
        'title': other_thread.title,
        'url': other_thread.get_absolute_url()
    })
def thread_merge_endpoint(request, thread, viewmodel):
    allow_merge_thread(request.user, thread)

    other_thread_id = get_thread_id_from_url(request, request.data.get('thread_url', None))
    if not other_thread_id:
        return Response({'detail': _("This is not a valid thread link.")}, status=400)
    if other_thread_id == thread.pk:
        return Response({'detail': _("You can't merge thread with itself.")}, status=400)

    try:
        other_thread = viewmodel(request, other_thread_id).unwrap()
        allow_merge_thread(request.user, other_thread, otherthread=True)
        if not can_reply_thread(request.user, other_thread):
            raise PermissionDenied(_("You can't merge this thread into thread you can't reply."))
    except PermissionDenied as e:
        return Response({'detail': e.args[0]}, status=400)
    except Http404:
        return Response(
            {
                'detail': _(
                    "The thread you have entered link to doesn't "
                    "exist or you don't have permission to see it."
                )
            },
            status=400,
        )

    polls_handler = PollMergeHandler([thread, other_thread])
    if len(polls_handler.polls) == 1:
        poll = polls_handler.polls[0]
        poll.move(other_thread)
    elif polls_handler.is_merge_conflict():
        if 'poll' in request.data:
            polls_handler.set_resolution(request.data.get('poll'))
            if polls_handler.is_valid():
                poll = polls_handler.get_resolution()
                if poll and poll.thread_id != other_thread.id:
                    other_thread.poll.delete()
                    poll.move(other_thread)
                elif not poll:
                    other_thread.poll.delete()
            else:
                return Response({'detail': _("Invalid choice.")}, status=400)
        else:
            return Response({'polls': polls_handler.get_available_resolutions()}, status=400)

    moderation.merge_thread(request, other_thread, thread)

    other_thread.synchronize()
    other_thread.save()

    other_thread.category.synchronize()
    other_thread.category.save()

    if thread.category != other_thread.category:
        thread.category.synchronize()
        thread.category.save()

    return Response({
        'id': other_thread.pk,
        'title': other_thread.title,
        'url': other_thread.get_absolute_url(),
    })