Skip to content

Commit

Permalink
Fix flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-Jess committed Oct 27, 2023
1 parent 08963d2 commit 4c98ef8
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 81 deletions.
132 changes: 91 additions & 41 deletions backend/sublet/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by Django 3.2.20 on 2023-10-25 14:38

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
Expand All @@ -15,63 +15,113 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='Amenity',
fields=[
('name', models.CharField(max_length=255, primary_key=True, serialize=False)),
],
name="Amenity",
fields=[("name", models.CharField(max_length=255, primary_key=True, serialize=False)),],
),
migrations.CreateModel(
name='Offer',
name="Offer",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(blank=True, max_length=255, null=True)),
('phone_number', models.CharField(blank=True, max_length=20, null=True)),
('message', models.CharField(blank=True, max_length=255)),
('created_date', models.DateTimeField(auto_now_add=True)),
(
"id",
models.AutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("email", models.EmailField(blank=True, max_length=255, null=True)),
("phone_number", models.CharField(blank=True, max_length=20, null=True)),
("message", models.CharField(blank=True, max_length=255)),
("created_date", models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='Sublet',
name="Sublet",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('address', models.CharField(blank=True, max_length=255, null=True)),
('beds', models.IntegerField(blank=True, null=True)),
('baths', models.IntegerField(blank=True, null=True)),
('description', models.TextField(blank=True, null=True)),
('external_link', models.URLField(max_length=255)),
('min_price', models.IntegerField()),
('max_price', models.IntegerField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('expires_at', models.DateTimeField()),
('start_date', models.DateField()),
('end_date', models.DateField()),
('amenities', models.ManyToManyField(blank=True, null=True, to='sublet.Amenity')),
('favorites', models.ManyToManyField(blank=True, null=True, related_name='sublets_favorited', to=settings.AUTH_USER_MODEL)),
('sublettees', models.ManyToManyField(blank=True, null=True, related_name='sublets_offered', through='sublet.Offer', to=settings.AUTH_USER_MODEL)),
('subletter', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
(
"id",
models.AutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("title", models.CharField(max_length=255)),
("address", models.CharField(blank=True, max_length=255, null=True)),
("beds", models.IntegerField(blank=True, null=True)),
("baths", models.IntegerField(blank=True, null=True)),
("description", models.TextField(blank=True, null=True)),
("external_link", models.URLField(max_length=255)),
("min_price", models.IntegerField()),
("max_price", models.IntegerField()),
("created_at", models.DateTimeField(auto_now_add=True)),
("expires_at", models.DateTimeField()),
("start_date", models.DateField()),
("end_date", models.DateField()),
("amenities", models.ManyToManyField(blank=True, null=True, to="sublet.Amenity")),
(
"favorites",
models.ManyToManyField(
blank=True,
null=True,
related_name="sublets_favorited",
to=settings.AUTH_USER_MODEL,
),
),
(
"sublettees",
models.ManyToManyField(
blank=True,
null=True,
related_name="sublets_offered",
through="sublet.Offer",
to=settings.AUTH_USER_MODEL,
),
),
(
"subletter",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
],
),
migrations.CreateModel(
name='SubletImage',
name="SubletImage",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(upload_to='sublet/images')),
('sublet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='sublet.sublet')),
(
"id",
models.AutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("image", models.ImageField(upload_to="sublet/images")),
(
"sublet",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="images",
to="sublet.sublet",
),
),
],
),
migrations.AddField(
model_name='offer',
name='sublet',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='offers', to='sublet.sublet'),
model_name="offer",
name="sublet",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="offers",
to="sublet.sublet",
),
),
migrations.AddField(
model_name='offer',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='offers_made', to=settings.AUTH_USER_MODEL),
model_name="offer",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="offers_made",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AddConstraint(
model_name='offer',
constraint=models.UniqueConstraint(fields=('user', 'sublet'), name='unique_offer'),
model_name="offer",
constraint=models.UniqueConstraint(fields=("user", "sublet"), name="unique_offer"),
),
]
7 changes: 4 additions & 3 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,19 @@ def create(self, validated_data):
# x = validated_data.pop("amenities")
# print(x)

instance = super().create(validated_data)
# instance = super().create(validated_data)

# # Update target populations
# # If none of a category was selected, then we will auto-select all populations in that categary
# # If none of a category was selected,
# then we will auto-select all populations in that categary
data = self.context["request"].data
print("HELLO")
print(data)
print(data["amenities"])
print(Amenity.objects.all())
amenities = Amenity.objects.filter(name__in=data["amenities"])
print(amenities)
# raw_amenities =
# raw_amenities =
# raw_target_populations = self.parse_target_populations(data["target_populations"])
# target_populations = self.update_target_populations(raw_target_populations)

Expand Down
3 changes: 1 addition & 2 deletions backend/sublet/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.urls import include, path
from django.urls import path
from rest_framework import routers
from rest_framework.routers import DefaultRouter

from sublet.views import Amenities, Favorites, Offers, Properties, UserFavorites, UserOffers

Expand Down
24 changes: 9 additions & 15 deletions backend/sublet/views.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
from django.contrib.auth import get_user_model
from django.db.models import Count, Q
from django.db.models.functions import Trunc
from django.shortcuts import render
from django.utils import timezone
from rest_framework import generics, status, viewsets, mixins
from rest_framework.decorators import action
from rest_framework import generics, mixins, status, viewsets
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from sublet.models import Amenity, Offer, Sublet, SubletImage
from sublet.permissions import IsSuperUser, SubletOwnerPermission, OfferOwnerPermission
from sublet.models import Amenity, Offer, Sublet
# , SubletImage
from sublet.permissions import IsSuperUser, OfferOwnerPermission, SubletOwnerPermission
from sublet.serializers import (
AmenitySerializer,
OfferSerializer,
SubletSerializer,
SimpleSubletSerializer,
SubletSerializer,
)


Expand Down Expand Up @@ -59,13 +55,13 @@ class Properties(viewsets.ModelViewSet):
"""
list:
Returns a list of Sublets that match query parameters (e.g., amenities) and belong to the user.
create:
Create a Sublet.
partial_update:
Update certain fields in the Sublet. Only the owner can edit it.
destroy:
Delete a Sublet.
"""
Expand All @@ -88,7 +84,6 @@ def get_queryset(self):
# new_data = request.data
# amenities = new_data.pop("amenities", [])


# # check if valid amenities
# try:
# amenities = [Amenity.objects.get(name=amenity) for amenity in amenities]
Expand All @@ -100,7 +95,7 @@ def get_queryset(self):
# sublet = serializer.save()
# sublet.amenities.set(amenities)
# sublet.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)
# return Response(serializer.data, status=status.HTTP_201_CREATED)

def list(self, request, *args, **kwargs):
"""Returns a list of Sublets that match query parameters and user ownership."""
Expand Down Expand Up @@ -158,7 +153,6 @@ def get_queryset(self):

def create(self, request, *args, **kwargs):
sublet_id = int(self.kwargs["sublet_id"])
user = self.request.user.id
sublet = get_object_or_404(Sublet, id=sublet_id)
# add validation
self.get_queryset().add(sublet)
Expand Down
38 changes: 18 additions & 20 deletions backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import datetime
import json
from unittest import mock

from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase
from rest_framework import status
from django.utils import timezone
from rest_framework.test import APIClient

from sublet.models import Amenity, Offer, Sublet, SubletImage
from sublet.models import Amenity, Offer, Sublet


# , SubletImage)


User = get_user_model()

Expand All @@ -26,8 +25,8 @@ def setUp(self):
Amenity.objects.create(name=f"Amenity{str(i)}")
with open("tests/sublet/mock_sublets.json") as data:
data = json.load(data)
sublet1 = Sublet.objects.create(subletter=self.user, **data[0])
sublet2 = Sublet.objects.create(subletter=test_user, **data[1])
Sublet.objects.create(subletter=self.user, **data[0])
Sublet.objects.create(subletter=test_user, **data[1])

def test_create_sublet(self):
# Create a new sublet using the serializer
Expand Down Expand Up @@ -101,15 +100,14 @@ def test_browse_sublets(self):
}
self.client.post("/sublet/properties/", payload)
response = self.client.get("/sublet/properties/")
self.assertEqual(1+first_length, len(json.loads(response.content)))
self.assertEqual(1 + first_length, len(json.loads(response.content)))
sublet = res_json[-1]
self.assertEqual(sublet["title"], "Test Sublet1")
self.assertEqual(sublet["address"], "1234 Test Street")
self.assertEqual(sublet["beds"], 2)
self.assertEqual(sublet["baths"], 1)
self.assertIsNotNone(sublet["created_date"])


def test_browse_sublet(self):
# browse single sublet by id
pass
Expand All @@ -131,8 +129,8 @@ def setUp(self):
# TODO: Not sure how to add these amenities to the sublets, but not important for now
with open("tests/sublet/mock_sublets.json") as data:
data = json.load(data)
sublet1 = Sublet.objects.create(subletter=self.user, **data[0])
sublet2 = Sublet.objects.create(subletter=test_user, **data[1])
Sublet.objects.create(subletter=self.user, **data[0])
Sublet.objects.create(subletter=test_user, **data[1])

def test_create_offer(self):
payload = {
Expand Down Expand Up @@ -268,31 +266,31 @@ def setUp(self):
# TODO: Not sure how to add these amenities to the sublets, but not important for now
with open("tests/sublet/mock_sublets.json") as data:
data = json.load(data)
sublet1 = Sublet.objects.create(subletter=self.user, **data[0])
sublet2 = Sublet.objects.create(subletter=test_user, **data[1])
Sublet.objects.create(subletter=self.user, **data[0])
Sublet.objects.create(subletter=test_user, **data[1])

def test_create_favorite(self):
response = self.client.post("/sublet/properties/2/favorites/")
self.client.post("/sublet/properties/2/favorites/")
self.assertTrue(self.user.sublets_favorited.filter(pk=2).exists())
self.assertFalse(self.user.sublets_favorited.filter(pk=1).exists())
# TODO: Write case for erroring out on already favorited once that's been implemented
response = self.client.post("/sublet/properties/1/favorites/")
self.client.post("/sublet/properties/1/favorites/")
self.assertTrue(self.user.sublets_favorited.filter(pk=2).exists())
self.assertTrue(self.user.sublets_favorited.filter(pk=1).exists())

def test_delete_favorite(self):
self.client.post("/sublet/properties/2/favorites/")
self.client.post("/sublet/properties/1/favorites/")
response = self.client.delete("/sublet/properties/1/favorites/")
self.client.delete("/sublet/properties/1/favorites/")
self.assertTrue(self.user.sublets_favorited.filter(pk=2).exists())
self.assertFalse(self.user.sublets_favorited.filter(pk=1).exists())
response = self.client.post("/sublet/properties/1/favorites/")
self.client.post("/sublet/properties/1/favorites/")
self.assertTrue(self.user.sublets_favorited.filter(pk=2).exists())
self.assertTrue(self.user.sublets_favorited.filter(pk=1).exists())
response = self.client.delete("/sublet/properties/1/favorites/")
self.client.delete("/sublet/properties/1/favorites/")
self.assertTrue(self.user.sublets_favorited.filter(pk=2).exists())
self.assertFalse(self.user.sublets_favorited.filter(pk=1).exists())
response = self.client.delete("/sublet/properties/2/favorites/")
self.client.delete("/sublet/properties/2/favorites/")
self.assertFalse(self.user.sublets_favorited.filter(pk=2).exists())
self.assertFalse(self.user.sublets_favorited.filter(pk=1).exists())
# TODO: Cases for proper error handling on unfound delete
Expand Down

0 comments on commit 4c98ef8

Please sign in to comment.