예제 #1
0
def patch_remove_participant(request, thread, value):
    try:
        user_id = int(value)
    except (ValueError, TypeError):
        raise PermissionDenied(_("A valid integer is required."))

    for participant in thread.participants_list:
        if participant.user_id == user_id:
            break
    else:
        raise PermissionDenied(_("Participant doesn't exist."))

    allow_remove_participant(request.user, thread, participant.user)
    remove_participant(request, thread, participant.user)

    if len(thread.participants_list) == 1:
        return {'deleted': True}
    else:
        make_participants_aware(request.user, thread)
        participants = ThreadParticipantSerializer(thread.participants_list,
                                                   many=True)

        return {
            'deleted': False,
            'participants': participants.data,
        }
예제 #2
0
파일: patch.py 프로젝트: vprime/Misago
def patch_add_participant(request, thread, value):
    allow_add_participants(request.user, thread)

    try:
        username = six.text_type(value).strip().lower()
        if not username:
            raise PermissionDenied(
                _("You have to enter new participant's username."))
        participant = UserModel.objects.get(slug=username)
    except UserModel.DoesNotExist:
        raise PermissionDenied(_("No user with such name exists."))

    if participant in [p.user for p in thread.participants_list]:
        raise PermissionDenied(_("This user is already thread participant."))

    allow_add_participant(request.user, participant)
    add_participant(request, thread, participant)

    make_participants_aware(request.user, thread)
    participants = ThreadParticipantSerializer(
        thread.participants_list, many=True)

    return {
        'participants': participants.data
    }
예제 #3
0
def patch_add_participant(request, thread, value):
    allow_add_participants(request.user, thread)

    try:
        username = six.text_type(value).strip().lower()
        if not username:
            raise ValidationError(
                _("You have to enter new participant's username."))
        participant = UserModel.objects.get(slug=username)
    except UserModel.DoesNotExist:
        raise ValidationError(_("No user with such name exists."))

    if participant in [p.user for p in thread.participants_list]:
        raise ValidationError(_("This user is already thread participant."))

    max_participants = request.user.acl_cache[
        'max_private_thread_participants']
    if max_participants:
        current_participants = len(thread.participants_list)
        if current_participants >= max_participants:
            raise ValidationError(
                _("You can't add any more new users to this thread."))

    try:
        allow_add_participant(request.user, participant)
    except PermissionDenied as e:
        raise ValidationError(six.text_type(e))

    add_participant(request, thread, participant)
    make_participants_aware(request.user, thread)
    participants = ThreadParticipantSerializer(thread.participants_list,
                                               many=True)

    return {'participants': participants.data}
예제 #4
0
def patch_replace_owner(request, thread, value):
    try:
        user_id = int(value)
    except (TypeError, ValueError):
        raise ValidationError(_("A valid integer is required."))

    for participant in thread.participants_list:
        if participant.user_id == user_id:
            if participant.is_owner:
                raise ValidationError(_("This user already is thread owner."))
            else:
                break
    else:
        raise ValidationError(_("Participant doesn't exist."))

    allow_change_owner(request.user, thread)
    change_owner(request, thread, participant.user)

    make_participants_aware(request.user, thread)
    participants = ThreadParticipantSerializer(thread.participants_list, many=True)
    return {'participants': participants.data}