コード例 #1
0
class ParticularReportView(APIView):
    http_method_names = [
        'get',
        'put',
        'delete',
    ]
    err_res = ErrorResponses()
    report_dal = ReportDal()

    def get(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        report_id = int(kwargs.get('report_id'))
        report = self.report_dal.get_by_id(report_id)
        if report is not None and report.submitter != user:
            return self.err_res.FORBIDDEN
        if report is None:
            return self.err_res.REPORT_NOT_FOUND
        res_data = ReportDeepSerializer(report).data
        return Response(status=status.HTTP_200_OK, data=res_data)

    def delete(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        report_id = int(kwargs.get('report_id'))
        report = self.report_dal.get_by_id(report_id)
        if report is not None and report.submitter != user:
            return self.err_res.FORBIDDEN
        if report is None:
            return self.err_res.REPORT_NOT_FOUND

        report.delete()
        res = {'message': "report deleted successfully"}
        return Response(status=status.HTTP_200_OK, data=res)

    def put(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED

        report_id = int(kwargs.get('report_id'))
        report = self.report_dal.get_by_id(report_id)
        if report is not None and report.submitter != user:
            return self.err_res.FORBIDDEN
        if report is None:
            return self.err_res.REPORT_NOT_FOUND

        given_data = request.data
        serializer = ReportSerializer(report, data=given_data)
        valid = serializer.is_valid()
        if not valid:
            raise ParseError(serializer.errors)
        instance = serializer.save()
        res_data = ReportDeepSerializer(instance=instance).data
        return Response(status=status.HTTP_200_OK, data=res_data)
コード例 #2
0
class ReportView(APIView):
    http_method_names = [ 'get', 'post', ]
    err_res = ErrorResponses()
    report_dal = ReportDal()

    def get(self, request, *args, **kwargs):
        parameters = request.GET
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED

        limit = parameters.get('limit')
        if limit is not None:
            if not 10 <= int(limit) <= 100:
                return self.err_res.BAD_PAGINATION

        type_id = parameters.get('report_type_id')

        paginator = LimitOffsetPagination()

        report_dal = ReportDal()
        if type_id is None:
            query_data = paginator.paginate_queryset(
                self.report_dal.get_all(sorted_by='report_time'),
                request
            )
        else:
            query_data = paginator.paginate_queryset(
                report_dal.get_by_type(type_id, sorted_by='report_time'),
                request
            )
        
        reports_data = ReportDeepSerializer(query_data, many=True).data
        if not reports_data:
            return self.err_res.REPORT_NOT_FOUND

        return paginator.get_paginated_response(reports_data)

    def post(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        
        given_data = request.data
        serializer = ReportSerializer(data=given_data)
        valid = serializer.is_valid()
        if not valid:
            raise ParseError(serializer.errors)
        instance = serializer.save(submitter=user)
        res_data = ReportDeepSerializer(instance=instance).data
        return Response(
            status=status.HTTP_200_OK,
            data=res_data    
        )
コード例 #3
0
class ReportLocationView(APIView):
    http_method_names = [
        'get',
    ]
    report_location_dal = ReportLocationDal()
    err_res = ErrorResponses()

    def get(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        query_set = self.report_location_dal.get_all()
        paginator = LimitOffsetPagination()
        paginated_data = paginator.paginate_queryset(query_set, request)
        res_data = ReportLocationsSerializer(paginated_data, many=True).data

        return paginator.get_paginated_response(res_data)
コード例 #4
0
class LogoutView(APIView):
    http_method_names = [
        'post',
    ]
    err_res = ErrorResponses()

    def post(self, request, *args, **kwargs):
        refresh_token = request.data['refresh']

        try:
            token = RefreshToken(refresh_token)
        except TokenError:
            return self.err_res.UNAUTHORIZED

        token.blacklist()

        return Response(status=status.HTTP_200_OK)
コード例 #5
0
class ReportAddImageView(APIView):
    LIMIT_FILE_SIZE = 20 * 10 ** 6 # 20 MB
    http_method_names = ['post',]
    err_res = ErrorResponses()
    report_dal = ReportDal()
    report_image_dal = ReportImageDal()

    def post(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED

        report_id = kwargs.get('report_id')

        report = self.report_dal.get_by_id(report_id)
        if not report:
            return self.err_res.REPORT_NOT_FOUND

        if report.submitter != user:
            return self.err_res.FORBIDDEN

        given_data = request.data
        file = given_data.get('image')
        if not file:
            return self.err_res.BAD_REQUEST
        
        if file.size > self.LIMIT_FILE_SIZE:
            return self.err_res.NOT_ACCEPTABLE

        print(given_data)
        serializer = AddReportImageSerializer(data=given_data)
        valid = serializer.is_valid()
        if not valid:
            raise NotAcceptable(serializer.errors)

        serializer.save(report=report)
        report = self.report_dal.get_by_id(report_id)
        res_data = ReportDeepSerializer(report).data

        return Response(
            status=status.HTTP_200_OK,
            data=res_data
        )
コード例 #6
0
class ReportTypeView(APIView):
    http_method_names = ['get',]
    report_type_dal = ReportTypeDal()
    err_res = ErrorResponses()

    def get(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        parameters = request.GET
        limit = parameters.get('limit')
        if limit is not None:
            if not 10 <= int(limit) <= 100:
                return self.err_res.BAD_PAGINATION
        query_set = self.report_type_dal.get_all()
        paginator = LimitOffsetPagination()
        paginated_data = paginator.paginate_queryset(query_set, request)
        print(paginated_data)
        res_data = ReportTypeSerializer(paginated_data, many=True).data
        return paginator.get_paginated_response(res_data)
コード例 #7
0
class ReportDeleteImageView(APIView):
    http_method_names = ['delete',]
    err_res = ErrorResponses()
    report_dal = ReportDal()
    report_image_dal = ReportImageDal()

    def delete(self, request, *args, **kwargs):
        user = request.user
        if not user.is_authenticated:
            return self.err_res.UNAUTHORIZED
        
        report_id = kwargs.get('report_id')
        image_id = kwargs.get('image_id')
        image = self.report_image_dal.get_by_id(image_id)
        if not image:
            return self.err_res.NOT_FOUND
        if image.report.id != report_id:
            return self.err_res.NOT_ACCEPTABLE
        if image.report.submitter != user:
            return self.err_res.FORBIDDEN
        
        if os.path.isfile(image.file.path):
            os.remove(image.file.path)

        image.delete()
        res = {
            'message': "Image deleted successfully"
        }

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