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.

124 lines
4.3 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 = "Export Selected"
  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')
  22. list_display = ('name', 'pid','finance_id', 'realname', 'start', 'end', 'participants_estimated', 'participants_real', 'cost', 'status')
  23. # action = ['export_as_csv']
  24. date_hierarchy = 'start'
  25. @admin.register(BusinessCard)
  26. class BusinessCardAdmin(admin.ModelAdmin):
  27. save_as = True
  28. search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  29. list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  30. list_display_links = ('realname', 'service_id')
  31. # action = ['export_as_csv']
  32. date_hierarchy = 'granted_date'
  33. class Media:
  34. js = ('dropdown/js/base.js',)
  35. @admin.register(Literature)
  36. class LiteratureAdmin(admin.ModelAdmin):
  37. save_as = True
  38. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  39. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  40. list_display_links = ('realname', 'service_id')
  41. date_hierarchy = 'granted_date'
  42. class Media:
  43. js = ('dropdown/js/base.js',)
  44. @admin.register(Account)
  45. class AccountAdmin(admin.ModelAdmin):
  46. save_as = True
  47. @admin.register(HonoraryCertificate)
  48. class HonoraryCertificateAdmin(admin.ModelAdmin):
  49. save_as = True
  50. search_fields = ('realname', 'granted', 'granted_date', 'project')
  51. list_display = ('realname', 'granted', 'granted_date', 'project')
  52. list_display_links = ('realname', '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. @admin.register(IFG)
  62. class IFGAdmin(admin.ModelAdmin):
  63. save_as = True
  64. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  65. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  66. list_display_links = ('realname', 'service_id')
  67. date_hierarchy = 'granted_date'
  68. @admin.register(Travel)
  69. class TravelAdmin(admin.ModelAdmin):
  70. save_as = True
  71. search_fields = ('realname', 'granted', 'granted_date', 'project')
  72. list_display = ('realname', 'granted', 'granted_date', 'project')
  73. list_display_links = ('realname', 'project')
  74. date_hierarchy = 'granted_date'
  75. @admin.register(Email)
  76. class EmailAdmin(admin.ModelAdmin):
  77. save_as = True
  78. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  79. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  80. list_display_links = ('realname', 'service_id')
  81. date_hierarchy = 'granted_date'
  82. @admin.register(List)
  83. class ListAdmin(admin.ModelAdmin):
  84. save_as = True
  85. search_fields = ('realname', 'service_id', 'granted', 'granted_date')
  86. list_display = ('realname', 'service_id', 'granted', 'granted_date')
  87. list_display_links = ('realname', 'service_id')
  88. date_hierarchy = 'granted_date'
  89. # commented out because of the individual registering to control displays in admin panel
  90. #admin.site.register([
  91. # Account,
  92. # HonoraryCertificate,
  93. # Library,
  94. # IFG,
  95. # Travel,
  96. # Email,
  97. # List,
  98. # ])