start to write contract apis
This commit is contained in:
@@ -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,
|
||||
}
|
||||
7
config/conf/drf_yasg.py
Normal file
7
config/conf/drf_yasg.py
Normal file
@@ -0,0 +1,7 @@
|
||||
SWAGGER_SETTINGS = {
|
||||
'SECURITY_DEFINITIONS': {
|
||||
'basic': {
|
||||
'type': 'basic'
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
UNFOLD = {
|
||||
"DASHBOARD_CALLBACK": "django_core.views.dashboard_callback",
|
||||
"SITE_TITLE": None,
|
||||
"SHOW_LANGUAGES": True,
|
||||
"SITE_HEADER": None,
|
||||
|
||||
@@ -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 *
|
||||
|
||||
@@ -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'),
|
||||
]
|
||||
|
||||
@@ -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)
|
||||
@@ -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')},
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
@@ -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']
|
||||
|
||||
39
core/apps/contracts/serializers/contract.py
Normal file
39
core/apps/contracts/serializers/contract.py
Normal file
@@ -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'
|
||||
]
|
||||
25
core/apps/contracts/serializers/contract_side.py
Normal file
25
core/apps/contracts/serializers/contract_side.py
Normal file
@@ -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
|
||||
|
||||
0
core/apps/contracts/tasks/__init__.py
Normal file
0
core/apps/contracts/tasks/__init__.py
Normal file
27
core/apps/contracts/tasks/contract_side.py
Normal file
27
core/apps/contracts/tasks/contract_side.py
Normal file
@@ -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,
|
||||
)
|
||||
@@ -1 +1,18 @@
|
||||
from django.urls import path, include
|
||||
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'),
|
||||
]
|
||||
))
|
||||
]
|
||||
23
core/apps/contracts/views/contract.py
Normal file
23
core/apps/contracts/views/contract.py
Normal file
@@ -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()
|
||||
20
core/apps/contracts/views/contract_side.py
Normal file
20
core/apps/contracts/views/contract_side.py
Normal file
@@ -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)
|
||||
|
||||
@@ -54,6 +54,7 @@ services:
|
||||
- web
|
||||
networks:
|
||||
- trustme
|
||||
restart: always
|
||||
|
||||
db:
|
||||
image: postgres:16
|
||||
|
||||
@@ -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
|
||||
django-unfold==0.62.0
|
||||
drf_yasg==1.21.10
|
||||
Reference in New Issue
Block a user