diff --git a/input/admin.py b/input/admin.py index 1e41504..3bacccb 100755 --- a/input/admin.py +++ b/input/admin.py @@ -4,6 +4,7 @@ from django.contrib import admin from django.db import models, transaction from django.http import HttpResponse +from input.utils.list import reorder_value from input.utils.mail import send_decision_mails from .forms import BaseProjectForm @@ -24,24 +25,21 @@ from .models import ( BusinessCard, List, Literature, - TYPE_PROJ, ) -class RequestURLBeforeInternNotesMixin: - """ - Ensures that 'request_url' appears directly before 'intern_notes'. - Works whether 'fields' is explicitly defined or derived from the Model/Form. - """ +class WMDEAdmin(admin.ModelAdmin): def get_fields(self, request, obj=None): - fields = [*super().get_fields(request, obj)] + fields = super().get_fields(request, obj=obj) - fields.remove('request_url') + if 'username' in fields: + fields = reorder_value(fields, 'username', after='email') - index = fields.index('intern_notes') + fields = reorder_value(fields, 'request_url', before='intern_notes') - fields.insert(index, 'request_url') + if 'terms_accepted' in fields: + fields = reorder_value(fields, 'terms_accepted', before='request_url') return fields @@ -199,7 +197,7 @@ class ProjectDeclinedAdmin(BaseProjectAdmin): @admin.register(BusinessCard) -class BusinessCardAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class BusinessCardAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project') list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project', 'terms_accepted') @@ -213,7 +211,7 @@ class BusinessCardAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): @admin.register(Literature) -class LiteratureAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class LiteratureAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted') @@ -243,7 +241,7 @@ class HonoraryCertificateAdmin(admin.ModelAdmin): @admin.register(Library, ELiterature, Software) -class LibraryAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class LibraryAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') @@ -270,7 +268,7 @@ class LibraryAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): @admin.register(IFG) -class IFGAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class IFGAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') @@ -283,7 +281,7 @@ class IFGAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): @admin.register(Travel) -class TravelAdmin(admin.ModelAdmin): +class TravelAdmin(WMDEAdmin): save_as = True search_fields = ['realname', 'service_id', 'granted_date', 'project__name', 'project__pid'] list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project_end', 'project', @@ -298,7 +296,7 @@ class TravelAdmin(admin.ModelAdmin): @admin.register(Email) -class EmailAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class EmailAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted') @@ -312,7 +310,7 @@ class EmailAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): @admin.register(List) -class ListAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): +class ListAdmin(WMDEAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted') diff --git a/input/utils/list.py b/input/utils/list.py new file mode 100644 index 0000000..1a4f624 --- /dev/null +++ b/input/utils/list.py @@ -0,0 +1,24 @@ +from typing import Iterable + + +def reorder_value(values: Iterable, value, *, after=None, before=None): + """ + Reorders a value after or before another value in the given list. + Does not work properly for duplicate or None values. + Raises ValueError when any of the values is not contained in the list. + """ + + assert (after is None) != (before is None), 'Either after or before is needed but not both.' + + values = list(values) + + values.remove(value) + + if after is None: + index = values.index(before) + else: + index = values.index(after) + 1 + + values.insert(index, value) + + return values