Skip to content

Commit

Permalink
Add phone number validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-Jess committed Oct 29, 2023
1 parent 8316664 commit ff963f3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 24 deletions.
3 changes: 3 additions & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ dj-database-url = "*"
djangorestframework = "*"
pandas = "*"
html5lib = "*"
# comment out psycopg2 for apple silicon
psycopg2 = "*"
sentry-sdk = "*"
django = "*"
django-cors-headers = "*"
pyyaml = "*"
uritemplate = "*"
# comment out uwsgi for apple silicon
uwsgi = "*"
django-filter = "*"
django-labs-accounts = "*"
django-debug-toolbar = "*"
django-runtime-options = "*"
django-storages = "*"
django-phonenumber-field = {extras = ["phonenumberslite"],version = "*"}
pillow = "*"
boto3 = "*"
apns2 = "*"
Expand Down
1 change: 1 addition & 0 deletions backend/pennmobile/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"portal",
"options.apps.OptionsConfig",
"sublet",
"phonenumber_field",
]

MIDDLEWARE = [
Expand Down
27 changes: 17 additions & 10 deletions backend/sublet/migrations/0002_auto_20231029_1258.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@ class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('sublet', '0001_initial'),
("sublet", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name='sublet',
name='amenities',
field=models.ManyToManyField(blank=True, to='sublet.Amenity'),
model_name="sublet",
name="amenities",
field=models.ManyToManyField(blank=True, to="sublet.Amenity"),
),
migrations.AlterField(
model_name='sublet',
name='favorites',
field=models.ManyToManyField(blank=True, related_name='sublets_favorited', to=settings.AUTH_USER_MODEL),
model_name="sublet",
name="favorites",
field=models.ManyToManyField(
blank=True, related_name="sublets_favorited", to=settings.AUTH_USER_MODEL
),
),
migrations.AlterField(
model_name='sublet',
name='sublettees',
field=models.ManyToManyField(blank=True, related_name='sublets_offered', through='sublet.Offer', to=settings.AUTH_USER_MODEL),
model_name="sublet",
name="sublettees",
field=models.ManyToManyField(
blank=True,
related_name="sublets_offered",
through="sublet.Offer",
to=settings.AUTH_USER_MODEL,
),
),
]
21 changes: 21 additions & 0 deletions backend/sublet/migrations/0003_alter_offer_phone_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.22 on 2023-10-29 17:24

import phonenumber_field.modelfields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("sublet", "0002_auto_20231029_1258"),
]

operations = [
migrations.AlterField(
model_name="offer",
name="phone_number",
field=phonenumber_field.modelfields.PhoneNumberField(
blank=True, max_length=128, null=True, region=None
),
),
]
3 changes: 2 additions & 1 deletion backend/sublet/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.auth import get_user_model
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField


User = get_user_model()
Expand All @@ -13,7 +14,7 @@ class Meta:
sublet = models.ForeignKey("Sublet", on_delete=models.CASCADE, related_name="offers")
# TODO: Make sure phone_number is being validated by serializers/frontend
email = models.EmailField(max_length=255, null=True, blank=True)
phone_number = models.CharField(max_length=20, null=True, blank=True)
phone_number = PhoneNumberField(null=True, blank=True)

This comment has been minimized.

Copy link
@tuneerroy

tuneerroy Oct 29, 2023

Contributor

nice :D

message = models.CharField(max_length=255, blank=True)
created_date = models.DateTimeField(auto_now_add=True)

Expand Down
3 changes: 3 additions & 0 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from phonenumber_field.serializerfields import PhoneNumberField
from rest_framework import serializers

from sublet.models import Amenity, Offer, Sublet
Expand All @@ -10,6 +11,8 @@ class Meta:


class OfferSerializer(serializers.ModelSerializer):
phone_number = PhoneNumberField()

class Meta:
model = Offer
fields = "__all__"
Expand Down
9 changes: 6 additions & 3 deletions backend/tests/sublet/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def create_create_offer(self):
prop_url = f"/sublet/properties/{str(self.sublet1.id)}/offers/"
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
users = [self.admin, self.user1]
Expand All @@ -53,7 +54,8 @@ def test_delete_offer(self):
prop_url = f"/sublet/properties/{str(self.sublet1.id)}/offers/"
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
users = [self.admin, self.user1]
Expand All @@ -66,7 +68,8 @@ def test_get_offers_property(self):
prop_url = f"/sublet/properties/{str(self.sublet2.id)}/offers/"
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
users = [self.admin, self.user1, self.user2]
Expand Down
26 changes: 16 additions & 10 deletions backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def test_create_offer(self):
prop_url = f"/sublet/properties/{str(self.second_sublet.id)}/offers/"
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
self.client.post(prop_url, payload)
Expand All @@ -190,7 +191,8 @@ def test_delete_offer(self):
prop_url2 = f"/sublet/properties/{str(self.second_sublet.id)}/offers/"
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
self.client.post(prop_url2, payload)
Expand All @@ -207,7 +209,8 @@ def test_get_offers_property(self):
self.assertEqual(0, len(res_json))
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
self.client.post(f"/sublet/properties/{str(self.first_sublet.id)}/offers/", payload)
Expand All @@ -217,7 +220,7 @@ def test_get_offers_property(self):
user=self.test_user,
sublet=self.first_sublet,
email="[email protected]",
phone_number="0987654321",
phone_number="+12155733334",
message="Message2",
)
response = self.client.get(f"/sublet/properties/{str(self.first_sublet.id)}/offers/")
Expand All @@ -226,15 +229,16 @@ def test_get_offers_property(self):
# TODO: this is really ugly, maybe clean up later haha
offer = res_json[0]
self.assertEqual(offer["email"], "[email protected]")
self.assertEqual(offer["phone_number"], "1234567890")
# This is the MERT number, please DO NOT call ;-;
self.assertEqual(offer["phone_number"], "+12155733333")
self.assertEqual(offer["message"], "Message")
self.assertEqual(offer["user"], self.user.id)
self.assertEqual(offer["sublet"], self.first_sublet.id)
self.assertIsNotNone(offer["id"])
self.assertIsNotNone(offer["created_date"])
offer = res_json[1]
self.assertEqual(offer["email"], "[email protected]")
self.assertEqual(offer["phone_number"], "0987654321")
self.assertEqual(offer["phone_number"], "+12155733334")
self.assertEqual(offer["message"], "Message2")
self.assertEqual(offer["user"], self.test_user.id)
self.assertEqual(offer["sublet"], self.first_sublet.id)
Expand All @@ -247,15 +251,16 @@ def test_get_offer_user(self):
self.assertEqual(0, len(res_json))
payload = {
"email": "[email protected]",
"phone_number": "1234567890",
# This is the MERT number, please DO NOT call ;-;
"phone_number": "+12155733333",
"message": "Message",
}
self.client.post(f"/sublet/properties/{str(self.first_sublet.id)}/offers/", payload)
response = self.client.get("/sublet/offers/")
self.assertEqual(1, len(json.loads(response.content)))
payload = {
"email": "[email protected]",
"phone_number": "0987654321",
"phone_number": "+12155733334",
"message": "Message2",
}
self.client.post(f"/sublet/properties/{str(self.second_sublet.id)}/offers/", payload)
Expand All @@ -264,15 +269,16 @@ def test_get_offer_user(self):
self.assertEqual(2, len(res_json))
offer = res_json[0]
self.assertEqual(offer["email"], "[email protected]")
self.assertEqual(offer["phone_number"], "1234567890")
# This is the MERT number, please DO NOT call ;-;
self.assertEqual(offer["phone_number"], "+12155733333")
self.assertEqual(offer["message"], "Message")
self.assertEqual(offer["user"], self.user.id)
self.assertEqual(offer["sublet"], self.first_sublet.id)
self.assertIsNotNone(offer["id"])
self.assertIsNotNone(offer["created_date"])
offer = res_json[1]
self.assertEqual(offer["email"], "[email protected]")
self.assertEqual(offer["phone_number"], "0987654321")
self.assertEqual(offer["phone_number"], "+12155733334")
self.assertEqual(offer["message"], "Message2")
self.assertEqual(offer["user"], self.user.id)
self.assertEqual(offer["sublet"], self.second_sublet.id)
Expand Down

0 comments on commit ff963f3

Please sign in to comment.