-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Add Sublet Drafting #271
Changes from all commits
3880930
0c23330
beaf30d
671e10a
af0f247
89b0d6a
c078671
3f3ca47
2e3439d
8e0d48d
d79127c
ffecf36
bdc4f4e
2cfb9dc
49793f9
8859861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 5.0.2 on 2024-03-22 21:50 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("sublet", "0005_sublet_is_draft_alter_sublet_end_date_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField(model_name="sublet", name="is_draft",), | ||
migrations.AddField( | ||
model_name="sublet", name="is_published", field=models.BooleanField(default=False), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,36 @@ class SubletSerializer(serializers.ModelSerializer): | |
many=True, queryset=Amenity.objects.all(), required=False | ||
) | ||
|
||
def validate_publish(self, validated_data, instance=None): | ||
fields = [ | ||
"title", | ||
"address", | ||
"price", | ||
"negotiable", | ||
"start_date", | ||
"end_date", | ||
"expires_at", | ||
] | ||
|
||
newest_fields = [ | ||
( | ||
field, | ||
( | ||
validated_data[field] | ||
if field in validated_data | ||
else getattr(instance, field, None) | ||
if instance | ||
else None | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically i think u can also do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
) | ||
for field in fields | ||
] | ||
|
||
if bad_fields := [field[0] for field in newest_fields if not field[1]]: | ||
raise serializers.ValidationError( | ||
f"fields: {', '.join(bad_fields)} are required to publish sublet." | ||
) | ||
|
||
class Meta: | ||
model = Sublet | ||
read_only_fields = [ | ||
|
@@ -72,7 +102,7 @@ class Meta: | |
] | ||
fields = [ | ||
"id", | ||
"subletter", | ||
"is_published", | ||
"amenities", | ||
"title", | ||
"address", | ||
|
@@ -93,6 +123,8 @@ class Meta: | |
|
||
def create(self, validated_data): | ||
validated_data["subletter"] = self.context["request"].user | ||
if validated_data["is_published"]: | ||
self.validate_publish(validated_data) | ||
instance = super().create(validated_data) | ||
instance.save() | ||
return instance | ||
|
@@ -104,6 +136,10 @@ def update(self, instance, validated_data): | |
self.context["request"].user == instance.subletter | ||
or self.context["request"].user.is_superuser | ||
): | ||
if ("is_published" in validated_data and validated_data["is_published"]) or ( | ||
"is_published" not in validated_data and instance.is_published | ||
): | ||
self.validate_publish(validated_data, instance) | ||
instance = super().update(instance, validated_data) | ||
instance.save() | ||
return instance | ||
|
@@ -133,6 +169,8 @@ class Meta: | |
fields = [ | ||
"id", | ||
"subletter", | ||
"sublettees", | ||
"is_published", | ||
"amenities", | ||
"title", | ||
"address", | ||
|
@@ -142,6 +180,7 @@ class Meta: | |
"external_link", | ||
"price", | ||
"negotiable", | ||
"created_at", | ||
"start_date", | ||
"end_date", | ||
"expires_at", | ||
|
@@ -160,6 +199,7 @@ class Meta: | |
model = Sublet | ||
fields = [ | ||
"id", | ||
"is_published", | ||
"subletter", | ||
"amenities", | ||
"title", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ def list(self, request, *args, **kwargs): | |
queryset = queryset.filter(subletter=request.user) | ||
else: | ||
queryset = queryset.filter(expires_at__gte=timezone.now()) | ||
queryset = queryset.filter(is_published=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice |
||
if title: | ||
queryset = queryset.filter(title__icontains=title) | ||
if address: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,7 @@ def setUp(self): | |
post_1 = Post.objects.all().first() | ||
post_1.status = Post.STATUS_APPROVED | ||
post_1.save() | ||
self.id = post_1.id | ||
self.post_id = post_1.id | ||
|
||
@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs) | ||
def test_create_post(self): | ||
|
@@ -100,10 +100,10 @@ def test_fail_post(self): | |
@mock.patch("portal.permissions.get_user_clubs", mock_get_user_clubs) | ||
def test_update_post(self): | ||
payload = {"title": "New Test Title 3"} | ||
response = self.client.patch(f"/portal/posts/{self.id}/", payload) | ||
response = self.client.patch(f"/portal/posts/{self.post_id}/", payload) | ||
res_json = json.loads(response.content) | ||
self.assertEqual(self.id, res_json["id"]) | ||
self.assertEqual("New Test Title 3", Post.objects.get(id=self.id).title) | ||
self.assertEqual(self.post_id, res_json["id"]) | ||
self.assertEqual("New Test Title 3", Post.objects.get(id=self.post_id).title) | ||
# since the user is not an admin, approved should be set to false after update | ||
self.assertEqual(Post.STATUS_DRAFT, res_json["status"]) | ||
|
||
|
@@ -113,9 +113,9 @@ def test_update_post_admin(self): | |
admin = User.objects.create_superuser("[email protected]", "admin", "admin") | ||
self.client.force_authenticate(user=admin) | ||
payload = {"title": "New Test Title 3"} | ||
response = self.client.patch(f"/portal/posts/{self.id}/", payload) | ||
response = self.client.patch(f"/portal/posts/{self.post_id}/", payload) | ||
res_json = json.loads(response.content) | ||
self.assertEqual(self.id, res_json["id"]) | ||
self.assertEqual(self.post_id, res_json["id"]) | ||
self.assertEqual(Post.STATUS_APPROVED, res_json["status"]) | ||
|
||
@mock.patch("portal.serializers.get_user_clubs", mock_get_user_clubs) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, a lot simpler and readable! :D