admin.py 3.2 KB

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