Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions api/authentication/apis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from rest_framework import permissions, serializers, status
from rest_framework.response import Response
from rest_framework.views import APIView

from api.authentication.selectors import validate_phone
from api.common.validators import is_phone


class ValidatePhoneAPI(APIView):
permission_classes = [permissions.AllowAny]

class InputSerializer(serializers.Serializer):
phone = serializers.CharField(validators=[is_phone])

def post(self, request):
serializer = self.InputSerializer(data=request.data)
serializer.is_valid(raise_exception=True)

# Raises validation error if phone is taken
validate_phone(**serializer.validated_data)

return Response(status=status.HTTP_200_OK)
12 changes: 12 additions & 0 deletions api/authentication/selectors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Union

from rest_framework.exceptions import ValidationError

from api.users.models import User


def validate_phone(*, phone: str) -> Union[None, ValidationError]:
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure that we include exceptions in the function type hint

Copy link
Member Author

Choose a reason for hiding this comment

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

No problem, other than that all good? In terms of following the style guide

if User.objects.filter(username=phone).exists():
raise ValidationError(f"Phone number: {phone} already taken")

return None
1 change: 0 additions & 1 deletion api/authentication/tests.py

This file was deleted.

3 changes: 3 additions & 0 deletions api/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
TokenVerifyView,
)

from api.authentication.apis import ValidatePhoneAPI

app_name = "auth"
urlpatterns = [
path("token/", TokenObtainPairView.as_view(), name="obtain_token"),
path("token/refresh/", TokenRefreshView.as_view(), name="refresh_token"),
path("token/verify/", TokenVerifyView.as_view(), name="verify_token"),
path("phone/validate/", ValidatePhoneAPI.as_view(), name="validate_phone"),
]
1 change: 0 additions & 1 deletion api/authentication/views.py

This file was deleted.

File renamed without changes.
4 changes: 2 additions & 2 deletions api/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Django 3.2.13 on 2022-04-19 22:39

import api.users.validators
import api.common.validators
import django.contrib.auth.models
from django.db import migrations, models
import django.db.models.deletion
Expand Down Expand Up @@ -29,7 +29,7 @@ class Migration(migrations.Migration):
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('name', models.CharField(max_length=256)),
('email', models.EmailField(blank=True, max_length=254, null=True)),
('username', models.CharField(max_length=10, unique=True, validators=[api.users.validators.is_phone])),
('username', models.CharField(max_length=10, unique=True, validators=[api.common.validators.is_phone])),
('id_exp_date', models.DateTimeField(blank=True, null=True)),
('id_photo_url', models.ImageField(blank=True, upload_to='id-photos/')),
('firebase_token', models.CharField(blank=True, max_length=256, unique=True)),
Expand Down
2 changes: 1 addition & 1 deletion api/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from django.urls import reverse
from django.utils import timezone

from api.common.validators import is_phone
from api.locations.models import Location
from api.users.validators import is_phone


class User(AbstractUser):
Expand Down