Moved actions to separate file

This commit is contained in:
Jonas Winkler 2018-07-11 11:56:53 +02:00
parent 270b0487ec
commit ba9d7c8892
3 changed files with 164 additions and 160 deletions

161
src/documents/actions.py Executable file
View File

@ -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"

View File

@ -1,16 +1,14 @@
from datetime import datetime from datetime import datetime
from django.conf import settings from django.conf import settings
from django.contrib import admin, messages from django.contrib import admin
from django.contrib.admin import helpers
from django.contrib.admin.utils import model_ngettext
from django.contrib.auth.models import User, Group 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.urls import reverse
from django.templatetags.static import static from django.templatetags.static import static
from django.utils.safestring import mark_safe 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 from .models import Correspondent, Tag, Document, Log
@ -144,160 +142,6 @@ class TagAdmin(CommonAdmin):
return obj.documents.count() 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 DocumentAdmin(CommonAdmin):
class Media: class Media:
@ -315,7 +159,6 @@ class DocumentAdmin(CommonAdmin):
ordering = ["-created", "correspondent"] ordering = ["-created", "correspondent"]
actions = [add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, remove_correspondent_from_selected] actions = [add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, remove_correspondent_from_selected]
def has_add_permission(self, request): def has_add_permission(self, request):

View File