Conform code to standards

This commit is contained in:
Daniel Quinn 2018-09-23 12:41:28 +01:00
parent 117d7dad04
commit 4130dd3465
4 changed files with 198 additions and 103 deletions

View File

@ -4,10 +4,13 @@ from django.contrib.admin.utils import model_ngettext
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from documents.models import Tag, Correspondent from documents.models import Correspondent, Tag
def select_action(modeladmin, request, queryset, title, action, modelclass, success_message="", document_action=None, queryset_action=None): def select_action(
modeladmin, request, queryset, title, action, modelclass,
success_message="", document_action=None, queryset_action=None):
opts = modeladmin.model._meta opts = modeladmin.model._meta
app_label = opts.app_label app_label = opts.app_label
@ -27,7 +30,9 @@ def select_action(modeladmin, request, queryset, title, action, modelclass, succ
queryset_action(queryset, selected_object) queryset_action(queryset, selected_object)
modeladmin.message_user(request, success_message % { modeladmin.message_user(request, success_message % {
"selected_object": selected_object.name, "count": n, "items": model_ngettext(modeladmin.opts, n) "selected_object": selected_object.name,
"count": n,
"items": model_ngettext(modeladmin.opts, n)
}, messages.SUCCESS) }, messages.SUCCESS)
# Return None to display the change list page again. # Return None to display the change list page again.
@ -47,10 +52,17 @@ def select_action(modeladmin, request, queryset, title, action, modelclass, succ
request.current_app = modeladmin.admin_site.name request.current_app = modeladmin.admin_site.name
return TemplateResponse(request, "admin/%s/%s/select_object.html" % (app_label, opts.model_name), context) return TemplateResponse(
request,
"admin/{}/{}/select_object.html".format(app_label, opts.model_name),
context
)
def simple_action(modeladmin, request, queryset, success_message="", document_action=None, queryset_action=None): def simple_action(
modeladmin, request, queryset, success_message="",
document_action=None, queryset_action=None):
if not modeladmin.has_change_permission(request): if not modeladmin.has_change_permission(request):
raise PermissionDenied raise PermissionDenied
@ -72,37 +84,63 @@ def simple_action(modeladmin, request, queryset, success_message="", document_ac
def add_tag_to_selected(modeladmin, request, queryset): def add_tag_to_selected(modeladmin, request, queryset):
return select_action(modeladmin=modeladmin, request=request, queryset=queryset, return select_action(
title="Add tag to multiple documents", modeladmin=modeladmin,
action="add_tag_to_selected", request=request,
modelclass=Tag, queryset=queryset,
success_message="Successfully added tag %(selected_object)s to %(count)d %(items)s.", title="Add tag to multiple documents",
document_action=lambda doc, tag: doc.tags.add(tag)) action="add_tag_to_selected",
add_tag_to_selected.short_description = "Add tag to selected documents" modelclass=Tag,
success_message="Successfully added tag %(selected_object)s to "
"%(count)d %(items)s.",
document_action=lambda doc, tag: doc.tags.add(tag)
)
def remove_tag_from_selected(modeladmin, request, queryset): def remove_tag_from_selected(modeladmin, request, queryset):
return select_action(modeladmin=modeladmin, request=request, queryset=queryset, return select_action(
title="Remove tag from multiple documents", modeladmin=modeladmin,
action="remove_tag_from_selected", request=request,
modelclass=Tag, queryset=queryset,
success_message="Successfully removed tag %(selected_object)s from %(count)d %(items)s.", title="Remove tag from multiple documents",
document_action=lambda doc, tag: doc.tags.remove(tag)) action="remove_tag_from_selected",
remove_tag_from_selected.short_description = "Remove tag from selected documents" modelclass=Tag,
success_message="Successfully removed tag %(selected_object)s from "
"%(count)d %(items)s.",
document_action=lambda doc, tag: doc.tags.remove(tag)
)
def set_correspondent_on_selected(modeladmin, request, queryset): def set_correspondent_on_selected(modeladmin, request, queryset):
return select_action(modeladmin=modeladmin, request=request, queryset=queryset,
title="Set correspondent on multiple documents", return select_action(
action="set_correspondent_on_selected", modeladmin=modeladmin,
modelclass=Correspondent, request=request,
success_message="Successfully set correspondent %(selected_object)s on %(count)d %(items)s.", queryset=queryset,
queryset_action=lambda qs, correspondent: qs.update(correspondent=correspondent)) title="Set correspondent on multiple documents",
set_correspondent_on_selected.short_description = "Set correspondent on selected 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, corr: qs.update(correspondent=corr)
)
def remove_correspondent_from_selected(modeladmin, request, queryset): def remove_correspondent_from_selected(modeladmin, request, queryset):
return simple_action(modeladmin=modeladmin, request=request, queryset=queryset, return simple_action(
success_message="Successfully removed correspondent from %(count)d %(items)s.", modeladmin=modeladmin,
queryset_action=lambda qs: qs.update(correspondent=None)) request=request,
remove_correspondent_from_selected.short_description = "Remove correspondent from selected documents" queryset=queryset,
success_message="Successfully removed correspondent from %(count)d "
"%(items)s.",
queryset_action=lambda qs: qs.update(correspondent=None)
)
add_tag_to_selected.short_description = "Add tag to selected documents"
remove_tag_from_selected.short_description = \
"Remove tag from selected documents"
set_correspondent_on_selected.short_description = \
"Set correspondent on selected documents"
remove_correspondent_from_selected.short_description = \
"Remove correspondent from selected documents"

View File

@ -3,21 +3,23 @@ from datetime import datetime, timedelta
from django.conf import settings from django.conf import settings
from django.contrib import admin, messages from django.contrib import admin, messages
from django.contrib.admin.templatetags.admin_urls import add_preserved_filters from django.contrib.admin.templatetags.admin_urls import add_preserved_filters
from django.contrib.auth.models import User, Group from django.contrib.auth.models import Group, User
from django.db import models
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
from django.templatetags.static import static from django.templatetags.static import static
from django.urls import reverse
from django.utils.html import format_html, format_html_join from django.utils.html import format_html, format_html_join
from django.utils.http import urlquote from django.utils.http import urlquote
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.db import models
from documents.actions import add_tag_to_selected, remove_tag_from_selected, set_correspondent_on_selected, \ from documents.actions import (
remove_correspondent_from_selected add_tag_to_selected,
from .models import Correspondent, Tag, Document, Log remove_correspondent_from_selected,
remove_tag_from_selected,
set_correspondent_on_selected
)
from .models import Correspondent, Document, Log, Tag
class FinancialYearFilter(admin.SimpleListFilter): class FinancialYearFilter(admin.SimpleListFilter):
@ -92,11 +94,18 @@ class RecentCorrespondentFilter(admin.RelatedFieldListFilter):
self.title = "correspondent (recent)" self.title = "correspondent (recent)"
def field_choices(self, field, request, model_admin): def field_choices(self, field, request, model_admin):
years = settings.PAPERLESS_RECENT_CORRESPONDENT_YEARS
days = 365 * years
lookups = [] lookups = []
if settings.PAPERLESS_RECENT_CORRESPONDENT_YEARS and settings.PAPERLESS_RECENT_CORRESPONDENT_YEARS > 0: if years and years > 0:
date_limit = datetime.now() - timedelta(days=365*settings.PAPERLESS_RECENT_CORRESPONDENT_YEARS) correspondents = Correspondent.objects.filter(
for c in Correspondent.objects.filter(documents__created__gte=date_limit).distinct(): documents__created__gte=datetime.now() - timedelta(days=days)
).distinct()
for c in correspondents:
lookups.append((c.id, c.name)) lookups.append((c.id, c.name))
return lookups return lookups
@ -106,13 +115,22 @@ class CommonAdmin(admin.ModelAdmin):
class CorrespondentAdmin(CommonAdmin): class CorrespondentAdmin(CommonAdmin):
list_display = ("name", "match", "matching_algorithm", "document_count", "last_correspondence") list_display = (
"name",
"match",
"matching_algorithm",
"document_count",
"last_correspondence"
)
list_filter = ("matching_algorithm",) list_filter = ("matching_algorithm",)
list_editable = ("match", "matching_algorithm") list_editable = ("match", "matching_algorithm")
def get_queryset(self, request): def get_queryset(self, request):
qs = super(CorrespondentAdmin, self).get_queryset(request) qs = super(CorrespondentAdmin, self).get_queryset(request)
qs = qs.annotate(document_count=models.Count("documents"), last_correspondence=models.Max("documents__created")) qs = qs.annotate(
document_count=models.Count("documents"),
last_correspondence=models.Max("documents__created")
)
return qs return qs
def document_count(self, obj): def document_count(self, obj):
@ -152,17 +170,29 @@ class DocumentAdmin(CommonAdmin):
readonly_fields = ("added",) readonly_fields = ("added",)
list_display = ("title", "created", "added", "thumbnail", "correspondent", list_display = ("title", "created", "added", "thumbnail", "correspondent",
"tags_") "tags_")
list_filter = ("tags", ('correspondent', RecentCorrespondentFilter), "correspondent", FinancialYearFilter) list_filter = (
"tags",
("correspondent", RecentCorrespondentFilter),
"correspondent",
FinancialYearFilter
)
filter_horizontal = ("tags",) filter_horizontal = ("tags",)
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
]
date_hierarchy = 'created' date_hierarchy = "created"
document_queue = None def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.document_queue = []
def has_add_permission(self, request): def has_add_permission(self, request):
return False return False
@ -172,25 +202,38 @@ class DocumentAdmin(CommonAdmin):
created_.short_description = "Created" created_.short_description = "Created"
def changelist_view(self, request, extra_context=None): def changelist_view(self, request, extra_context=None):
response = super().changelist_view(request, extra_context)
if request.method == 'GET': response = super().changelist_view(
request,
extra_context=extra_context
)
if request.method == "GET":
cl = self.get_changelist_instance(request) cl = self.get_changelist_instance(request)
self.document_queue = [doc.id for doc in cl.queryset] self.document_queue = [doc.id for doc in cl.queryset]
return response return response
def change_view(self, request, object_id=None, form_url='', extra_context=None): def change_view(self, request, object_id=None, form_url='',
extra_context=None):
extra_context = extra_context or {} extra_context = extra_context or {}
doc = Document.objects.get(id=object_id)
if self.document_queue and object_id and int(object_id) in self.document_queue: if self.document_queue and object_id:
# There is a queue of documents if int(object_id) in self.document_queue:
current_index = self.document_queue.index(int(object_id)) # There is a queue of documents
if current_index < len(self.document_queue) - 1: current_index = self.document_queue.index(int(object_id))
# ... and there are still documents in the queue if current_index < len(self.document_queue) - 1:
extra_context['next_object'] = self.document_queue[current_index + 1] # ... and there are still documents in the queue
extra_context["next_object"] = self.document_queue[
current_index + 1
]
return super(DocumentAdmin, self).change_view( return super(DocumentAdmin, self).change_view(
request, object_id, form_url, extra_context=extra_context, request,
object_id,
form_url,
extra_context=extra_context,
) )
def response_change(self, request, obj): def response_change(self, request, obj):
@ -200,25 +243,35 @@ class DocumentAdmin(CommonAdmin):
preserved_filters = self.get_preserved_filters(request) preserved_filters = self.get_preserved_filters(request)
msg_dict = { msg_dict = {
'name': opts.verbose_name, "name": opts.verbose_name,
'obj': format_html('<a href="{}">{}</a>', urlquote(request.path), obj), "obj": format_html(
'<a href="{}">{}</a>',
urlquote(request.path),
obj
),
} }
if "_saveandeditnext" in request.POST: if "_saveandeditnext" in request.POST:
msg = format_html( msg = format_html(
'The {name} "{obj}" was changed successfully. Editing next object.', 'The {name} "{obj}" was changed successfully. '
'Editing next object.',
**msg_dict **msg_dict
) )
self.message_user(request, msg, messages.SUCCESS) self.message_user(request, msg, messages.SUCCESS)
redirect_url = reverse('admin:%s_%s_change' % redirect_url = reverse(
(opts.app_label, opts.model_name), "admin:{}_{}_change".format(opts.app_label, opts.model_name),
args=(request.POST['_next_object'],), args=(request.POST["_next_object"],),
current_app=self.admin_site.name) current_app=self.admin_site.name
redirect_url = add_preserved_filters({'preserved_filters': preserved_filters, 'opts': opts}, redirect_url) )
response = HttpResponseRedirect(redirect_url) redirect_url = add_preserved_filters(
else: {
response = super().response_change(request, obj) "preserved_filters": preserved_filters,
"opts": opts
},
redirect_url
)
return HttpResponseRedirect(redirect_url)
return response return super().response_change(request, obj)
@mark_safe @mark_safe
def thumbnail(self, obj): def thumbnail(self, obj):

View File

@ -1,46 +1,50 @@
{% extends "admin/base_site.html" %} {% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls static %} {% load i18n l10n admin_urls static %}
{% load staticfiles %} {% load staticfiles %}
{% block extrahead %}
{{ block.super }}
{{ media }}
<script type="text/javascript" src="{% static 'admin/js/cancel.js' %}"></script>
{% block extrahead %}
{{ block.super }}
{{ media }}
<script type="text/javascript" src="{% static 'admin/js/cancel.js' %}"></script>
{% endblock %} {% endblock %}
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} delete-confirmation delete-selected-confirmation{% endblock %} {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} delete-confirmation delete-selected-confirmation{% endblock %}
{% block breadcrumbs %} {% block breadcrumbs %}
<div class="breadcrumbs"> <div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
&rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a> &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
&rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a> &rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
&rsaquo; {{title}} &rsaquo; {{ title }}
</div> </div>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<p>Please select the {{itemname}}.</p> <p>Please select the {{itemname}}.</p>
<form method="post">{% csrf_token %} <form method="post">{% csrf_token %}
<div> <div>
{% for obj in queryset %} {% for obj in queryset %}
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}"/> <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}"/>
{% endfor %} {% endfor %}
<p> <p>
<select name="obj_id"> <select name="obj_id">
{% for obj in objects %} {% for obj in objects %}
<option value="{{obj.id}}">{{obj.name}}</option> <option value="{{ obj.id }}">{{ obj.name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</p> </p>
<input type="hidden" name="action" value="{{action}}"/> <input type="hidden" name="action" value="{{ action }}"/>
<input type="hidden" name="post" value="yes"/> <input type="hidden" name="post" value="yes" />
<p> <p>
<input type="submit" value="{% trans "Confirm" %}" /> <input type="submit" value="{% trans 'Confirm' %}" />
<a href="#" class="button cancel-link">{% trans "Go back" %}</a> <a href="#" class="button cancel-link">{% trans "Go back" %}</a>
</p> </p>
</div> </div>
</form> </form>
{% endblock %} {% endblock %}

View File

@ -81,7 +81,7 @@ INSTALLED_APPS = [
"rest_framework", "rest_framework",
"crispy_forms", "crispy_forms",
"django_filters" "django_filters",
] ]