This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Debug mode is now configurable in the configuration file. This way, we don't have to edit versioned files to disable it on production systems. - Recent correspondents filter (enable in configuration file) - Document actions: Edit tags and correspondents on multiple documents at once - Replaced month list filter with date drilldown - Sortable document count columns on Tag and Correspondent admin - Last correspondence column on Correspondent admin - Save and edit next functionality for document editing
- Loading branch information
Jonas Winkler
committed
Sep 13, 2018
1 parent
2edf65d
commit fb6f2e0
Showing
8 changed files
with
301 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
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 select_action(modeladmin, request, queryset, title, action, modelclass, success_message="", document_action=None, queryset_action=None): | ||
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() | ||
selected_object = modelclass.objects.get(id=request.POST.get('obj_id')) | ||
if n: | ||
for document in queryset: | ||
if document_action: | ||
document_action(document, selected_object) | ||
document_display = str(document) | ||
modeladmin.log_change(request, document, document_display) | ||
if queryset_action: | ||
queryset_action(queryset, selected_object) | ||
|
||
modeladmin.message_user(request, success_message % { | ||
"selected_object": selected_object.name, "count": n, "items": model_ngettext(modeladmin.opts, n) | ||
}, messages.SUCCESS) | ||
|
||
# Return None to display the change list page again. | ||
return None | ||
|
||
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=action, | ||
objects=modelclass.objects.all(), | ||
itemname=model_ngettext(modelclass, 1) | ||
) | ||
|
||
request.current_app = modeladmin.admin_site.name | ||
|
||
return TemplateResponse(request, "admin/%s/%s/select_object.html" % (app_label, opts.model_name), context) | ||
|
||
|
||
def simple_action(modeladmin, request, queryset, success_message="", document_action=None, queryset_action=None): | ||
if not modeladmin.has_change_permission(request): | ||
raise PermissionDenied | ||
|
||
n = queryset.count() | ||
if n: | ||
for document in queryset: | ||
if document_action: | ||
document_action(document) | ||
document_display = str(document) | ||
modeladmin.log_change(request, document, document_display) | ||
if queryset_action: | ||
queryset_action(queryset) | ||
modeladmin.message_user(request, success_message % { | ||
"count": n, "items": model_ngettext(modeladmin.opts, n) | ||
}, messages.SUCCESS) | ||
|
||
# Return None to display the change list page again. | ||
return None | ||
|
||
|
||
def add_tag_to_selected(modeladmin, request, queryset): | ||
return select_action(modeladmin=modeladmin, request=request, queryset=queryset, | ||
title="Add tag to multiple documents", | ||
action="add_tag_to_selected", | ||
modelclass=Tag, | ||
success_message="Successfully added tag %(selected_object)s to %(count)d %(items)s.", | ||
document_action=lambda doc, tag: doc.tags.add(tag)) | ||
add_tag_to_selected.short_description = "Add tag to selected documents" | ||
|
||
|
||
def remove_tag_from_selected(modeladmin, request, queryset): | ||
return select_action(modeladmin=modeladmin, request=request, queryset=queryset, | ||
title="Remove tag from multiple documents", | ||
action="remove_tag_from_selected", | ||
modelclass=Tag, | ||
success_message="Successfully removed tag %(selected_object)s from %(count)d %(items)s.", | ||
document_action=lambda doc, tag: doc.tags.remove(tag)) | ||
remove_tag_from_selected.short_description = "Remove tag from selected documents" | ||
|
||
|
||
def set_correspondent_on_selected(modeladmin, request, queryset): | ||
return select_action(modeladmin=modeladmin, request=request, queryset=queryset, | ||
title="Set correspondent on multiple documents", | ||
action="set_correspondent_on_selected", | ||
modelclass=Correspondent, | ||
success_message="Successfully set correspondent %(selected_object)s on %(count)d %(items)s.", | ||
queryset_action=lambda qs, correspondent: qs.update(correspondent=correspondent)) | ||
set_correspondent_on_selected.short_description = "Set correspondent on selected documents" | ||
|
||
|
||
def remove_correspondent_from_selected(modeladmin, request, queryset): | ||
return simple_action(modeladmin=modeladmin, request=request, queryset=queryset, | ||
success_message="Successfully removed correspondent from %(count)d %(items)s.", | ||
queryset_action=lambda qs: qs.update(correspondent=None)) | ||
remove_correspondent_from_selected.short_description = "Remove correspondent from selected documents" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.