admin.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib import admin
  4. from polymorphic.admin import PolymorphicChildModelAdmin
  5. from coin.configuration.admin import ConfigurationAdminFormMixin
  6. from coin.utils import delete_selected
  7. from .models import HousingConfiguration
  8. class HousingConfigurationInline(admin.StackedInline):
  9. model = HousingConfiguration
  10. # fk_name = 'offersubscription'
  11. readonly_fields = ['configuration_ptr']
  12. class HousingConfigurationAdmin(ConfigurationAdminFormMixin, PolymorphicChildModelAdmin):
  13. base_model = HousingConfiguration
  14. list_display = ('offersubscription', 'activated',
  15. 'ipv4_endpoint', 'ipv6_endpoint', 'comment')
  16. list_filter = ('activated',)
  17. search_fields = ('comment',
  18. # TODO: searching on member directly doesn't work
  19. 'offersubscription__member__first_name',
  20. 'offersubscription__member__last_name',
  21. 'offersubscription__member__email')
  22. actions = (delete_selected, "generate_endpoints", "generate_endpoints_v4",
  23. "generate_endpoints_v6", "activate", "deactivate")
  24. inline = HousingConfigurationInline
  25. def get_readonly_fields(self, request, obj=None):
  26. if obj:
  27. return []
  28. else:
  29. return []
  30. def set_activation(self, request, queryset, value):
  31. count = 0
  32. # We must update each object individually, because we want to run
  33. # the save() method to update the backend.
  34. for housing in queryset:
  35. if housing.activated != value:
  36. housing.activated = value
  37. housing.full_clean()
  38. housing.save()
  39. count += 1
  40. action = "activated" if value else "deactivated"
  41. msg = "{} Housing subscription(s) {}.".format(count, action)
  42. self.message_user(request, msg)
  43. def activate(self, request, queryset):
  44. self.set_activation(request, queryset, True)
  45. activate.short_description = "Activate selected Housings"
  46. def deactivate(self, request, queryset):
  47. self.set_activation(request, queryset, False)
  48. deactivate.short_description = "Deactivate selected Housings"
  49. def generate_endpoints_generic(self, request, queryset, v4=True, v6=True):
  50. count = 0
  51. for housing in queryset:
  52. if housing.generate_endpoints(v4, v6):
  53. housing.full_clean()
  54. housing.save()
  55. count += 1
  56. msg = "{} Housing subscription(s) updated.".format(count)
  57. self.message_user(request, msg)
  58. def generate_endpoints(self, request, queryset):
  59. self.generate_endpoints_generic(request, queryset)
  60. generate_endpoints.short_description = "Generate IPv4 and IPv6 endpoints"
  61. def generate_endpoints_v4(self, request, queryset):
  62. self.generate_endpoints_generic(request, queryset, v6=False)
  63. generate_endpoints_v4.short_description = "Generate IPv4 endpoints"
  64. def generate_endpoints_v6(self, request, queryset):
  65. self.generate_endpoints_generic(request, queryset, v4=False)
  66. generate_endpoints_v6.short_description = "Generate IPv6 endpoints"
  67. admin.site.register(HousingConfiguration, HousingConfigurationAdmin)