Ejemplo n.º 1
0
def create_location(request):
    serializer = LocationSerializer(data=request.data["data"])
    if serializer.is_valid():
        serializer.save(user=request.user)
        return Response(JSONResponse().addData("Location",
                                               serializer.data).addData(
                                                   "status",
                                                   "Location created!").send(),
                        status=status.HTTP_201_CREATED)
    else:
        return Response(JSONResponse().addError(
            0, "Location could not be created").addError(
                1, serializer.errors).send(),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
def unpost_message(request, id):
    response = JSONResponse()
    with transaction.atomic():
        try:
            message = Message.objects.get(id=id, author=request.user)
            message.delete()
            return Response(response.addData(
                "status", "completed deletion").addData(
                    id,
                    "Message with id: " + str(id) + " was deleted.").send(),
                            status=status.HTTP_200_OK)
        except Message.DoesNotExist:
            return Response(response.addError(
                0, "Message with id: " + str(id) +
                " does not exist or cannot be deleted").send(),
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
def create_user(request):
    if request.method == "POST":
        user_serializer = UserSerializer(data=request.data)
        if user_serializer.is_valid():
            User.objects.create_user(
                username=user_serializer.validated_data["username"],
                password=user_serializer.validated_data["password"])
            return Response(JSONResponse().addData(
                "User", user_serializer.data).addData("status",
                                                      "User created!").send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "User could not be created").addError(
                    1, user_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 4
0
def delete_information(request, id):
    response = JSONResponse()
    with transaction.atomic():
        try:
            information = Info.objects.get(id=id, user=request.user)
            information.delete()
            return Response(response.addData(
                "status", "completed deletion").addData(
                    id, "Information with id: " + str(id) +
                    " was deleted.").send(),
                            status=status.HTTP_200_OK)
        except Info.DoesNotExist:
            return Response(response.addError(
                0, "Information with id: " + str(id) +
                " does not exist or cannot be deleted").send(),
                            status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 5
0
def create_key(request):
    if request.data["data"]:
        key_serializer = KeySerializer(data=request.data["data"])
        if key_serializer.is_valid():
            key_serializer.save()
            return Response(JSONResponse().addData(
                "Key", key_serializer.data).addData("status",
                                                    "Key created!").send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "Key could not be created").addError(
                    1, key_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(JSONResponse().addError(0,
                                                "No data in request").send(),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 6
0
def create_message(request):
    if request.data["data"]:
        message_serializer = MessageSerializer(data=request.data["data"])
        if message_serializer.is_valid():
            m = message_serializer.save(user=request.user)
            return Response(JSONResponse().addData(
                "Message",
                message_serializer.data).addData("status",
                                                 "Message created!").send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "Message could not be created").addError(
                    1, message_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(JSONResponse().addError(0,
                                                "No data in request").send(),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 7
0
def post_info(request):
    if request.data["data"]:
        info_serializer = InfoSerializer(data=request.data["data"])
        if info_serializer.is_valid():
            info_serializer.save(user=request.user)
            return Response(JSONResponse().addData(
                "Information", info_serializer.data).addData(
                    "status", "Information added to user " +
                    request.user.username).send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "Information could not be added").addError(
                    1, info_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(JSONResponse().addError(0,
                                                "No data in request").send(),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 8
0
def user_update_location(request):
    if request.data["data"]:
        user_location_serializer = UserLocationSerializer(
            data=request.data['data'])
        if user_location_serializer.is_valid():
            messages = UserLocation(
                request.user,
                user_location_serializer.validated_data).getMessages()
            messages_serializer = MessageSerializer(messages, many=True)
            return Response(JSONResponse().addData(
                "Location", user_location_serializer.data).addData(
                    "Messages", messages_serializer.data).send(),
                            status=status.HTTP_201_CREATED)
        else:
            return Response(JSONResponse().addError(
                0, "User location could not be loaded").addError(
                    1, user_location_serializer.errors).send(),
                            status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(JSONResponse().addError(0,
                                                "No data in request").send(),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 9
0
def delete_location(request, location_id):
    response = JSONResponse()
    with transaction.atomic():
        try:
            location = Location.objects.get(id=location_id)
            location.delete()
            response.addData(
                location_id,
                "Location with id: " + str(location_id) + " was deleted.")
        except Location.DoesNotExist:
            response.addError(
                0,
                "Location with id: " + str(location_id) + " does not exist.")
    return Response(response.addData("status", "completed deletion").send(),
                    status=status.HTTP_200_OK)
Ejemplo n.º 10
0
def list_messages(request):
    serializer = MessageSerializer(Message.objects.filter(author=request.user),
                                   many=True)
    return Response(JSONResponse().addData("Messages", serializer.data).send(),
                    status=status.HTTP_200_OK)
Ejemplo n.º 11
0
def list_locations(request):
    serializer = LocationSerializer(Location.objects.all(), many=True)
    return Response(JSONResponse().addData("Locations",
                                           serializer.data).send(),
                    status=status.HTTP_200_OK)
Ejemplo n.º 12
0
def list_keys(request):
    serializer = KeySerializer(Keys.objects.all(), many=True)
    return Response(JSONResponse().addData("Keys", serializer.data).send(),
                    status=status.HTTP_200_OK)
Ejemplo n.º 13
0
def list_information(request):
    serializer = InfoSerializer(Info.objects.filter(user=request.user),
                                many=True)
    return Response(JSONResponse().addData("User Information",
                                           serializer.data).send(),
                    status=status.HTTP_200_OK)