Added user registration
This commit is contained in:
+1
-1
@@ -15,4 +15,4 @@ WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
CMD ["sh", "-c", "cd pulse && exec python3 manage.py runserver 0.0.0.0:$SERVER_PORT"]
|
||||
CMD ["sh", "-c", "cd pulse && python3 manage.py migrate && exec python3 manage.py runserver 0.0.0.0:$SERVER_PORT"]
|
||||
|
||||
@@ -5,7 +5,7 @@ import countries.views
|
||||
urlpatterns = [
|
||||
path("", countries.views.CountryListView.as_view(), name="countries"),
|
||||
path(
|
||||
"<str:alpha2>/",
|
||||
"/<str:alpha2>",
|
||||
countries.views.CountryByAlpha2View.as_view(),
|
||||
name="country_by_alpha2",
|
||||
),
|
||||
|
||||
@@ -28,13 +28,13 @@ INSTALLED_APPS = [
|
||||
# Developed apps
|
||||
"ping.apps.PingConfig",
|
||||
"countries.apps.CountriesConfig",
|
||||
"users.apps.UsersConfig",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
"django.middleware.security.SecurityMiddleware",
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||
@@ -115,6 +115,8 @@ REST_FRAMEWORK = {
|
||||
]
|
||||
}
|
||||
|
||||
APPEND_SLASH = False
|
||||
|
||||
if DEBUG:
|
||||
INSTALLED_APPS.insert(0, "debug_toolbar")
|
||||
MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
|
||||
|
||||
@@ -13,8 +13,9 @@ urlpatterns = [
|
||||
),
|
||||
),
|
||||
# API
|
||||
path("api/ping/", include("ping.urls")),
|
||||
path("api/countries/", include("countries.urls")),
|
||||
path("api/ping", include("ping.urls")),
|
||||
path("api/countries", include("countries.urls")),
|
||||
path("api/auth/", include("users.urls")),
|
||||
]
|
||||
|
||||
if settings.DEBUG:
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UsersConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "users"
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 4.2.10 on 2024-02-29 14:37
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Profile',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('login', models.CharField(max_length=30, validators=[django.core.validators.RegexValidator('[a-zA-Z0-9-]+')])),
|
||||
('email', models.EmailField(max_length=50)),
|
||||
('password', models.CharField(max_length=100, validators=[django.core.validators.MinLengthValidator(6), django.core.validators.MaxLengthValidator(100), django.core.validators.RegexValidator('^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).+$')])),
|
||||
('countryCode', models.CharField(max_length=2, validators=[django.core.validators.RegexValidator('[a-zA-Z]{2}')])),
|
||||
('isPublic', models.BooleanField()),
|
||||
('phone', models.CharField(blank=True, max_length=20, null=True, validators=[django.core.validators.RegexValidator('\\+[\\d]+')])),
|
||||
('image', models.URLField(blank=True, null=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,38 @@
|
||||
from django.core.validators import (
|
||||
MaxLengthValidator,
|
||||
MinLengthValidator,
|
||||
RegexValidator,
|
||||
)
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Profile(models.Model):
|
||||
login = models.CharField(
|
||||
max_length=30,
|
||||
validators=[RegexValidator(r"[a-zA-Z0-9-]+")],
|
||||
)
|
||||
email = models.EmailField(max_length=50)
|
||||
password = models.CharField(
|
||||
max_length=100,
|
||||
validators=[
|
||||
MinLengthValidator(6),
|
||||
MaxLengthValidator(100),
|
||||
RegexValidator(r"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).+$"),
|
||||
],
|
||||
)
|
||||
# ruff: noqa: DJ001 N815
|
||||
countryCode = models.CharField(
|
||||
max_length=2,
|
||||
validators=[RegexValidator(r"[a-zA-Z]{2}")],
|
||||
)
|
||||
isPublic = models.BooleanField()
|
||||
phone = models.CharField(
|
||||
max_length=20,
|
||||
validators=[RegexValidator(r"\+[\d]+")],
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
image = models.URLField(max_length=200, blank=True, null=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.login
|
||||
@@ -0,0 +1,19 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from users.models import Profile
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
password = serializers.CharField(write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = [
|
||||
"login",
|
||||
"email",
|
||||
"password",
|
||||
"countryCode",
|
||||
"isPublic",
|
||||
"phone",
|
||||
"image",
|
||||
]
|
||||
@@ -0,0 +1,11 @@
|
||||
from django.urls import path
|
||||
|
||||
import users.views
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
"register",
|
||||
users.views.register_user,
|
||||
name="register",
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,87 @@
|
||||
import re
|
||||
|
||||
import bcrypt
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
|
||||
from users.models import Profile
|
||||
from users.serializers import UserSerializer
|
||||
|
||||
MIN_PASSWORD_LEN = 6
|
||||
MAX_PASSWORD_LEN = 100
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
def register_user(request):
|
||||
serializer = UserSerializer(data=request.data)
|
||||
|
||||
if serializer.is_valid():
|
||||
if (
|
||||
Profile.objects.filter(
|
||||
login=serializer.validated_data["login"]
|
||||
).first()
|
||||
is not None
|
||||
):
|
||||
return Response(
|
||||
{"error": "User with this login already exists"},
|
||||
status=status.HTTP_409_CONFLICT,
|
||||
)
|
||||
if (
|
||||
Profile.objects.filter(
|
||||
email=serializer.validated_data["email"]
|
||||
).first()
|
||||
is not None
|
||||
):
|
||||
return Response(
|
||||
{"error": "User with this email already exists"},
|
||||
status=status.HTTP_409_CONFLICT,
|
||||
)
|
||||
if (
|
||||
Profile.objects.filter(
|
||||
phone=serializer.validated_data["phone"]
|
||||
).first()
|
||||
is not None
|
||||
):
|
||||
return Response(
|
||||
{"error": "User with this phone already exists"},
|
||||
status=status.HTTP_409_CONFLICT,
|
||||
)
|
||||
|
||||
password = serializer.validated_data["password"]
|
||||
password_pattern = re.compile(
|
||||
r"^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,100}$"
|
||||
)
|
||||
|
||||
if not (
|
||||
bool(re.match(password_pattern, password))
|
||||
):
|
||||
error = {"message": "Your password does not meet our requirements"}
|
||||
return Response(
|
||||
error,
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
password_hash = bcrypt.hashpw(
|
||||
password.encode("utf-8"), bcrypt.gensalt()
|
||||
).decode("utf-8")
|
||||
serializer.validated_data["password"] = password_hash
|
||||
|
||||
user = serializer.save()
|
||||
|
||||
profile = {
|
||||
"profile": {
|
||||
"login": user.login,
|
||||
"email": user.email,
|
||||
"countryCode": user.countryCode,
|
||||
"isPublic": user.isPublic,
|
||||
}
|
||||
}
|
||||
if user.phone is not None:
|
||||
profile["profile"]["phone"] = user.phone
|
||||
if user.image is not None:
|
||||
profile["profile"]["image"] = user.image
|
||||
|
||||
return Response(profile, status=status.HTTP_201_CREATED)
|
||||
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
@@ -4,3 +4,4 @@ python-dotenv==1.0.1
|
||||
psycopg2-binary==2.9.9
|
||||
dj-database-url==2.1.0
|
||||
django-filter==23.5
|
||||
bcrypt==4.1.2
|
||||
|
||||
@@ -2,8 +2,6 @@ DJANGO_DEBUG = False
|
||||
SECRET_KEY = secret_key
|
||||
DJANGO_ALLOWED_HOSTS = 127.0.0.1
|
||||
INTERNAL_IPS = 127.0.0.1
|
||||
POSTGRES_CONN = postgres://login:pass@localhost:5432/postgres
|
||||
POSTGRES_JDBC_URL = jdbc:postgresql://host:port/dbname
|
||||
POSTGRES_USERNAME = postgres
|
||||
POSTGRES_PASSWORD = password
|
||||
POSTGRES_HOST = localhost
|
||||
|
||||
Reference in New Issue
Block a user