You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.7 KiB

  1. import csv
  2. from django.contrib import admin
  3. from django.http import HttpResponse
  4. from .models import Account, Project, HonoraryCertificate, Library, IFG, Travel,\
  5. Email, BusinessCard, List, Literature
  6. def export_as_csv(self, request, queryset):
  7. meta = self.model._meta
  8. field_names = [field.name for field in meta.fields]
  9. response = HttpResponse(content_type='text/csv')
  10. response['Content-Disposition'] = 'attachment; filename={}.csv'.format(meta)
  11. writer = csv.writer(response)
  12. writer.writerow(field_names)
  13. for obj in queryset:
  14. row = writer.writerow([getattr(obj, field) for field in field_names])
  15. return response
  16. export_as_csv.short_description = "Ausgewähltes zu CSV exportieren"
  17. admin.site.add_action(export_as_csv)
  18. @admin.register(Project)
  19. class ProjectAdmin(admin.ModelAdmin):
  20. save_as = True
  21. search_fields = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal')
  22. list_display = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status', 'end_quartal')
  23. # action = ['export_as_csv']
  24. date_hierarchy = 'end'
  25. readonly_fields = ('end_quartal', 'project_of_year', 'pid', 'finance_id')
  26. @admin.register(BusinessCard)
  27. class BusinessCardAdmin(admin.ModelAdmin):
  28. save_as = True
  29. search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  30. list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  31. list_display_links = ('realname', 'service_id')
  32. # action = ['export_as_csv']
  33. date_hierarchy = 'granted_date'
  34. readonly_fields = ['service_id']
  35. class Media:
  36. js = ('dropdown/js/base.js',)
  37. @admin.register(Literature)
  38. class LiteratureAdmin(admin.ModelAdmin):
  39. save_as = True
  40. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  41. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  42. list_display_links = ('realname', 'service_id')
  43. date_hierarchy = 'granted_date'
  44. readonly_fields = ['service_id']
  45. @admin.register(Account)
  46. class AccountAdmin(admin.ModelAdmin):
  47. save_as = True
  48. @admin.register(HonoraryCertificate)
  49. class HonoraryCertificateAdmin(admin.ModelAdmin):
  50. save_as = True
  51. search_fields = ['realname', 'granted']
  52. list_display = ('realname', 'granted','project')
  53. date_hierarchy = 'granted_date'
  54. @admin.register(Library)
  55. class LibraryAdmin(admin.ModelAdmin):
  56. save_as = True
  57. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  58. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  59. list_display_links = ('realname', 'service_id')
  60. date_hierarchy = 'granted_date'
  61. readonly_fields = ['service_id']
  62. @admin.register(IFG)
  63. class IFGAdmin(admin.ModelAdmin):
  64. save_as = True
  65. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  66. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  67. list_display_links = ('realname', 'service_id')
  68. date_hierarchy = 'granted_date'
  69. readonly_fields = ['service_id']
  70. @admin.register(Travel)
  71. class TravelAdmin(admin.ModelAdmin):
  72. save_as = True
  73. search_fields = ['realname', 'granted_date']
  74. list_display = ('realname', 'granted', 'granted_date', 'project_end', 'project', 'project_end_quartal')
  75. list_display_links = ('realname', 'project')
  76. date_hierarchy = 'project_end'
  77. readonly_fields = ('project_end_quartal', 'project_end')
  78. autocomplete_fields = ['project']
  79. @admin.register(Email)
  80. class EmailAdmin(admin.ModelAdmin):
  81. save_as = True
  82. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  83. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  84. list_display_links = ('realname', 'service_id')
  85. date_hierarchy = 'granted_date'
  86. radio_fields = {'adult': admin.VERTICAL}
  87. readonly_fields = ['service_id']
  88. class Media:
  89. js = ('dropdown/js/base.js',)
  90. @admin.register(List)
  91. class ListAdmin(admin.ModelAdmin):
  92. save_as = True
  93. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  94. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  95. list_display_links = ('realname', 'service_id')
  96. date_hierarchy = 'granted_date'
  97. readonly_fields = ['service_id']
  98. # commented out because of the individual registering to control displays in admin panel
  99. #admin.site.register([
  100. # Account,
  101. # HonoraryCertificate,
  102. # Library,
  103. # IFG,
  104. # Travel,
  105. # Email,
  106. # List,
  107. # ])