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 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib import admin
  4. from django.core.exceptions import PermissionDenied
  5. from django.utils.translation import ugettext_lazy as _
  6. from .models import Hit, HitCount
  7. class HitAdmin(admin.ModelAdmin):
  8. list_display = ('created', 'user', 'ip', 'user_agent', 'hitcount')
  9. search_fields = ('ip', 'user_agent')
  10. date_hierarchy = 'created'
  11. actions = ['blacklist_ips',
  12. 'blacklist_user_agents',
  13. 'blacklist_delete_ips',
  14. 'blacklist_delete_user_agents',
  15. 'delete_queryset',
  16. ]
  17. def __init__(self, *args, **kwargs):
  18. super(HitAdmin, self).__init__(*args, **kwargs)
  19. self.list_display_links = None
  20. def has_add_permission(self, request):
  21. return False
  22. def get_actions(self, request):
  23. actions = super(HitAdmin, self).get_actions(request)
  24. if 'delete_selected' in actions:
  25. del actions['delete_selected']
  26. return actions
  27. def delete_queryset(self, request, queryset):
  28. if not self.has_delete_permission(request):
  29. raise PermissionDenied
  30. else:
  31. if queryset.count() == 1:
  32. msg = "1 hit was"
  33. else:
  34. msg = "%s hits were" % queryset.count()
  35. for obj in queryset.iterator():
  36. obj.delete() # calling it this way to get custom delete() method
  37. self.message_user(request, "%s successfully deleted." % msg)
  38. delete_queryset.short_description = _("Delete selected hits")
  39. admin.site.register(Hit, HitAdmin)
  40. class HitCountAdmin(admin.ModelAdmin):
  41. list_display = ('content_object', 'hits', 'modified')
  42. fields = ('hits',)
  43. def has_add_permission(self, request):
  44. return False
  45. admin.site.register(HitCount, HitCountAdmin)