Esempio n. 1
0
def clean_threads_for_merge(request):
    try:
        threads_ids = list(map(int, request.data.get('threads', [])))
    except (ValueError, TypeError):
        raise MergeError(_("One or more thread ids received were invalid."))

    if len(threads_ids) < 2:
        raise MergeError(
            _("You have to select at least two threads to merge."))
    elif len(threads_ids) > MERGE_LIMIT:
        message = ungettext(
            "No more than %(limit)s thread can be merged at single time.",
            "No more than %(limit)s threads can be merged at single time.",
            MERGE_LIMIT)
        raise MergeError(message % {'limit': MERGE_LIMIT})

    threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)

    threads_queryset = Thread.objects.filter(
        id__in=threads_ids,
        category__tree_id=threads_tree_id,
    ).select_for_update().select_related('category').order_by('-id')

    threads = []
    for thread in threads_queryset:
        add_acl(request.user, thread)
        if can_see_thread(request.user, thread):
            threads.append(thread)

    if len(threads) != len(threads_ids):
        raise MergeError(_("One or more threads to merge could not be found."))

    return threads
Esempio n. 2
0
    def validate_threads(self, data):
        if len(data) > THREADS_LIMIT:
            message = ngettext(
                "No more than %(limit)s thread can be merged at single time.",
                "No more than %(limit)s threads can be merged at single time.",
                POSTS_LIMIT,
            )
            raise ValidationError(message % {'limit': THREADS_LIMIT})

        threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)

        threads_queryset = Thread.objects.filter(
            id__in=data,
            category__tree_id=threads_tree_id,
        ).select_related('category').order_by('-id')

        user = self.context['user']

        threads = []
        for thread in threads_queryset:
            add_acl(user, thread)
            if can_see_thread(user, thread):
                threads.append(thread)

        if len(threads) != len(data):
            raise ValidationError(_("One or more threads to merge could not be found."))

        return threads
Esempio n. 3
0
def clean_threads_for_merge(request):
    try:
        threads_ids = map(int, request.data.get('threads', []))
    except (ValueError, TypeError):
        raise MergeError(_("One or more thread ids received were invalid."))

    if len(threads_ids) < 2:
        raise MergeError(_("You have to select at least two threads to merge."))
    elif len(threads_ids) > MERGE_LIMIT:
        message = ungettext(
            "No more than %(limit)s thread can be merged at single time.",
            "No more than %(limit)s threads can be merged at single time.",
            MERGE_LIMIT)
        raise MergeError(message % {'limit': MERGE_LIMIT})

    threads_queryset = Thread.objects.filter(
        id__in=threads_ids,
        category__tree_id=CATEGORIES_TREE_ID,
    ).select_related('category').order_by('-id')

    threads = []
    for thread in threads_queryset:
        add_acl(request.user, thread)
        if can_see_thread(request.user, thread):
            threads.append(thread)

    if len(threads) != len(threads_ids):
        raise MergeError(_("One or more threads to merge could not be found."))

    return threads
Esempio n. 4
0
def clean_threads_for_merge(request):
    threads_ids = clean_ids_list(
        request.data.get('threads', []),
        _("One or more thread ids received were invalid."),
    )

    if len(threads_ids) < 2:
        raise MergeError(_("You have to select at least two threads to merge."))
    elif len(threads_ids) > MERGE_LIMIT:
        message = ungettext(
            "No more than %(limit)s thread can be merged at single time.",
            "No more than %(limit)s threads can be merged at single time.",
            MERGE_LIMIT,
        )
        raise MergeError(message % {'limit': MERGE_LIMIT})

    threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)

    threads_queryset = Thread.objects.filter(
        id__in=threads_ids,
        category__tree_id=threads_tree_id,
    ).select_related('category').order_by('-id')

    threads = []
    for thread in threads_queryset:
        add_acl(request.user, thread)
        if can_see_thread(request.user, thread):
            threads.append(thread)

    if len(threads) != len(threads_ids):
        raise MergeError(_("One or more threads to merge could not be found."))

    return threads
Esempio n. 5
0
    def get_valid_threads(self, threads_ids):
        user = self.context['user']

        threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT)
        threads_queryset = Thread.objects.filter(
            id__in=threads_ids,
            category__tree_id=threads_tree_id,
        ).select_related('category').order_by('-id')

        invalid_threads = []
        valid_threads = []
        for thread in threads_queryset:
            add_acl(user, thread)
            if can_see_thread(user, thread):
                valid_threads.append(thread)
                try:
                    allow_merge_thread(user, thread)
                except PermissionDenied as permission_error:
                    invalid_threads.append({
                        'id': thread.id,
                        'status': 403,
                        'detail': permission_error
                    })

        not_found_ids = set(threads_ids) - set([t.id for t in valid_threads])
        for not_found_id in not_found_ids:
            invalid_threads.append({
                'id':
                not_found_id,
                'status':
                404,
                'detail':
                _("Requested thread doesn't exist or you don't have permission to see it."
                  ),
            })

        if invalid_threads:
            invalid_threads.sort(key=lambda item: item['id'])
            raise ValidationError({'merge': invalid_threads})

        return valid_threads
Esempio n. 6
0
 def notify_user_of_post(self, subscriber):
     see_thread = can_see_thread(subscriber, self.thread)
     see_post = can_see_post(subscriber, self.post)
     return see_thread and see_post