diff --git a/src/documents/actions.py b/src/documents/actions.py new file mode 100755 index 000000000..7f23baabd --- /dev/null +++ b/src/documents/actions.py @@ -0,0 +1,161 @@ +from django.contrib import messages +from django.contrib.admin import helpers +from django.contrib.admin.utils import model_ngettext +from django.core.exceptions import PermissionDenied +from django.template.response import TemplateResponse + +from documents.models import Tag, Correspondent + + +def add_tag_to_selected(modeladmin, request, queryset): + opts = modeladmin.model._meta + app_label = opts.app_label + + if not modeladmin.has_change_permission(request): + raise PermissionDenied + + if request.POST.get('post'): + n = queryset.count() + tag = Tag.objects.get(id=request.POST.get('tag_id')) + if n: + for obj in queryset: + obj.tags.add(tag) + obj_display = str(obj) + modeladmin.log_change(request, obj, obj_display) + modeladmin.message_user(request, "Successfully added tag %(tag)s to %(count)d %(items)s." % { + "tag": tag.name, "count": n, "items": model_ngettext(modeladmin.opts, n) + }, messages.SUCCESS) + + # Return None to display the change list page again. + return None + + title = "Add tag to multiple documents" + + context = dict( + modeladmin.admin_site.each_context(request), + title=title, + queryset=queryset, + opts=opts, + action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, + media=modeladmin.media, + action="add_tag_to_selected", + tags=Tag.objects.all() + ) + + request.current_app = modeladmin.admin_site.name + + return TemplateResponse(request, + "admin/%s/%s/mass_modify_tag.html" % (app_label, opts.model_name) + , context) + + +add_tag_to_selected.short_description = "Add tag to selected documents" + + +def remove_tag_from_selected(modeladmin, request, queryset): + opts = modeladmin.model._meta + app_label = opts.app_label + + if not modeladmin.has_change_permission(request): + raise PermissionDenied + + if request.POST.get('post'): + n = queryset.count() + tag = Tag.objects.get(id=request.POST.get('tag_id')) + if n: + for obj in queryset: + obj.tags.remove(tag) + obj_display = str(obj) + modeladmin.log_change(request, obj, obj_display) + modeladmin.message_user(request, "Successfully removed tag %(tag)s from %(count)d %(items)s." % { + "tag": tag.name, "count": n, "items": model_ngettext(modeladmin.opts, n) + }, messages.SUCCESS) + + # Return None to display the change list page again. + return None + + title = "Remove tag from multiple documents" + + context = dict( + modeladmin.admin_site.each_context(request), + title=title, + queryset=queryset, + opts=opts, + action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, + media=modeladmin.media, + action="remove_tag_from_selected", + tags=Tag.objects.all() + ) + + request.current_app = modeladmin.admin_site.name + + return TemplateResponse(request, + "admin/%s/%s/mass_modify_tag.html" % (app_label, opts.model_name) + , context) + + +remove_tag_from_selected.short_description = "Remove tag from selected documents" + + +def set_correspondent_on_selected(modeladmin, request, queryset): + opts = modeladmin.model._meta + app_label = opts.app_label + + if not modeladmin.has_change_permission(request): + raise PermissionDenied + + if request.POST.get('post'): + n = queryset.count() + correspondent = Correspondent.objects.get(id=request.POST.get('correspondent_id')) + if n: + for obj in queryset: + obj_display = str(obj) + modeladmin.log_change(request, obj, obj_display) + queryset.update(correspondent=correspondent) + modeladmin.message_user(request, "Successfully set correspondent %(correspondent)s on %(count)d %(items)s." % { + "correspondent": correspondent.name, "count": n, "items": model_ngettext(modeladmin.opts, n) + }, messages.SUCCESS) + + # Return None to display the change list page again. + return None + + title = "Set correspondent on multiple documents" + + context = dict( + modeladmin.admin_site.each_context(request), + title=title, + queryset=queryset, + opts=opts, + action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, + media=modeladmin.media, + correspondents=Correspondent.objects.all() + ) + + request.current_app = modeladmin.admin_site.name + + return TemplateResponse(request, + "admin/%s/%s/set_correspondent.html" % (app_label, opts.model_name) + , context) + + +set_correspondent_on_selected.short_description = "Set correspondent on selected documents" + + +def remove_correspondent_from_selected(modeladmin, request, queryset): + if not modeladmin.has_change_permission(request): + raise PermissionDenied + + n = queryset.count() + if n: + for obj in queryset: + obj_display = str(obj) + modeladmin.log_change(request, obj, obj_display) + queryset.update(correspondent=None) + modeladmin.message_user(request, "Successfully removed correspondent from %(count)d %(items)s." % { + "count": n, "items": model_ngettext(modeladmin.opts, n) + }, messages.SUCCESS) + + return None + + +remove_correspondent_from_selected.short_description = "Remove correspondent from selected documents" diff --git a/src/documents/admin.py b/src/documents/admin.py index a2fb6cf8b..a85b087a3 100755 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -1,16 +1,14 @@ from datetime import datetime from django.conf import settings -from django.contrib import admin, messages -from django.contrib.admin import helpers -from django.contrib.admin.utils import model_ngettext +from django.contrib import admin from django.contrib.auth.models import User, Group -from django.core.exceptions import PermissionDenied -from django.template.response import TemplateResponse from django.urls import reverse from django.templatetags.static import static from django.utils.safestring import mark_safe +from documents.actions import add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, \ + remove_correspondent_from_selected from .models import Correspondent, Tag, Document, Log @@ -144,160 +142,6 @@ class TagAdmin(CommonAdmin): return obj.documents.count() -def add_tag_to_selected(modeladmin, request, queryset): - opts = modeladmin.model._meta - app_label = opts.app_label - - if not modeladmin.has_change_permission(request): - raise PermissionDenied - - if request.POST.get('post'): - n = queryset.count() - tag = Tag.objects.get(id=request.POST.get('tag_id')) - if n: - for obj in queryset: - obj.tags.add(tag) - obj_display = str(obj) - modeladmin.log_change(request, obj, obj_display) - modeladmin.message_user(request, "Successfully added tag %(tag)s to %(count)d %(items)s." % { - "tag": tag.name, "count": n, "items": model_ngettext(modeladmin.opts, n) - }, messages.SUCCESS) - - # Return None to display the change list page again. - return None - - title = "Add tag to multiple documents" - - context = dict( - modeladmin.admin_site.each_context(request), - title=title, - queryset=queryset, - opts=opts, - action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, - media=modeladmin.media, - action="add_tag_to_selected", - tags=Tag.objects.all() - ) - - request.current_app = modeladmin.admin_site.name - - return TemplateResponse(request, - "admin/%s/%s/mass_modify_tag.html" % (app_label, opts.model_name) - , context) - - -add_tag_to_selected.short_description = "Add tag to selected documents" - - -def remove_tag_from_selected(modeladmin, request, queryset): - opts = modeladmin.model._meta - app_label = opts.app_label - - if not modeladmin.has_change_permission(request): - raise PermissionDenied - - if request.POST.get('post'): - n = queryset.count() - tag = Tag.objects.get(id=request.POST.get('tag_id')) - if n: - for obj in queryset: - obj.tags.remove(tag) - obj_display = str(obj) - modeladmin.log_change(request, obj, obj_display) - modeladmin.message_user(request, "Successfully removed tag %(tag)s from %(count)d %(items)s." % { - "tag": tag.name, "count": n, "items": model_ngettext(modeladmin.opts, n) - }, messages.SUCCESS) - - # Return None to display the change list page again. - return None - - title = "Remove tag from multiple documents" - - context = dict( - modeladmin.admin_site.each_context(request), - title=title, - queryset=queryset, - opts=opts, - action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, - media=modeladmin.media, - action="remove_tag_from_selected", - tags=Tag.objects.all() - ) - - request.current_app = modeladmin.admin_site.name - - return TemplateResponse(request, - "admin/%s/%s/mass_modify_tag.html" % (app_label, opts.model_name) - , context) - - -remove_tag_from_selected.short_description = "Remove tag from selected documents" - - -def set_correspondent_on_selected(modeladmin, request, queryset): - opts = modeladmin.model._meta - app_label = opts.app_label - - if not modeladmin.has_change_permission(request): - raise PermissionDenied - - if request.POST.get('post'): - n = queryset.count() - correspondent = Correspondent.objects.get(id=request.POST.get('correspondent_id')) - if n: - for obj in queryset: - obj_display = str(obj) - modeladmin.log_change(request, obj, obj_display) - queryset.update(correspondent=correspondent) - modeladmin.message_user(request, "Successfully set correspondent %(correspondent)s on %(count)d %(items)s." % { - "correspondent": correspondent.name, "count": n, "items": model_ngettext(modeladmin.opts, n) - }, messages.SUCCESS) - - # Return None to display the change list page again. - return None - - title = "Set correspondent on multiple documents" - - context = dict( - modeladmin.admin_site.each_context(request), - title=title, - queryset=queryset, - opts=opts, - action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, - media=modeladmin.media, - correspondents=Correspondent.objects.all() - ) - - request.current_app = modeladmin.admin_site.name - - return TemplateResponse(request, - "admin/%s/%s/set_correspondent.html" % (app_label, opts.model_name) - , context) - - -set_correspondent_on_selected.short_description = "Set correspondent on selected documents" - - -def remove_correspondent_from_selected(modeladmin, request, queryset): - if not modeladmin.has_change_permission(request): - raise PermissionDenied - - n = queryset.count() - if n: - for obj in queryset: - obj_display = str(obj) - modeladmin.log_change(request, obj, obj_display) - queryset.update(correspondent=None) - modeladmin.message_user(request, "Successfully removed correspondent from %(count)d %(items)s." % { - "count": n, "items": model_ngettext(modeladmin.opts, n) - }, messages.SUCCESS) - - return None - - -remove_correspondent_from_selected.short_description = "Remove correspondent from selected documents" - - class DocumentAdmin(CommonAdmin): class Media: @@ -315,7 +159,6 @@ class DocumentAdmin(CommonAdmin): ordering = ["-created", "correspondent"] - actions = [add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, remove_correspondent_from_selected] def has_add_permission(self, request): diff --git a/src/documents/templates/admin/documents/document/change_form.html b/src/documents/templates/admin/documents/document/change_form.html old mode 100644 new mode 100755