def get(self,request): queryset = Address.objects.all() serializer = UserAddressSerializer(queryset,many=True) user = self.request.user return Response({ 'user_id':user.id, 'addresses':serializer.data, })
def list(self, request, *args, **kwargs): queryset = self.get_queryset() serializer = UserAddressSerializer(queryset, many=True) user = self.request.user return Response({ 'user_id': user.id, 'default_address_id': user.default_address_id, 'limit': constants.USER_ADDRESS_COUNT_LIMIT, 'addresses': serializer.data, })
def test_address_location(self): """test for address location""" # true serializer = UserAddressSerializer(data={'title': 'title', 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30}) self.assertTrue(serializer.is_valid()) # wrong longitude serializer = UserAddressSerializer(data={'title': 'title', 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 1000, 'location_latitude': 30}) self.assertFalse(serializer.is_valid()) # wrong latitude serializer = UserAddressSerializer(data={'title': 'title', 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': -200}) self.assertFalse(serializer.is_valid())
def post(self, request): data = request.data serializer = UserAddressSerializer(data=data, context={ 'request': request, 'view': self }) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def test_address_type(self): """test for address type choices""" # right serializer = UserAddressSerializer(data={'title': 'title', 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30}) self.assertTrue(serializer.is_valid()) # wrong serializer = UserAddressSerializer(data={'title': 'title', 'area': 'area', 'type': 'S', # wrong 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30}) self.assertFalse(serializer.is_valid())