forked from beba/foerderbarometer
WM-16: reorder fields in admin
This commit is contained in:
parent
0a2bcac684
commit
714d249608
|
|
@ -4,6 +4,7 @@ from django.contrib import admin
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
from input.utils.list import reorder_value
|
||||||
from input.utils.mail import send_decision_mails
|
from input.utils.mail import send_decision_mails
|
||||||
|
|
||||||
from .forms import BaseProjectForm
|
from .forms import BaseProjectForm
|
||||||
|
|
@ -24,24 +25,21 @@ from .models import (
|
||||||
BusinessCard,
|
BusinessCard,
|
||||||
List,
|
List,
|
||||||
Literature,
|
Literature,
|
||||||
TYPE_PROJ,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RequestURLBeforeInternNotesMixin:
|
class WMDEAdmin(admin.ModelAdmin):
|
||||||
"""
|
|
||||||
Ensures that 'request_url' appears directly before 'intern_notes'.
|
|
||||||
Works whether 'fields' is explicitly defined or derived from the Model/Form.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def get_fields(self, request, obj=None):
|
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
|
return fields
|
||||||
|
|
||||||
|
|
@ -199,7 +197,7 @@ class ProjectDeclinedAdmin(BaseProjectAdmin):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(BusinessCard)
|
@admin.register(BusinessCard)
|
||||||
class BusinessCardAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class BusinessCardAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
|
||||||
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project', 'terms_accepted')
|
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project', 'terms_accepted')
|
||||||
|
|
@ -213,7 +211,7 @@ class BusinessCardAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Literature)
|
@admin.register(Literature)
|
||||||
class LiteratureAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class LiteratureAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
||||||
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
||||||
|
|
@ -243,7 +241,7 @@ class HonoraryCertificateAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Library, ELiterature, Software)
|
@admin.register(Library, ELiterature, Software)
|
||||||
class LibraryAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class LibraryAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
||||||
list_display = ('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)
|
@admin.register(IFG)
|
||||||
class IFGAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class IFGAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
||||||
list_display = ('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)
|
@admin.register(Travel)
|
||||||
class TravelAdmin(admin.ModelAdmin):
|
class TravelAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ['realname', 'service_id', 'granted_date', 'project__name', 'project__pid']
|
search_fields = ['realname', 'service_id', 'granted_date', 'project__name', 'project__pid']
|
||||||
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project_end', 'project',
|
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project_end', 'project',
|
||||||
|
|
@ -298,7 +296,7 @@ class TravelAdmin(admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Email)
|
@admin.register(Email)
|
||||||
class EmailAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class EmailAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
||||||
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
||||||
|
|
@ -312,7 +310,7 @@ class EmailAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
||||||
|
|
||||||
|
|
||||||
@admin.register(List)
|
@admin.register(List)
|
||||||
class ListAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
|
class ListAdmin(WMDEAdmin):
|
||||||
save_as = True
|
save_as = True
|
||||||
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
|
||||||
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue