2018-03-23 11:46:46 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2020-10-14 11:39:55 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework.views import APIView, Response
|
|
|
|
from rest_framework.serializers import ValidationError
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
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):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Domain
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("name", )
|
|
|
|
search_fields = filterset_fields
|
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):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Gateway
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("domain__name", "name", "username", "ip", "domain")
|
2019-12-04 09:07:57 +00:00
|
|
|
search_fields = ("domain__name", "name", "username", "ip")
|
2021-07-26 08:32:01 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
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
|
|
|
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
|
2020-10-14 11:39:55 +00:00
|
|
|
try:
|
|
|
|
local_port = int(local_port)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'port': _('Number required')})
|
2019-03-19 11:09:09 +00:00
|
|
|
ok, e = self.object.test_connective(local_port=local_port)
|
2018-03-23 11:46:46 +00:00
|
|
|
if ok:
|
|
|
|
return Response("ok")
|
|
|
|
else:
|
2019-11-13 08:40:34 +00:00
|
|
|
return Response({"error": e}, status=400)
|