Пример #1
0
    def form_valid(self, form):
        coordinators = get_coordinators()
        restricted = get_restricted()
        if form.cleaned_data["restricted"]:
            self.request.user.groups.add(restricted)

            # If a coordinator requests we stop processing their data, we
            # shouldn't allow them to continue being one.
            if test_func_coordinators_only(self.request.user):
                self.request.user.groups.remove(coordinators)
        else:
            self.request.user.groups.remove(restricted)

        return HttpResponseRedirect(self.get_success_url())
Пример #2
0
    def get_context_data(self, **kwargs):
        context = super(EditorDetailView, self).get_context_data(**kwargs)
        editor = self.get_object()
        user = self.request.user
        context["editor"] = editor  # allow for more semantic templates
        context["form"] = EditorUpdateForm(instance=editor)
        context["language_form"] = SetLanguageForm(user=user)
        context["email_form"] = UserEmailForm(user=user)
        # Check if the user is in the group: 'coordinators',
        # and add the reminder email preferences form.
        if test_func_coordinators_only(user):
            context["coordinator_email_form"] = CoordinatorEmailForm(user=user)

        try:
            if user.editor == editor and not editor.contributions:
                messages.add_message(
                    self.request,
                    messages.WARNING,
                    # fmt: off
                    # Translators: This message is shown on user's own profile page, encouraging them to make sure their information is up to date, so that account coordinators can use the information to judge applications.
                    _("Please <a href='{url}'>update your contributions</a> to Wikipedia to help coordinators evaluate your applications.")
                    .format(
                        url=reverse_lazy(
                        "users:editor_update", kwargs={"pk": editor.pk}
                        )
                    ),
                    # fmt: on
                )
        except Editor.DoesNotExist:
            """
            If the user viewing the site does not have an attached editor
            (which can happen for administrative users), this error will be
            thrown, preventing the user from viewing the site. We don't actually
            want to have a 500 in this case, though; we just want to not add the
            message, and move on.
            """
            pass

        context["password_form"] = PasswordChangeForm(user=user)
        context["terms_form"] = TermsForm(user_profile=user.userprofile)

        return context
Пример #3
0
    def post(self, request, *args, **kwargs):
        editor = self.get_object()
        if "download" in request.POST:
            # When users click the Download button in the Data section of
            # their user page, provide a json with any personal information
            # they submitted to the site.

            user_json = {}
            editor_data = Editor.objects.get(pk=editor.pk)

            user_json["user_data"] = {}
            user_field_list = [
                "wp_username",
                "contributions",
                "real_name",
                "country_of_residence",
                "occupation",
                "affiliation",
            ]
            for field in user_field_list:
                field_data = getattr(editor_data, field)
                if field_data:
                    user_json["user_data"][field] = field_data

            user_apps = Application.objects.filter(editor=editor)
            user_json["applications"] = {}
            for app in user_apps:
                user_json["applications"][app.id] = {}
                for field in ["rationale", "comments", "account_email"]:
                    field_data = getattr(app, field)
                    if field_data:
                        user_json["applications"][app.id][field] = field_data

            user_comments = Comment.objects.filter(user_id=editor.user.id)
            user_json["comments"] = []
            for comment in user_comments:
                user_json["comments"].append(comment.comment)

            json_data = json.dumps(user_json, indent=2)
            response = HttpResponse(json_data, content_type="application/json")
            response[
                "Content-Disposition"] = "attachment; filename=user_data.json"
            return response

        if "update_email_settings" in request.POST:
            # Unchecked checkboxes just don't send POST data
            if "send_renewal_notices" in request.POST:
                send_renewal_notices = True
            else:
                send_renewal_notices = False

            editor.user.userprofile.send_renewal_notices = send_renewal_notices
            editor.user.userprofile.save()

            user = self.request.user
            # Again, process email preferences data only if the user
            # is present in the group: 'coordinators'.
            if test_func_coordinators_only(user):
                # Unchecked checkboxes doesn't send POST data
                if "send_pending_application_reminders" in request.POST:
                    send_pending_app_reminders = True
                else:
                    send_pending_app_reminders = False
                if "send_discussion_application_reminders" in request.POST:
                    send_discussion_app_reminders = True
                else:
                    send_discussion_app_reminders = False
                if "send_approved_application_reminders" in request.POST:
                    send_approved_app_reminders = True
                else:
                    send_approved_app_reminders = False
                user.userprofile.pending_app_reminders = send_pending_app_reminders
                user.userprofile.discussion_app_reminders = (
                    send_discussion_app_reminders)
                user.userprofile.approved_app_reminders = send_approved_app_reminders
                user.userprofile.save()

                # Although not disallowed, we'd prefer if coordinators opted
                # to receive at least one (of the 3) type of reminder emails.
                # If they choose to receive none, we post a warning message.
                if (not send_pending_app_reminders
                        and not send_discussion_app_reminders
                        and not send_pending_app_reminders):
                    messages.add_message(
                        request,
                        messages.WARNING,
                        # fmt: off
                        # Translators: Coordinators are shown this message when they unselect all three types of reminder email options under preferences.
                        _("You have chosen not to receive reminder emails. As a coordinator, you should receive at least one type of reminder emails, consider changing your preferences."
                          ),
                        # fmt: on
                    )
                else:
                    messages.add_message(
                        request,
                        messages.SUCCESS,
                        # Translators: Coordinators are shown this message when they make changes to their reminder email options under preferences.
                        _("Your reminder email preferences are updated."),
                    )

        return HttpResponseRedirect(reverse_lazy("users:home"))