From 2cad77e4fd4a6f34dcfa61d225472b4cdaf3fbcf Mon Sep 17 00:00:00 2001 From: Raj Ghodasara <71821708+rajghodasara1@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:46:44 -0500 Subject: [PATCH 1/6] updated get_all function in jobview updated job feed to not display jobs created by the user browsing the feed --- furbaby/api/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/furbaby/api/views.py b/furbaby/api/views.py index 00ab8fd..16439e9 100644 --- a/furbaby/api/views.py +++ b/furbaby/api/views.py @@ -761,8 +761,10 @@ def job_status_check(self): job.save() return - def get_all(self): + def get_all(self, owner_id=None): self.job_status_check() + if owner_id: + return Jobs.objects.filter(status="open").exclude(user=owner_id) return Jobs.objects.filter(status="open") def get_queryset(self): @@ -789,7 +791,7 @@ def get(self, request, *args, **kwargs): print(request.user.user_type) if "owner" in request.user.user_type and "sitter" in request.user.user_type: queryset_owner = self.get_queryset() - queryset_sitter = self.get_all() + queryset_sitter = self.get_all(request.user.id) serializer_owner = JobSerializer(queryset_owner, many=True) serializer_sitter = JobSerializer(queryset_sitter, many=True) From b5a8d3e296c1c06c25fdcf776690377a44ba3d63 Mon Sep 17 00:00:00 2001 From: Raj Ghodasara <71821708+rajghodasara1@users.noreply.github.com> Date: Thu, 7 Dec 2023 19:11:04 -0500 Subject: [PATCH 2/6] Fixed location update bug, fixed serializer for location update, applied black formatter changes --- frontend/src/Locations.tsx | 24 ++++++++----- furbaby/api/serializers.py | 8 +++++ furbaby/api/views.py | 70 ++++++++++++++++++++++---------------- 3 files changed, 64 insertions(+), 38 deletions(-) diff --git a/frontend/src/Locations.tsx b/frontend/src/Locations.tsx index 451b2c0..3512d5d 100644 --- a/frontend/src/Locations.tsx +++ b/frontend/src/Locations.tsx @@ -15,7 +15,7 @@ const Locations = () => { const [locations, setLocations] = useState([]); const [address, setAddress] = useState(""); const [city, setCity] = useState("New York City"); - const [country, setCountry] = useState("usa"); + const [country, setCountry] = useState("USA"); const [zipcode, setZipcode] = useState(""); const onClickConfirm = () => { @@ -46,9 +46,11 @@ const Locations = () => { }; const onCloseModal = () => { + setEditLocationId(""); setAddress(""); - setCity(""); - setCountry(""); + setCity("New York City"); + setCountry("USA"); + setZipcode(""); setOpen(false); }; @@ -131,23 +133,28 @@ const Locations = () => { }, [locations]); const onClickEditConfirm = () => { + console.log(editLocationId); axios .put( API_ROUTES.USER.LOCATION, - JSON.stringify({ + { id: editLocationId, address, city, - zipcode, country, - }) + zipcode, + } ) .then((response) => { // TODO: handle response - //console.log(response); + if (response.status === 200) { + onCloseModal(); + toast.success("Location updated successfully."); + } }) .catch((err) => { // TODO: handle error + toast.error("Failed to update location."); console.error(err); }); }; @@ -184,10 +191,9 @@ const Locations = () => { autoComplete="country-name" value={country} onChange={(e) => setCountry(e.target.value)} - defaultValue="usa" className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:max-w-xs sm:text-sm sm:leading-6" > - + diff --git a/furbaby/api/serializers.py b/furbaby/api/serializers.py index 9578344..262aa3c 100644 --- a/furbaby/api/serializers.py +++ b/furbaby/api/serializers.py @@ -99,6 +99,14 @@ def validate(self, data): def create(self, validated_data): return Locations.objects.create(**validated_data) + def update(self, instance, validated_data): + instance.address = validated_data.get("address", instance.address) + instance.city = validated_data.get("city", instance.city) + instance.country = validated_data.get("country", instance.country) + instance.zipcode = validated_data.get("zipcode", instance.zipcode) + instance.save() + return instance + class PetSerializer(serializers.ModelSerializer): owner = serializers.HiddenField(default=serializers.CurrentUserDefault()) diff --git a/furbaby/api/views.py b/furbaby/api/views.py index 86b5e13..33db70c 100644 --- a/furbaby/api/views.py +++ b/furbaby/api/views.py @@ -435,7 +435,7 @@ def get_user_location_by_id(self, request): # takes as input a user_id and returns a JSON of all the locations for that user def get_user_locations(self, request): - locations = Locations.objects.filter(user_id=request.user.id) + locations = Locations.objects.filter(user_id=request.user.id).order_by("created_at") location_list = [ { "id": location.id, @@ -460,36 +460,53 @@ def update_location_record(self, request): ) try: - updated_fields = [] location = Locations.objects.get(id=location_id) - if "address" in request.data: - location.address = request.data["address"] - updated_fields.append("address") - if "city" in request.data: - location.city = request.data["city"] - updated_fields.append("city") - if "country" in request.data: - location.country = request.data["country"] - updated_fields.append("country") - if "zipcode" in request.data: - location.zipcode = request.data["zipcode"] - updated_fields.append("zipcode") - if "default_location" in request.data: - if request.data["default_location"] == True: - # unset all other locations as default - Locations.objects.filter(user_id=request.user.id).update(default_location=False) - location.default_location = request.data["default_location"] - updated_fields.append("default_location") - - # location.save() - location.save(update_fields=updated_fields) + request.data.pop("id") + request.data["default_location"] = location.default_location + serializer = self.serializer_class( + location, data=request.data, partial=True, context={"request": request} + ) + if not serializer.is_valid(): + return json_response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) + serializer.save() + print(request.data) return json_response( - self.get_location_record(location), + location.id, status.HTTP_200_OK, safe=False, include_data=False, ) + + # updated_fields = [] + # if "address" in request.data: + # location.address = request.data["address"] + # updated_fields.append("address") + # if "city" in request.data: + # location.city = request.data["city"] + # updated_fields.append("city") + # if "country" in request.data: + # location.country = request.data["country"] + # updated_fields.append("country") + # if "zipcode" in request.data: + # location.zipcode = request.data["zipcode"] + # updated_fields.append("zipcode") + # if "default_location" in request.data: + # if request.data["default_location"] == True: + # # unset all other locations as default + # Locations.objects.filter(user_id=request.user.id).update(default_location=False) + # location.default_location = request.data["default_location"] + # updated_fields.append("default_location") + # + # # location.save() + # location.save(update_fields=updated_fields) + # + # return json_response( + # self.get_location_record(location), + # status.HTTP_200_OK, + # safe=False, + # include_data=False, + # ) except Locations.DoesNotExist: return json_response( data={ @@ -812,7 +829,6 @@ def get(self, request, *args, **kwargs): return JsonResponse(response_data, safe=False) elif "sitter" in request.user.user_type: - print("here") queryset_sitter = self.get_all() serializer_sitter = JobSerializer(queryset_sitter, many=True) response_data = { @@ -825,7 +841,6 @@ def put(self, request, *args, **kwargs): job_id = self.request.data.get("id") # type: ignore try: job = Jobs.objects.get(id=job_id) - # print(job.status) if job.status == "open": job.status = "acceptance_complete" job.save() @@ -885,7 +900,6 @@ def get(self, request, *args, **kwargs): def put(self, request, *args, **kwargs): # Retrieve the application ID from the URL or request data application_id = request.data.get("id") - # print(application_id) try: application = Applications.objects.get(id=application_id) except Applications.DoesNotExist: @@ -894,7 +908,6 @@ def put(self, request, *args, **kwargs): # Check if the user making the request is the owner of the application if request.user == job_instance.user: - # print(request.user) # Check if the job status is "open" if job_instance.status == "open": # Update the application status based on your requirements @@ -928,7 +941,6 @@ def put(self, request, *args, **kwargs): def post(self, request, *args, **kwargs): job_id = self.request.data.get("id") # type: ignore - # print(request.data) # print(job_id) try: job = Jobs.objects.get(id=job_id) From 54373ff720c1dc504e35b196e7b96110d142da5e Mon Sep 17 00:00:00 2001 From: Raj Ghodasara <71821708+rajghodasara1@users.noreply.github.com> Date: Sun, 10 Dec 2023 13:26:57 -0500 Subject: [PATCH 3/6] fixed location update and default location bug --- frontend/src/Locations.tsx | 5 ++--- furbaby/api/serializers.py | 6 +++--- furbaby/api/tests/test_views.py | 5 +---- furbaby/api/views.py | 5 ++--- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/frontend/src/Locations.tsx b/frontend/src/Locations.tsx index ac1aa9b..1b38590 100644 --- a/frontend/src/Locations.tsx +++ b/frontend/src/Locations.tsx @@ -75,7 +75,7 @@ const Locations = () => { const setAsDefault = (location: FurbabyLocation) => { axios - .put(API_ROUTES.USER.LOCATION, JSON.stringify({ ...location, default_location: true })) + .put(API_ROUTES.USER.LOCATION, { ...location, default_location: true }) .then((resp) => { console.log(resp); }) @@ -146,8 +146,7 @@ const Locations = () => { zipcode, } ) - .then(() => { - toast.success("your changes have been saved"); + .then((response) => { // TODO: handle response if (response.status === 200) { onCloseModal(); diff --git a/furbaby/api/serializers.py b/furbaby/api/serializers.py index 0ef4eb2..d93dbe6 100644 --- a/furbaby/api/serializers.py +++ b/furbaby/api/serializers.py @@ -91,9 +91,6 @@ def validate(self, data): raise ValidationError("Users must be located in New York City/NYC") if country not in countries_allowed_list: raise ValidationError("Users must be located in the United States of America/USA") - if data.get("default_location", True): - # If the user is setting a default location, set all other locations to false - Locations.objects.filter(user_id=data.get("user").id).update(default_location=False) return data def create(self, validated_data): @@ -104,6 +101,9 @@ def update(self, instance, validated_data): instance.city = validated_data.get("city", instance.city) instance.country = validated_data.get("country", instance.country) instance.zipcode = validated_data.get("zipcode", instance.zipcode) + if validated_data.get("default_location"): + Locations.objects.filter(user=instance.user).update(default_location=False) + instance.default_location = True; instance.save() return instance diff --git a/furbaby/api/tests/test_views.py b/furbaby/api/tests/test_views.py index 8133a27..2da0753 100644 --- a/furbaby/api/tests/test_views.py +++ b/furbaby/api/tests/test_views.py @@ -885,7 +885,6 @@ def test_location_update_missing_location_id(self): "city": "New York City", "country": "USA", "zipcode": "12345", - "default_location": True, } response = client.put(url, data, format="json") data = json.loads(response.content) @@ -912,7 +911,6 @@ def test_location_update_location_does_not_exist(self): "city": "New York City", "country": "USA", "zipcode": "12345", - "default_location": True, } response = client.put(url, data, format="json") data = json.loads(response.content) @@ -937,9 +935,8 @@ def test_location_update_successful(self): "id": location.id, "address": "456 Main St", "city": "NYC", - "country": "US", + "country": "USA", "zipcode": "45678", - "default_location": True, } response = client.put(url, data, format="json") data = json.loads(response.content) diff --git a/furbaby/api/views.py b/furbaby/api/views.py index d2d4fba..3edb2d9 100644 --- a/furbaby/api/views.py +++ b/furbaby/api/views.py @@ -465,8 +465,8 @@ def update_location_record(self, request): try: location = Locations.objects.get(id=location_id) - request.data.pop("id") - request.data["default_location"] = location.default_location + if "default_location" not in request.data: + request.data["default_location"] = location.default_location serializer = self.serializer_class( location, data=request.data, partial=True, context={"request": request} ) @@ -474,7 +474,6 @@ def update_location_record(self, request): return json_response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) serializer.save() - print(request.data) return json_response( location.id, status.HTTP_200_OK, From 4d41e953df7e269f2dca38f660cbc63f81bd5e2e Mon Sep 17 00:00:00 2001 From: Raj Ghodasara <71821708+rajghodasara1@users.noreply.github.com> Date: Sun, 10 Dec 2023 15:03:35 -0500 Subject: [PATCH 4/6] added default location to be auto-selected in Add Job form --- frontend/src/Jobs.tsx | 9 ++++++--- frontend/src/Locations.tsx | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/frontend/src/Jobs.tsx b/frontend/src/Jobs.tsx index 0fa3d0c..8b105d6 100644 --- a/frontend/src/Jobs.tsx +++ b/frontend/src/Jobs.tsx @@ -23,6 +23,7 @@ interface Location { city: string; country: string; zipcode: string; + default_location: boolean; } interface Pet { @@ -310,9 +311,11 @@ const JobPage: React.FC = () => { return axios .get(API_ROUTES.USER.LOCATION) .then((response) => { - //console.log(response, response.data); setLocations(response?.data ?? []); - // return response; + const default_location = response.data.filter((location: Location) => location.default_location); + if (default_location.length > 0) { + setJobFormData({ ...jobFormData, location: default_location[0].id }); + } }) .catch((err) => { console.error("failed to fetch locations", err); @@ -333,7 +336,7 @@ const JobPage: React.FC = () => { .post(API_ROUTES.JOBS, jobFormData) .then((response) => { if (response.status === 201) { - toast.success("Job Addedd Successfully"); + toast.success("Job Added Successfully"); setJobFormData({ pet: "", location: "", diff --git a/frontend/src/Locations.tsx b/frontend/src/Locations.tsx index 1b38590..416f384 100644 --- a/frontend/src/Locations.tsx +++ b/frontend/src/Locations.tsx @@ -108,10 +108,10 @@ const Locations = () => { {loc.city}, {loc.country} - {loc.zipcode}

- {loc.default_location &&
Default
} + {loc.default_location &&
Default
} {!loc.default_location && ( - {loc.default_location &&
Default
} - {!loc.default_location && ( - + ) : ( + )} diff --git a/furbaby/api/serializers.py b/furbaby/api/serializers.py index d93dbe6..8f7facc 100644 --- a/furbaby/api/serializers.py +++ b/furbaby/api/serializers.py @@ -104,6 +104,8 @@ def update(self, instance, validated_data): if validated_data.get("default_location"): Locations.objects.filter(user=instance.user).update(default_location=False) instance.default_location = True; + if not validated_data.get("default_location"): + instance.default_location = False; instance.save() return instance From 8e299a35e4c6f5989fa0885ef84dad592c5df939 Mon Sep 17 00:00:00 2001 From: Raj Ghodasara <71821708+rajghodasara1@users.noreply.github.com> Date: Sun, 10 Dec 2023 16:32:31 -0500 Subject: [PATCH 6/6] apply black formatter changes --- furbaby/api/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/furbaby/api/serializers.py b/furbaby/api/serializers.py index 8f7facc..e6d234c 100644 --- a/furbaby/api/serializers.py +++ b/furbaby/api/serializers.py @@ -103,9 +103,9 @@ def update(self, instance, validated_data): instance.zipcode = validated_data.get("zipcode", instance.zipcode) if validated_data.get("default_location"): Locations.objects.filter(user=instance.user).update(default_location=False) - instance.default_location = True; + instance.default_location = True if not validated_data.get("default_location"): - instance.default_location = False; + instance.default_location = False instance.save() return instance