2018-03-23 11:46:46 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2018-03-23 11:46:46 +00:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-08-21 12:27:21 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser
|
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2018-03-23 11:46:46 +00:00
|
|
|
from ..models import Domain, Gateway
|
|
|
|
from .. import serializers
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = ['DomainViewSet', 'GatewayViewSet', "GatewayTestConnectionApi"]
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class DomainViewSet(OrgBulkModelViewSet):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Domain.objects.all()
|
2019-09-23 06:41:34 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-03-23 11:46:46 +00:00
|
|
|
serializer_class = serializers.DomainSerializer
|
2018-10-31 07:31:09 +00:00
|
|
|
|
2018-03-25 13:47:29 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.request.query_params.get('gateway'):
|
|
|
|
return serializers.DomainWithGatewaySerializer
|
|
|
|
return super().get_serializer_class()
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class GatewayViewSet(OrgBulkModelViewSet):
|
2018-11-12 08:43:35 +00:00
|
|
|
filter_fields = ("domain__name", "name", "username", "ip", "domain")
|
2018-03-23 11:46:46 +00:00
|
|
|
search_fields = filter_fields
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Gateway.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-23 11:46:46 +00:00
|
|
|
serializer_class = serializers.GatewaySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class GatewayTestConnectionApi(SingleObjectMixin, APIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-23 11:46:46 +00:00
|
|
|
model = Gateway
|
|
|
|
object = None
|
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2018-03-23 11:46:46 +00:00
|
|
|
self.object = self.get_object(Gateway.objects.all())
|
2019-03-19 11:09:09 +00:00
|
|
|
local_port = self.request.data.get('port') or self.object.port
|
|
|
|
ok, e = self.object.test_connective(local_port=local_port)
|
2018-03-23 11:46:46 +00:00
|
|
|
if ok:
|
|
|
|
return Response("ok")
|
|
|
|
else:
|
|
|
|
return Response({"failed": e}, status=404)
|