Beispiel #1
0
def target_list(request):
    """List all targets or add a target."""
    if request.method == 'GET':
        targets = get_all_targets(request.user)
        serializer = TargetSerializer(targets, many=True)
        request.accepted_media_type = 'application/json; indent=4'
        return JSONResponse(serializer.data)

    if request.method == 'POST':
        # check user write permissions
        if not permission_to_write(request.user, request.data['target_type']):
            return JSONResponse(
                {'target_type': ['Insufficiant permissions.']},
                status=status.HTTP_403_FORBIDDEN)
        # serialize data
        serializer = TargetSerializer(
            data=request.data, context={'request': request})
        if serializer.is_valid():
            try:
                save_target(serializer)
            except ValidationError as err:
                return JSONResponse(err, status=status.HTTP_400_BAD_REQUEST)
            return JSONResponse(serializer.data,
                                status=status.HTTP_201_CREATED)
        return JSONResponse(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Beispiel #2
0
 def put(self, request):
     uid = request.data['id']
     count = Target.objects.filter(target=request.data['target'], center_id=request.data['center_id']).exclude(id=uid).count()
     if count > 0 :
         response = {
             'code': 0,
             'message': 'Target 已存在',
         }
         return Response(response)
     else:
         try:
             target = Target.objects.get(id=uid)
         except:
             response = {
                 'code': 0,
                 'message': 'id不存在',
             }
             return Response(response)
         else:
             serializer = TargetSerializer(data=request.data, instance=target)
             if serializer.is_valid():
                 serializer.save()
                 response = {
                     'code': 1,
                     'data': serializer.data,
                 }
                 return Response(response)
             else:
                 return Response(serializer.errors)
Beispiel #3
0
    def post(self, request):
        print(request.data)

        serializer = TargetSerializer(data=request.data)
        if serializer.is_valid():
            target = serializer.save()
            response = {
                'code': 1,
                'data': serializer.data,
            }
            return Response(response)
        else:
            return Response(serializer.errors)
Beispiel #4
0
def get_target(type):
    target = []
    queryset = Target.objects.filter(type=type, status=1).distinct()
    serializer = TargetSerializer(queryset, many=True)
    for row in serializer.data:
            target.append(row['target'].strip(' '))
    return list(set(target))
Beispiel #5
0
 def get(self, request):
     target = Target.objects.all()
     serializer = TargetSerializer(target, many=True)
     response = {
         'code': 1,
         'data': serializer.data,
     }
     return Response(response)
Beispiel #6
0
 def post(self, request):
     serializer = TargetSerializer(data=request.data)
     if serializer.is_valid():
         if not Target.objects.filter(target=request.data['target'], center_id=request.data['center_id']):
             target = serializer.save()
             response = {
                 'code': 1,
                 'data': serializer.data,
             }
             return Response(response)
         else:
             response = {
                 'code': 1,
                 'data': ['目标已存在']
             }
             return Response(response)
     else:
         return Response(serializer.errors)
Beispiel #7
0
def target_list_bytype(request, target_type):
    """List all targets by type."""
    if request.method == 'GET':
        # check user read permissions
        if not permission_to_read(request.user, target_type):
            return JSONResponse(
                {'target_type': ['Insufficiant permissions.']},
                status=status.HTTP_403_FORBIDDEN)
        targets = Target.objects.filter(target_type=target_type)
        serializer = TargetSerializer(targets, many=True)
        return JSONResponse(serializer.data)
Beispiel #8
0
 def put(self, request):
     uid = request.data['id']
     try:
         target = Target.objects.get(id=uid)
     except:
         response = {
             'code': 0,
             'data': [],
         }
         return Response(response)
     else:
         serializer = TargetSerializer(data=request.data, instance=target)
         if serializer.is_valid():
             serializer.save()
             response = {
                 'code': 1,
                 'data': serializer.data,
             }
             return Response(response)
         else:
             return Response(serializer.errors)
Beispiel #9
0
 def post(self, request):
     uid = request.data['id']
     target = Target.objects.get(id=uid)
     serializer = TargetSerializer(target)
     if serializer:
         response = {
             'code': 1,
             'data': serializer.data,
         }
         return Response(response)
     else:
         return Response(serializer.errors)
Beispiel #10
0
 def put(self, request):
     page = request.data['page']
     size = request.data['size']
     # count = request.data['count']
     target = Target.objects.all()[(page-1)*size:page*size]
     count = Target.objects.count()
     serializer = TargetSerializer(target, many=True)
     response = {
         'code': 1,
         'data': serializer.data,
         'count': count
     }
     return Response(response)
Beispiel #11
0
    def post(self, request):
        if 'id' in request.data.keys():
            id = request.data['id']
            target = Target.objects.get(id=id)
            serializer = TargetSerializer(target)
        elif 'center_id' in request.data.keys():
            center_id = request.data['center_id']
            target = Target.objects.filter(center_id=center_id)
            serializer = TargetSerializer(target, many=True)
        else:
            response = {
                'code': 0,
                'data': ['id or center_id is required'],
            }
            return Response(response)

        if serializer:
            response = {
                'code': 1,
                'data': serializer.data,
            }
            return Response(response)
        else:
            return Response(serializer.errors)
Beispiel #12
0
def target_detail(request, target_id):
    """Retrieve a target."""
    try:
        target = Target.objects.get(id=target_id)
    except Target.DoesNotExist:
        return JSONResponse(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        # check user read permissions
        if not permission_to_read(request.user, target.target_type):
            return JSONResponse(status=status.HTTP_404_NOT_FOUND)
        serializer = TargetSerializer(target)
        return JSONResponse(serializer.data)

    if request.method == 'DELETE':
        # check user write permissions
        if not permission_to_write(request.user, target.target_type):
            return JSONResponse(status=status.HTTP_404_NOT_FOUND)
        try:
            delete_target(request.user, target)
        except ValidationError as err:
            return JSONResponse(
                err, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        return JSONResponse({}, status=status.HTTP_204_NO_CONTENT)
 def get(self, request):
     resp_list = map(lambda x: { "target" : x}, get_push_targets())
     serializer = TargetSerializer(resp_list)
     return Response(serializer.source, status=status.HTTP_200_OK)