Skip to content

Commit

Permalink
Change Sublet Amenity Return Formatting (#251)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
dr-Jess and judtinzhang authored Mar 6, 2024
1 parent 78dbf2f commit 7e43efe
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
18 changes: 18 additions & 0 deletions backend/sublet/migrations/0003_alter_sublet_baths.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
2 changes: 1 addition & 1 deletion backend/sublet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions backend/sublet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<sublet_id>/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/<sublet_id>/offers/",
Offers.as_view({"get": "list", "post": "create", "delete": "destroy"}),
),
# Image Creation
path("properties/<sublet_id>/images/", CreateImages.as_view()),
# Image Deletion
path("properties/images/<image_id>/", DeleteImage.as_view()),
]

Expand Down
8 changes: 5 additions & 3 deletions backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 7e43efe

Please sign in to comment.