コード例 #1
0
def create_tag(request):
    if request.method == 'POST':
        serializer = TagSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
コード例 #2
0
def __create_tag(request):
    data = JSONParser().parse(request)
    serializer = TagSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        return JsonResponse(serializer.data, status=HTTP_201_CREATED)
    return JsonResponse(serializer.errors, status=HTTP_400_BAD_REQUEST)
コード例 #3
0
def __update_tag_detail(request, tag):
    data = JSONParser().parse(request)
    serializer = TagSerializer(tag, data=data)
    if serializer.is_valid():
        serializer.save()
        return JsonResponse(serializer.data)
    return JsonResponse(serializer.errors, status=HTTP_400_BAD_REQUEST)
コード例 #4
0
def test_schema(fake):
    address = Address(point=Point(1, 2),
                      neighborhood="Some neighborhood",
                      street=fake.street_address(),
                      city=fake.city(),
                      state=fake.state(),
                      zip=fake.zipcode(),
                      creator_user_id="some id",
                      last_updated_user_id="some id",
                      owner_name=fake.first_name(),
                      owner_email=fake.safe_email(),
                      tenant_name=fake.name(),
                      tenant_email=fake.safe_email(),
                      follow_up_owner_needed=True,
                      land_bank_property=True,
                      type_of_property=1)
    address.save()
    address.refresh_from_db()

    tag = Tag(
        address=address,
        creator_user_id="some id",
        last_updated_user_id="some id",
        date_taken=timezone.now(),
        description=fake.text(),
    )
    tag.save()
    tag.refresh_from_db()

    data = TagSerializer(tag).data
    TAG_SCHEMA.validate(dict(data))
コード例 #5
0
def tag_list(request):
    """
    List all tags or create a new one.
    :param request:
    :return: list of tags
    """
    if request.method == "GET":
        tags = Tag.objects.all()
        serializer = TagSerializer(tags, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == "POST":
        authentication_request = get_authenticated_user(request)

        if not authentication_request.status_code == 200:
            return authentication_request

        authenticated_data = json.loads(
            authentication_request.content.decode('utf-8'))
        request_data = json.loads(request.body.decode('utf-8'))

        if not authenticated_data['id'] == request_data['creator_user_id']:
            return JsonResponse({'Error': 'Unauthorized to create tags'},
                                status=403)

        data = JSONParser().parse(request)
        serializer = TagSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
コード例 #6
0
    def test_tag_list__get(self):
        self.request.method = 'GET'
        response = tag.tag_list(self.request)
        actual = response.getvalue().decode("utf-8")

        serializer = TagSerializer(self.tags, many=True)
        expected = JsonResponse(serializer.data,
                                safe=False).getvalue().decode("utf-8")

        self.assertEqual(actual, expected)
コード例 #7
0
    def test_tag_detail__get(self):
        self.request.method = 'GET'
        pk = self.tags[0].id
        response = tag.tag_detail(self.request, pk)
        actual = response.getvalue().decode("utf-8")

        serializer = TagSerializer(self.tags[0])
        expected = JsonResponse(serializer.data,
                                safe=False).getvalue().decode("utf-8")

        self.assertEqual(actual, expected)
コード例 #8
0
    def test_retrieve_tags_by_address__get(self):
        self.request.method = 'GET'
        pk = self.addresses[0].id
        response = address.address_tags(self.request, pk)
        actual = response.getvalue().decode("utf=8")

        # filter() used by address_tags() returns list in reverse traversal order
        serializer = TagSerializer(self.tags[-2::-1], many=True)
        expected = JsonResponse(serializer.data, safe=False).getvalue().decode("utf-8")

        self.assertEqual(actual, expected)
コード例 #9
0
def address_tags(request, pk):
    """
    Retrieve, update or delete a tag
    :param request:
    :param pk:
    :return: details of a tag
    """
    try:
        address = Address.objects.get(pk=pk)
    except Address.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        tags = Tag.objects.all()
        serializer = TagSerializer(tags, many=True)
        return JsonResponse(serializer.data, safe=False)
コード例 #10
0
def tag_detail(request, pk):
    """
    Retrieve, update or delete a tag
    :param request:
    :param pk:
    :return: details of a tag
    """
    try:
        tag = Tag.objects.get(pk=pk)
    except Tag.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = TagSerializer(tag)
        return JsonResponse(serializer.data)

    elif request.method == "PUT":
        authentication_request = get_authenticated_user(request)

        if not authentication_request.status_code == 200:
            return authentication_request

        authenticated_data = json.loads(
            authentication_request.content.decode('utf-8'))
        request_data = json.loads(request.body.decode('utf-8'))

        if not authenticated_data['id'] == request_data['last_updated_user_id']:
            return JsonResponse({'Error': 'Unauthorized to update tag'},
                                status=403)

        data = JSONParser().parse(request)
        serializer = TagSerializer(tag, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        authentication_request = get_authenticated_user(request)

        if not authentication_request.status_code == 200:
            return authentication_request

        tag.delete()
        return HttpResponse(status=204)
コード例 #11
0
def __retrieve_tags_by_address(address):
    tags = Tag.objects.filter(address=address)
    serializer = TagSerializer(tags, many=True)
    return JsonResponse(serializer.data, safe=False)
コード例 #12
0
def __list_tags():
    tags = Tag.objects.all()
    serializer = TagSerializer(tags, many=True)
    return JsonResponse(serializer.data, safe=False)
コード例 #13
0
def __retrieve_tag_detail(tag):
    serializer = TagSerializer(tag)
    return JsonResponse(serializer.data)