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.

57 lines
1.5 KiB

10 years ago
  1. from django.contrib import admin
  2. from django.contrib.auth.admin import UserAdmin
  3. from django.contrib.auth.models import User
  4. from hc.accounts.models import Profile
  5. from hc.api.models import Channel, Check
  6. @admin.register(Profile)
  7. class ProfileAdmin(admin.ModelAdmin):
  8. list_display = ("id", "email", "reports_allowed", "next_report_date")
  9. search_fields = ["user__email"]
  10. def email(self, obj):
  11. return obj.user.email
  12. class HcUserAdmin(UserAdmin):
  13. actions = ["send_report"]
  14. list_display = ('id', 'username', 'email', 'date_joined', 'involvement',
  15. 'is_staff')
  16. ordering = ["-id"]
  17. def involvement(self, user):
  18. result = ""
  19. num_checks = Check.objects.filter(user=user).count()
  20. num_channels = Channel.objects.filter(user=user).count()
  21. if num_checks == 0:
  22. result += "0 checks, "
  23. elif num_checks == 1:
  24. result += "1 check, "
  25. else:
  26. result += "<strong>%d checks</strong>, " % num_checks
  27. if num_channels == 0:
  28. result += "0 channels"
  29. elif num_channels == 1:
  30. result += "1 channel, "
  31. else:
  32. result += "<strong>%d channels</strong>, " % num_channels
  33. return result
  34. involvement.allow_tags = True
  35. def send_report(self, request, qs):
  36. for user in qs:
  37. profile = Profile.objects.for_user(user)
  38. profile.send_report()
  39. self.message_user(request, "%d email(s) sent" % qs.count())
  40. admin.site.unregister(User)
  41. admin.site.register(User, HcUserAdmin)