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')
|
||||
0
core/apps/authentication/__init__.py
Normal file
0
core/apps/authentication/__init__.py
Normal file
6
core/apps/authentication/apps.py
Normal file
6
core/apps/authentication/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthenticationConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core.apps.authentication'
|
||||
0
core/apps/authentication/migrations/__init__.py
Normal file
0
core/apps/authentication/migrations/__init__.py
Normal file
5
core/apps/authentication/serializers/login.py
Normal file
5
core/apps/authentication/serializers/login.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class LoginSerializer(serializers.Serializer):
|
||||
telegram_id = serializers.CharField()
|
||||
5
core/apps/authentication/serializers/response.py
Normal file
5
core/apps/authentication/serializers/response.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class LoginResponseSerializer(serializers.Serializer):
|
||||
token = serializers.CharField(required=False, allow_null=True)
|
||||
8
core/apps/authentication/urls.py
Normal file
8
core/apps/authentication/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.urls import path
|
||||
|
||||
from core.apps.authentication.views import login
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', login.LoginApiView.as_view(), name='login-api'),
|
||||
]
|
||||
45
core/apps/authentication/views/login.py
Normal file
45
core/apps/authentication/views/login.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# rest framework
|
||||
from rest_framework import generics
|
||||
# rest framework simple jwt
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
# drf yasg
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer
|
||||
# accounts
|
||||
from core.apps.accounts.models import User
|
||||
# authentication
|
||||
from core.apps.authentication.serializers.login import LoginSerializer
|
||||
from core.apps.authentication.serializers import response as response_serializers
|
||||
|
||||
|
||||
class LoginApiView(generics.GenericAPIView, ResponseMixin):
|
||||
serializer_class = LoginSerializer
|
||||
queryset = User.objects.all()
|
||||
|
||||
@swagger_auto_schema(
|
||||
responses={
|
||||
200: SuccessResponseSerializer(data_serializer=response_serializers.LoginResponseSerializer()),
|
||||
400: BaseResponseSerializer(),
|
||||
500: BaseResponseSerializer(),
|
||||
}
|
||||
)
|
||||
def post(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
telegram_id = serializer.validated_data.get('telegram_id')
|
||||
user = User.objects.filter(telegram_id=telegram_id).first()
|
||||
if not user:
|
||||
return self.failure_response(message="User topilmadi")
|
||||
if not user.is_active:
|
||||
return self.failure_response(message="User tasdiqlanmagan")
|
||||
|
||||
token = RefreshToken.for_user(user)
|
||||
return self.success_response(data={'token': str(token.access_token)})
|
||||
|
||||
return self.failure_response(data=serializer.errors, message='siz tarafdan xatolik')
|
||||
except Exception as e:
|
||||
return self.error_response(data=str(e), message='xatolik')
|
||||
0
core/apps/shared/__init__.py
Normal file
0
core/apps/shared/__init__.py
Normal file
1
core/apps/shared/admin/__init__.py
Normal file
1
core/apps/shared/admin/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .region import *
|
||||
10
core/apps/shared/admin/region.py
Normal file
10
core/apps/shared/admin/region.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import Region
|
||||
|
||||
|
||||
@admin.register(Region)
|
||||
class RegionAdmin(admin.ModelAdmin):
|
||||
list_display = ['name', 'users_count']
|
||||
search_fields = ['name']
|
||||
9
core/apps/shared/apps.py
Normal file
9
core/apps/shared/apps.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SharedConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'core.apps.shared'
|
||||
|
||||
def ready(self):
|
||||
import core.apps.shared.admin
|
||||
14
core/apps/shared/middlewares/response_time.py
Normal file
14
core/apps/shared/middlewares/response_time.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import time
|
||||
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
|
||||
class ResponseTimeMiddleware(MiddlewareMixin):
|
||||
def process_request(self, request):
|
||||
request.start_time = time.time()
|
||||
|
||||
def process_response(self, request, response):
|
||||
if hasattr(request, "start_time"):
|
||||
response_time = time.time() - request.start_time
|
||||
response["X-Response-Time"] = f"{response_time:.3f}s"
|
||||
return response
|
||||
27
core/apps/shared/migrations/0001_initial.py
Normal file
27
core/apps/shared/migrations/0001_initial.py
Normal file
@@ -0,0 +1,27 @@
|
||||
# Generated by Django 5.2 on 2025-11-21 12:35
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Region',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('name', models.CharField(max_length=200, unique=True)),
|
||||
('users_count', models.PositiveIntegerField(default=0)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
0
core/apps/shared/migrations/__init__.py
Normal file
0
core/apps/shared/migrations/__init__.py
Normal file
2
core/apps/shared/models/__init__.py
Normal file
2
core/apps/shared/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .base import *
|
||||
from .region import Region
|
||||
9
core/apps/shared/models/base.py
Normal file
9
core/apps/shared/models/base.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class BaseModel(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
11
core/apps/shared/models/region.py
Normal file
11
core/apps/shared/models/region.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.db import models
|
||||
|
||||
from core.apps.shared.models.base import BaseModel
|
||||
|
||||
|
||||
class Region(BaseModel):
|
||||
name = models.CharField(max_length=200, unique=True)
|
||||
users_count = models.PositiveIntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
16
core/apps/shared/serializers/base.py
Normal file
16
core/apps/shared/serializers/base.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class BaseResponseSerializer(serializers.Serializer):
|
||||
status_code = serializers.IntegerField()
|
||||
message = serializers.CharField(required=False, allow_null=True)
|
||||
data = serializers.JSONField(required=False, allow_null=True)
|
||||
|
||||
|
||||
class SuccessResponseSerializer(BaseResponseSerializer):
|
||||
def __init__(self, data_serializer=None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
if data_serializer:
|
||||
self.fields['data'] = data_serializer
|
||||
else:
|
||||
self.fields['data'] = serializers.JSONField(required=False)
|
||||
12
core/apps/shared/serializers/region.py
Normal file
12
core/apps/shared/serializers/region.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
# shared
|
||||
from core.apps.shared.models import Region
|
||||
|
||||
|
||||
class RegionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Region
|
||||
fields = [
|
||||
'id', 'name', 'created_at'
|
||||
]
|
||||
13
core/apps/shared/urls.py
Normal file
13
core/apps/shared/urls.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from django.urls import path, include
|
||||
|
||||
# shared region view
|
||||
from core.apps.shared.views import region as region_view
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('region/', include(
|
||||
[
|
||||
path('list/', region_view.RegionListApiView.as_view(), name='region-list-api'),
|
||||
],
|
||||
)),
|
||||
]
|
||||
64
core/apps/shared/utils/response_mixin.py
Normal file
64
core/apps/shared/utils/response_mixin.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
||||
class ResponseMixin:
|
||||
"""
|
||||
Mixin to customize the response format
|
||||
|
||||
Example Usage:
|
||||
|
||||
class MyAPIView(APIView, APIViewResponseMixin):
|
||||
def get(self, request):
|
||||
try:
|
||||
# Your logic here
|
||||
data = {"key": "value"}
|
||||
return self.success_response(data=data, message="Data retrieved successfully")
|
||||
except Exception as e:
|
||||
return self.error_response(message=str(e))
|
||||
|
||||
"""
|
||||
|
||||
SUCCESS = "success" # 200
|
||||
FAILURE = "failure" # 400
|
||||
ERROR = "error" # 500
|
||||
|
||||
@classmethod
|
||||
def success_response(cls, data=None, message=None, status_code=status.HTTP_200_OK):
|
||||
"""
|
||||
Returns Success Response
|
||||
"""
|
||||
response_data = {"status_code": status_code, "status": cls.SUCCESS}
|
||||
if message is not None:
|
||||
response_data["message"] = message
|
||||
if data is not None:
|
||||
response_data["data"] = data
|
||||
return Response(response_data, status=status_code)
|
||||
|
||||
@classmethod
|
||||
def failure_response(
|
||||
cls, data=None, message=None, status_code=status.HTTP_400_BAD_REQUEST
|
||||
):
|
||||
"""
|
||||
Returns Failure Response
|
||||
"""
|
||||
response_data = {"status_code": status_code, "status": cls.FAILURE}
|
||||
if message is not None:
|
||||
response_data["message"] = message
|
||||
if data is not None:
|
||||
response_data["data"] = data
|
||||
return Response(response_data, status=status_code)
|
||||
|
||||
@classmethod
|
||||
def error_response(
|
||||
cls, data=None, message=None, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
):
|
||||
"""
|
||||
Returns Error Response
|
||||
"""
|
||||
response_data = {"status_code": status_code, "status": cls.ERROR}
|
||||
if message is not None:
|
||||
response_data["message"] = message
|
||||
if data is not None:
|
||||
response_data["data"] = data
|
||||
return Response(response_data, status=status_code)
|
||||
34
core/apps/shared/views/region.py
Normal file
34
core/apps/shared/views/region.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# rest framework
|
||||
from rest_framework import generics
|
||||
|
||||
# drf yasg
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
|
||||
# shared
|
||||
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||
from core.apps.shared.serializers.region import RegionSerializer
|
||||
from core.apps.shared.models import Region
|
||||
from core.apps.shared.serializers.base import BaseResponseSerializer, SuccessResponseSerializer
|
||||
|
||||
|
||||
|
||||
class RegionListApiView(generics.ListAPIView, ResponseMixin):
|
||||
serializer_class = RegionSerializer
|
||||
queryset = Region.objects.order_by('name')
|
||||
pagination_class = None
|
||||
|
||||
@swagger_auto_schema(
|
||||
operation_description="Get region list",
|
||||
responses={
|
||||
200: SuccessResponseSerializer(data_serializer=RegionSerializer()),
|
||||
400: BaseResponseSerializer(),
|
||||
500: BaseResponseSerializer(),
|
||||
},
|
||||
|
||||
)
|
||||
def get(self, request):
|
||||
try:
|
||||
serializer = self.serializer_class(self.get_queryset(), many=True)
|
||||
return self.success_response(data=serializer.data, message="malumotlar fetch qilindi")
|
||||
except Exception as e:
|
||||
return self.error_response(data=str(e), message="malumotlarni fetch qilishda xatolik")
|
||||
Reference in New Issue
Block a user