uncloud/uncloud_net/views.py

60 lines
1.7 KiB
Python
Raw Normal View History

2020-12-09 19:22:33 +00:00
from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.shortcuts import render
from rest_framework import viewsets, permissions
from .models import *
from .serializers import *
2020-04-06 20:30:01 +00:00
class VPNPoolViewSet(viewsets.ModelViewSet):
serializer_class = VPNPoolSerializer
permission_classes = [permissions.IsAdminUser]
queryset = VPNPool.objects.all()
class VPNNetworkReservationViewSet(viewsets.ModelViewSet):
serializer_class = VPNNetworkReservationSerializer
permission_classes = [permissions.IsAdminUser]
queryset = VPNNetworkReservation.objects.all()
2020-04-06 20:30:01 +00:00
class VPNNetworkViewSet(viewsets.ModelViewSet):
serializer_class = VPNNetworkSerializer
2020-05-07 18:22:42 +00:00
# permission_classes = [permissions.IsAdminUser]
permission_classes = [permissions.IsAuthenticated]
def get_queryset(self):
if self.request.user.is_superuser:
2020-04-06 20:30:01 +00:00
obj = VPNNetwork.objects.all()
else:
2020-04-06 20:30:01 +00:00
obj = VPNNetwork.objects.filter(owner=self.request.user)
return obj
2020-12-09 19:22:33 +00:00
class VPNCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
model = VPNNetwork
login_url = '/login/'
success_url = '/'
success_message = "%(the_prefix)s/48 was created successfully"
gen_method = "undef"
fields = [ "wireguard_public_key" ]
def get_success_message(self, cleaned_data):
return self.success_message % dict(cleaned_data,
the_prefix = self.object.prefix)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['method'] = self.gen_method
return context