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
|
2020-12-13 16:59:35 +00:00
|
|
|
from rest_framework.response import Response
|
2020-04-07 17:45:16 +00:00
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
from django.shortcuts import render
|
2020-04-07 17:45:16 +00:00
|
|
|
|
2020-04-03 17:27:49 +00:00
|
|
|
from rest_framework import viewsets, permissions
|
2020-03-02 06:17:04 +00:00
|
|
|
|
2020-04-03 17:27:49 +00:00
|
|
|
from .models import *
|
|
|
|
from .serializers import *
|
2020-12-13 10:38:41 +00:00
|
|
|
from .selectors import *
|
2020-12-13 16:59:35 +00:00
|
|
|
from .services import *
|
2020-12-13 10:38:41 +00:00
|
|
|
from .forms import *
|
2020-12-20 11:20:54 +00:00
|
|
|
from .tasks import *
|
2020-04-03 17:27:49 +00:00
|
|
|
|
2020-12-13 10:38:41 +00:00
|
|
|
# class VPNPoolViewSet(viewsets.ModelViewSet):
|
|
|
|
# serializer_class = VPNPoolSerializer
|
|
|
|
# permission_classes = [permissions.IsAdminUser]
|
|
|
|
# queryset = VPNPool.objects.all()
|
2020-04-03 17:27:49 +00:00
|
|
|
|
2020-12-13 12:28:43 +00:00
|
|
|
class WireGuardVPNViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = WireGuardVPNSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-04-06 20:30:01 +00:00
|
|
|
|
2020-12-13 12:28:43 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
if self.request.user.is_superuser:
|
|
|
|
obj = WireGuardVPN.objects.all()
|
|
|
|
else:
|
|
|
|
obj = WireGuardVPN.objects.filter(owner=self.request.user)
|
2020-04-03 17:27:49 +00:00
|
|
|
|
2020-12-13 12:28:43 +00:00
|
|
|
return obj
|
2020-04-03 17:27:49 +00:00
|
|
|
|
2020-12-13 16:59:35 +00:00
|
|
|
def create(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
|
|
vpn = create_wireguard_vpn(
|
|
|
|
owner=self.request.user,
|
|
|
|
public_key=serializer.validated_data['wireguard_public_key'],
|
|
|
|
network_mask=serializer.validated_data['network_mask']
|
|
|
|
)
|
2020-12-20 11:45:36 +00:00
|
|
|
configure_wireguard_server(vpn.vpnpool)
|
2020-12-13 16:59:35 +00:00
|
|
|
return Response(WireGuardVPNSerializer(vpn).data)
|
|
|
|
|
2020-12-09 19:22:33 +00:00
|
|
|
|
2020-12-13 10:38:41 +00:00
|
|
|
class WireGuardVPNCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
|
|
|
|
model = WireGuardVPN
|
2020-12-09 19:22:33 +00:00
|
|
|
|
|
|
|
login_url = '/login/'
|
|
|
|
success_url = '/'
|
2020-12-13 10:38:41 +00:00
|
|
|
success_message = "%(network) was created successfully"
|
2020-12-09 19:22:33 +00:00
|
|
|
|
2020-12-13 10:38:41 +00:00
|
|
|
form_class = WireGuardVPNForm
|
2020-12-09 19:22:33 +00:00
|
|
|
|
|
|
|
def get_success_message(self, cleaned_data):
|
|
|
|
return self.success_message % dict(cleaned_data,
|
|
|
|
the_prefix = self.object.prefix)
|