def get(self, request, pk, format=None): try: address = Address.objects.get(pk=pk) except Address.DoesNotExist: return HttpResponse(status=404) serializer = AddressSerializer(address, context={'request': request}) return Response(serializer.data)
def update_address(self, request, pk=None): s = AddressSerializer(data=request.data, many=False) s.is_valid(raise_exception=True) u = Address.objects.filter(user=request.user).first() if u == None: s.create(s.validated_data) else: s.save(address=u.address) return Response(status=200)
def post(self, request, format=None): data = JSONParser().parse(request) lat, lon = geo.getAddressData(data["address"]) if lat == None or lon == None: return Response(serializer.errors, status=404) data["latitude"] = round(lat, 8) data["longitude"] = round(lon, 8) elevation = geo.getElevation(lat, lon) if elevation == None: return Response(serializer.errors, status=404) data["elevation"] = round(elevation, 2) serializer = AddressSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=201) return Response(serializer.errors, status=400)
def test_address_serializer_create_object(self): json_payload = """ { "uprn": "12345678", "address_line_1": "Dataland", "address_line_2": "Dataland Village Main Street", "city": "Dataland City", "county": "Cucumberland", "postcode": "AA11 1ZZ", "country_code": "GB", "point": { "type": "Point", "coordinates": [55.4168769443259, -1.83356713993623] }, "srid": 4326 } """ data = json.loads(json_payload) serializer = AddressSerializer(data=data) self.assertTrue(serializer.is_valid()) serializer.save() self.assertEqual(Address.objects.count(), 1)
def test_retrieve_address_list(self): """ Test address list of authenticated user""" Address.objects.create( user=self.user, address="Block 1, Lot 1, Phase 1, Ashton Fields South", city="Calamba City", latitude=14.204780, longitude=121.154716, ) res = self.client.get(ADDRESS_URL) addressess = Address.objects.all() serializer = AddressSerializer(addressess, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_retrieve_address_details(self): """Test viewing a address detail""" address = Address.objects.create( user=self.user, address="Block 1, Lot 1, Phase 1, Ashton Fields South", city="Calamba City", latitude=14.204780, longitude=121.154716, ) # get the address id url = detail_url(address.id) res = self.client.get(url) serializer = AddressSerializer(address) self.assertEqual(res.data, serializer.data)
def list(self, request): return Response(AddressSerializer(Address.objects.filter(user=request.user).first(), many=False).data)