Пример #1
0
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.exceptions import ParseError

from fridgify_backend.models.backends import APIAuthentication
from fridgify_backend.models import UserSerializer, Users

logger = logging.getLogger(__name__)


@swagger_auto_schema(
    method="get",
    manual_parameters=[
        openapi.Parameter("Authorization",
                          openapi.IN_HEADER,
                          "API-Token",
                          required=True,
                          type=openapi.TYPE_STRING)
    ],
    operation_description="Retrieve information for current user",
    responses={
        200: openapi.Response("Retrieved current user", UserSerializer),
        401: "Not authorized"
    },
    security=[{
        'FridgifyAPI_Token_Auth': []
    }])
@swagger_auto_schema(
    method="patch",
    manual_parameters=[
        openapi.Parameter("Authorization",
Пример #2
0
class TeacherOtherView(ModelViewSet):
    queryset = Teacher.objects.all()
    serializer_class = TeacherInfoSerializersUpdate
    parser_classes = [MultiPartParser]

    @swagger_auto_schema(
        operation_summary="根据id删除老师信息及用户信息",
        required=[],
        manual_parameters=[
            openapi.Parameter('TOKEN',
                              openapi.IN_HEADER,
                              type=openapi.TYPE_STRING,
                              description='管理员TOKEN')
        ],
    )
    def destroy(self, request, *args, **kwargs):
        check_token = pd_adm_token(request)
        if check_token:
            return check_token
        # 先删除用户
        check_del = del_user_and_user_details(0, kwargs.get("pk"))
        if check_del:
            return check_del
        # 删除老师
        # super().destroy(request, *args, **kwargs)
        return response_success_200(message="成功")

    @swagger_auto_schema(
        operation_summary="根据id列表批量删除老师信息及用户信息",
        operation_description="说明:无",
        # request_body=request_body(properties={
        #     'list_id': array_schema('老师ID列表')
        # }),
        manual_parameters=[
            openapi.Parameter('TOKEN',
                              openapi.IN_HEADER,
                              type=openapi.TYPE_STRING,
                              description='管理员TOKEN'),
            openapi.Parameter('list_id',
                              openapi.IN_QUERY,
                              type=openapi.TYPE_ARRAY,
                              description='老师ID列表',
                              items=openapi.Schema(type=openapi.TYPE_INTEGER))
        ])
    def destroy_all(self, request, *args, **kwargs):
        check_token = pd_adm_token(request)
        if check_token:
            return check_token
        list = request.query_params.get('list_id')
        print(list)
        # 先删除用户
        for i in list:
            check_del = del_user_and_user_details(0, int(i))
        if check_del:
            return check_del
        # 删除老师
        # super().destroy(request, *args, **kwargs)
        return response_success_200(message="成功")

    @swagger_auto_schema(
        operation_summary="修改",
        required=[],
        manual_parameters=[
            openapi.Parameter('title',
                              openapi.IN_FORM,
                              type=openapi.TYPE_INTEGER,
                              description='身份'),
            openapi.Parameter('TOKEN',
                              openapi.IN_HEADER,
                              type=openapi.TYPE_STRING,
                              description='TOKEN')
        ],
    )
    def partial_update(self, request, *args, **kwargs):
        check_token = pd_token(request)
        if check_token:
            return check_token

        print(request.data)
        resp = super().partial_update(request, *args, **kwargs)
        return response_success_200(data=resp.data)

    def get_object(self):
        if self.action == "partial_update":
            print(self.request.user)
            # return self.queryset.get(user=user_id)
            return get_object_or_404(self.queryset, user_id=self.request.user)
        return super().get_object()
Пример #3
0
class Payments(APIView):
    events_repository = EventsRepository()
    users_service = UsersService()
    payments_repository = PaymentsRepository()

    authorization_token = openapi.Parameter(
        'Authorization',
        openapi.IN_HEADER,
        description="Authorization token which starts with Bearer",
        type=openapi.TYPE_STRING)

    payments_response = openapi.Response('response description',
                                         PaymentSerializer(many=True))

    @swagger_auto_schema(
        operation_description='Endpoint for confirming payments.',
        request_body=CreatePaymentSerializer,
        manual_parameters=[authorization_token],
        responses={
            202: 'Payment confirmed',
            400: 'Event does not require payment',
            403: 'User is not a participant',
            404: 'Event not found',
            409: 'User already payed'
        })
    def post(self, request):
        jwt = request.headers['Authorization']
        event_id = request.data['event_id']

        user = self.users_service.fetch_by_jwt(jwt)
        event = self.events_repository.get_event_by_id(event_id)

        payment = self.payments_repository.get_payment_for_user_and_event(
            user, event)

        if not event:
            return JsonResponse(data={'error': 'Event not found'},
                                status=status.HTTP_404_NOT_FOUND,
                                safe=False)
        if event.price is None:
            return JsonResponse(
                data={'error': 'Event does not require payment'},
                status=status.HTTP_400_BAD_REQUEST,
                safe=False)
        if payment:
            return JsonResponse(data={'error': 'User already payed'},
                                status=status.HTTP_409_CONFLICT,
                                safe=False)
        if not event.participants.filter(id=user.id):
            return JsonResponse(data={'error': 'User is not a participant'},
                                status=status.HTTP_400_BAD_REQUEST,
                                safe=False)

        serializer = CreatePaymentSerializer(data=request.data,
                                             context=dict(user=user,
                                                          event=event,
                                                          price=event.price))

        if serializer.is_valid(raise_exception=True):
            payment_to_save = serializer.save()
            self.events_repository.save(payment_to_save)
            return JsonResponse(data={'status': 'Payment confirmed'},
                                status=status.HTTP_202_ACCEPTED,
                                safe=False)
        return HttpResponseBadRequest()

    @swagger_auto_schema(
        responses={200: payments_response},
        operation_description='Endpoint for retrieving users payments.',
        manual_parameters=[authorization_token])
    def get(self, request):
        jwt = request.headers['Authorization']
        user = self.users_service.fetch_by_jwt(jwt)

        users_payments = self.payments_repository.get_payments_for_user(user)

        serializer = PaymentSerializer(users_payments,
                                       many=True,
                                       context=dict(user=user))
        response = serializer.data

        return JsonResponse(response, safe=False)
Пример #4
0
class TaskViewSet(auth.TaskGetQuerySetMixin, viewsets.ModelViewSet):
    queryset = Task.objects.all().prefetch_related(
        "label_set__attributespec_set",
        "segment_set__job_set",
    ).order_by('-id')
    serializer_class = TaskSerializer
    search_fields = ("name", "owner__username", "mode", "status")
    filterset_class = TaskFilter
    ordering_fields = ("id", "name", "owner", "status", "assignee")

    def get_permissions(self):
        http_method = self.request.method
        permissions = [IsAuthenticated]

        if http_method in SAFE_METHODS:
            permissions.append(auth.TaskAccessPermission)
        elif http_method in ["POST"]:
            permissions.append(auth.TaskCreatePermission)
        elif self.action == 'annotations' or http_method in ["PATCH", "PUT"]:
            permissions.append(auth.TaskChangePermission)
        elif http_method in ["DELETE"]:
            permissions.append(auth.TaskDeletePermission)
        else:
            permissions.append(auth.AdminRolePermission)

        return [perm() for perm in permissions]

    def perform_create(self, serializer):
        def validate_task_limit(owner):
            admin_perm = auth.AdminRolePermission()
            is_admin = admin_perm.has_permission(self.request, self)
            if not is_admin and settings.RESTRICTIONS['task_limit'] is not None and \
                Task.objects.filter(owner=owner).count() >= settings.RESTRICTIONS['task_limit']:
                raise serializers.ValidationError(
                    'The user has the maximum number of tasks')

        owner = self.request.data.get('owner', None)
        if owner:
            validate_task_limit(owner)
            serializer.save()
        else:
            validate_task_limit(self.request.user)
            serializer.save(owner=self.request.user)

    def perform_destroy(self, instance):
        task_dirname = instance.get_task_dirname()
        super().perform_destroy(instance)
        shutil.rmtree(task_dirname, ignore_errors=True)
        if instance.data and not instance.data.tasks.all():
            shutil.rmtree(instance.data.get_data_dirname(), ignore_errors=True)
            instance.data.delete()

    @swagger_auto_schema(
        method='get',
        operation_summary='Returns a list of jobs for a specific task',
        responses={'200': JobSerializer(many=True)})
    @action(detail=True, methods=['GET'], serializer_class=JobSerializer)
    def jobs(self, request, pk):
        self.get_object()  # force to call check_object_permissions
        queryset = Job.objects.filter(segment__task_id=pk)
        serializer = JobSerializer(queryset,
                                   many=True,
                                   context={"request": request})

        return Response(serializer.data)

    @swagger_auto_schema(
        method='post',
        operation_summary=
        'Method permanently attaches images or video to a task',
        request_body=DataSerializer,
    )
    @swagger_auto_schema(
        method='get',
        operation_summary='Method returns data for a specific task',
        manual_parameters=[
            openapi.Parameter(
                'type',
                in_=openapi.IN_QUERY,
                required=True,
                type=openapi.TYPE_STRING,
                enum=['chunk', 'frame', 'preview'],
                description="Specifies the type of the requested data"),
            openapi.Parameter(
                'quality',
                in_=openapi.IN_QUERY,
                required=True,
                type=openapi.TYPE_STRING,
                enum=['compressed', 'original'],
                description=
                "Specifies the quality level of the requested data, doesn't matter for 'preview' type"
            ),
            openapi.Parameter(
                'number',
                in_=openapi.IN_QUERY,
                required=True,
                type=openapi.TYPE_NUMBER,
                description=
                "A unique number value identifying chunk or frame, doesn't matter for 'preview' type"
            ),
        ])
    @action(detail=True, methods=['POST', 'GET'])
    def data(self, request, pk):
        if request.method == 'POST':
            db_task = self.get_object(
            )  # call check_object_permissions as well
            serializer = DataSerializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            db_data = serializer.save()
            db_task.data = db_data
            db_task.save()
            data = {k: v for k, v in serializer.data.items()}
            data['use_zip_chunks'] = serializer.validated_data[
                'use_zip_chunks']
            # if the value of stop_frame is 0, then inside the function we cannot know
            # the value specified by the user or it's default value from the database
            if 'stop_frame' not in serializer.validated_data:
                data['stop_frame'] = None
            task.create(db_task.id, data)
            return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
        else:
            data_type = request.query_params.get('type', None)
            data_id = request.query_params.get('number', None)
            data_quality = request.query_params.get('quality', 'compressed')

            possible_data_type_values = ('chunk', 'frame', 'preview')
            possible_quality_values = ('compressed', 'original')

            if not data_type or data_type not in possible_data_type_values:
                return Response(
                    data='data type not specified or has wrong value',
                    status=status.HTTP_400_BAD_REQUEST)
            elif data_type == 'chunk' or data_type == 'frame':
                if not data_id:
                    return Response(data='number not specified',
                                    status=status.HTTP_400_BAD_REQUEST)
                elif data_quality not in possible_quality_values:
                    return Response(data='wrong quality value',
                                    status=status.HTTP_400_BAD_REQUEST)

            try:
                db_task = self.get_object()
                frame_provider = FrameProvider(db_task.data)

                if data_type == 'chunk':
                    data_id = int(data_id)
                    data_quality = FrameProvider.Quality.COMPRESSED \
                        if data_quality == 'compressed' else FrameProvider.Quality.ORIGINAL
                    path = os.path.realpath(
                        frame_provider.get_chunk(data_id, data_quality))

                    # Follow symbol links if the chunk is a link on a real image otherwise
                    # mimetype detection inside sendfile will work incorrectly.
                    return sendfile(request, path)

                elif data_type == 'frame':
                    data_id = int(data_id)
                    data_quality = FrameProvider.Quality.COMPRESSED \
                        if data_quality == 'compressed' else FrameProvider.Quality.ORIGINAL
                    buf, mime = frame_provider.get_frame(data_id, data_quality)

                    return HttpResponse(buf.getvalue(), content_type=mime)

                elif data_type == 'preview':
                    return sendfile(request, frame_provider.get_preview())
                else:
                    return Response(
                        data='unknown data type {}.'.format(data_type),
                        status=status.HTTP_400_BAD_REQUEST)
            except APIException as e:
                return Response(data=e.default_detail, status=e.status_code)
            except Exception as e:
                msg = 'cannot get requested data type: {}, number: {}, quality: {}'.format(
                    data_type, data_id, data_quality)
                slogger.task[pk].error(msg, exc_info=True)
                return Response(data=msg + '\n' + str(e),
                                status=status.HTTP_400_BAD_REQUEST)

    @swagger_auto_schema(
        method='get',
        operation_summary='Method allows to download task annotations',
        manual_parameters=[
            openapi.Parameter(
                'format',
                openapi.IN_QUERY,
                description=
                "Desired output format name\nYou can get the list of supported formats at:\n/server/annotation/formats",
                type=openapi.TYPE_STRING,
                required=False),
            openapi.Parameter('filename',
                              openapi.IN_QUERY,
                              description="Desired output file name",
                              type=openapi.TYPE_STRING,
                              required=False),
            openapi.Parameter(
                'action',
                in_=openapi.IN_QUERY,
                description=
                'Used to start downloading process after annotation file had been created',
                type=openapi.TYPE_STRING,
                required=False,
                enum=['download'])
        ],
        responses={
            '202':
            openapi.Response(
                description='Dump of annotations has been started'),
            '201':
            openapi.Response(
                description='Annotations file is ready to download'),
            '200':
            openapi.Response(description='Download of file started'),
            '405':
            openapi.Response(description='Format is not available'),
        })
    @swagger_auto_schema(
        method='put',
        operation_summary='Method allows to upload task annotations',
        manual_parameters=[
            openapi.Parameter(
                'format',
                openapi.IN_QUERY,
                description=
                "Input format name\nYou can get the list of supported formats at:\n/server/annotation/formats",
                type=openapi.TYPE_STRING,
                required=False),
        ],
        responses={
            '202': openapi.Response(description='Uploading has been started'),
            '201': openapi.Response(description='Uploading has finished'),
            '405': openapi.Response(description='Format is not available'),
        })
    @swagger_auto_schema(
        method='patch',
        operation_summary=
        'Method performs a partial update of annotations in a specific task',
        manual_parameters=[
            openapi.Parameter('action',
                              in_=openapi.IN_QUERY,
                              required=True,
                              type=openapi.TYPE_STRING,
                              enum=['create', 'update', 'delete'])
        ])
    @swagger_auto_schema(
        method='delete',
        operation_summary='Method deletes all annotations for a specific task')
    @action(detail=True,
            methods=['GET', 'DELETE', 'PUT', 'PATCH'],
            serializer_class=LabeledDataSerializer)
    def annotations(self, request, pk):
        db_task = self.get_object()  # force to call check_object_permissions
        if request.method == 'GET':
            format_name = request.query_params.get('format')
            if format_name:
                return _export_annotations(
                    db_task=db_task,
                    rq_id="/api/v1/tasks/{}/annotations/{}".format(
                        pk, format_name),
                    request=request,
                    action=request.query_params.get("action", "").lower(),
                    callback=dm.views.export_task_annotations,
                    format_name=format_name,
                    filename=request.query_params.get("filename", "").lower(),
                )
            else:
                data = dm.task.get_task_data(pk)
                serializer = LabeledDataSerializer(data=data)
                if serializer.is_valid(raise_exception=True):
                    return Response(serializer.data)
        elif request.method == 'PUT':
            format_name = request.query_params.get('format')
            if format_name:
                return _import_annotations(
                    request=request,
                    rq_id="{}@/api/v1/tasks/{}/annotations/upload".format(
                        request.user, pk),
                    rq_func=dm.task.import_task_annotations,
                    pk=pk,
                    format_name=format_name,
                )
            else:
                serializer = LabeledDataSerializer(data=request.data)
                if serializer.is_valid(raise_exception=True):
                    data = dm.task.put_task_data(pk, serializer.data)
                    return Response(data)
        elif request.method == 'DELETE':
            dm.task.delete_task_data(pk)
            return Response(status=status.HTTP_204_NO_CONTENT)
        elif request.method == 'PATCH':
            action = self.request.query_params.get("action", None)
            if action not in dm.task.PatchAction.values():
                raise serializers.ValidationError(
                    "Please specify a correct 'action' for the request")
            serializer = LabeledDataSerializer(data=request.data)
            if serializer.is_valid(raise_exception=True):
                try:
                    data = dm.task.patch_task_data(pk, serializer.data, action)
                except (AttributeError, IntegrityError) as e:
                    return Response(data=str(e),
                                    status=status.HTTP_400_BAD_REQUEST)
                return Response(data)

    @swagger_auto_schema(
        method='get',
        operation_summary=
        'When task is being created the method returns information about a status of the creation process'
    )
    @action(detail=True, methods=['GET'], serializer_class=RqStatusSerializer)
    def status(self, request, pk):
        self.get_object()  # force to call check_object_permissions
        response = self._get_rq_response(queue="default",
                                         job_id="/api/{}/tasks/{}".format(
                                             request.version, pk))
        serializer = RqStatusSerializer(data=response)

        if serializer.is_valid(raise_exception=True):
            return Response(serializer.data)

    @staticmethod
    def _get_rq_response(queue, job_id):
        queue = django_rq.get_queue(queue)
        job = queue.fetch_job(job_id)
        response = {}
        if job is None or job.is_finished:
            response = {"state": "Finished"}
        elif job.is_queued:
            response = {"state": "Queued"}
        elif job.is_failed:
            response = {"state": "Failed", "message": job.exc_info}
        else:
            response = {"state": "Started"}
            if 'status' in job.meta:
                response['message'] = job.meta['status']

        return response

    @staticmethod
    @swagger_auto_schema(
        method='get',
        operation_summary=
        'Method provides a meta information about media files which are related with the task',
        responses={'200': DataMetaSerializer()})
    @action(detail=True,
            methods=['GET'],
            serializer_class=DataMetaSerializer,
            url_path='data/meta')
    def data_info(request, pk):
        db_task = models.Task.objects.prefetch_related(
            'data__images').select_related('data__video').get(pk=pk)

        if hasattr(db_task.data, 'video'):
            media = [db_task.data.video]
        else:
            media = list(db_task.data.images.order_by('frame'))

        frame_meta = [{
            'width': item.width,
            'height': item.height,
            'name': item.path,
        } for item in media]

        db_data = db_task.data
        db_data.frames = frame_meta

        serializer = DataMetaSerializer(db_data)
        return Response(serializer.data)

    @swagger_auto_schema(
        method='get',
        operation_summary='Export task as a dataset in a specific format',
        manual_parameters=[
            openapi.Parameter(
                'format',
                openapi.IN_QUERY,
                description=
                "Desired output format name\nYou can get the list of supported formats at:\n/server/annotation/formats",
                type=openapi.TYPE_STRING,
                required=True),
            openapi.Parameter('filename',
                              openapi.IN_QUERY,
                              description="Desired output file name",
                              type=openapi.TYPE_STRING,
                              required=False),
            openapi.Parameter(
                'action',
                in_=openapi.IN_QUERY,
                description=
                'Used to start downloading process after annotation file had been created',
                type=openapi.TYPE_STRING,
                required=False,
                enum=['download'])
        ],
        responses={
            '202':
            openapi.Response(description='Exporting has been started'),
            '201':
            openapi.Response(
                description='Output file is ready for downloading'),
            '200':
            openapi.Response(description='Download of file started'),
            '405':
            openapi.Response(description='Format is not available'),
        })
    @action(detail=True,
            methods=['GET'],
            serializer_class=None,
            url_path='dataset')
    def dataset_export(self, request, pk):
        db_task = self.get_object()  # force to call check_object_permissions

        format_name = request.query_params.get("format", "")
        return _export_annotations(
            db_task=db_task,
            rq_id="/api/v1/tasks/{}/dataset/{}".format(pk, format_name),
            request=request,
            action=request.query_params.get("action", "").lower(),
            callback=dm.views.export_task_as_dataset,
            format_name=format_name,
            filename=request.query_params.get("filename", "").lower(),
        )
Пример #5
0
class JobViewSet(viewsets.GenericViewSet, mixins.RetrieveModelMixin,
                 mixins.UpdateModelMixin):
    queryset = Job.objects.all().order_by('id')
    serializer_class = JobSerializer

    def get_permissions(self):
        http_method = self.request.method
        permissions = [IsAuthenticated]

        if http_method in SAFE_METHODS:
            permissions.append(auth.JobAccessPermission)
        elif http_method in ["PATCH", "PUT", "DELETE"]:
            permissions.append(auth.JobChangePermission)
        else:
            permissions.append(auth.AdminRolePermission)

        return [perm() for perm in permissions]

    @swagger_auto_schema(
        method='get',
        operation_summary='Method returns annotations for a specific job')
    @swagger_auto_schema(
        method='put',
        operation_summary=
        'Method performs an update of all annotations in a specific job')
    @swagger_auto_schema(
        method='patch',
        manual_parameters=[
            openapi.Parameter('action',
                              in_=openapi.IN_QUERY,
                              type=openapi.TYPE_STRING,
                              required=True,
                              enum=['create', 'update', 'delete'])
        ],
        operation_summary=
        'Method performs a partial update of annotations in a specific job')
    @swagger_auto_schema(
        method='delete',
        operation_summary='Method deletes all annotations for a specific job')
    @action(detail=True,
            methods=['GET', 'DELETE', 'PUT', 'PATCH'],
            serializer_class=LabeledDataSerializer)
    def annotations(self, request, pk):
        self.get_object()  # force to call check_object_permissions
        if request.method == 'GET':
            data = dm.task.get_job_data(pk)
            return Response(data)
        elif request.method == 'PUT':
            format_name = request.query_params.get("format", "")
            if format_name:
                return _import_annotations(
                    request=request,
                    rq_id="{}@/api/v1/jobs/{}/annotations/upload".format(
                        request.user, pk),
                    rq_func=dm.task.import_job_annotations,
                    pk=pk,
                    format_name=format_name)
            else:
                serializer = LabeledDataSerializer(data=request.data)
                if serializer.is_valid(raise_exception=True):
                    try:
                        data = dm.task.put_job_data(pk, serializer.data)
                    except (AttributeError, IntegrityError) as e:
                        return Response(data=str(e),
                                        status=status.HTTP_400_BAD_REQUEST)
                    return Response(data)
        elif request.method == 'DELETE':
            dm.task.delete_job_data(pk)
            return Response(status=status.HTTP_204_NO_CONTENT)
        elif request.method == 'PATCH':
            action = self.request.query_params.get("action", None)
            if action not in dm.task.PatchAction.values():
                raise serializers.ValidationError(
                    "Please specify a correct 'action' for the request")
            serializer = LabeledDataSerializer(data=request.data)
            if serializer.is_valid(raise_exception=True):
                try:
                    data = dm.task.patch_job_data(pk, serializer.data, action)
                except (AttributeError, IntegrityError) as e:
                    return Response(data=str(e),
                                    status=status.HTTP_400_BAD_REQUEST)
                return Response(data)
Пример #6
0
    get_participant_team_id_of_user_for_a_challenge, )

from .models import Submission
from .sender import publish_submission_message
from .serializers import (SubmissionSerializer,
                          CreateLeaderboardDataSerializer)
from .utils import get_submission_model

logger = logging.getLogger(__name__)


@swagger_auto_schema(methods=['post'],
                     manual_parameters=[
                         openapi.Parameter(name='challenge_id',
                                           in_=openapi.IN_PATH,
                                           type=openapi.TYPE_STRING,
                                           description="Challenge ID",
                                           required=True),
                         openapi.Parameter(name='challenge_phase_id',
                                           in_=openapi.IN_PATH,
                                           type=openapi.TYPE_STRING,
                                           description="Challenge Phase ID",
                                           required=True)
                     ],
                     responses={
                         status.HTTP_201_CREATED: openapi.Response(''),
                     })
@swagger_auto_schema(methods=['get'],
                     manual_parameters=[
                         openapi.Parameter(name='challenge_id',
                                           in_=openapi.IN_PATH,
Пример #7
0
class OpenEdxInstanceDeploymentViewSet(CreateAPIView, RetrieveDestroyAPIView,
                                       GenericViewSet):
    """
    Open edX Instance Deployment API.

    This API can be used to manage the configuration for Open edX instances
    owned by clients.
    """
    serializer_class = OpenEdXInstanceDeploymentStatusSerializer
    permission_classes = [IsAuthenticated]

    def get_queryset(self):
        """
        Get `BetaTestApplication` instances owned by current user.
        For a regular user it should return a single object (for now).
        """
        user: User = self.request.user
        if not user.is_authenticated:
            return BetaTestApplication.objects.none()
        elif user.is_staff:
            return BetaTestApplication.objects.all()
        else:
            return BetaTestApplication.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.action == "create":
            return OpenEdXInstanceDeploymentCreateSerializer

        return OpenEdXInstanceDeploymentStatusSerializer

    @swagger_auto_schema(
        responses={
            **VALIDATION_AND_AUTH_RESPONSES,
            200:
            openapi.Response("Changes committed"),
        },
        manual_parameters=[
            openapi.Parameter(
                "force",
                openapi.IN_QUERY,
                type=openapi.TYPE_BOOLEAN,
                description=
                "Force launching a new instance even if one is already in progress.",
            ),
            openapi.Parameter(
                "deployment_type",
                openapi.IN_QUERY,
                type=openapi.TYPE_STRING,
                enum=DeploymentType.names(),
                description="The type of deployment being initiated.",
            )
        ],
    )
    def create(self, request, *args, **kwargs):
        """
        Commit configuration changes to instance and launch new AppServer.

        This API call will copy over any changes made to the instance config to
        the actual instance used to launch AppServers and launch a new AppServer
        with the applied changes.

        It checks if an AppServer is already being provisioned and in that
        case prevents a new one from being launched unless forced.
        """
        force = request.query_params.get("force", False)
        if self.request.user.is_superuser:
            default_deployment_type = DeploymentType.admin.name
        else:
            default_deployment_type = DeploymentType.user.name
        # Allow overriding trigger in case deployment is created by API in some other way.
        deployment_type = request.query_params.get("deployment_type",
                                                   default_deployment_type)
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        try:
            instance_config = BetaTestApplication.objects.get(
                id=serializer.data['id'])
        except BetaTestApplication.DoesNotExist:
            raise ValidationError(
                "The specified instance was not found.",
                code="instance-not-found",
            )

        instance = instance_config.instance
        if instance is None:
            # For a new user an instance will not exist until they've verified their email.
            raise ValidationError(
                "Must verify email before launching an instance",
                code="email-unverified",
            )

        if not force and instance.get_provisioning_appservers().exists():
            raise ValidationError("Instance launch already in progress",
                                  code="in-progress")

        if not EmailAddress.objects.get(
                email=instance_config.public_contact_email).is_confirmed:
            raise ValidationError(
                "Updated public email needs to be confirmed.",
                code="email-unverified")

        instance_config.commit_changes_to_instance(
            deploy_on_commit=True,
            retry_attempts=settings.SELF_SERVICE_SPAWN_RETRY_ATTEMPTS,
            creator=self.request.user.id,
            deployment_type=deployment_type,
        )

        return Response(status=status.HTTP_200_OK)

    def list(self, request, *args, **kwargs):
        """
        List method not allowed.
        """
        return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)

    def retrieve(self, request, *args, **kwargs):
        """
        Retrieves the deployment status for a given betatest instance.

        This API will check for provisioning appservers or changes in settings
        that need to be deployed and return a status code to the frontend.
        """
        application = self.get_object()
        instance = application.instance
        undeployed_changes = build_instance_config_diff(application)
        deployed_changes = None
        deployment_type = None

        if not instance or not instance.get_latest_deployment():
            deployment_status = DeploymentState.preparing
        else:
            deployment = instance.get_latest_deployment()
            deployment_status = deployment.status()
            if deployment_status == DeploymentState.healthy and undeployed_changes:
                deployment_status = DeploymentState.changes_pending
            deployment_type = deployment.type
            deployed_changes = deployment.changes

        data = {
            'undeployed_changes': undeployed_changes,
            'deployed_changes': deployed_changes,
            'status': deployment_status.name,
            'deployment_type': deployment_type,
        }

        return Response(
            status=status.HTTP_200_OK,
            data=OpenEdXInstanceDeploymentStatusSerializer(data).data)

    def destroy(self, request, *args, **kwargs):
        """
        Stops all current redeployments.

        This allows the user to cancel an ongoing deployment, note that this can
        can cancel both user-triggered deployments and OpenCraft triggered
        deployments.
        """
        application = self.get_object()
        instance = application.instance

        if not instance.get_active_appservers().exists():
            return Response(
                status=status.HTTP_400_BAD_REQUEST,
                data={
                    'details':
                    "Can't cancel deployment while server is being prepared."
                })
        deployment = instance.get_latest_deployment()

        if deployment.type != DeploymentType.user.name:
            return Response(
                status=status.HTTP_403_FORBIDDEN,
                data={
                    'details':
                    "Can't cancel deployment not manually initiated by user."
                })

        deployment.terminate_deployment()
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #8
0
class ProjectsViewSet(
    HtmxListMixin,
    HtmxCreateMixin,
    HtmxRetrieveMixin,
    HtmxUpdateMixin,
    HtmxDestroyMixin,
    viewsets.GenericViewSet,
):
    """
    A view set for projects.

    Provides basic CRUD views for projects.
    """

    lookup_url_kwarg = "project"
    object_name = "project"
    queryset_name = "projects"

    def get_permissions(self):
        """
        Get the permissions that the current action requires.

        Override defaults so that, `create`, `list` and `retrive` do not require
        authentication (although other restrictions do apply for anon users).
        """
        if self.action in ["create", "list", "retrieve"]:
            return [permissions.AllowAny()]
        return [permissions.IsAuthenticated()]

    def get_throttles(self):
        """
        Get the throttles to apply to the current request.
        """
        if self.action == "create" and self.request.user.is_anonymous:
            return [ProjectsCreateAnonThrottle()]
        return super().get_throttles()

    def get_queryset(self):
        """
        Get the set of projects that the user has access to and which meet filter criteria.

        Does not return temporary projects.

        TODO: Currently this ignores an authenticated user's access to
              projects inherited from membership of a team.
        """
        queryset = get_projects(self.request.user).select_related("account")

        account = self.request.GET.get("account")
        if account:
            queryset = queryset.filter(account_id=account)

        role = self.request.GET.get("role")
        if self.request.user.is_authenticated and role:
            roles = re.split(r"\s*,\s*", role)
            q = Q()
            for part in roles:
                match = re.match(r"([a-zA-Z]+)(\+)?", part)
                if match:
                    role_name, and_above = match.groups()
                    if role_name.lower() == "member":
                        q |= Q(role__isnull=False)
                    else:
                        try:
                            project_role = ProjectRole.from_string(role_name)
                        except ValueError as exc:
                            raise exceptions.ValidationError({"role": str(exc)})
                        else:
                            if and_above:
                                q |= Q(
                                    role__in=[
                                        role.name
                                        for role in ProjectRole.and_above(project_role)
                                    ]
                                )
                            else:
                                q |= Q(role=project_role.name)
                else:
                    raise exceptions.ValidationError(
                        {"role": "Invalid role specification {}".format(part)}
                    )
            queryset = queryset.filter(q)

        public = self.request.GET.get("public")
        if public:
            if public.lower() in ["false", "no", "0"]:
                queryset = queryset.filter(public=False)
            else:
                queryset = queryset.filter(public=True)

        source = self.request.GET.get("source")
        if source:
            try:
                query = Source.query_from_address(source, prefix="sources")
            except ValueError as exc:
                raise exceptions.ValidationError({"source": str(exc)})
            else:
                queryset = queryset.filter(query)

        search = self.request.GET.get("search")
        if search:
            queryset = queryset.filter(
                Q(name__icontains=search)
                | Q(title__icontains=search)
                | Q(description__icontains=search)
            )

        # Ordering favoring those that the user has a role
        # on, has an image set, has a description set, etc
        return (
            queryset.filter(temporary=False)
            .annotate(
                role_rank=Case(
                    When(role=ProjectRole.OWNER.name, then=Value(6)),
                    When(role=ProjectRole.MANAGER.name, then=Value(5)),
                    When(role=ProjectRole.AUTHOR.name, then=Value(4)),
                    When(role=ProjectRole.EDITOR.name, then=Value(3)),
                    When(role=ProjectRole.REVIEWER.name, then=Value(2)),
                    When(role=ProjectRole.READER.name, then=Value(1)),
                    When(role__isnull=True, then=Value(0)),
                    output_field=IntegerField(),
                )
                if self.request.user.is_authenticated
                else Value(0, output_field=IntegerField()),
                has_image=Case(
                    When(image_file__isnull=False, then=Value(True)),
                    default=Value(False),
                    output_field=BooleanField(),
                ),
                # Use regex filter here to exclude nulls, blanks and very short strings
                has_title=Case(
                    When(title__regex=r"^.{1,}$", then=Value(True)),
                    default=Value(False),
                    output_field=BooleanField(),
                ),
                has_description=Case(
                    When(description__regex=r"^.{1,}$", then=Value(True)),
                    default=Value(False),
                    output_field=BooleanField(),
                ),
            )
            .order_by(
                "-featured",
                "-has_image",
                "-has_title",
                "-has_description",
                "-role_rank",
                "-created",
            )
        )

    def get_object(self):
        """
        Get the project.

        Read access control is done in the `get_project` function.
        For `partial-update` and `destroy` does an additional
        check that the user is a project AUTHOR, MANAGER or OWNER.

        Note that there is a specific `partial_update` serializer for AUTHORs
        which limits which fields of a project they can modify,

        For temporary projects, ensure that the project was accessed
        by it's name, not it's id (this prevent access to a
        temporary project by guessing it's integer id).
        Because temporary objects do not have any users with roles,
        anyone with their name can modify or delete them.
        """
        if hasattr(self, "project"):
            return self.project

        project = get_project(self.kwargs, self.request.user)

        if project.temporary is True:
            if "name" not in filter_from_ident(self.kwargs["project"]):
                raise exceptions.NotFound
            return project

        if (
            self.action == "partial_update"
            and project.role
            not in [
                ProjectRole.AUTHOR.name,
                ProjectRole.MANAGER.name,
                ProjectRole.OWNER.name,
            ]
        ) or (self.action == "destroy" and project.role != ProjectRole.OWNER.name):
            raise exceptions.PermissionDenied

        self.project = project
        return self.project

    def get_serializer_class(self):
        """Get the serializer class for the current action."""
        if self.action == "partial_update":
            if (
                not getattr(self, "swagger_fake_view", False)
                and self.get_object().role == ProjectRole.AUTHOR.name
            ):
                return ProjectAuthorUpdateSerializer
            else:
                return ProjectUpdateSerializer
        return {
            "list": ProjectListSerializer,
            "create": ProjectCreateSerializer,
            "retrieve": ProjectRetrieveSerializer,
            "destroy": ProjectDestroySerializer,
        }.get(self.action, ProjectListSerializer)

    def get_response_context(self, *args, **kwargs):
        """
        Override to add account and account quota usage to the template context.
        """
        context = super().get_response_context(*args, **kwargs)

        project = kwargs.get("instance")
        if project:
            account = project.account
            context["account"] = account
            context[
                "account_project_private_usage"
            ] = AccountQuotas.PROJECTS_PRIVATE.usage(account)

        return context

    def get_success_url(self, serializer):
        """
        Get the URL to use in the Location header when an action is successful.

        For `create`, redirects to the "main" page for the project.
        """
        if self.action in ["create", "partial_update"]:
            project = serializer.instance
            return reverse(
                "ui-projects-retrieve", args=[project.account.name, project.name]
            )
        else:
            return None

    # Most of the following views serve simply to provide docstrings
    # from which API documentation is generated.

    @swagger_auto_schema(
        manual_parameters=[
            openapi.Parameter(
                "account",
                openapi.IN_QUERY,
                description="The integer of the id of the account that the project belongs to.",
                type=openapi.TYPE_INTEGER,
            ),
            openapi.Parameter(
                "role",
                openapi.IN_QUERY,
                description="The role that the currently authenticated user has on the project "
                'e.g. "editor", "owner" (for any role, use "member")',
                type=openapi.TYPE_STRING,
            ),
            openapi.Parameter(
                "public",
                openapi.IN_QUERY,
                description="Whether or not the project is public.",
                type=openapi.TYPE_BOOLEAN,
            ),
            openapi.Parameter(
                "search",
                openapi.IN_QUERY,
                description="A string to search for in the project `name`, `title` or `description`.",
                type=openapi.TYPE_STRING,
            ),
            openapi.Parameter(
                "source",
                openapi.IN_QUERY,
                description="The address of a project source e.g. `github://<org>/<repo>`, `gdoc://<id>`.",
                type=openapi.TYPE_STRING,
            ),
        ]
    )
    def list(self, request: Request, *args, **kwargs) -> Response:
        """
        List projects.

        Returns a list of projects that are accessible to the user, including those that are
        public and those that the user is a member of (i.e. has a project role for).

        The returned list can be filtered using query parameters, `account`, `role`, `public`,
        `search`, `source`. The `role` filter applies to the currently authenticated user, and
        as such has no effected for unauthenticated requests. Roles can be specified as a
        comma separated list e.g. `role=author,manager,owner` or using to the `+` operator
        to indicate the minimum required role e.g. `role=author+` (equivalent to the previous
        example).

        For example, to list all projects for which the authenticated user is a member and which
        uses a particular Google Doc as a source:

            GET /projects?role=member&source=gdoc://1BW6MubIyDirCGW9Wq-tSqCma8pioxBI6VpeLyXn5mZA
        """
        return super().list(request, *args, **kwargs)

    def create(self, request: Request, *args, **kwargs) -> Response:
        """
        Create a project.

        Receives details of the project.
        Returns details of the new project.
        """
        return super().create(request, *args, **kwargs)

    def retrieve(self, request: Request, *args, **kwargs) -> Response:
        """
        Retrieve a project.

        Returns details of the project.
        """
        return super().retrieve(request, *args, **kwargs)

    def partial_update(self, request: Request, *args, **kwargs) -> Response:
        """
        Update a project.

        Receives details of the project.
        Returns updated details of the project.
        """
        return super().partial_update(request, *args, **kwargs)

    def destroy(self, request: Request, *args, **kwargs) -> Response:
        """
        Destroy a project.

        Returns an empty response on success.
        """
        return super().destroy(request, *args, **kwargs)

    @swagger_auto_schema(responses={302: "Redirect to job"})
    @action(detail=True, methods=["POST"])
    def pull(self, request: Request, *args, **kwargs) -> Response:
        """
        Pull the project.

        Creates a pull job and redirects to it.
        """
        project = self.get_object()
        job = project.pull(request.user)
        job.dispatch()
        return redirect_to_job(job, accepts_html=self.accepts_html())

    @swagger_auto_schema(responses={302: "Redirect to job"})
    @action(detail=True, methods=["post"])
    def session(self, request: Request, *args, **kwargs) -> Response:
        """
        Get a session for the project.

        If the user has already created, or is connected to,
        a `session` job for this project, and that job is still running,
        then returns that job. Otherwise, creates a new session.
        """
        project = self.get_object()
        try:
            job = Job.objects.filter(
                project=project,
                snapshot__isnull=True,
                is_active=True,
                **(
                    {"users": request.user}
                    if request.user.is_authenticated
                    else {"anon_users__id": AnonUser.get_id(request)}
                ),
            ).order_by("-created")[0]
        except IndexError:
            job = project.session(request)
            job.dispatch()

        return redirect_to_job(job)
Пример #9
0
        counts = {
            mname: model_counts(model, cursor)
            for (mname, model) in count_models.items()
        }
    cred_counts = solr_counts()
    return JsonResponse({
        "counts": counts,
        "credential_counts": cred_counts,
    })


@swagger_auto_schema(method='post',
                     manual_parameters=[
                         openapi.Parameter(
                             "from_name",
                             openapi.IN_FORM,
                             description="Sender name",
                             type=openapi.TYPE_STRING,
                         ),
                         openapi.Parameter(
                             "from_email",
                             openapi.IN_FORM,
                             description="Sender email address",
                             type=openapi.TYPE_STRING,
                             format=openapi.FORMAT_EMAIL,
                         ),
                         openapi.Parameter(
                             "comments",
                             openapi.IN_FORM,
                             description="Comments",
                             type=openapi.TYPE_STRING,
                         ),
Пример #10
0
class Update_Delete_Post(GenericAPIView):
    serializer_class = PostSerializers
    permission_classes = (IsAuthenticated, )

    def get_queryset(self, pk):
        try:
            post = Post.objects.get(pk=pk)
        except Post.DoesNotExist:
            raise ParseError({
                "error_code": 400,
                "message": "Not Found",
                "data": []
            })
        return post

    @swagger_auto_schema(operation_description="chinh sua bai viet",
                         request_body=openapi.Schema(
                             type=openapi.TYPE_OBJECT,
                             required=['title', 'image', 'category'],
                             properties={
                                 'id':
                                 openapi.Schema(type=openapi.TYPE_INTEGER),
                                 'title':
                                 openapi.Schema(type=openapi.TYPE_STRING),
                                 'image':
                                 openapi.Schema(type=openapi.TYPE_FILE),
                                 'category':
                                 openapi.Schema(type=openapi.TYPE_INTEGER),
                             },
                         ),
                         security=[],
                         manual_parameters=[
                             openapi.Parameter('title',
                                               openapi.IN_QUERY,
                                               "test query title",
                                               type=openapi.TYPE_STRING),
                             openapi.Parameter('image',
                                               openapi.IN_QUERY,
                                               "test query image",
                                               type=openapi.TYPE_FILE),
                             openapi.Parameter('category',
                                               openapi.IN_QUERY,
                                               "test query category",
                                               type=openapi.TYPE_INTEGER),
                             openapi.Parameter('id',
                                               openapi.IN_QUERY,
                                               "post_id",
                                               type=openapi.TYPE_INTEGER,
                                               required=True),
                         ],
                         responses={
                             401: openapi.Response('401 : UNAUTHORIZED'),
                         })
    def patch(self, request, pk):
        post = self.get_queryset(pk)
        data = request.data.copy()
        if not request.data.get('title'):
            data.update({'title': post.title})
        if not request.data.get('image'):
            data.update({'image': post.image})
        if not request.data.get('category'):
            data.update({'category': post.category_id})

        if request.user == post.author:

            serializer = PostSerializers(post, data=data)

            if serializer.is_valid():
                serializer.save()
                data_all = {
                    "error_code": 0,
                    "message": "update post success",
                    "data": serializer.data
                }
                return Response(data_all, status=status.HTTP_201_CREATED)
            else:
                return Response(serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
        else:
            raise ParseError({
                "error_code": 401,
                "message": "UNAUTHORIZED",
                "data": []
            })

    @swagger_auto_schema(operation_description="xoa bai viet",
                         operation_id="post_id",
                         manual_parameters=[
                             openapi.Parameter('id',
                                               openapi.IN_QUERY,
                                               "post_id",
                                               type=openapi.TYPE_INTEGER,
                                               required=True),
                         ],
                         responses={
                             401: openapi.Response('401 : UNAUTHORIZED'),
                         })
    def delete(self, request, pk):
        post = self.get_queryset(pk)

        if request.user == post.author:

            post.delete()
            data = {"error_code": 0, "message": "delete success", "data": []}
            return Response(data, status=status.HTTP_204_NO_CONTENT)
        else:
            raise ParseError({
                "error_code": 401,
                "message": "UNAUTHORIZED",
                "data": []
            })
Пример #11
0
        return Response(status=status.HTTP_204_NO_CONTENT)


@method_decorator(
    name='post',
    decorator=swagger_auto_schema(
        tags=['Projects'],
        operation_summary='Validate project label config',
        operation_description="""
        Determine whether the label configuration for a specific project is valid.
        """,
        manual_parameters=[
            openapi.Parameter(
                name='id',
                type=openapi.TYPE_INTEGER,
                in_=openapi.IN_PATH,
                description='A unique integer value identifying this project.'
            ),
        ],
    ))
class ProjectLabelConfigValidateAPI(generics.RetrieveAPIView):
    """ Validate label config
    """
    parser_classes = (JSONParser, FormParser, MultiPartParser)
    serializer_class = ProjectLabelConfigSerializer
    permission_required = all_permissions.projects_change
    queryset = Project.objects.all()

    def post(self, request, *args, **kwargs):
        project = self.get_object()
        label_config = self.request.data.get('label_config')
Пример #12
0
from rest_framework.decorators import api_view
import time
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from . import functions as f

param_search_hint = openapi.Parameter(
    "search",
    openapi.IN_QUERY,
    description="검색하고 싶은 키워드를 입력하세요.",
    type=openapi.TYPE_STRING,
)
from django.http import HttpResponseNotFound


@swagger_auto_schema(method="get", manual_parameters=[param_search_hint])
@api_view(["GET"])
def keyword(request):
    """
    키워드 검색 API
    ---
    검색한 키워드와 관련된 최근 영상과, 인기있는 영상, 관련 키워드 워드맵 정보 등을 제공하는 api입니다.
    """
    start_time = time.time()
    search = request.query_params.get("search")
    if search:
        return f.search_keyword(search)
    else:
        # 키워드 없을 시 404 not found
        return HttpResponseNotFound("없는 페이지 입니다.")
Пример #13
0
class ApiSwaggerAutoSchema(SwaggerAutoSchema):
    pagination_parameters = [
        openapi.Parameter(name=settings.PAGE_QUERY_PARAM,
                          description='Pagination page number',
                          required=False,
                          in_=openapi.IN_QUERY,
                          type=openapi.TYPE_INTEGER,
                          default=0),
        openapi.Parameter(name=settings.PAGE_SIZE_QUERY_PARAM,
                          description='Pagination page size',
                          required=False,
                          in_=openapi.IN_QUERY,
                          type=openapi.TYPE_INTEGER,
                          default=settings.DEFAULT_PAGE_SIZE),
    ]

    authentication_parameters = [
        openapi.Parameter(settings.API_KEY_HEADER_NAME,
                          description='Header with Api Key',
                          required=True,
                          in_=openapi.IN_HEADER,
                          type=openapi.TYPE_STRING)
    ]

    format_parameters = [
        openapi.Parameter(api_settings.URL_FORMAT_OVERRIDE,
                          description='Response format',
                          required=False,
                          in_=openapi.IN_QUERY,
                          type=openapi.TYPE_STRING,
                          enum=['json', 'xml'])
    ]

    def __init__(self, view, path, method, components, request, overrides):
        super(SwaggerAutoSchema, self).__init__(view, path, method, components,
                                                request, overrides)
        endpoint: Endpoint = view.endpoint
        parameter_fields = self.endpoint_parameters(endpoint)
        normal_response = self.endpoint_response(endpoint)

        self.overrides[
            'operation_summary'] = endpoint.description or f'Get list of {endpoint.name}'
        self.overrides[
            'operation_description'] = endpoint.description or f'{endpoint.name} endpoint'
        self.overrides['manual_parameters'] = parameter_fields
        self.overrides['responses'] = {200: normal_response}

    def endpoint_parameters(self, endpoint: Endpoint):
        parameter_fields = [
            openapi.Parameter(name=param.name,
                              required=param.required,
                              in_=openapi.IN_QUERY,
                              type=param.type.value,
                              description=param.description or None,
                              example=param.example or None,
                              default=param.default or None)
            for param in endpoint.params
        ]

        parameter_fields += [
            openapi.Parameter(name=param.name,
                              required=True,
                              in_=openapi.IN_QUERY,
                              type=param.type.value,
                              description=param.description or None,
                              example=param.example or None,
                              default=param.default or None)
            for param in endpoint.sql_params
        ]

        if endpoint.pagination_enabled:
            parameter_fields += self.pagination_parameters

        parameter_fields += self.format_parameters
        parameter_fields += self.authentication_parameters
        return parameter_fields

    def endpoint_response(self, endpoint: Endpoint):
        schema = self.endpoint_schema(endpoint)
        if endpoint.pagination_enabled:
            schema = openapi.Schema(properties={
                'has_next':
                openapi.Schema(description='next page is exist',
                               type=openapi.TYPE_BOOLEAN),
                'has_prev':
                openapi.Schema(description='previous page is exist',
                               type=openapi.TYPE_BOOLEAN),
                'next':
                openapi.Schema(description='next page link',
                               type=openapi.TYPE_STRING),
                'prev':
                openapi.Schema(description='previous page link',
                               type=openapi.TYPE_STRING),
                'data':
                openapi.Schema(description='response data',
                               items=schema,
                               type=openapi.TYPE_ARRAY)
            },
                                    type=openapi.TYPE_OBJECT)
        return openapi.Response('Normal', schema=schema)

    def endpoint_schema_field(self, field: SchemaFieldType):
        if isinstance(field, Field):
            return openapi.Schema(type=field.type.value,
                                  description=field.description or None,
                                  example=field.example or None)
        elif isinstance(field, Select):
            nested_endpoint = EndpointStorage.endpoints[field.endpoint]
            nested_endpoint_scheme = self.endpoint_schema(nested_endpoint)
            return openapi.Schema(description=field.description,
                                  items=nested_endpoint_scheme,
                                  type=openapi.TYPE_ARRAY)
        elif isinstance(field, Object):
            nested_result = {
                key: self.endpoint_schema_field(value)
                for key, value in field.fields.items()
            }
            schema = openapi.Schema(type=openapi.TYPE_OBJECT,
                                    properties=nested_result,
                                    title=field.name or None,
                                    description=field.description or None)
            if field.many:
                return openapi.Schema(description='aggregated data',
                                      items=schema,
                                      type=openapi.TYPE_ARRAY)
            else:
                return schema
        else:
            raise ValueError(f"Unknown schema field type: {field}")

    def endpoint_schema(self, endpoint: Endpoint):
        result = self.endpoint_schema_field(endpoint.schema)
        result.title = endpoint.name
        return result
                'usd_amount': openapi.Schema(type=openapi.TYPE_NUMBER),
                'is_active': openapi.Schema(type=openapi.TYPE_BOOLEAN),
                'lock_days': openapi.Schema(type=openapi.TYPE_INTEGER),
            },
            required=['voucher_code', 'usd_amount']),
        responses={200: VoucherSerializer()},
    )
    def update(self, request: Request, *args, **kwargs):
        return super().update(request, *args, **kwargs)


@swagger_auto_schema(
    method='get',
    manual_parameters=[
        openapi.Parameter('voucher_id',
                          openapi.IN_QUERY,
                          type=openapi.TYPE_STRING)
    ],
    responses={200: FreezingVoucherSerializer()},
)
@api_view(http_method_names=['GET'])
def get_withdraw_info(request: Request):
    voucher_id = request.query_params.get('voucher_id')

    try:
        frozen_voucher = FreezingVoucher.objects.get(id=voucher_id)
    except FreezingVoucher.DoesNotExist:
        raise NotFound

    response_data = FreezingVoucherSerializer().to_representation(
        frozen_voucher)
Пример #15
0
from drf_yasg import openapi

# Manual Parameter
page_field = openapi.Parameter('page',
                               openapi.IN_QUERY,
                               description='this is a page number',
                               type=openapi.TYPE_STRING)
page_size_field = openapi.Parameter('page_size',
                                    openapi.IN_QUERY,
                                    description='this is a page size',
                                    type=openapi.TYPE_INTEGER)

member_list_parameter = [page_field, page_size_field]

# Schema
member_list_schema = {
            'name': openapi.Schema(type=openapi.TYPE_STRING, \
              description='회원 이름'),
            'nick_name': openapi.Schema(type=openapi.TYPE_STRING, \
              description='사용될 닉네임'),
            'user_id': openapi.Schema(type=openapi.TYPE_STRING, \
              description='회원 아이디 (유니크)'),
            'user_pw': openapi.Schema(type=openapi.TYPE_STRING, \
              description='회원 비밀번호'),
            'tel': openapi.Schema(type=openapi.TYPE_STRING, \
              description='전화번호 ex) 010-0000-0000'),
            'birth': openapi.Schema(type=openapi.FORMAT_DATE, \
              description='생년월일 ex) 2002-05-09'),
            'email': openapi.Schema(type=openapi.TYPE_STRING, \
              description='이메일 ex) [email protected]'),
            'gender': openapi.Schema(type=openapi.TYPE_STRING, \
Пример #16
0
class ListAppGatewayRuleView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="获取应用访问策略列表",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
        ],
        responses={200: GatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    def get(self, req, app_id, *args, **kwargs):
        query = req.GET.get("query", None)
        data = {}
        if query == "http":
            http_rules = domain_service.get_http_rules_by_app_id(app_id)
            data["http"] = http_rules
        elif query == "tcp":
            tcp_rules = domain_service.get_tcp_rules_by_app_id(app_id)
            data["tcp"] = tcp_rules
        else:
            http_rules = domain_service.get_http_rules_by_app_id(app_id)
            tcp_rules = domain_service.get_tcp_rules_by_app_id(app_id)
            data["http"] = http_rules
            data["tcp"] = tcp_rules

        re = GatewayRuleSerializer(data)
        return Response(re.data, status=status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="创建网关策略",
        request_body=PostGatewayRuleSerializer(),
        responses={200: GatewayRuleSerializer()},
        tags=['openapi-apps'],
    )
    def post(self, request, app_id, *args, **kwargs):
        ads = PostGatewayRuleSerializer(data=request.data)
        ads.is_valid(raise_exception=True)
        if ads.data.get("protocol") == "tcp":
            tcpdomain = ads.data.get("tcp")
            if not tcpdomain:
                raise ServiceHandleException(msg="Missing parameters: tcp", msg_show="缺少参数: tcp")

            container_port = tcpdomain.get("container_port", None)
            service_id = tcpdomain.get("service_id", None)
            end_point = tcpdomain.get("end_point", None)
            rule_extensions = tcpdomain.get("rule_extensions", None)
            default_port = tcpdomain.get("default_port", None)
            default_ip = tcpdomain.get("default_ip", None)
            service = service_repo.get_service_by_service_id(service_id)
            if not service:
                raise ServiceHandleException(msg="not service", msg_show="组件不存在")

            # Check if the given endpoint exists.
            service_tcpdomain = tcp_domain.get_tcpdomain_by_end_point(self.region.region_id, end_point)
            if service_tcpdomain:
                raise ServiceHandleException(msg="exist", msg_show="策略已存在")

            if service.service_source == "third_party":
                msg, msg_show, code = port_service.check_domain_thirdpart(self.team, service)
                if code != 200:
                    raise ServiceHandleException(msg=msg, msg_show=msg_show)
            try:
                tenant_service_port = port_service.get_service_port_by_port(service, container_port)
                # 仅打开对外端口
                code, msg, data = port_service.manage_port(self.team, service, service.service_region,
                                                           int(tenant_service_port.container_port), "only_open_outer",
                                                           tenant_service_port.protocol, tenant_service_port.port_alias)
                if code != 200:
                    raise ServiceHandleException(status_code=code, msg="change port fail", msg_show=msg)
            except Exception as e:
                logger.exception(e)
                raise ServiceHandleException(status_code=code, msg="change port fail", msg_show="open port failure")
            # 添加tcp策略
            domain_service.bind_tcpdomain(self.team, self.user, service, end_point, container_port, default_port,
                                          rule_extensions, default_ip)

        elif ads.data.get("protocol") == "http":
            httpdomain = ads.data.get("http")
            if not httpdomain:
                raise ServiceHandleException(msg="Missing parameters: tcp", msg_show="缺少参数: http")
            httpdomain["domain_heander"] = httpdomain.get("domain_header", None)
            httpdomain["domain_type"] = DomainType.WWW
            protocol = "http"
            if httpdomain.get("certificate_id", None):
                protocol = "https"
            httpdomain["protocol"] = protocol
            service = service_repo.get_service_by_tenant_and_id(self.team.tenant_id, httpdomain["service_id"])
            if not service:
                rst = {"msg": "组件不存在"}
                return Response(rst, status=status.HTTP_400_BAD_REQUEST)
            if domain_service.check_domain_exist(httpdomain["service_id"], httpdomain["container_port"],
                                                 httpdomain["domain_name"], protocol, httpdomain.get("domain_path"),
                                                 httpdomain.get("rule_extensions")):
                rst = {"msg": "策略已存在"}
                return Response(rst, status=status.HTTP_400_BAD_REQUEST)

            if service.service_source == "third_party":
                msg, msg_show, code = port_service.check_domain_thirdpart(self.team, service)
                if code != 200:
                    logger.exception(msg, msg_show)
                    return Response({"msg": msg, "msg_show": msg_show}, status=code)
            if httpdomain.get("whether_open", True):
                tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
                # 仅开启对外端口
                code, msg, data = port_service.manage_port(self.team, service, service.service_region,
                                                           int(tenant_service_port.container_port), "only_open_outer",
                                                           tenant_service_port.protocol, tenant_service_port.port_alias)
                if code != 200:
                    return Response({"msg": "change port fail"}, status=code)
            tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
            if not tenant_service_port:
                raise ServiceHandleException("port not found", "端口不存在", 404, 404)
            if not tenant_service_port.is_outer_service:
                return Response({"msg": "没有开启对外端口"}, status=status.HTTP_400_BAD_REQUEST)
            domain_service.bind_httpdomain(self.team, self.request.user, service, httpdomain, True)
        else:
            raise ServiceHandleException(msg="error parameters: protocol", msg_show="错误参数: protocol")
        data = {}
        http_rules = domain_service.get_http_rules_by_app_id(app_id)
        tcp_rules = domain_service.get_tcp_rules_by_app_id(app_id)
        data["http"] = http_rules
        data["tcp"] = tcp_rules
        re = GatewayRuleSerializer(data)
        return Response(re.data, status=status.HTTP_200_OK)
Пример #17
0
from rest_framework.response import Response

from api.permissions import CanCreateOrDestroyOrReadonly
from api.models.user import User
from api.serializers import UserSerializer


@method_decorator(
    name='list',
    decorator=swagger_auto_schema(
        operation_description=
        "Return list of users based on given query string",
        manual_parameters=[
            openapi.Parameter(
                name='id',
                in_=openapi.IN_QUERY,
                type=openapi.TYPE_INTEGER,
                description="Id of a user",
            ),
            openapi.Parameter(
                name='icon_hash',
                in_=openapi.IN_QUERY,
                type=openapi.TYPE_STRING,
                description="Icon hash of a user",
            ),
            openapi.Parameter(
                name='is_social',
                in_=openapi.IN_QUERY,
                type=openapi.TYPE_BOOLEAN,
                description=
                "Social status of a user. Whether a user was created via social media or identicon.",
            )
Пример #18
0
class ListAppGatewayHTTPRuleView(TeamAppAPIView):
    @swagger_auto_schema(
        operation_description="获取应用http访问策略列表",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
        ],
        responses={200: HTTPGatewayRuleSerializer(many=True)},
        tags=['openapi-gateway'],
    )
    def get(self, req, app_id, *args, **kwargs):
        app = group_service.get_app_by_id(self.team, self.region_name, app_id)
        if not app:
            raise ErrAppNotFound
        rules = domain_service.get_http_rules_by_app_id(app_id)
        re = HTTPGatewayRuleSerializer(rules, many=True)
        return Response(re.data, status=status.HTTP_200_OK)

    @swagger_auto_schema(
        operation_description="创建HTTP网关策略",
        manual_parameters=[
            openapi.Parameter("app_id", openapi.IN_PATH, description="应用ID", type=openapi.TYPE_INTEGER),
        ],
        request_body=PostHTTPGatewayRuleSerializer(),
        responses={200: HTTPGatewayRuleSerializer()},
        tags=['openapi-gateway'],
    )
    def post(self, request, app_id, *args, **kwargs):
        ads = PostHTTPGatewayRuleSerializer(data=request.data)
        ads.is_valid(raise_exception=True)
        app = group_service.get_app_by_id(self.team, self.region_name, app_id)
        if not app:
            raise ErrAppNotFound
        httpdomain = ads.data
        # Compatible history code
        httpdomain["domain_heander"] = httpdomain.get("domain_header", None)
        httpdomain["domain_type"] = DomainType.WWW
        protocol = "http"
        if httpdomain.get("certificate_id", None):
            protocol = "https"
        httpdomain["protocol"] = protocol
        service = service_repo.get_service_by_tenant_and_id(self.team.tenant_id, httpdomain["service_id"])
        if not service:
            rst = {"msg": "组件不存在"}
            return Response(rst, status=status.HTTP_400_BAD_REQUEST)
        if domain_service.check_domain_exist(httpdomain["service_id"], httpdomain["container_port"], httpdomain["domain_name"],
                                             protocol, httpdomain.get("domain_path"), httpdomain.get("rule_extensions")):
            rst = {"msg": "策略已存在"}
            return Response(rst, status=status.HTTP_400_BAD_REQUEST)

        if service.service_source == "third_party":
            msg, msg_show, code = port_service.check_domain_thirdpart(self.team, service)
            if code != 200:
                logger.exception(msg, msg_show)
                return Response({"msg": msg, "msg_show": msg_show}, status=code)
        if httpdomain.get("whether_open", True):
            tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
            # 仅开启对外端口
            code, msg, data = port_service.manage_port(self.team, service, service.service_region,
                                                       int(tenant_service_port.container_port), "only_open_outer",
                                                       tenant_service_port.protocol, tenant_service_port.port_alias)
            if code != 200:
                return Response({"msg": "change port fail"}, status=code)
        tenant_service_port = port_service.get_service_port_by_port(service, httpdomain["container_port"])
        if not tenant_service_port.is_outer_service:
            return Response({"msg": "没有开启对外端口"}, status=status.HTTP_400_BAD_REQUEST)
        data = domain_service.bind_httpdomain(self.team, self.request.user, service, httpdomain, True)
        configuration = httpdomain.get("configuration", None)
        if configuration:
            domain_service.update_http_rule_config(self.team, self.region_name, data.http_rule_id, configuration)
        serializer = HTTPGatewayRuleSerializer(data=data.to_dict())
        serializer.is_valid()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
Пример #19
0
        if getattr(self, 'swagger_fake_view', False):
            return None
        pass

    def get_serializer_context(self):
        return {'request': self.request, 'kwargs': self.kwargs}

    def patch(self, request, **kwargs):
        serializer = self.get_serializer()
        serializer.join_team()
        return Response("Successfully jonied team!", status=status.HTTP_200_OK)


query_param = openapi.Parameter(
    'query',
    openapi.IN_QUERY,
    description="Query parameter - Returns all hackthons if not specified.",
    type=openapi.TYPE_STRING,
    enum=['completed', 'upcoming', 'ongoing'])


@method_decorator(
    name="get", decorator=swagger_auto_schema(manual_parameters=[query_param]))
class HackathonListCreateView(generics.ListCreateAPIView):
    """
    get:
    Returns list of Hackathons according to query parameter.

    post:
    Creates a new hackathon. Only admin can create a hackathon
    """
    serializer_class = HackathonSerializer
class UserDetailsOtherView(ModelViewSet):
    queryset = UserDetails.objects.all()
    serializer_class = UserDetailsInfoSerializersUpdate
    parser_classes = [MultiPartParser]

    @swagger_auto_schema(
        operation_summary="根据token修改用户信息",
        required=[],
        manual_parameters=[
            openapi.Parameter('sex',
                              openapi.IN_FORM,
                              type=openapi.TYPE_INTEGER,
                              description='性别((-1, 女), (0, 保密), (1, 男))',
                              enum=[-1, 0, 1]),
            openapi.Parameter('birthday',
                              openapi.IN_FORM,
                              type=openapi.TYPE_INTEGER,
                              description=f'生日(当前时间戳是{int(time.time())})'),
            openapi.Parameter('TOKEN',
                              openapi.IN_HEADER,
                              type=openapi.TYPE_STRING,
                              description='TOKEN')
        ])
    def partial_update(self, request, *args, **kwargs):
        # 检测token
        check_token = pd_token(request)
        if check_token:
            return check_token

        # 检测qq和邮箱的合法性
        qq = request.data.get("qq")
        email = request.data.get("email")
        birthday = request.data.get("birthday")

        if qq and not pd_qq(qq):
            return response_success_200(code=STATUS_PARAMETER_ERROR,
                                        message="qq格式不正确")
        if email and not pd_email(email):
            return response_success_200(code=STATUS_PARAMETER_ERROR,
                                        message="email格式不正确")
        if birthday:
            check_time = check_time_stamp(int(birthday))
            if check_time:
                return response_success_200(code=STATUS_PARAMETER_ERROR,
                                            message=check_time)

        resp = super().partial_update(request, *args, **kwargs)
        return response_success_200(data=resp.data)

    def get_object(self):
        if self.action == "partial_update":
            print(self.request.user)
            # return self.queryset.get(user=user_id)
            return User.objects.get(id=self.request.user).user_details
            # return get_object_or_404(self.queryset, id=self.request.user)
        return super().get_object()

    @swagger_auto_schema(operation_summary="根据id删除用户详情",
                         required=[],
                         deprecated=True)
    def destroy(self, request, *args, **kwargs):
        return response_success_200(message="过时!!")
Пример #21
0
                                  lookup_expr="icontains")

    class Meta:
        model = models.Project
        fields = ("id", "name", "owner", "status", "assignee")


@method_decorator(
    name='list',
    decorator=swagger_auto_schema(
        operation_summary=
        'Returns a paginated list of projects according to query parameters (10 projects per page)',
        manual_parameters=[
            openapi.Parameter(
                'id',
                openapi.IN_QUERY,
                description="A unique number value identifying this project",
                type=openapi.TYPE_NUMBER),
            openapi.Parameter(
                'name',
                openapi.IN_QUERY,
                description=
                "Find all projects where name contains a parameter value",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
                'owner',
                openapi.IN_QUERY,
                description=
                "Find all project where owner name contains a parameter value",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
class AllTransactionsListView(ListAPIView):
    filter_backends = (django_filters.rest_framework.DjangoFilterBackend,
                       OrderingFilter)
    pagination_class = SmallPagination
    serializer_class = _AllTransactionsSchemaSerializer  # Just for docs, not used

    _schema_queued_param = openapi.Parameter(
        'queued',
        openapi.IN_QUERY,
        type=openapi.TYPE_BOOLEAN,
        default=False,
        description=
        'If `True` transactions with `nonce >= Safe current nonce` are '
        'also shown')
    _schema_trusted_param = openapi.Parameter(
        'trusted',
        openapi.IN_QUERY,
        type=openapi.TYPE_BOOLEAN,
        default=True,
        description=
        'If `True` just trusted transactions are shown (indexed, added by a '
        'delegate or with at least one confirmation)')
    _schema_200_response = openapi.Response(
        'A list with every element with the structure of one of these transaction'
        'types', _AllTransactionsSchemaSerializer)

    def _parse_boolean(self, value: Union[bool, str]) -> bool:
        if value in (True, 'True', 'true', '1'):
            return True
        else:
            return False

    def get_parameters(self) -> Tuple[bool, bool]:
        """
        Parse query parameters:
        - queued: Default, True. If `queued=True` transactions with `nonce >= Safe current nonce` are also shown
        - trusted: Default, True. If `trusted=True` just trusted transactions are shown (indexed, added by a delegate
        or with at least one confirmation)
        :return: Tuple with queued, trusted
        """
        queued = self._parse_boolean(
            self.request.query_params.get('queued', True))
        trusted = self._parse_boolean(
            self.request.query_params.get('trusted', True))
        return queued, trusted

    def list(self, request, *args, **kwargs):
        transaction_service = TransactionServiceProvider()
        safe = self.kwargs['address']
        queued, trusted = self.get_parameters()
        queryset = self.filter_queryset(
            transaction_service.get_all_tx_hashes(safe,
                                                  queued=queued,
                                                  trusted=trusted))
        page = self.paginate_queryset(queryset)

        if not page:
            return self.get_paginated_response([])

        all_tx_hashes = [element['safe_tx_hash'] for element in page]
        all_txs = transaction_service.get_all_txs_from_hashes(
            safe, all_tx_hashes)
        all_txs_serialized = transaction_service.serialize_all_txs(all_txs)
        return self.get_paginated_response(all_txs_serialized)

    @swagger_auto_schema(
        responses={
            200: _schema_200_response,
            400: 'Invalid data',
            404: 'Not found',
            422: 'Invalid ethereum address'
        },
        manual_parameters=[_schema_queued_param, _schema_trusted_param])
    def get(self, request, *args, **kwargs):
        """
        Returns a paginated list of transactions for a Safe. The list has different structures depending on the
        transaction type:
        - Multisig Transactions for a Safe. `tx_type=MULTISIG_TRANSACTION`. If the query parameter `queued=False` is
        set only the transactions with `safe nonce < current Safe nonce` will be displayed. By default, only the
        `trusted` transactions will be displayed (transactions indexed, with at least one confirmation or proposed
        by a delegate). If you need that behaviour to be disabled set the query parameter `trusted=False`
        - Module Transactions for a Safe. `tx_type=MODULE_TRANSACTION`
        - Incoming Transfers of Ether/ERC20 Tokens/ERC721 Tokens. `tx_type=ETHEREUM_TRANSACTION`
        """
        address = kwargs['address']
        if not Web3.isChecksumAddress(address):
            return Response(status=status.HTTP_422_UNPROCESSABLE_ENTITY,
                            data='Invalid ethereum address')

        response = super().get(request, *args, **kwargs)
        response.setdefault(
            'ETag', 'W/' +
            hashlib.md5(str(response.data['results']).encode()).hexdigest())
        return response
Пример #23
0
class ServerViewSet(viewsets.ViewSet):
    serializer_class = None

    # To get nice documentation about ServerViewSet actions it is necessary
    # to implement the method. By default, ViewSet doesn't provide it.
    def get_serializer(self, *args, **kwargs):
        pass

    @staticmethod
    @swagger_auto_schema(
        method='get',
        operation_summary='Method provides basic CVAT information',
        responses={'200': AboutSerializer})
    @action(detail=False, methods=['GET'], serializer_class=AboutSerializer)
    def about(request):
        from cvat import __version__ as cvat_version
        about = {
            "name":
            "Computer Vision Annotation Tool",
            "version":
            cvat_version,
            "description":
            "CVAT is completely re-designed and re-implemented " +
            "version of Video Annotation Tool from Irvine, California " +
            "tool. It is free, online, interactive video and image annotation "
            + "tool for computer vision. It is being used by our team to " +
            "annotate million of objects with different properties. Many UI " +
            "and UX decisions are based on feedbacks from professional data " +
            "annotation team."
        }
        serializer = AboutSerializer(data=about)
        if serializer.is_valid(raise_exception=True):
            return Response(data=serializer.data)

    @staticmethod
    @swagger_auto_schema(method='post', request_body=ExceptionSerializer)
    @action(detail=False,
            methods=['POST'],
            serializer_class=ExceptionSerializer)
    def exception(request):
        """
        Saves an exception from a client on the server

        Sends logs to the ELK if it is connected
        """
        serializer = ExceptionSerializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            additional_info = {
                "username": request.user.username,
                "name": "Send exception",
            }
            message = JSONRenderer().render({
                **serializer.data,
                **additional_info
            }).decode('UTF-8')
            jid = serializer.data.get("job_id")
            tid = serializer.data.get("task_id")
            if jid:
                clogger.job[jid].error(message)
            elif tid:
                clogger.task[tid].error(message)
            else:
                clogger.glob.error(message)

            return Response(serializer.data, status=status.HTTP_201_CREATED)

    @staticmethod
    @swagger_auto_schema(method='post',
                         request_body=LogEventSerializer(many=True))
    @action(detail=False,
            methods=['POST'],
            serializer_class=LogEventSerializer)
    def logs(request):
        """
        Saves logs from a client on the server

        Sends logs to the ELK if it is connected
        """
        serializer = LogEventSerializer(many=True, data=request.data)
        if serializer.is_valid(raise_exception=True):
            user = {"username": request.user.username}
            for event in serializer.data:
                message = JSONRenderer().render({
                    **event,
                    **user
                }).decode('UTF-8')
                jid = event.get("job_id")
                tid = event.get("task_id")
                if jid:
                    clogger.job[jid].info(message)
                elif tid:
                    clogger.task[tid].info(message)
                else:
                    clogger.glob.info(message)
            return Response(serializer.data, status=status.HTTP_201_CREATED)

    @staticmethod
    @swagger_auto_schema(
        method='get',
        operation_summary=
        'Returns all files and folders that are on the server along specified path',
        manual_parameters=[
            openapi.Parameter('directory',
                              openapi.IN_QUERY,
                              type=openapi.TYPE_STRING,
                              description='Directory to browse')
        ],
        responses={'200': FileInfoSerializer(many=True)})
    @action(detail=False, methods=['GET'], serializer_class=FileInfoSerializer)
    def share(request):
        param = request.query_params.get('directory', '/')
        if param.startswith("/"):
            param = param[1:]
        directory = os.path.abspath(os.path.join(settings.SHARE_ROOT, param))

        if directory.startswith(
                settings.SHARE_ROOT) and os.path.isdir(directory):
            data = []
            content = os.scandir(directory)
            for entry in content:
                entry_type = None
                if entry.is_file():
                    entry_type = "REG"
                elif entry.is_dir():
                    entry_type = "DIR"

                if entry_type:
                    data.append({"name": entry.name, "type": entry_type})

            serializer = FileInfoSerializer(many=True, data=data)
            if serializer.is_valid(raise_exception=True):
                return Response(serializer.data)
        else:
            return Response("{} is an invalid directory".format(param),
                            status=status.HTTP_400_BAD_REQUEST)

    @staticmethod
    @swagger_auto_schema(
        method='get',
        operation_summary=
        'Method provides the list of supported annotations formats',
        responses={'200': DatasetFormatsSerializer()})
    @action(detail=False, methods=['GET'], url_path='annotation/formats')
    def annotation_formats(request):
        data = dm.views.get_all_formats()
        return Response(DatasetFormatsSerializer(data).data)
Пример #24
0
from notifications.signals import notify
from api.models.project import Project
from api.views.feed import add_activity_to_feed
from rest_framework.decorators import action
from api.models.profile import Profile
from api.models.following import Following
from api.models.collaboration_request import CollaborationRequest
import math
import re
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from django.db.models import Case, When
from api.utils import get_user_rating

user_param = openapi.Parameter('user_id',
                               openapi.IN_QUERY,
                               description="User id to get recommendations",
                               type=openapi.TYPE_INTEGER)
count_param = openapi.Parameter('user_count',
                                openapi.IN_QUERY,
                                description="Number of projects requested",
                                type=openapi.TYPE_INTEGER)


class ProjectViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.
    """
    queryset = Project.objects.all()
    serializer_class = ProjectPublicSerializer
    permission_classes = [
Пример #25
0
class SignUp(APIView):
    # parser_classes = (MultiPartParser,)
    serializer_class = CustomUserSerializer
    permission_classes = (permissions.AllowAny, )

    @swagger_auto_schema(method='post',
                         manual_parameters=[
                             openapi.Parameter(name='first_name',
                                               in_=openapi.IN_FORM,
                                               type=openapi.TYPE_STRING,
                                               description="First Name"),
                             openapi.Parameter(name='last_name',
                                               in_=openapi.IN_FORM,
                                               type=openapi.TYPE_STRING,
                                               description="Last Name"),
                             openapi.Parameter(name='username',
                                               in_=openapi.IN_FORM,
                                               type=openapi.TYPE_STRING,
                                               description="Username"),
                             openapi.Parameter(name='email',
                                               in_=openapi.IN_FORM,
                                               type=openapi.TYPE_STRING,
                                               description="Email"),
                             openapi.Parameter(name='password',
                                               in_=openapi.IN_FORM,
                                               type=openapi.TYPE_STRING,
                                               description="Password")
                         ],
                         tags=['auth'],
                         responses={
                             status.HTTP_200_OK:
                             openapi.Response(description="Sucess"),
                             status.HTTP_401_UNAUTHORIZED:
                             openapi.Response(description="Unauthorized"),
                         })
    @action(methods=['post'],
            detail=False,
            permission_classes=[],
            url_path='',
            url_name='')
    def post(self, request, format='json'):
        try:
            with transaction.atomic():
                validator = SignUpValidator(data=request.data)
                if validator.is_valid():
                    validated_data = validator.validated_data
                    if validated_data['should_verify']:
                        email = request.data['email']
                        user = User.objects.get(email=email)
                        return Response(
                            {
                                'should_verify': True,
                                'user_id': user.id
                            },
                            status=status.HTTP_200_OK)
                    user = User(
                        first_name=validated_data['first_name'],
                        last_name=validated_data['last_name'],
                        email=validated_data['email'],
                        username=validated_data['username'],
                    )
                    user.set_password(validated_data['password'])
                    user.save()

                    user = registered_user(data=validated_data)
                    send_signup_verificaion_mail(user=user, request=request)
                    return Response(
                        {
                            "message":
                            "Please confirm your email address to complete the registration",
                            "user_id": user.id
                        },
                        status=status.HTTP_201_CREATED)
                else:
                    return Response(validator.errors,
                                    status=status.HTTP_400_BAD_REQUEST)
        except Exception as error:
            return Response('Exception: {}'.format(error),
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #26
0
    serializer_class = NotificationMessageSerializer
    lookup_field = "id"
    permission_classes = (permissions.DjangoModelPermissions, )


@method_decorator(
    name="list",
    decorator=swagger_auto_schema(
        tags=["Base Data"],
        operation_id="v1_vendor_list",
        operation_description=
        "list all vendor entries that are stored in the database",
        manual_parameters=[
            openapi.Parameter(
                "name",
                openapi.IN_QUERY,
                description="filter by Vendor name (exact match)",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
                "search",
                openapi.IN_QUERY,
                description="search Vendor Names using a regex string",
                type=openapi.TYPE_STRING),
        ]))
@method_decorator(name="retrieve",
                  decorator=swagger_auto_schema(
                      tags=["Base Data"],
                      operation_id="v1_vendor_read",
                      operation_description="get a vendor entry by `id`",
                  ))
class VendorViewSet(viewsets.ReadOnlyModelViewSet):
Пример #27
0
from drf_yasg import openapi

company_params_in_header = openapi.Parameter('company',
                                             openapi.IN_HEADER,
                                             required=True,
                                             type=openapi.TYPE_STRING)

login_page_params = [
    company_params_in_header,
    openapi.Parameter('email',
                      openapi.IN_QUERY,
                      required=True,
                      type=openapi.TYPE_STRING),
    openapi.Parameter('password',
                      openapi.IN_QUERY,
                      format='password',
                      required=True,
                      type=openapi.TYPE_STRING)
]

change_password_params = [
    company_params_in_header,
    openapi.Parameter('old_password',
                      openapi.IN_QUERY,
                      format='password',
                      required=True,
                      type=openapi.TYPE_STRING),
    openapi.Parameter('new_password',
                      openapi.IN_QUERY,
                      format='password',
                      required=True,
Пример #28
0
class ProductIdNormalizationRuleViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint to match strings against a given input to map/normalize Product IDs
    """
    queryset = ProductIdNormalizationRule.objects.all()
    serializer_class = ProductIdNormalizationRuleSerializer
    filter_backends = (DjangoFilterBackend, )
    filter_class = ProductIdNormalizationRuleFilter
    permission_classes = (permissions.DjangoModelPermissions, )

    @swagger_auto_schema(
        tags=["Product ID Normalization Rules"],
        operation_id="v1_productidnormalizationrule_apply",
        manual_parameters=[
            openapi.Parameter(
                "input_string",
                openapi.IN_QUERY,
                description="String that should be converted to a Product ID",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
                "vendor_name",
                openapi.IN_QUERY,
                description=
                "Vendor Name (case-sensitive starts-with match) to use for the rule lookup (alternative to `vendor`)",
                type=openapi.TYPE_NUMBER),
            openapi.Parameter(
                "vendor",
                openapi.IN_QUERY,
                description="Vendor ID to use for the rule lookup",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
                "id",
                openapi.IN_QUERY,
                description="(not implemented for this endpoint)",
                type=openapi.TYPE_NUMBER),
            openapi.Parameter(
                "page",
                openapi.IN_QUERY,
                description="(not implemented for this endpoint)",
                type=openapi.TYPE_STRING),
            openapi.Parameter(
                "page_size",
                openapi.IN_QUERY,
                description="(not implemented for this endpoint)",
                type=openapi.TYPE_STRING),
        ],
        responses={
            status.HTTP_200_OK:
            openapi.Response(
                "lookup result",
                schema=openapi.Schema(
                    type=openapi.TYPE_OBJECT,
                    properties={
                        "vendor_id":
                        openapi.Schema(
                            type=openapi.TYPE_INTEGER,
                            description=
                            "ID of the vendor object that was used for the lookup"
                        ),
                        "product_id":
                        openapi.Schema(
                            type=openapi.TYPE_STRING,
                            description=
                            "either the converted string (if rules are applied) or the unmodified input string"
                        ),
                        "product_in_database":
                        openapi.Schema(
                            type=openapi.TYPE_STRING,
                            description=
                            "Database ID for the product if the resulting string was found in the database, otherwise None"
                        ),
                        "matched_rule_id":
                        openapi.Schema(
                            type=openapi.TYPE_STRING,
                            description=
                            "Database ID of the rule that matched the request")
                    })),
            status.HTTP_400_BAD_REQUEST:
            openapi.Response("invalid response (e.g. parameters missing)",
                             schema=openapi.Schema(
                                 type=openapi.TYPE_OBJECT,
                                 properties={
                                     "error":
                                     openapi.Schema(
                                         type=openapi.TYPE_STRING,
                                         description="error message")
                                 }))
        })
    @action(detail=False, methods=["get"])
    def apply(self, request):
        """
        This endpoint provides a mechanism to convert well known string (e.g. from SNMP data) to a valid SKU based on configured rules per vendor. If nothing is matched, the input_string is returned unmodified.

        Try to normalize the input_string based on the configured rules for the given vendor_name, requires an `input_string` and a `vendor_name` or `vendor` id as `GET` parameter.
        """
        input_string = request.GET.get("input_string", None)
        vendor_name = request.GET.get("vendor_name", None)

        if not input_string or not vendor_name:
            return Response(
                {"error": "input_string and vendor_name parameter required"},
                status=status.HTTP_400_BAD_REQUEST)

        # lookup vendor
        vendor_qs = Vendor.objects.filter(name__startswith=vendor_name)
        vendor_count = vendor_qs.count()
        if vendor_count == 0:
            return Response({"error": "vendor_name returns no result"},
                            status=status.HTTP_400_BAD_REQUEST)

        elif vendor_count > 1:
            return Response(
                {"error": "vendor_name not unique, multiple entries found"},
                status=status.HTTP_400_BAD_REQUEST)

        vendor = vendor_qs.first()
        product_id = input_string
        product_in_database = None
        matched_rule = None

        # apply rules on input string
        rules = ProductIdNormalizationRule.objects.filter(
            vendor=vendor).order_by("priority").order_by("product_id")
        for rule in rules:
            if rule.matches(input_string):
                product_id = rule.get_normalized_product_id(product_id)

                # lookup in local database
                pqs = Product.objects.filter(product_id=product_id,
                                             vendor=vendor)
                if pqs.count() != 0:
                    product_in_database = pqs.first().id

                matched_rule = rule.id
                break

        return Response(
            {
                "vendor_id": vendor.id,
                "product_id": product_id,
                "product_in_database": product_in_database,
                "matched_rule_id": matched_rule
            },
            status=status.HTTP_200_OK)
Пример #29
0
class PaymentsURL(APIView):
    events_repository = EventsRepository()
    users_service = UsersService()
    payments_repository = PaymentsRepository()

    payment_url_response = openapi.Response('response description',
                                            PaymentURLSerializer)

    event_id = openapi.Parameter('event_id',
                                 openapi.IN_QUERY,
                                 description="Event id",
                                 type=openapi.TYPE_INTEGER)

    authorization_token = openapi.Parameter(
        'Authorization',
        openapi.IN_HEADER,
        description="Authorization token which starts with Bearer",
        type=openapi.TYPE_STRING)

    @swagger_auto_schema(
        operation_description='Endpoint for acquiring urls for payments.',
        manual_parameters=[authorization_token, event_id],
        responses={
            404: None,
            200: payment_url_response,
            409: 'Payment already made',
            400: 'Event does not require payment',
            403: 'User is not a participant'
        },
    )
    def get(self, request):
        jwt = request.headers['Authorization']
        user = self.users_service.fetch_by_jwt(jwt)
        request_data = dict(request.GET)
        event_id = request_data.get('event_id', None)

        if event_id is None:
            return JsonResponse(data=None,
                                status=status.HTTP_404_NOT_FOUND,
                                safe=False)

        event_id = int(event_id[0])

        event = self.events_repository.get_event_by_id(event_id)

        payment = self.payments_repository.get_payment_for_user_and_event(
            user, event)

        if not event:
            return JsonResponse(data=None,
                                status=status.HTTP_404_NOT_FOUND,
                                safe=False)
        if event.price is None:
            return JsonResponse(
                data={'error': 'Event does not require payment'},
                status=status.HTTP_400_BAD_REQUEST,
                safe=False)
        if payment:
            return JsonResponse(data={'error': 'User already payed'},
                                status=status.HTTP_409_CONFLICT,
                                safe=False)
        if not event.participants.filter(id=user.id).exists():
            return JsonResponse(data={'error': 'User is not a participant'},
                                status=status.HTTP_403_FORBIDDEN,
                                safe=False)

        serializer = PaymentURLSerializer({
            'event_id': event_id,
            'price': event.price
        })
        response = serializer.data
        return JsonResponse(response, safe=False)
Пример #30
0
Schema_email = {'email': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_EMAIL, description='邮箱')}
Schema_password = {'password': openapi.Schema(type=openapi.TYPE_STRING, description='密码')}
Schema_token = {'token': openapi.Schema(type=openapi.TYPE_STRING, description='由邮件提供')}
Schema_old_password = {'old_password': openapi.Schema(type=openapi.TYPE_STRING, description='旧密码')}
Schema_new_password = {'new_password': openapi.Schema(type=openapi.TYPE_STRING, description='新密码')}
Schema_new_email = {'new_email': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_EMAIL, description='新邮箱')}

# 沙龙
Schema_activity_id = {'activity_id': openapi.Schema(type=openapi.TYPE_NUMBER, description='沙龙 id')}
Schema_title = {'title': openapi.Schema(type=openapi.TYPE_STRING, description='沙龙标题')}
Schema_datetime = {'datetime': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_DATETIME, description='日期时间')}
Schema_location = {'location': openapi.Schema(type=openapi.TYPE_STRING, description='地点')}
Schema_presenter_ids = {'presenter': Schema_array(Schema_object(Schema_id))}  # POST 只需要提交 id
Schema_check_in_code = {'check_in_code': openapi.Schema(type=openapi.TYPE_STRING, description='签到码')}
Schema_check_in_open = {'check_in_open': openapi.Schema(type=openapi.TYPE_BOOLEAN, description='管理员开放签到')}
Schema_add = {'add': Schema_array(openapi.Schema(type=openapi.TYPE_NUMBER, description='用户 id'))}
Schema_remove = {'remove': Schema_array(openapi.Schema(type=openapi.TYPE_NUMBER, description='用户 id'))}
Schema_url = {'url': openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_URI, description='链接')}

# 云盘
Schema_status = {'status': openapi.Schema(type=openapi.TYPE_STRING, description='状态')}
Schema_file_id = {'file_id': openapi.Schema(type=openapi.TYPE_STRING, description='文件 id')}
Schema_filename = {'filename': openapi.Schema(type=openapi.TYPE_STRING, description='文件名')}
# Schema_filetype = {'filetype': openapi.Schema(type=openapi.TYPE_STRING,
#                                                  description='文件类型,可选 `activity_file` `photo`')}

Param_search = openapi.Parameter('search', openapi.IN_QUERY, type=openapi.TYPE_STRING, description='搜索关键字(为空时表示不搜索)')
Param_page = openapi.Parameter('page', openapi.IN_QUERY, type=openapi.TYPE_NUMBER, description='页数(不正确时返回 404)')
Param_page_size = openapi.Parameter('page_size', openapi.IN_QUERY, type=openapi.TYPE_NUMBER,
                                    description='页大小(不为正数时表示不分页)')