kop narsalar qoshildi
This commit is contained in:
0
core/apps/accounts/__init__.py
Normal file
0
core/apps/accounts/__init__.py
Normal file
1
core/apps/accounts/admin/__init__.py
Normal file
1
core/apps/accounts/admin/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .user import *
|
||||
14
core/apps/accounts/admin/user.py
Normal file
14
core/apps/accounts/admin/user.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class UserAdmin(admin.ModelAdmin):
|
||||
list_display = ['telegram_id', 'first_name', 'last_name', 'is_active']
|
||||
search_fields = ['telegram_id', 'first_name', 'last_name']
|
||||
|
||||
|
||||
admin.site.unregister(Group)
|
||||
9
core/apps/accounts/apps.py
Normal file
9
core/apps/accounts/apps.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core.apps.accounts'
|
||||
|
||||
def ready(self):
|
||||
import core.apps.accounts.admin
|
||||
47
core/apps/accounts/migrations/0001_initial.py
Normal file
47
core/apps/accounts/migrations/0001_initial.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Generated by Django 5.2 on 2025-11-21 12:23
|
||||
|
||||
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')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('telegram_id', models.CharField(max_length=200, null=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': 'user',
|
||||
'verbose_name_plural': 'users',
|
||||
'abstract': False,
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,25 @@
|
||||
# Generated by Django 5.2 on 2025-11-21 12:35
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('accounts', '0001_initial'),
|
||||
('shared', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='user',
|
||||
name='region',
|
||||
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='regions', to='shared.region'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='user',
|
||||
name='is_active',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
0
core/apps/accounts/migrations/__init__.py
Normal file
0
core/apps/accounts/migrations/__init__.py
Normal file
1
core/apps/accounts/models/__init__.py
Normal file
1
core/apps/accounts/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .user import *
|
||||
16
core/apps/accounts/models/user.py
Normal file
16
core/apps/accounts/models/user.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import BaseModel, Region
|
||||
|
||||
|
||||
class User(AbstractUser, BaseModel):
|
||||
telegram_id = models.CharField(max_length=200, null=True)
|
||||
region = models.ForeignKey(Region, on_delete=models.SET_NULL, related_name='regions', null=True)
|
||||
is_active = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.first_name} {self.last_name} - {self.telegram_id}"
|
||||
|
||||
|
||||
41
core/apps/accounts/serializers/user.py
Normal file
41
core/apps/accounts/serializers/user.py
Normal file
@@ -0,0 +1,41 @@
|
||||
# django
|
||||
from django.db import transaction
|
||||
|
||||
# rest framework
|
||||
from rest_framework import serializers
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
# shared
|
||||
from core.apps.shared.models import Region
|
||||
|
||||
|
||||
class UserCreateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
'first_name', 'last_name', 'telegram_id', 'region'
|
||||
]
|
||||
|
||||
def validate(self, data):
|
||||
if User.objects.filter(username=data['telegram_id']).exists():
|
||||
raise serializers.ValidationError("User mavjud")
|
||||
region = Region.objects.filter(id=data['region']).first()
|
||||
if not region:
|
||||
raise serializers.ValidationError("Region topilmadi")
|
||||
data['region'] = region
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
user = User.objects.create(
|
||||
first_name=validated_data.get('first_name'),
|
||||
last_name=validated_data.get('last_name'),
|
||||
telegram_id=validated_data.get('telegram_id'),
|
||||
region=validated_data.get('region'),
|
||||
is_active=False,
|
||||
username=validated_data.get('telegram_id'),
|
||||
)
|
||||
user.region.users_count += 1
|
||||
user.region.save()
|
||||
return user
|
||||
11
core/apps/accounts/urls.py
Normal file
11
core/apps/accounts/urls.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from core.apps.accounts.views import user as user_views
|
||||
|
||||
urlpatterns = [
|
||||
path('user/', include(
|
||||
[
|
||||
path('create', user_views.RegisterUserApiView.as_view(), name='user-register-api'),
|
||||
],
|
||||
)),
|
||||
]
|
||||
37
core/apps/accounts/views/user.py
Normal file
37
core/apps/accounts/views/user.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# rest framework
|
||||
from rest_framework import generics
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
from core.apps.accounts.serializers import user as serializers
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer
|
||||
|
||||
|
||||
class RegisterUserApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = serializers.UserCreateSerializer
|
||||
queryset = User.objects.all()
|
||||
|
||||
@swagger_auto_schema(
|
||||
operation_description='Create User',
|
||||
responses={
|
||||
200: SuccessResponseSerializer(),
|
||||
400: BaseResponseSerializer(),
|
||||
500: BaseResponseSerializer(),
|
||||
}
|
||||
)
|
||||
def post(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
serializer.save()
|
||||
return self.success_response(message='Foydalanuvchi qoshildi', status_code=201)
|
||||
return self.failure_response(data=serializer.errors, message='Foydalanuvchi qoshilmadi')
|
||||
except Exception as e:
|
||||
return self.error_response(data=str(e), message='xatolik')
|
||||
Reference in New Issue
Block a user