Add mixin to auto-insert before and remove redundant fields definitions

This commit is contained in:
Roman 2025-10-08 12:34:17 +02:00
parent f646561136
commit 48e0d1d2b1
1 changed files with 61 additions and 127 deletions

View File

@ -2,6 +2,7 @@ import csv
from django.contrib import admin
from django.http import HttpResponse
from django.core.exceptions import FieldDoesNotExist
from .models import (
Account,
@ -19,8 +20,44 @@ from .models import (
)
def export_as_csv(self, request, queryset):
class RequestURLBeforeInternNotesMixin:
"""
Ensures that 'request_url' (if present) appears directly before 'intern_notes'.
Works whether 'fields' is explicitly defined or derived from the Model/Form.
"""
request_url_field_name = 'request_url'
anchor_field_name = 'intern_notes' # field to insert before or after
insert_before = True # set False to insert after the anchor
def get_fields(self, request, obj=None):
# Get the current list of fields from the parent class
fields = list(super().get_fields(request, obj))
req = self.request_url_field_name
anchor = self.anchor_field_name
# Skip modification if the model doesn't have 'request_url'
try:
self.model._meta.get_field(req)
except FieldDoesNotExist:
return fields
# Ensure 'request_url' is included in the field list
if req not in fields:
fields.append(req)
# If the anchor field exists, move 'request_url' to the desired position
if anchor in fields and req in fields:
# Remove and reinsert 'request_url' at the correct index
fields = [f for f in fields if f != req]
idx = fields.index(anchor)
insert_at = idx if self.insert_before else idx + 1
fields.insert(insert_at, req)
return fields
def export_as_csv(self, request, queryset):
meta = self.model._meta
field_names = [field.name for field in meta.fields]
@ -34,16 +71,24 @@ def export_as_csv(self, request, queryset):
return response
export_as_csv.short_description = "Ausgewähltes zu CSV exportieren"
admin.site.add_action(export_as_csv)
@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
save_as = True
search_fields = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal')
list_display = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal')
fields = ('realname', 'email', 'granted', 'granted_date', 'mail_state', 'end_mail_send', 'survey_mail_send', 'survey_mail_date', 'name', 'description', 'pid', 'finance_id', 'start', 'end', 'otrs', 'plan', 'page', 'urls', 'group', 'location', 'participants_estimated', 'participants_real', 'insurance', 'insurance_technic', 'support', 'cost', 'account', 'granted_from', 'notes', 'intern_notes', 'status', 'project_of_year', 'end_quartal')
search_fields = ('name', 'pid', 'finance_id', 'realname', 'start', 'end', 'participants_estimated',
'participants_real', 'cost', 'status', 'end_quartal')
list_display = ('name', 'pid', 'finance_id', 'realname', 'start', 'end', 'participants_estimated',
'participants_real', 'cost', 'status', 'end_quartal')
fields = ('realname', 'email', 'granted', 'granted_date', 'mail_state', 'end_mail_send', 'survey_mail_send',
'survey_mail_date', 'name', 'description', 'pid', 'finance_id', 'start', 'end', 'otrs', 'plan', 'page',
'urls', 'group', 'location', 'participants_estimated', 'participants_real', 'insurance',
'insurance_technic', 'support', 'cost', 'account', 'granted_from', 'notes', 'intern_notes', 'status',
'project_of_year', 'end_quartal')
# action = ['export_as_csv']
date_hierarchy = 'end'
readonly_fields = ('end_quartal', 'project_of_year', 'pid', 'finance_id')
@ -53,7 +98,7 @@ class ProjectAdmin(admin.ModelAdmin):
@admin.register(BusinessCard)
class BusinessCardAdmin(admin.ModelAdmin):
class BusinessCardAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project', 'terms_accepted')
@ -62,33 +107,12 @@ class BusinessCardAdmin(admin.ModelAdmin):
date_hierarchy = 'granted_date'
readonly_fields = ['service_id']
fields = [
'terms_accepted',
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'project',
'data',
'variant',
'url_of_pic',
'sent_to',
'send_data_to_print',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/base.js', 'dropdown/js/otrs_link.js')
@admin.register(Literature)
class LiteratureAdmin(admin.ModelAdmin):
class LiteratureAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
@ -96,28 +120,6 @@ class LiteratureAdmin(admin.ModelAdmin):
date_hierarchy = 'granted_date'
readonly_fields = ['service_id']
fields = [
'terms_accepted',
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'cost',
'notes',
'info',
'source',
'selfbuy',
'selfbuy_give_data',
'selfbuy_data',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/otrs_link.js',)
@ -134,12 +136,13 @@ class HonoraryCertificateAdmin(admin.ModelAdmin):
list_display = ('realname', 'granted', 'project')
date_hierarchy = 'granted_date'
autocomplete_fields = ['project']
class Media:
js = ('dropdown/js/otrs_link.js',)
@admin.register(Library, ELiterature, Software)
class LibraryAdmin(admin.ModelAdmin):
class LibraryAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
list_display = ('realname', 'service_id', 'granted', 'granted_date')
@ -148,24 +151,6 @@ class LibraryAdmin(admin.ModelAdmin):
readonly_fields = ['service_id']
exclude = ['type']
fields = [
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'cost',
'notes',
'library',
'duration',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/otrs_link.js',)
@ -184,7 +169,7 @@ class LibraryAdmin(admin.ModelAdmin):
@admin.register(IFG)
class IFGAdmin(admin.ModelAdmin):
class IFGAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
list_display = ('realname', 'service_id', 'granted', 'granted_date')
@ -192,31 +177,16 @@ class IFGAdmin(admin.ModelAdmin):
date_hierarchy = 'granted_date'
readonly_fields = ['service_id']
fields = [
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'cost',
'notes',
'url',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/otrs_link.js',)
@admin.register(Travel)
class TravelAdmin(admin.ModelAdmin):
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', 'project_end_quartal')
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project_end', 'project',
'project_end_quartal')
list_display_links = ('realname', 'project')
date_hierarchy = 'project_end'
autocomplete_fields = ['project']
@ -227,7 +197,7 @@ class TravelAdmin(admin.ModelAdmin):
@admin.register(Email)
class EmailAdmin(admin.ModelAdmin):
class EmailAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
@ -236,31 +206,12 @@ class EmailAdmin(admin.ModelAdmin):
radio_fields = {'adult': admin.VERTICAL}
readonly_fields = ['service_id']
fields = [
'terms_accepted',
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'domain',
'address',
'other',
'adult',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/base.js', 'dropdown/js/otrs_link.js')
@admin.register(List)
class ListAdmin(admin.ModelAdmin):
class ListAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin):
save_as = True
search_fields = ('realname', 'service_id', 'granted', 'granted_date')
list_display = ('realname', 'service_id', 'granted', 'granted_date', 'terms_accepted')
@ -268,23 +219,6 @@ class ListAdmin(admin.ModelAdmin):
date_hierarchy = 'granted_date'
readonly_fields = ['service_id']
fields = [
'terms_accepted',
'realname',
'email',
'granted',
'granted_date',
'survey_mail_date',
'mail_state',
'survey_mail_send',
'username',
'domain',
'address',
'request_url',
'intern_notes',
'service_id',
]
class Media:
js = ('dropdown/js/otrs_link.js',)