From 7e43efe8a015f3b4476de2eff7edeb563a443cec Mon Sep 17 00:00:00 2001 From: Jesse Zong <63322084+dr-Jess@users.noreply.github.com> Date: Tue, 5 Mar 2024 21:50:29 -0500 Subject: [PATCH] Change Sublet Amenity Return Formatting (#251) * Sublet Image Handling * Fix linting whoops * Add list comprehensions + prevalidation to images * Fix misnamed serializer field * Fix test cases * Add separate routes for image creation and deletion * Begin work on sublet testing * Mocked AWS call * Add tests for multiple images + deletion * How did this not fail in previous runs haha * Add sublet negotiable, swap max/min to reg price fields * Fix filtering * Change amenities return formatting + add comments for sublet/urls * Alter baths to be 1-point decimal * Fix testing for decimal baths + add testing for new amenities formatting --------- Co-authored-by: Justin Zhang --- .../migrations/0003_alter_sublet_baths.py | 18 ++++++++++++++++++ backend/sublet/models.py | 2 +- backend/sublet/serializers.py | 8 ++++++-- backend/sublet/urls.py | 12 ++++++++++++ backend/tests/sublet/test_sublets.py | 8 +++++--- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 backend/sublet/migrations/0003_alter_sublet_baths.py diff --git a/backend/sublet/migrations/0003_alter_sublet_baths.py b/backend/sublet/migrations/0003_alter_sublet_baths.py new file mode 100644 index 00000000..7f8b65b9 --- /dev/null +++ b/backend/sublet/migrations/0003_alter_sublet_baths.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.9 on 2024-03-05 21:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("sublet", "0002_auto_20240209_1649"), + ] + + operations = [ + migrations.AlterField( + model_name="sublet", + name="baths", + field=models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True), + ), + ] diff --git a/backend/sublet/models.py b/backend/sublet/models.py index 0a8c1bc7..747634c3 100644 --- a/backend/sublet/models.py +++ b/backend/sublet/models.py @@ -39,7 +39,7 @@ class Sublet(models.Model): title = models.CharField(max_length=255) address = models.CharField(max_length=255, null=True, blank=True) beds = models.IntegerField(null=True, blank=True) - baths = models.IntegerField(null=True, blank=True) + baths = models.DecimalField(max_digits=3, decimal_places=1, null=True, blank=True) description = models.TextField(null=True, blank=True) external_link = models.URLField(max_length=255) price = models.IntegerField() diff --git a/backend/sublet/serializers.py b/backend/sublet/serializers.py index fecceef7..549e9e2b 100644 --- a/backend/sublet/serializers.py +++ b/backend/sublet/serializers.py @@ -122,7 +122,9 @@ def destroy(self, instance): class SubletSerializerRead(serializers.ModelSerializer): - amenities = AmenitySerializer(many=True, required=False) + amenities = serializers.PrimaryKeyRelatedField( + many=True, queryset=Amenity.objects.all(), required=False + ) images = SubletImageURLSerializer(many=True, required=False) class Meta: @@ -149,7 +151,9 @@ class Meta: # simple sublet serializer for use when pulling all serializers/etc class SubletSerializerSimple(serializers.ModelSerializer): - amenities = AmenitySerializer(many=True, required=False) + amenities = serializers.PrimaryKeyRelatedField( + many=True, queryset=Amenity.objects.all(), required=False + ) images = SubletImageURLSerializer(many=True, required=False) class Meta: diff --git a/backend/sublet/urls.py b/backend/sublet/urls.py index 7d3c4495..edfde8bf 100644 --- a/backend/sublet/urls.py +++ b/backend/sublet/urls.py @@ -19,18 +19,30 @@ router.register(r"properties", Properties, basename="properties") additional_urls = [ + # List of all amenities path("amenities/", Amenities.as_view(), name="amenities"), + # All favorites for user path("favorites/", UserFavorites.as_view(), name="user-favorites"), + # All offers made by user path("offers/", UserOffers.as_view(), name="user-offers"), + # Favorites + # post: add a sublet to the user's favorites + # delete: remove a sublet from the user's favorites path( "properties//favorites/", Favorites.as_view({"post": "create", "delete": "destroy"}), ), + # Offers + # get: list all offers for a sublet + # post: create an offer for a sublet + # delete: delete an offer for a sublet path( "properties//offers/", Offers.as_view({"get": "list", "post": "create", "delete": "destroy"}), ), + # Image Creation path("properties//images/", CreateImages.as_view()), + # Image Deletion path("properties/images//", DeleteImage.as_view()), ] diff --git a/backend/tests/sublet/test_sublets.py b/backend/tests/sublet/test_sublets.py index ced48494..38199c85 100644 --- a/backend/tests/sublet/test_sublets.py +++ b/backend/tests/sublet/test_sublets.py @@ -40,7 +40,7 @@ def test_create_sublet(self): "title": "Test Sublet1", "address": "1234 Test Street", "beds": 2, - "baths": 1, + "baths": "1.5", "description": "This is a test sublet.", "external_link": "https://example.com", "price": 1000, @@ -64,6 +64,7 @@ def test_create_sublet(self): "expires_at", "start_date", "end_date", + "amenities", ] [self.assertEqual(payload[key], res_json[key]) for key in match_keys] self.assertIn("id", res_json) @@ -184,7 +185,7 @@ def test_browse_sublet(self): "title": "Test Sublet2", "address": "1234 Test Street", "beds": 2, - "baths": 1, + "baths": "1.5", "description": "This is a test sublet.", "external_link": "https://example.com", "price": 1000, @@ -201,7 +202,8 @@ def test_browse_sublet(self): self.assertEqual(res_json["title"], "Test Sublet2") self.assertEqual(res_json["address"], "1234 Test Street") self.assertEqual(res_json["beds"], 2) - self.assertEqual(res_json["baths"], 1) + self.assertEqual(res_json["baths"], "1.5") + self.assertEqual(res_json["amenities"], ["Amenity1", "Amenity2"]) def test_delete_sublet(self): sublets_count = Sublet.objects.all().count()