From 834ca060efb99b98b2bb7b750855c5c877e6925e Mon Sep 17 00:00:00 2001 From: behruz-dev Date: Wed, 16 Jul 2025 14:48:56 +0500 Subject: [PATCH] start to write contract apis --- config/conf/drf_spectacular.py | 7 ---- config/conf/drf_yasg.py | 7 ++++ config/conf/rest_framework.py | 9 ++++- config/conf/unfold.py | 1 - config/settings/base.py | 4 +- config/urls.py | 22 +++++++++-- core/apps/accounts/views/auth.py | 12 +----- .../apps/contracts/migrations/0001_initial.py | 11 +++--- core/apps/contracts/models/contract.py | 12 +++--- core/apps/contracts/serializers/contract.py | 39 +++++++++++++++++++ .../contracts/serializers/contract_side.py | 25 ++++++++++++ core/apps/contracts/tasks/__init__.py | 0 core/apps/contracts/tasks/contract_side.py | 27 +++++++++++++ core/apps/contracts/urls.py | 19 ++++++++- core/apps/contracts/views/contract.py | 23 +++++++++++ core/apps/contracts/views/contract_side.py | 20 ++++++++++ docker-compose.yaml | 1 + requirements.txt | 4 +- 18 files changed, 203 insertions(+), 40 deletions(-) delete mode 100644 config/conf/drf_spectacular.py create mode 100644 config/conf/drf_yasg.py create mode 100644 core/apps/contracts/serializers/contract.py create mode 100644 core/apps/contracts/serializers/contract_side.py create mode 100644 core/apps/contracts/tasks/__init__.py create mode 100644 core/apps/contracts/tasks/contract_side.py create mode 100644 core/apps/contracts/views/contract.py create mode 100644 core/apps/contracts/views/contract_side.py diff --git a/config/conf/drf_spectacular.py b/config/conf/drf_spectacular.py deleted file mode 100644 index 24f1b26..0000000 --- a/config/conf/drf_spectacular.py +++ /dev/null @@ -1,7 +0,0 @@ -SPECTACULAR_SETTINGS = { - "TITLE": "Your Project API", - "DESCRIPTION": "Your project description", - "VERSION": "1.0.0", - "SERVE_INCLUDE_SCHEMA": False, - "CAMELIZE_NAMES": True, -} \ No newline at end of file diff --git a/config/conf/drf_yasg.py b/config/conf/drf_yasg.py new file mode 100644 index 0000000..fd404b7 --- /dev/null +++ b/config/conf/drf_yasg.py @@ -0,0 +1,7 @@ +SWAGGER_SETTINGS = { + 'SECURITY_DEFINITIONS': { + 'basic': { + 'type': 'basic' + } + }, +} \ No newline at end of file diff --git a/config/conf/rest_framework.py b/config/conf/rest_framework.py index 87d24a6..f6efcb3 100644 --- a/config/conf/rest_framework.py +++ b/config/conf/rest_framework.py @@ -1,6 +1,11 @@ REST_FRAMEWORK = { - "DEFAULT_AUTHENTICATION_CLASSES": ("rest_framework_simplejwt.authentication.JWTAuthentication",), - "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", + "DEFAULT_AUTHENTICATION_CLASSES": ( + "rest_framework_simplejwt.authentication.JWTAuthentication", + "rest_framework.authentication.BasicAuthentication", + 'rest_framework.authentication.SessionAuthentication', + ), "DEFAULT_PERMISSION_CLASSES": ["rest_framework.permissions.IsAuthenticated"], + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'PAGE_SIZE': 10 } \ No newline at end of file diff --git a/config/conf/unfold.py b/config/conf/unfold.py index 948b824..005fbf6 100644 --- a/config/conf/unfold.py +++ b/config/conf/unfold.py @@ -1,6 +1,5 @@ UNFOLD = { - "DASHBOARD_CALLBACK": "django_core.views.dashboard_callback", "SITE_TITLE": None, "SHOW_LANGUAGES": True, "SITE_HEADER": None, diff --git a/config/settings/base.py b/config/settings/base.py index a5a51b5..c0a92b8 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -42,7 +42,7 @@ APPS = [ ] PACKAGES = [ - 'drf_spectacular', + 'drf_yasg', 'rest_framework', 'rest_framework_simplejwt', 'cacheops', @@ -146,7 +146,7 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' AUTH_USER_MODEL = 'accounts.User' -from config.conf.drf_spectacular import * +from config.conf.drf_yasg import * from config.conf.rest_framework import * from config.conf.simplejwt import * from config.conf.celery import * diff --git a/config/urls.py b/config/urls.py index 2fbd1d2..224251d 100644 --- a/config/urls.py +++ b/config/urls.py @@ -1,18 +1,32 @@ from django.contrib import admin from django.urls import path, include -from drf_spectacular.views import SpectacularSwaggerView, SpectacularAPIView +from rest_framework import permissions +from drf_yasg.views import get_schema_view +from drf_yasg import openapi + +schema_view = get_schema_view( + openapi.Info( + title="Snippets API", + default_version='v1', + description="Test description", + terms_of_service="https://www.google.com/policies/terms/", + contact=openapi.Contact(email="contact@snippets.local"), + license=openapi.License(name="BSD License"), + ), + public=True, + permission_classes=[permissions.AllowAny] +) urlpatterns = [ path('admin/', admin.site.urls), + path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), path('api/v1/', include( [ path('', include('core.apps.accounts.urls')), + path('contracts/', include('core.apps.contracts.urls')), ] )), - # swagger - path('api/schema/', SpectacularAPIView.as_view(), name='schema'), - path('swagger/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), ] diff --git a/core/apps/accounts/views/auth.py b/core/apps/accounts/views/auth.py index bad36dd..ab87299 100644 --- a/core/apps/accounts/views/auth.py +++ b/core/apps/accounts/views/auth.py @@ -6,14 +6,12 @@ from rest_framework.response import Response from rest_framework_simplejwt.tokens import RefreshToken -from drf_spectacular.utils import extend_schema - from core.apps.accounts.serializers import auth as auth_serializer from core.apps.accounts.models.verification_code import VerificationCode User = get_user_model() -@extend_schema(tags=['auth']) + class LoginApiView(generics.GenericAPIView): serializer_class = auth_serializer.LoginSerializer queryset = User.objects.all() @@ -28,14 +26,12 @@ class LoginApiView(generics.GenericAPIView): return Response(serializer.errors, status=status.HTTP_404_NOT_FOUND) -@extend_schema(tags=['auth']) class RegisterApiView(generics.CreateAPIView): serializer_class = auth_serializer.RegisterSerializer queryset = User.objects.all() permission_classes = [] -@extend_schema(tags=['auth']) class ConfirUserApiView(generics.GenericAPIView): serializer_class = auth_serializer.ConfirmUserSerializer queryset = User.objects.all() @@ -61,13 +57,11 @@ class ConfirUserApiView(generics.GenericAPIView): return Response({"success": False, "message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) -@extend_schema(tags=['auth']) class ChoiceUserRoleApiView(generics.GenericAPIView): serializer_class = auth_serializer.ChoiseRoleSerializer queryset = User.objects.all() permission_classes = [] - @extend_schema(description="roles -> PP(physcal person) or LP(legal person)") def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): @@ -79,7 +73,6 @@ class ChoiceUserRoleApiView(generics.GenericAPIView): return Response({'success': False, "message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) -@extend_schema(tags=['auth']) class CompliteUserProfileApiView(generics.GenericAPIView): serializer_class = auth_serializer.CompliteUserProfileSerializer queryset = User.objects.all() @@ -94,5 +87,4 @@ class CompliteUserProfileApiView(generics.GenericAPIView): token = RefreshToken.for_user(user) return Response({'access_token': str(token.access_token), "refresh_token": str(token), "role": user.role}, status=status.HTTP_200_OK) return Response({'success': False, 'message': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) - return Response({'success': False, "message": "User not found"}, status=status.HTTP_404_NOT_FOUND) - + return Response({'success': False, "message": "User not found"}, status=status.HTTP_404_NOT_FOUND) \ No newline at end of file diff --git a/core/apps/contracts/migrations/0001_initial.py b/core/apps/contracts/migrations/0001_initial.py index c9b716c..f953f58 100644 --- a/core/apps/contracts/migrations/0001_initial.py +++ b/core/apps/contracts/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.2 on 2025-07-15 15:55 +# Generated by Django 5.2 on 2025-07-16 14:45 import django.contrib.postgres.fields import django.db.models.deletion @@ -26,12 +26,11 @@ class Migration(migrations.Migration): ('contract_number', models.PositiveIntegerField()), ('name', models.CharField(max_length=200)), ('sides', models.CharField(choices=[('two_or_more', 'two or more'), ('customer_only', 'customer only'), ('only_company', 'onlycompany')], max_length=13)), - ('status', models.CharField(choices=[('created', 'created'), ('signed_company', 'signed by company'), ('signed_customer', 'signed by customer'), ('cancelled', 'cancelled')], max_length=15)), + ('status', models.CharField(choices=[('created', 'created'), ('signed_company', 'signed by company'), ('signed_customer', 'signed by customer'), ('cancelled', 'cancelled')], default='created', max_length=15)), ('face_id', models.BooleanField(default=False)), ('attach_file', models.BooleanField(default=False)), ('add_folder', models.BooleanField(default=False)), ('add_notification', models.BooleanField(default=False)), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contracts', to=settings.AUTH_USER_MODEL)), ], options={ 'verbose_name': 'contract', @@ -94,6 +93,7 @@ class Migration(migrations.Migration): 'verbose_name': 'contract side', 'verbose_name_plural': 'contract sides', 'db_table': 'contracts_sides', + 'unique_together': {('contract', 'user')}, }, ), migrations.CreateModel( @@ -102,8 +102,8 @@ class Migration(migrations.Migration): ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)), ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), - ('status', models.CharField(choices=[('signed', 'signed'), ('organized', 'organized')], max_length=20)), - ('signature_type', models.CharField(choices=[('sms_sign', 'sms signature'), ('electronic_sing', 'electronic signature')], max_length=20)), + ('status', models.CharField(choices=[('signed', 'signed'), ('organized', 'organized')], default='organized', max_length=20)), + ('signature_type', models.CharField(blank=True, choices=[('sms_sign', 'sms signature'), ('electronic_sing', 'electronic signature')], max_length=20, null=True)), ('is_signature', models.BooleanField(default=False)), ('contract', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contract_signatures', to='contracts.contract')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contract_users', to=settings.AUTH_USER_MODEL)), @@ -111,6 +111,7 @@ class Migration(migrations.Migration): options={ 'verbose_name': 'contract signatures', 'db_table': 'contract_signatures', + 'unique_together': {('contract', 'user')}, }, ), ] diff --git a/core/apps/contracts/models/contract.py b/core/apps/contracts/models/contract.py index 6aee237..e47ea44 100644 --- a/core/apps/contracts/models/contract.py +++ b/core/apps/contracts/models/contract.py @@ -17,17 +17,15 @@ class Contract(BaseModel): name = models.CharField(max_length=200) sides = models.CharField(max_length=13, choices=SIDES) # choices - status = models.CharField(max_length=15, choices=STATUS) # choices + status = models.CharField(max_length=15, choices=STATUS, default='created') # choices face_id = models.BooleanField(default=False) attach_file = models.BooleanField(default=False) add_folder = models.BooleanField(default=False) add_notification = models.BooleanField(default=False) - user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='contracts') - def __str__(self): - return f'{self.name} - {self.user}' + return f'{self.name}' class Meta: verbose_name = 'contract' @@ -54,6 +52,7 @@ class ContractSide(BaseModel): verbose_name = 'contract side' verbose_name_plural = 'contract sides' db_table = 'contracts_sides' + unique_together = ['contract', 'user'] class ContractFile(BaseModel): @@ -103,8 +102,8 @@ class ContractSignature(BaseModel): contract = models.ForeignKey(Contract, on_delete=models.CASCADE, related_name='contract_signatures') user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='contract_users') - status = models.CharField(max_length=20, choices=SIGNATURE_STATUS) # choices - signature_type = models.CharField(max_length=20, choices=SIGNATURE_TYPE) # choices + status = models.CharField(max_length=20, choices=SIGNATURE_STATUS, default='organized') + signature_type = models.CharField(max_length=20, choices=SIGNATURE_TYPE, null=True, blank=True) is_signature = models.BooleanField(default=False) @@ -115,3 +114,4 @@ class ContractSignature(BaseModel): verbose_name = 'contract signature' verbose_name = 'contract signatures' db_table = 'contract_signatures' + unique_together = ['contract', 'user'] diff --git a/core/apps/contracts/serializers/contract.py b/core/apps/contracts/serializers/contract.py new file mode 100644 index 0000000..8db7ce4 --- /dev/null +++ b/core/apps/contracts/serializers/contract.py @@ -0,0 +1,39 @@ +from django.db import transaction + +from rest_framework import serializers + +from core.apps.contracts.models.contract import Contract +from core.apps.contracts.serializers.contract_side import ContractSideCreateSerializer + + +class ContractCreateSerializer(serializers.Serializer): + file = serializers.FileField(max_length=None, allow_empty_file=False) + contract_number = serializers.IntegerField() + name = serializers.CharField() + sides = serializers.ChoiceField(choices=('two_or_more', 'customer_only', 'only_company')) + face_id = serializers.BooleanField() + attach_file = serializers.BooleanField() + add_folder = serializers.BooleanField() + add_notification = serializers.BooleanField() + + def create(self, validated_data): + with transaction.atomic(): + contract = Contract.objects.create( + file=validated_data.pop('file'), + contract_number=validated_data.pop('contract_number'), + name=validated_data.pop('name'), + sides=validated_data.pop('sides'), + face_id=validated_data.pop('face_id'), + attach_file=validated_data.pop('attach_file'), + add_folder=validated_data.pop('add_folder'), + add_notification=validated_data.pop('add_notification'), + ) + return contract + + +class ContractListSerializer(serializers.ModelSerializer): + class Meta: + model = Contract + fields = [ + 'id', 'name', 'file', 'contract_number', 'sides', 'face_id', 'add_folder', 'attach_file', 'add_notification', 'created_at' + ] \ No newline at end of file diff --git a/core/apps/contracts/serializers/contract_side.py b/core/apps/contracts/serializers/contract_side.py new file mode 100644 index 0000000..e339b94 --- /dev/null +++ b/core/apps/contracts/serializers/contract_side.py @@ -0,0 +1,25 @@ +from django.contrib.auth import get_user_model + +from rest_framework import serializers + +from core.apps.contracts.models.contract import ContractSide, Contract +from core.apps.contracts.enums.contract_side import ROLE + +User = get_user_model() + +class ContractSideCreateSerializer(serializers.Serializer): + full_name = serializers.CharField() + indentification = serializers.CharField() + position = serializers.CharField(required=False) + has_indentification = serializers.BooleanField() + user_role = serializers.ChoiceField(choices=ROLE) + phone = serializers.CharField() + contract_id = serializers.UUIDField() + + def validate(self, data): + if not User.objects.filter(phone=data.get('phone')).exists(): + raise serializers.ValidationError({'detail': "User not found!"}) + if not Contract.objects.filter(id=data.get('contract_id')).exists(): + raise serializers.ValidationError({'detail': 'Contract not found!'}) + return data + \ No newline at end of file diff --git a/core/apps/contracts/tasks/__init__.py b/core/apps/contracts/tasks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/apps/contracts/tasks/contract_side.py b/core/apps/contracts/tasks/contract_side.py new file mode 100644 index 0000000..dd5ea21 --- /dev/null +++ b/core/apps/contracts/tasks/contract_side.py @@ -0,0 +1,27 @@ +from django.contrib.auth import get_user_model + +from celery import shared_task + +from core.apps.contracts.models.contract import ContractSide, Contract, ContractSignature + +@shared_task +def create_contract_side(data): + User = get_user_model() + + contract = Contract.objects.get(id=data['contract_id']) + user = User.objects.get(phone=data['phone']) + + ContractSide.objects.create( + full_name=data.get('full_name'), + indentification=data.get('indentification'), + position=data.get('position'), + has_indentification=data.get('has_indentification'), + user_role=data.get('user_role'), + contract=contract, + user=user + ) + + ContractSignature.objects.create( + contract=contract, + user=user, + ) \ No newline at end of file diff --git a/core/apps/contracts/urls.py b/core/apps/contracts/urls.py index 3888427..6207ac2 100644 --- a/core/apps/contracts/urls.py +++ b/core/apps/contracts/urls.py @@ -1 +1,18 @@ -from django.urls import path, include \ No newline at end of file +from django.urls import path, include + +from core.apps.contracts.views import contract as contract_views +from core.apps.contracts.views import contract_side as contract_side_views + + +urlpatterns = [ + path('contract/', include( + [ + path('create/', contract_views.ContractCreateApiView.as_view(), name='create-contract'), + path('list/', contract_views.ContractListApiView.as_view(), name='list-contract'), + ] + )), + path('contract_side/', include([ + path('create/', contract_side_views.ConstartSideCreateApiView.as_view(), name='contract-side-create'), + ] + )) +] \ No newline at end of file diff --git a/core/apps/contracts/views/contract.py b/core/apps/contracts/views/contract.py new file mode 100644 index 0000000..2f942e5 --- /dev/null +++ b/core/apps/contracts/views/contract.py @@ -0,0 +1,23 @@ +from rest_framework import generics, views, status, permissions, parsers +from rest_framework.response import Response + +from core.apps.contracts.serializers import contract as contract_serializer +from core.apps.contracts.models.contract import Contract + + +class ContractCreateApiView(generics.CreateAPIView): + serializer_class = contract_serializer.ContractCreateSerializer + queryset = Contract.objects.all() + permission_classes = [permissions.IsAuthenticated] + parser_classes = [parsers.MultiPartParser, parsers.FormParser] + + def get_serializer_context(self): + return {'user': self.request.user} + + +class ContractListApiView(generics.ListAPIView): + serializer_class = contract_serializer.ContractListSerializer + queryset = Contract.objects.all() + + def get_queryset(self): + return super().get_queryset() diff --git a/core/apps/contracts/views/contract_side.py b/core/apps/contracts/views/contract_side.py new file mode 100644 index 0000000..0300bbc --- /dev/null +++ b/core/apps/contracts/views/contract_side.py @@ -0,0 +1,20 @@ +from rest_framework import generics, status, parsers +from rest_framework.response import Response + +from core.apps.contracts.serializers import contract_side as contract_side_serializer +from core.apps.contracts.models.contract import ContractSide +from core.apps.contracts.tasks.contract_side import create_contract_side + + +class ConstartSideCreateApiView(generics.GenericAPIView): + serializer_class = contract_side_serializer.ContractSideCreateSerializer + queryset = ContractSide.objects.all() + + def post(self, request): + serializer = self.serializer_class(data=request.data) + if serializer.is_valid(): + # TODO: call celery task + create_contract_side.delay(serializer.validated_data) + return Response({"success": True, "message": "contract side created"}, status=status.HTTP_201_CREATED) + return Response({"success": False, "message": serializer.errors}, status=status.HTTP_400_BAD_REQUEST) + diff --git a/docker-compose.yaml b/docker-compose.yaml index 72654f6..579d82d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -54,6 +54,7 @@ services: - web networks: - trustme + restart: always db: image: postgres:16 diff --git a/requirements.txt b/requirements.txt index 32fab1e..01b0f15 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ uvicorn psycopg2 django-environ==0.12.0 pillow -drf-spectacular==0.28.0 djangorestframework_simplejwt==5.5.0 djangorestframework requests @@ -14,4 +13,5 @@ django-redis==6.0.0 pytest pytest-django django-cacheops==7.2 -django-unfold==0.62.0 \ No newline at end of file +django-unfold==0.62.0 +drf_yasg==1.21.10 \ No newline at end of file