Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sublet Drafting #271

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0.2 on 2024-03-15 22:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sublet", "0004_alter_sublet_external_link"),
]

operations = [
migrations.AddField(
model_name="sublet", name="is_draft", field=models.BooleanField(default=True),
),
migrations.AlterField(
model_name="sublet", name="end_date", field=models.DateField(blank=True, null=True),
),
migrations.AlterField(
model_name="sublet",
name="expires_at",
field=models.DateTimeField(blank=True, null=True),
),
migrations.AlterField(
model_name="sublet", name="price", field=models.IntegerField(blank=True, null=True),
),
migrations.AlterField(
model_name="sublet", name="start_date", field=models.DateField(blank=True, null=True),
),
]
9 changes: 5 additions & 4 deletions backend/sublet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Sublet(models.Model):
sublettees = models.ManyToManyField(
User, through=Offer, related_name="sublets_offered", blank=True
)
is_draft = models.BooleanField(default=True)
favorites = models.ManyToManyField(User, related_name="sublets_favorited", blank=True)
amenities = models.ManyToManyField(Amenity, blank=True)

Expand All @@ -42,12 +43,12 @@ class Sublet(models.Model):
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, null=True, blank=True)
price = models.IntegerField()
price = models.IntegerField(null=True, blank=True)
negotiable = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
expires_at = models.DateTimeField()
start_date = models.DateField()
end_date = models.DateField()
expires_at = models.DateTimeField(null=True, blank=True)
start_date = models.DateField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)

def __str__(self):
return f"{self.title} by {self.subletter}"
Expand Down
34 changes: 33 additions & 1 deletion backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@
many=True, queryset=Amenity.objects.all(), required=False
)

def validate_not_draft(self, validated_data, instance=None):
if "is_draft" in validated_data and not validated_data["is_draft"]:
# check that certain fields are there
fields = [
"title",
"address",
"price",
"negotiable",
"start_date",
"end_date",
"expires_at",
]
if bad_fields := [
field
for field in fields
if (field in validated_data and not validated_data[field])
or (
field not in validated_data
and (
instance is None
or not hasattr(instance, field)
or not getattr(instance, field)
)
)
]:
raise serializers.ValidationError(

Check warning on line 89 in backend/sublet/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/sublet/serializers.py#L89

Added line #L89 was not covered by tests
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha this doesn't work I don't know why haha 😭 will fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks haha. is there any way we can break this down? i get that its the least amount of lines but i feel like its getting a little hard to read

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would really suggest breaking it into first checking if the function came from a PUT/PATCH or a POST

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it would seem that patching with "" as the address passes the old address into validated_data??? really not sure why but will investigate further tomorrow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is weird ...

f"{', '.join(bad_fields)} are required to publish sublet."
)

class Meta:
model = Sublet
read_only_fields = [
Expand All @@ -72,7 +101,7 @@
]
fields = [
"id",
"subletter",
"is_draft",
"amenities",
"title",
"address",
Expand All @@ -93,6 +122,7 @@

def create(self, validated_data):
validated_data["subletter"] = self.context["request"].user
self.validate_not_draft(validated_data)
instance = super().create(validated_data)
instance.save()
return instance
Expand All @@ -104,6 +134,8 @@
self.context["request"].user == instance.subletter
or self.context["request"].user.is_superuser
):
if instance.is_draft:
self.validate_not_draft(validated_data, instance)

Check warning on line 138 in backend/sublet/serializers.py

View check run for this annotation

Codecov / codecov/patch

backend/sublet/serializers.py#L138

Added line #L138 was not covered by tests
instance = super().update(instance, validated_data)
instance.save()
return instance
Expand Down
Loading