Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ManaliTanna committed Nov 5, 2023
1 parent a591aa4 commit 8ab904a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
4 changes: 2 additions & 2 deletions furbaby/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UserLoginSerializer(serializers.Serializer):


class UserLocationSerializer(serializers.Serializer):
user_id = serializers.UUIDField()
user = serializers.HiddenField(default=serializers.CurrentUserDefault())
address = serializers.CharField(max_length=200)
city = serializers.CharField(max_length=100)
country = serializers.CharField(max_length=100)
Expand All @@ -57,7 +57,7 @@ class UserLocationSerializer(serializers.Serializer):

class Meta:
model = Locations
fields = ["user_id" "address", "city", "country", "zipcode", "deault_location"]
fields = ["user_id" "address", "city", "country", "zipcode", "default_location"]

# TODO: There should be only one default address per user
def validate(self, data):
Expand Down
42 changes: 20 additions & 22 deletions furbaby/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,15 @@ def password_reset_token_created(sender, instance, reset_password_token, *args,

# This class is for the user location(s)
class UserLocationView(APIView):

# Fetch the locations serializer
serializer_class = UserLocationSerializer

def get_exception_handler(self):
return exception_handler

# takes as input the user id, request and inserts a new location record for the user
def insert_location_record(self, request_data):
serializer = self.serializer_class(data=request_data)
def insert_location_record(self, request):
serializer = self.serializer_class(data=request.data, context={"request": request})

if not serializer.is_valid():
return json_response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Expand All @@ -226,25 +225,25 @@ def get_location_record(self, location=None):
}

# takes as input a user_id and returns a JSON of all the locations for that user
def get_user_locations(self, user_id):
locations = Locations.objects.filter(user_id=user_id)
def get_user_locations(self, request):
locations = Locations.objects.filter(user_id=request.user.id)
serialized_data = serialize("json", locations)
serialized_data = json.loads(serialized_data)
return serialized_data

# takes as input a location_id and location fields and updates the location record
def update_location_record(self, user_id, request_data):
def update_location_record(self, request):
try:
location_id = request_data["id"]
location_id = request.data["id"]
location = Locations.objects.get(id=location_id)
location.address = request_data["address"]
location.city = request_data["city"]
location.country = request_data["country"]
location.zipcode = request_data["zipcode"]
location.default_location = request_data["default_location"]
location.address = request.data["address"]
location.city = request.data["city"]
location.country = request.data["country"]
location.zipcode = request.data["zipcode"]
location.default_location = request.data["default_location"]
location.save()
return json_response(
self.get_location_record(Locations.objects.get(id=location_id)),
self.get_location_record(location),
status.HTTP_200_OK,
safe=False,
include_data=False,
Expand All @@ -254,14 +253,14 @@ def update_location_record(self, user_id, request_data):
data={
"error": "location not found for user",
"location id": location_id,
"user id": user_id,
"user id": request.user.id,
},
status=status.HTTP_404_NOT_FOUND,
)

def delete_location_record(self, user_id, request_data):
def delete_location_record(self, request):
try:
location_id = request_data["id"]
location_id = request.data["id"]
location = Locations.objects.get(id=location_id)
location.delete()
return json_response(
Expand All @@ -273,7 +272,7 @@ def delete_location_record(self, user_id, request_data):
data={
"error": "location not found for user",
"location id": location_id,
"user id": user_id,
"user id": request.user.id,
},
status=status.HTTP_404_NOT_FOUND,
)
Expand All @@ -288,21 +287,20 @@ def user_location_view(request):

# fetch all user locations for the user
if request.method == "GET":
locations_list = location_view.get_user_locations(request.user.id)
locations_list = location_view.get_user_locations(request)
return json_response(locations_list, status=status.HTTP_200_OK, safe=False)

# insert a new location record for the user
if request.method in ["POST"]:
request.data["user_id"] = request.user.id
return location_view.insert_location_record(request.data)
return location_view.insert_location_record(request)

# update a location record for the user
if request.method in ["PUT", "PATCH"]:
return location_view.update_location_record(request.user.id, request.data)
return location_view.update_location_record(request)

# delete a location record for the user
if request.method == "DELETE":
return location_view.delete_location_record(request.user.id, request.data)
return location_view.delete_location_record(request)

return json_response(
{"error": "incorrect request method supplied"},
Expand Down

0 comments on commit 8ab904a

Please sign in to comment.