Archived
feat: uv config other feats
- add uv configuration for the backend - update frontend to make auth work - add new auth endpoints - add bookmars feat - add reader feat
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# Generated by Django 5.1.7 on 2026-06-03 22:26
|
||||
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
import django.utils.timezone
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0012_alter_user_first_name_max_length'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
|
||||
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
|
||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
|
||||
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
|
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
|
||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'User',
|
||||
'verbose_name_plural': 'Users',
|
||||
'db_table': 'users_user',
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,93 @@
|
||||
import re
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.password_validation import validate_password
|
||||
from rest_framework import serializers
|
||||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def _derive_username(email: str) -> str:
|
||||
local = email.split("@", 1)[0]
|
||||
candidate = re.sub(r"[^\w.@+-]", "_", local).strip("._")
|
||||
return candidate[:150] if candidate else "user"
|
||||
|
||||
|
||||
def _unique_username(base: str) -> str:
|
||||
username = base[:150]
|
||||
if not User.objects.filter(username=username).exists():
|
||||
return username
|
||||
suffix = 1
|
||||
while User.objects.filter(username=f"{username[:140]}_{suffix}").exists():
|
||||
suffix += 1
|
||||
return f"{username[:140]}_{suffix}"
|
||||
|
||||
|
||||
class RegisterSerializer(serializers.Serializer):
|
||||
email = serializers.EmailField()
|
||||
password = serializers.CharField(write_only=True, min_length=8)
|
||||
|
||||
def validate_email(self, value: str) -> str:
|
||||
email = value.lower()
|
||||
if User.objects.filter(email__iexact=email).exists():
|
||||
raise serializers.ValidationError("A user with this email already exists.")
|
||||
return email
|
||||
|
||||
def validate_password(self, value: str) -> str:
|
||||
validate_password(value)
|
||||
return value
|
||||
|
||||
def create(self, validated_data: dict) -> User:
|
||||
email = validated_data["email"]
|
||||
username = _unique_username(_derive_username(email))
|
||||
return User.objects.create_user(
|
||||
username=username,
|
||||
email=email,
|
||||
password=validated_data["password"],
|
||||
)
|
||||
|
||||
def to_representation(self, instance: User) -> dict:
|
||||
return {
|
||||
"id": instance.id,
|
||||
"email": instance.email,
|
||||
"username": instance.username,
|
||||
}
|
||||
|
||||
|
||||
class EmailTokenObtainPairSerializer(TokenObtainPairSerializer):
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields.pop(self.username_field, None)
|
||||
self.fields["email"] = serializers.EmailField(required=True)
|
||||
|
||||
def validate(self, attrs: dict) -> dict:
|
||||
email = attrs.get("email", "").lower()
|
||||
password = attrs.get("password")
|
||||
|
||||
try:
|
||||
user = User.objects.get(email__iexact=email)
|
||||
except User.DoesNotExist as exc:
|
||||
raise serializers.ValidationError(
|
||||
{"detail": "No active account found with the given credentials."}
|
||||
) from exc
|
||||
|
||||
if not user.check_password(password):
|
||||
raise serializers.ValidationError(
|
||||
{"detail": "No active account found with the given credentials."}
|
||||
)
|
||||
|
||||
if not user.is_active:
|
||||
raise serializers.ValidationError({"detail": "User account is disabled."})
|
||||
|
||||
refresh = self.get_token(user)
|
||||
return {
|
||||
"refresh": str(refresh),
|
||||
"access": str(refresh.access_token),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_token(cls, user: User) -> object:
|
||||
token = super().get_token(user)
|
||||
token["email"] = user.email
|
||||
return token
|
||||
@@ -1,8 +1,11 @@
|
||||
from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
|
||||
from django.urls import path
|
||||
from rest_framework_simplejwt.views import TokenRefreshView
|
||||
|
||||
from apps.users.views import EmailTokenObtainPairView, RegisterView
|
||||
|
||||
urlpatterns = [
|
||||
path("token/", TokenObtainPairView.as_view(), name="token_obtain_pair"),
|
||||
path("register/", RegisterView.as_view(), name="register"),
|
||||
path("token/", EmailTokenObtainPairView.as_view(), name="token_obtain_pair"),
|
||||
path("login/", EmailTokenObtainPairView.as_view(), name="login"),
|
||||
path("token/refresh/", TokenRefreshView.as_view(), name="token_refresh"),
|
||||
]
|
||||
]
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
from rest_framework import generics
|
||||
from rest_framework.permissions import AllowAny
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
|
||||
from apps.users.serializers import EmailTokenObtainPairSerializer, RegisterSerializer
|
||||
|
||||
|
||||
class RegisterView(generics.CreateAPIView):
|
||||
serializer_class = RegisterSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
|
||||
class EmailTokenObtainPairView(TokenObtainPairView):
|
||||
serializer_class = EmailTokenObtainPairSerializer
|
||||
Reference in New Issue
Block a user