Skip to content

Commit 7ef8d28

Browse files
Merge pull request #139 from jettysaivarun/11-backenddrf-create-appsauthentication-django-app-structure
/api/auth.register is implemented
2 parents 4f5b6b2 + 894f091 commit 7ef8d28

8 files changed

Lines changed: 59 additions & 10 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from rest_framework import serializers
2+
from django.contrib.auth.models import User
3+
4+
5+
class RegisterSerializer(serializers.ModelSerializer):
6+
username = serializers.CharField(validators=[])
7+
password = serializers.CharField(write_only=True)
8+
9+
class Meta:
10+
model = User
11+
fields = ["username", "email", "password"]
12+
13+
def validate_username(self, value):
14+
if not all(char.isalnum() or char == "_" for char in value):
15+
raise serializers.ValidationError(
16+
"Username should contain only alpha,numerics and underscore"
17+
)
18+
19+
if User.objects.filter(username=value).exists():
20+
raise serializers.ValidationError("Username already exists")
21+
return value
22+
23+
def create(self, validated_data):
24+
user = User.objects.create_user(
25+
username=validated_data["username"],
26+
email=validated_data["email"],
27+
password=validated_data["password"],
28+
)
29+
return user
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
path("register/", views.RegisterationView.as_view(), name="register"),
6+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from rest_framework import generics
2+
from django.contrib.auth.models import User
3+
from .serializers import RegisterSerializer
4+
5+
6+
class RegisterationView(generics.CreateAPIView):
7+
queryset = User.objects.all()
8+
serializer_class = RegisterSerializer
-182 Bytes
Binary file not shown.
-2.49 KB
Binary file not shown.

backend/config/settings.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
from pathlib import Path
14-
from os import os
14+
import os
1515

1616
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1717
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -38,11 +38,8 @@
3838
"django.contrib.sessions",
3939
"django.contrib.messages",
4040
"django.contrib.staticfiles",
41+
"rest_framework",
4142
"apps.authentication",
42-
"apps.catalog",
43-
"apps.orders",
44-
"apps.profiles",
45-
"apps.sellers",
4643
]
4744

4845
MIDDLEWARE = [

backend/config/urls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
urlpatterns = [
2222
path("admin/", admin.site.urls),
23-
path("authentication/", include("apps.authentication.urls")),
24-
path("catalog/", include("apps.catalog.urls")),
25-
path("order/", include("apps.orders.urls")),
26-
path("profile/", include("apps.profiles.urls")),
27-
path("seller/", include("apps.sellers.urls")),
23+
path("api/auth/", include("apps.authentication.urls")),
24+
# path("catalog/", include("apps.catalog.urls")),
25+
# path("order/", include("apps.orders.urls")),
26+
# path("profile/", include("apps.profiles.urls")),
27+
# path("seller/", include("apps.sellers.urls")),
2828
]

backend/docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
db:
3+
image: postgres:16
4+
environment:
5+
POSTGRES_DB: fashio
6+
POSTGRES_USER: postgres
7+
POSTGRES_PASSWORD: postgres
8+
ports:
9+
- "5432:5432"

0 commit comments

Comments
 (0)