Development of an internal social media platform with personalised dashboards for students
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.

admin.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.contrib import admin
  2. from django.contrib.auth import get_user_model
  3. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  4. from django.contrib.auth.models import User
  5. from .models import Post, CustomUser
  6. from .models import ScheduledReport, ReportRecipient, ScheduledReportGroup
  7. from .forms import ScheduledReportForm
  8. class CustomUserInline(admin.StackedInline):
  9. model = CustomUser
  10. can_delete = False
  11. verbose_name_plural = 'customUsers'
  12. # Define a new User admin
  13. class UserAdmin(BaseUserAdmin):
  14. inlines = (CustomUserInline, )
  15. # Re-register UserAdmin
  16. admin.site.unregister(User)
  17. admin.site.register(User, UserAdmin)
  18. admin.site.register(Post)
  19. class ReportRecipientAdmin(admin.TabularInline):
  20. model = ReportRecipient
  21. class ScheduledReportAdmin(admin.ModelAdmin):
  22. """
  23. List display for Scheduled reports in Django admin
  24. """
  25. model = ScheduledReport
  26. list_display = ('id', 'get_recipients')
  27. inlines = [
  28. ReportRecipientAdmin
  29. ]
  30. form = ScheduledReportForm
  31. def get_recipients(self, model):
  32. recipients = model.reportrecep.all().values_list('email', flat=True)
  33. if not recipients:
  34. return 'No recipients added'
  35. recipient_list = ''
  36. for recipient in recipients:
  37. recipient_list = recipient_list + recipient + ', '
  38. return recipient_list[:-2]
  39. get_recipients.short_description = 'Recipients'
  40. get_recipients.allow_tags = True
  41. class ScheduledReportGroupAdmin(admin.ModelAdmin):
  42. """
  43. List display for ScheduledReportGroup Admin
  44. """
  45. model = ScheduledReportGroup
  46. list_display = ('get_scheduled_report_name','get_report_name')
  47. def get_scheduled_report_name(self, model):
  48. return model.scheduled_report.subject
  49. def get_report_name(self, model):
  50. return model.report.name
  51. get_scheduled_report_name.short_description = "Scheduled Report Name"
  52. get_report_name.short_description = "Report Name"
  53. show_change_link = True
  54. get_report_name.allow_tags = True
  55. admin.site.register(ScheduledReport, ScheduledReportAdmin)
  56. admin.site.register(ScheduledReportGroup, ScheduledReportGroupAdmin)