import csv from django.contrib import admin from django.http import HttpResponse from django.core.exceptions import FieldDoesNotExist from .models import ( Account, Project, HonoraryCertificate, Library, ELiterature, Software, IFG, Travel, Email, BusinessCard, List, Literature, ) 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] response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta) writer = csv.writer(response) writer.writerow(field_names) for obj in queryset: row = writer.writerow([getattr(obj, field) for field in field_names]) 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') # action = ['export_as_csv'] date_hierarchy = 'end' readonly_fields = ('end_quartal', 'project_of_year', 'pid', 'finance_id') class Media: js = ('dropdown/js/otrs_link.js',) @admin.register(BusinessCard) 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') list_display_links = ('realname', 'service_id') # action = ['export_as_csv'] date_hierarchy = 'granted_date' readonly_fields = ['service_id'] class Media: js = ('dropdown/js/base.js', 'dropdown/js/otrs_link.js') @admin.register(Literature) 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') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] class Media: js = ('dropdown/js/otrs_link.js',) @admin.register(Account) class AccountAdmin(admin.ModelAdmin): save_as = True @admin.register(HonoraryCertificate) class HonoraryCertificateAdmin(admin.ModelAdmin): save_as = True search_fields = ['realname', 'granted', 'project__name', 'project__pid'] 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(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] exclude = ['type'] class Media: js = ('dropdown/js/otrs_link.js',) def get_queryset(self, request): return super().get_queryset(request).filter(type=self.model.TYPE) def formfield_for_dbfield(self, db_field, request, **kwargs): if db_field.name == 'library': kwargs['label'] = self.model.LIBRARY_LABEL kwargs['help_text'] = self.model.LIBRARY_HELP_TEXT elif db_field.name == 'duration': kwargs['help_text'] = self.model.DURATION_HELP_TEXT return super().formfield_for_dbfield(db_field, request, **kwargs) @admin.register(IFG) class IFGAdmin(RequestURLBeforeInternNotesMixin, admin.ModelAdmin): save_as = True search_fields = ('realname', 'service_id', 'granted', 'granted_date') list_display = ('realname', 'service_id', 'granted', 'granted_date') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['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_links = ('realname', 'project') date_hierarchy = 'project_end' autocomplete_fields = ['project'] readonly_fields = ['service_id', 'project_end', 'project_end_quartal'] class Media: js = ('dropdown/js/otrs_link.js',) @admin.register(Email) 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') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' radio_fields = {'adult': admin.VERTICAL} readonly_fields = ['service_id'] class Media: js = ('dropdown/js/base.js', 'dropdown/js/otrs_link.js') @admin.register(List) 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') list_display_links = ('realname', 'service_id') date_hierarchy = 'granted_date' readonly_fields = ['service_id'] class Media: js = ('dropdown/js/otrs_link.js',) # commented out because of the individual registering to control displays in admin panel # admin.site.register([ # Account, # HonoraryCertificate, # Library, # IFG, # Travel, # Email, # List, # ])