Exemplo n.º 1
0
    def post(self, request):
        reference = request.POST.get("reference") == "yes"
        target_topic = request.POST.get("target_topic", "").strip()
        from_date = parse_date_or_none(request.POST.get("from_date"))
        to_date = parse_date_or_none(request.POST.get("to_date"))

        try:
            target_topic = Topic.objects.get(title=target_topic)
            generic_superuser = get_generic_superuser()

            topic_list_raw = list(self.get_object_list())
            entries_list = Entry.objects_published.filter(
                topic__in=topic_list_raw)

            if from_date:
                entries_list = entries_list.filter(date_created__gte=from_date)

            if to_date:
                entries_list = entries_list.filter(date_created__lte=to_date)

            entries_count = entries_list.count()
            entries_list.update(
                topic=target_topic)  # Bulk update, does not call save()
            target_topic.register_wishes()

            # Include an informative entry on old topic to indicate the new topic
            if reference:
                bulk_list = [
                    Entry(
                        topic=obj,
                        content=
                        f"({pgettext('editor', 'see')}: {target_topic.title})",
                        author=generic_superuser,
                    ) for obj in topic_list_raw
                ]
                Entry.objects.bulk_create(bulk_list)

            # Admin log
            log_admin(
                f"TopicMove action, count: {entries_count}. sources->{topic_list_raw},"
                f"from->{from_date} to->{to_date}",
                request.user,
                Topic,
                target_topic,
            )

            notifications.success(
                request,
                ngettext("%(count)d entry was transferred",
                         "%(count)d entries were transferred", entries_count) %
                {"count": entries_count},
            )
        except Topic.DoesNotExist:
            notifications.error(request,
                                gettext("Couldn't find the target topic."))
        except Author.DoesNotExist:
            notifications.error(request,
                                gettext("GENERIC_SUPERUSER is missing?"))

        return redirect(self.get_changelist_url())
Exemplo n.º 2
0
    def accept_application(self):
        # Alter the user status
        user = self.novice
        user.application_status = Author.APPROVED
        user.is_novice = False
        user.save()

        # Log admin info
        admin_info_msg = _(
            "Authorship of the user '%(username)s' was approved.") % {
                "username": user.username
            }
        log_admin(admin_info_msg, self.request.user, Author, user)

        # Send information messages to the user
        user_info_msg = _(
            "dear %(username)s, congratulations! your application"
            " of authorship has been approved. you can utilize your"
            " authorship by logging in.") % {
                "username": user.username
            }
        Message.objects.compose(get_generic_superuser(), user, user_info_msg)
        user.email_user(_("your authorship has been approved"), user_info_msg,
                        settings.FROM_EMAIL)

        notifications.success(self.request, admin_info_msg)
        return True
Exemplo n.º 3
0
    def decline_application(self):
        # Alter the user status & delete entries
        user = self.novice
        Entry.objects_published.filter(
            author=user).delete()  # does not trigger model's delete()
        user.application_status = Author.ON_HOLD
        user.application_date = None
        user.save()

        # Log admin info
        admin_info_msg = _(
            "Authorship of the user '%(username)s' was rejected.") % {
                "username": user.username
            }
        log_admin(admin_info_msg, self.request.user, Author, user)

        # Send information messages to the user
        user_info_msg = _(
            "dear %(username)s, your application of authorship has been"
            " rejected and all of your entries has been deleted. if you"
            " fill up 10 entries, you will be admitted to novice list again."
        ) % {
            "username": user.username
        }
        Message.objects.compose(get_generic_superuser(), user, user_info_msg)
        user.email_user(_("your authorship has been rejected"), user_info_msg,
                        settings.FROM_EMAIL)

        notifications.success(self.request, admin_info_msg)
        return True
Exemplo n.º 4
0
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.shortcuts import redirect, reverse
from django.utils.translation import gettext as _
from django.views.generic import TemplateView

from dictionary.utils.admin import log_admin


class ClearCache(PermissionRequiredMixin, TemplateView):
    template_name = "admin/sites/clear_cache.html"
    permission_required = "dictionary.can_clear_cache"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update(admin.site.each_context(self.request))
        context["title"] = _("Clear cache")
        return context

    def post(self, request, *args, **kwargs):
        if (key := request.POST.get("cache_key") or None) is not None:
            message = _("The cache with key '%(key)s' has been invalidated.") % {"key": key}
            cache.delete(key)
        else:
            message = _("All of cache has been invalidated.")
            cache.clear()

        log_admin(f"Cleared cache. /cache_key: {key}/", request.user, Site, request.site)
        notifications.warning(request, message)
        return redirect(reverse("admin:index"))