kopia lustrzana https://github.com/wagtail/wagtail
[feat] Add view and template to toggle user activity in bulk
rodzic
51b6d4f637
commit
f55dec7aeb
|
@ -0,0 +1,42 @@
|
|||
{% extends 'wagtailadmin/bulk_actions/confirmation/base.html' %}
|
||||
{% load i18n wagtailusers_tags wagtailadmin_tags %}
|
||||
|
||||
{% block titletag %}{% blocktrans count counter=users|length %}Toggle activty of 1 user {% plural %}Toggle activty of {{ counter }} users{% endblocktrans %}{% endblock %}
|
||||
|
||||
{% block header %}
|
||||
{% trans "Toggle activity" as header_str %}
|
||||
{% include "wagtailadmin/shared/header.html" with title=header_str icon="doc-empty-inverse" %}
|
||||
{% endblock header %}
|
||||
|
||||
{% block objects_with_access %}
|
||||
{% if users %}
|
||||
<p>{% trans "Are you sure you want to change these users' activity status?" %}</p>
|
||||
<ul>
|
||||
{% for user in users %}
|
||||
<li>
|
||||
<a href="{% url 'wagtailusers_users:edit' user.user.pk %}" target="_blank" rel="noopener noreferrer">{{user.user|user_display_name }}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endif %}
|
||||
{% endblock objects_with_access %}
|
||||
|
||||
{% block objects_with_no_access %}
|
||||
|
||||
{% blocktrans asvar no_access_msg count counter=users_with_no_access|length %}You don't have permission to edit this user{% plural %}You don't have permission to edit these users{% endblocktrans %}
|
||||
{% include 'wagtailusers/bulk_actions/list_objects_with_no_access.html' with objects=users_with_no_access no_access_msg=no_access_msg %}
|
||||
|
||||
{% trans "You cannot mark yourself as inactive" as no_access_msg %}
|
||||
{% include 'wagtailusers/bulk_actions/list_objects_with_no_access.html' with objects=mark_self_as_inactive no_access_msg=no_access_msg %}
|
||||
|
||||
{% endblock objects_with_no_access %}
|
||||
|
||||
{% block form_section %}
|
||||
{% if users %}
|
||||
{% trans 'Yes, toggle' as action_button_text %}
|
||||
{% trans "No, don't toggle" as no_action_button_text %}
|
||||
{% include 'wagtailadmin/bulk_actions/confirmation/form_with_fields.html' %}
|
||||
{% else %}
|
||||
{% include 'wagtailadmin/bulk_actions/confirmation/go_back.html' %}
|
||||
{% endif %}
|
||||
{% endblock form_section %}
|
|
@ -1,5 +1,6 @@
|
|||
from .assign_role import assign_role
|
||||
from .delete import delete
|
||||
from .toggle_activity import toggle_activity
|
||||
|
||||
|
||||
__all__ = ['assign_role', 'delete']
|
||||
__all__ = ['assign_role', 'delete', 'toggle_activity']
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
from django import forms
|
||||
from django.template.response import TemplateResponse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils.translation import ngettext
|
||||
|
||||
from wagtail.core import hooks
|
||||
from wagtail.core.compat import AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME
|
||||
from wagtail.users.views.bulk_actions.user_bulk_action import UserBulkAction
|
||||
|
||||
|
||||
change_user_perm = "{0}.change_{1}".format(AUTH_USER_APP_LABEL, AUTH_USER_MODEL_NAME.lower())
|
||||
|
||||
|
||||
class ActivityForm(forms.Form):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['mark_as_active'] = forms.BooleanField(
|
||||
required=False
|
||||
)
|
||||
self.fields['apply_on_applicable'] = forms.BooleanField(
|
||||
required=False
|
||||
)
|
||||
|
||||
|
||||
class ToggleActivityBulkAction(UserBulkAction):
|
||||
display_name = _("Toggle activity")
|
||||
action_type = "toggle_activity"
|
||||
aria_label = _("Mark users as active or inactive")
|
||||
template_name = "wagtailusers/bulk_actions/confirm_bulk_toggle_activity.html"
|
||||
action_priority = 20
|
||||
form_class = ActivityForm
|
||||
|
||||
def check_perm(self, obj):
|
||||
return self.request.user.has_perm(change_user_perm)
|
||||
|
||||
def get_execution_context(self):
|
||||
return {
|
||||
'mark_as_active': self.cleaned_form.cleaned_data['mark_as_active'],
|
||||
'user': self.request.user
|
||||
}
|
||||
|
||||
def prepare_action(self, objects):
|
||||
if self.cleaned_form.cleaned_data['apply_on_applicable']:
|
||||
return
|
||||
if not self.request.user.is_superuser:
|
||||
return
|
||||
if not self.cleaned_form.cleaned_data['mark_as_active']:
|
||||
for user in objects:
|
||||
if user == self.request.user:
|
||||
context = self.get_context_data()
|
||||
context['users'] = list(filter(lambda x: x['user'].pk != user.pk, context['users']))
|
||||
context['mark_self_as_inactive'] = [user]
|
||||
return TemplateResponse(self.request, self.template_name, context)
|
||||
|
||||
@classmethod
|
||||
def execute_action(cls, objects, **kwargs):
|
||||
cls.mark_as_active = kwargs.get('mark_as_active', False)
|
||||
user = kwargs.get('user', None)
|
||||
if user is not None:
|
||||
objects = list(filter(lambda x: x.pk != user.pk, objects))
|
||||
for user in objects:
|
||||
user.is_active = cls.mark_as_active
|
||||
user.save()
|
||||
cls.num_parent_objects += 1
|
||||
|
||||
def get_success_message(self):
|
||||
activity_status = "active" if self.mark_as_active else "inactive"
|
||||
return ngettext(
|
||||
"%(num_parent_objects)d user has been marked as %(activity_status)s",
|
||||
"%(num_parent_objects)d users have been marked as %(activity_status)s",
|
||||
self.num_parent_objects
|
||||
) % {
|
||||
'num_parent_objects': self.num_parent_objects,
|
||||
'activity_status': activity_status
|
||||
}
|
||||
|
||||
|
||||
@hooks.register('register_user_bulk_action')
|
||||
def toggle_activity(request):
|
||||
return ToggleActivityBulkAction(request)
|
Ładowanie…
Reference in New Issue