admin.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 VPSConfiguration, FingerPrint, Console
  8. class ConsoleInline(admin.TabularInline):
  9. model = Console
  10. extra = 0
  11. class FingerPrintInline(admin.TabularInline):
  12. model = FingerPrint
  13. extra = 0
  14. class VPSConfigurationInline(admin.StackedInline):
  15. model = VPSConfiguration
  16. # fk_name = 'offersubscription'
  17. readonly_fields = ['configuration_ptr']
  18. class VPSConfigurationAdmin(ConfigurationAdminFormMixin, PolymorphicChildModelAdmin):
  19. base_model = VPSConfiguration
  20. list_display = ('offersubscription', 'activated',
  21. 'ipv4_endpoint', 'ipv6_endpoint', 'comment')
  22. list_filter = ('activated',)
  23. search_fields = ('comment',
  24. # TODO: searching on member directly doesn't work
  25. 'offersubscription__member__first_name',
  26. 'offersubscription__member__last_name',
  27. 'offersubscription__member__email')
  28. actions = (delete_selected, "generate_endpoints", "generate_endpoints_v4",
  29. "generate_endpoints_v6", "activate", "deactivate")
  30. inline = VPSConfigurationInline
  31. def get_readonly_fields(self, request, obj=None):
  32. if obj:
  33. return []
  34. else:
  35. return []
  36. def set_activation(self, request, queryset, value):
  37. count = 0
  38. # We must update each object individually, because we want to run
  39. # the save() method to update the backend.
  40. for vps in queryset:
  41. if vps.activated != value:
  42. vps.activated = value
  43. vps.full_clean()
  44. vps.save()
  45. count += 1
  46. action = "activated" if value else "deactivated"
  47. msg = "{} VPS subscription(s) {}.".format(count, action)
  48. self.message_user(request, msg)
  49. def activate(self, request, queryset):
  50. self.set_activation(request, queryset, True)
  51. activate.short_description = "Activate selected VPSs"
  52. def deactivate(self, request, queryset):
  53. self.set_activation(request, queryset, False)
  54. deactivate.short_description = "Deactivate selected VPSs"
  55. def generate_endpoints_generic(self, request, queryset, v4=True, v6=True):
  56. count = 0
  57. for vps in queryset:
  58. if vps.generate_endpoints(v4, v6):
  59. vps.full_clean()
  60. vps.save()
  61. count += 1
  62. msg = "{} VPS subscription(s) updated.".format(count)
  63. self.message_user(request, msg)
  64. def generate_endpoints(self, request, queryset):
  65. self.generate_endpoints_generic(request, queryset)
  66. generate_endpoints.short_description = "Generate IPv4 and IPv6 endpoints"
  67. def generate_endpoints_v4(self, request, queryset):
  68. self.generate_endpoints_generic(request, queryset, v6=False)
  69. generate_endpoints_v4.short_description = "Generate IPv4 endpoints"
  70. def generate_endpoints_v6(self, request, queryset):
  71. self.generate_endpoints_generic(request, queryset, v4=False)
  72. generate_endpoints_v6.short_description = "Generate IPv6 endpoints"
  73. VPSConfigurationAdmin.inlines = VPSConfigurationAdmin.inlines + (FingerPrintInline, ConsoleInline )
  74. admin.site.register(VPSConfiguration, VPSConfigurationAdmin)