Ejemplo n.º 1
0
 def setup_data(self, status):
     data = {'user_status': status}
     self.moderator = self.create_user('moderator_user')
     self.moderator.set_status('m')
     self.subject = self.create_user('normal_user')
     self.subject.set_status('a')
     self.form = forms.ChangeUserStatusForm(data, moderator = self.moderator, 
                                            subject = self.subject)
Ejemplo n.º 2
0
def user_moderate(request, subject, context):
    """user subview for moderation
    """
    moderator = request.user

    if moderator.is_authenticated(
    ) and not moderator.can_moderate_user(subject):
        raise Http404

    user_rep_changed = False
    user_status_changed = False
    message_sent = False
    email_error_message = None

    user_rep_form = forms.ChangeUserReputationForm()
    send_message_form = forms.SendMessageForm()
    if request.method == 'POST':
        if 'change_status' in request.POST:
            user_status_form = forms.ChangeUserStatusForm(request.POST,
                                                          moderator=moderator,
                                                          subject=subject)
            if user_status_form.is_valid():
                subject.set_status(
                    user_status_form.cleaned_data['user_status'])
            user_status_changed = True
        elif 'send_message' in request.POST:
            send_message_form = forms.SendMessageForm(request.POST)
            if send_message_form.is_valid():
                subject_line = send_message_form.cleaned_data['subject_line']
                body_text = send_message_form.cleaned_data['body_text']

                try:
                    send_mail(subject_line=subject_line,
                              body_text=body_text,
                              recipient_list=[subject.email],
                              headers={'Reply-to': moderator.email},
                              raise_on_failure=True)
                    message_sent = True
                except exceptions.EmailNotSent, e:
                    email_error_message = unicode(e)
                send_message_form = forms.SendMessageForm()
        else:
            reputation_change_type = None
            if 'subtract_reputation' in request.POST:
                rep_change_type = 'subtract'
            elif 'add_reputation' in request.POST:
                rep_change_type = 'add'
            else:
                raise Http404

            user_rep_form = forms.ChangeUserReputationForm(request.POST)
            if user_rep_form.is_valid():
                rep_delta = user_rep_form.cleaned_data['user_reputation_delta']
                comment = user_rep_form.cleaned_data['comment']

                if rep_change_type == 'subtract':
                    rep_delta = -1 * rep_delta

                moderator.moderate_user_reputation(
                    user=subject,
                    reputation_change=rep_delta,
                    comment=comment,
                    timestamp=datetime.datetime.now(),
                )
                #reset form to preclude accidentally repeating submission
                user_rep_form = forms.ChangeUserReputationForm()
                user_rep_changed = True
Ejemplo n.º 3
0
                if rep_change_type == 'subtract':
                    rep_delta = -1 * rep_delta

                moderator.moderate_user_reputation(
                    user=subject,
                    reputation_change=rep_delta,
                    comment=comment,
                    timestamp=datetime.datetime.now(),
                )
                #reset form to preclude accidentally repeating submission
                user_rep_form = forms.ChangeUserReputationForm()
                user_rep_changed = True

    #need to re-initialize the form even if it was posted, because
    #initial values will most likely be different from the previous
    user_status_form = forms.ChangeUserStatusForm(moderator=moderator,
                                                  subject=subject)
    data = {
        'active_tab': 'users',
        'page_class': 'user-profile-page',
        'tab_name': 'moderation',
        'tab_description': _('moderate this user'),
        'page_title': _('moderate user'),
        'change_user_status_form': user_status_form,
        'change_user_reputation_form': user_rep_form,
        'send_message_form': send_message_form,
        'message_sent': message_sent,
        'email_error_message': email_error_message,
        'user_rep_changed': user_rep_changed,
        'user_status_changed': user_status_changed
    }
    context.update(data)
Ejemplo n.º 4
0
def user_moderate(request, subject, context):
    """user subview for moderation
    """
    moderator = request.user

    if moderator.is_authenticated(
    ) and not moderator.can_moderate_user(subject):
        raise Http404

    user_rep_changed = False
    user_status_changed = False
    user_status_changed_message = _('User status changed')
    message_sent = False
    email_error_message = None

    user_rep_form = forms.ChangeUserReputationForm()
    send_message_form = forms.SendMessageForm()
    if request.method == 'POST':
        if 'change_status' in request.POST or 'hard_block' in request.POST:
            user_status_form = forms.ChangeUserStatusForm(request.POST,
                                                          moderator=moderator,
                                                          subject=subject)
            if user_status_form.is_valid():
                subject.set_status(
                    user_status_form.cleaned_data['user_status'])
                if user_status_form.cleaned_data['delete_content'] is True:
                    num_deleted = request.user.delete_all_content_authored_by_user(
                        subject)
                    if num_deleted:
                        num_deleted_message = ungettext(
                            '%d post deleted', '%d posts deleted',
                            num_deleted) % num_deleted
                        user_status_changed_message = string_concat(
                            user_status_changed_message, ', ',
                            num_deleted_message)
            user_status_changed = True
        elif 'send_message' in request.POST:
            send_message_form = forms.SendMessageForm(request.POST)
            if send_message_form.is_valid():
                subject_line = send_message_form.cleaned_data['subject_line']
                body_text = send_message_form.cleaned_data['body_text']

                try:
                    send_mail(subject_line=subject_line,
                              body_text=body_text,
                              recipient_list=[subject.email],
                              headers={'Reply-to': moderator.email},
                              raise_on_failure=True)
                    message_sent = True
                except exceptions.EmailNotSent as e:
                    email_error_message = force_text(e)
                send_message_form = forms.SendMessageForm()
        else:
            reputation_change_type = None
            if 'subtract_reputation' in request.POST:
                rep_change_type = 'subtract'
            elif 'add_reputation' in request.POST:
                rep_change_type = 'add'
            else:
                raise Http404

            user_rep_form = forms.ChangeUserReputationForm(request.POST)
            if user_rep_form.is_valid():
                rep_delta = user_rep_form.cleaned_data['user_reputation_delta']
                comment = user_rep_form.cleaned_data['comment']

                if rep_change_type == 'subtract':
                    rep_delta = -1 * rep_delta

                moderator.moderate_user_reputation(user=subject,
                                                   reputation_change=rep_delta,
                                                   comment=comment,
                                                   timestamp=timezone.now())

                # reset form to preclude accidentally repeating submission
                user_rep_form = forms.ChangeUserReputationForm()
                user_rep_changed = True

    # need to re-initialize the form even if it was posted, because
    # initial values will most likely be different from the previous
    user_status_form = forms.ChangeUserStatusForm(moderator=moderator,
                                                  subject=subject)

    context.update({
        'active_tab': 'users',
        'page_class': 'user-profile-page',
        'tab_name': 'moderation',
        'page_title': _('moderate user'),
        'change_user_status_form': user_status_form,
        'change_user_reputation_form': user_rep_form,
        'send_message_form': send_message_form,
        'message_sent': message_sent,
        'email_error_message': email_error_message,
        'user_rep_changed': user_rep_changed,
        'user_status_changed': user_status_changed,
        'user_status_changed_message': user_status_changed_message
    })
    return render(request, 'user_profile/user_moderate.jinja', context)