views.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.shortcuts import get_object_or_404
  4. from django.views.generic.edit import UpdateView
  5. from django.conf import settings
  6. from django.contrib.messages.views import SuccessMessageMixin
  7. from django.contrib.auth.decorators import login_required
  8. from django.utils.decorators import method_decorator
  9. from coin.members.models import Member
  10. from .models import VPSConfiguration
  11. class VPSView(SuccessMessageMixin, UpdateView):
  12. model = VPSConfiguration
  13. fields = ['ipv4_endpoint', 'ipv6_endpoint', 'comment']
  14. success_message = "Configuration enregistrée avec succès !"
  15. @method_decorator(login_required)
  16. def dispatch(self, *args, **kwargs):
  17. return super(VPSView, self).dispatch(*args, **kwargs)
  18. def get_form(self, form_class=None):
  19. if settings.MEMBER_CAN_EDIT_VPS_CONF:
  20. return super(VPSView, self).get_form(form_class)
  21. return None
  22. def get_object(self):
  23. if self.request.user.is_superuser:
  24. return get_object_or_404(VPSConfiguration, pk=self.kwargs.get("pk"))
  25. # For normal users, ensure the VPS belongs to them.
  26. return get_object_or_404(VPSConfiguration, pk=self.kwargs.get("pk"),
  27. offersubscription__member=self.request.user)