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.

52 lines
1.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 = "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. @admin.register(BusinessCard)
  25. class BusinessCardAdmin(admin.ModelAdmin):
  26. save_as = True
  27. search_fields = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  28. list_display = ('realname', 'service_id', 'granted', 'granted_date', 'project')
  29. # action = ['export_as_csv']
  30. admin.site.register([
  31. Account,
  32. HonoraryCertificate,
  33. Library,
  34. IFG,
  35. Travel,
  36. Email,
  37. List,
  38. Literature,
  39. ])