initial commit

This commit is contained in:
behruz-dev
2025-08-26 10:12:09 +05:00
commit 8feea731b9
55 changed files with 1066 additions and 0 deletions

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core.apps.accounts'

View File

@@ -0,0 +1,30 @@
import redis
r = redis.StrictRedis.from_url('redis://redis:6379')
def cache_user_credentials(email, password, passport_id, pnlf, time):
key = f"user_credentials:{email}"
r.hmset(key, {
"email": email,
"password": password,
"passport_id": passport_id,
"pnlf": pnlf,
})
r.expire(key, time)
def get_user_credentials(email):
key = f"user_credentials:{email}"
data = r.hgetall(key)
if not data:
return None
return {
"email": data.get(b'email').decode() if data.get(b'email') else None,
"password": data.get(b'password').decode() if data.get(b'password') else None,
"passport_id": data.get(b'passport_id').decode() if data.get(b'passport_id') else None,
"pnlf": data.get(b'pnlf').decode() if data.get(b'pnlf') else None,
}

View File

@@ -0,0 +1,33 @@
from django.contrib.auth.models import UserManager as DjangoUserManager
from django.contrib.auth.hashers import make_password
class UserManager(DjangoUserManager):
def _create_user_object(self, email, password, **extra_fields):
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.password = make_password(password)
return user
def _create_user(self, email, password, **extra_fields):
user = self._create_user_object(email, password, **extra_fields)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
return self._create_user(email, password, **extra_fields)

View File

@@ -0,0 +1,42 @@
# Generated by Django 5.2 on 2025-08-25 16:14
import django.contrib.auth.models
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=[
('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')),
('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')),
('id', models.UUIDField(db_index=True, editable=False, primary_key=True, serialize=False, unique=True)),
('created_at', models.DateField(auto_now_add=True)),
('updated_at', models.DateField(auto_now=True)),
('full_name', models.CharField(max_length=200)),
('email', models.EmailField(max_length=254, unique=True)),
('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': 'foydalanuvchi',
'verbose_name_plural': 'foydalanuvchilar',
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]

View File

@@ -0,0 +1,20 @@
# Generated by Django 5.2 on 2025-08-25 16:18
import core.apps.accounts.manager
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.AlterModelManagers(
name='user',
managers=[
('objects', core.apps.accounts.manager.UserManager()),
],
),
]

View File

@@ -0,0 +1,29 @@
# Generated by Django 5.2 on 2025-08-25 16:30
import uuid
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('accounts', '0002_alter_user_managers'),
]
operations = [
migrations.AddField(
model_name='user',
name='passport_id',
field=models.CharField(max_length=20, null=True),
),
migrations.AddField(
model_name='user',
name='pnfl',
field=models.CharField(max_length=20, null=True),
),
migrations.AlterField(
model_name='user',
name='id',
field=models.UUIDField(db_index=True, default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True),
),
]

View File

@@ -0,0 +1,27 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
from core.apps.common.models import BaseModel
from core.apps.accounts.manager import UserManager
class User(AbstractUser, BaseModel):
full_name = models.CharField(max_length=200)
email = models.EmailField(unique=True)
passport_id = models.CharField(max_length=20, null=True)
pnfl = models.CharField(max_length=20, null=True)
first_name = None
last_name = None
username = None
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
class Meta:
verbose_name = 'foydalanuvchi'
verbose_name_plural = 'foydalanuvchilar'

View File

@@ -0,0 +1,20 @@
from django.db import transaction
from rest_framework import serializers
from core.apps.accounts.models import User
from core.apps.accounts.cache import get_user_credentials
class RegisterSerializer(serializers.ModelSerializer):
passport_id = serializers.CharField()
pnfl = serializers.CharField()
email = serializers.EmailField()
password = serializers.CharField()
def validate_email(self, value):
if User.objects.filter(email=value).exists():
raise serializers.ValidationError("User with this email already exists")
if get_user_credentials(value):
raise serializers.ValidationError("User with this email already exists")
return value

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,8 @@
from django.urls import path, include
from rest_framework_simplejwt.views import TokenObtainPairView
urlpatterns = [
path('login/', TokenObtainPairView.as_view()),
]

View File

@@ -0,0 +1,23 @@
from rest_framework import generics, views
from rest_framework.response import Response
from core.apps.accounts import serializers
from core.apps.accounts import models
from core.apps.accounts.cache import cache_user_credentials
class RegisterApiView(generics.GenericAPIView):
serializer_class = serializers.RegisterSerializer
queryset = models.User.objects.all()
def post(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid(raise_exception=True):
data = serializer.validated_data
cache_user_credentials(
email=data['email'], password=data['password'],
passport_id=data['passport_id'], pnlf=data['pnlf'], time=60*5
)
return Response(
{'success': True, 'message': "code sent"},
status=200
)

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
core/apps/common/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CommonConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core.apps.common'

View File

View File

@@ -0,0 +1,11 @@
import uuid
from django.db import models
class BaseModel(models.Model):
id = models.UUIDField(primary_key=True, editable=False, unique=True, db_index=True, default=uuid.uuid4)
created_at = models.DateField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
class Meta:
abstract = True

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
core/apps/orders/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class OrdersConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'core.apps.orders'

View File

View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.