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.

79 lines
2.2 KiB

10 years ago
8 years ago
8 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 django.template.loader import render_to_string
  5. from django.urls import reverse
  6. from hc.accounts.models import Profile
  7. from hc.api.models import Channel, Check
  8. @admin.register(Profile)
  9. class ProfileAdmin(admin.ModelAdmin):
  10. class Media:
  11. css = {
  12. 'all': ('css/admin/profiles.css',)
  13. }
  14. list_display = ("id", "users", "reports_allowed", "next_report_date",
  15. "ping_log_limit")
  16. search_fields = ["id", "user__email"]
  17. list_filter = ("reports_allowed", "team_access_allowed",
  18. "next_report_date")
  19. def users(self, obj):
  20. if obj.member_set.count() == 0:
  21. return obj.user.email
  22. else:
  23. return render_to_string("admin/profile_list_team.html", {
  24. "profile": obj
  25. })
  26. users.allow_tags = True
  27. class HcUserAdmin(UserAdmin):
  28. actions = ["send_report"]
  29. list_display = ('id', 'email', 'date_joined', 'involvement',
  30. 'is_staff', 'checks')
  31. ordering = ["-id"]
  32. def involvement(self, user):
  33. result = ""
  34. num_checks = Check.objects.filter(user=user).count()
  35. num_channels = Channel.objects.filter(user=user).count()
  36. if num_checks == 0:
  37. result += "0 checks, "
  38. elif num_checks == 1:
  39. result += "1 check, "
  40. else:
  41. result += "<strong>%d checks</strong>, " % num_checks
  42. if num_channels == 0:
  43. result += "0 channels"
  44. elif num_channels == 1:
  45. result += "1 channel, "
  46. else:
  47. result += "<strong>%d channels</strong>, " % num_channels
  48. return result
  49. involvement.allow_tags = True
  50. def checks(self, user):
  51. url = reverse("hc-switch-team", args=[user.username])
  52. return "<a href='%s'>Checks</a>" % url
  53. checks.allow_tags = True
  54. def send_report(self, request, qs):
  55. for user in qs:
  56. user.profile.send_report()
  57. self.message_user(request, "%d email(s) sent" % qs.count())
  58. admin.site.unregister(User)
  59. admin.site.register(User, HcUserAdmin)