first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-
This commit is contained in:
@@ -1,35 +1,197 @@
|
||||
import typing
|
||||
|
||||
from apps.authentication.api.v1.serializers.jwt import CustomizedTokenObtainPairSerializer
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from rest_framework.decorators import action, permission_classes
|
||||
from apps.authentication import permissions as auth_permissions
|
||||
from apps.authentication.api.v1.serializers.serializer import (
|
||||
CitySerializer,
|
||||
ProvinceSerializer,
|
||||
OrganizationTypeSerializer,
|
||||
OrganizationSerializer,
|
||||
UserSerializer,
|
||||
BankAccountSerializer
|
||||
)
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
from apps.authorization.api.v1 import api as authorize_view
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.decorators import action
|
||||
from apps.authentication.models import User
|
||||
from rest_framework.views import APIView
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
Province,
|
||||
Organization,
|
||||
OrganizationType,
|
||||
BankAccountInformation
|
||||
)
|
||||
from django.db import transaction
|
||||
from rest_framework.response import Response
|
||||
from common.tools import CustomOperations
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class CustomizedTokenObtainPairView(TokenObtainPairView):
|
||||
""" Generate Customize token """
|
||||
serializer_class = CustomizedTokenObtainPairSerializer
|
||||
|
||||
|
||||
# Example Code
|
||||
class Authentication(ModelViewSet):
|
||||
queryset = User
|
||||
serializer_class = ''
|
||||
permission_classes = ''
|
||||
authentication_classes = [JWTAuthentication]
|
||||
|
||||
@action(
|
||||
methods=['post', ],
|
||||
detail=False,
|
||||
name='login',
|
||||
url_name='login',
|
||||
url_path='login'
|
||||
)
|
||||
@transaction.atomic
|
||||
def login(self, request):
|
||||
pass
|
||||
|
||||
|
||||
class UserViewSet(ModelViewSet):
|
||||
pass
|
||||
""" Crud operations for user model """
|
||||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
permission_classes = [
|
||||
auth_permissions.CreateUser,
|
||||
]
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
Customizing create user & bank account information with
|
||||
permission levels
|
||||
"""
|
||||
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.save()
|
||||
if 'organization' in request.data.keys():
|
||||
organization = CustomOperations().custom_create( # create organization for user
|
||||
request=request,
|
||||
view=OrganizationViewSet(),
|
||||
data_key='organization'
|
||||
)
|
||||
else:
|
||||
organization = {}
|
||||
if 'user_relations' in request.data.keys():
|
||||
user_relations = CustomOperations().custom_create( # create user relations
|
||||
user=user,
|
||||
request=request,
|
||||
view=authorize_view.UserRelationViewSet(),
|
||||
data_key='user_relations',
|
||||
additional_data={'organization': organization['id']} # noqa
|
||||
)
|
||||
else:
|
||||
user_relations = {}
|
||||
if 'bank_account' in request.data.keys():
|
||||
bank_account = CustomOperations().custom_create( # create user bank account info
|
||||
user=user,
|
||||
request=request,
|
||||
view=BankAccountViewSet(),
|
||||
data_key='bank_account'
|
||||
)
|
||||
else:
|
||||
bank_account = {}
|
||||
serializer_data = serializer.data
|
||||
serializer_data.update({
|
||||
'organization': organization,
|
||||
'user_relations': user_relations, # noqa
|
||||
'bank_account': bank_account # noqa
|
||||
})
|
||||
return Response(serializer_data, status=status.HTTP_201_CREATED)
|
||||
else:
|
||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
@transaction.atomic
|
||||
def update(self, request, pk=None, *args, **kwargs):
|
||||
"""
|
||||
Customizing update user & bank account info with
|
||||
permission levels
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.update(self.queryset.get(id=pk), validated_data=request.data)
|
||||
if 'organization' in request.data.keys(): # noqa
|
||||
organization = CustomOperations().custom_update( # update organization for user
|
||||
request=request,
|
||||
view=OrganizationViewSet(),
|
||||
data_key='organization',
|
||||
obj_id=request.data['organization']['id']
|
||||
)
|
||||
else:
|
||||
organization = {}
|
||||
if 'user_relations' in request.data.keys():
|
||||
user_relations = CustomOperations().custom_update( # update user relations
|
||||
user=user,
|
||||
request=request,
|
||||
view=authorize_view.UserRelationViewSet(),
|
||||
data_key='user_relations',
|
||||
additional_data={'organization': request.data['organization']['id']}, # noqa
|
||||
obj_id=request.data['user_relations']['id']
|
||||
)
|
||||
else:
|
||||
user_relations = {}
|
||||
if 'bank_account' in request.data.keys():
|
||||
bank_account = CustomOperations().custom_update( # update user bank account info
|
||||
user=user,
|
||||
request=request,
|
||||
view=BankAccountViewSet(),
|
||||
data_key='bank_account',
|
||||
obj_id=request.data['bank_account']['id']
|
||||
)
|
||||
else:
|
||||
bank_account = {}
|
||||
serializer_data = serializer.data
|
||||
serializer_data.update({
|
||||
'organization': organization,
|
||||
'user_relations': user_relations, # noqa
|
||||
'bank_account': bank_account # noqa
|
||||
})
|
||||
return Response(serializer_data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
|
||||
class CityViewSet(ModelViewSet):
|
||||
""" Crud operations for city model """ #
|
||||
queryset = City.objects.all()
|
||||
serializer_class = CitySerializer
|
||||
|
||||
|
||||
class ProvinceViewSet(ModelViewSet):
|
||||
""" Crud operations for province model """ #
|
||||
queryset = Province.objects.all()
|
||||
serializer_class = ProvinceSerializer
|
||||
|
||||
|
||||
class OrganizationTypeViewSet(ModelViewSet):
|
||||
""" Crud operations for Organization Type model """ #
|
||||
queryset = OrganizationType.objects.all()
|
||||
serializer_class = OrganizationTypeSerializer
|
||||
|
||||
|
||||
class OrganizationViewSet(ModelViewSet):
|
||||
""" Crud operations for organization model """ #
|
||||
queryset = Organization.objects.all()
|
||||
serializer_class = OrganizationSerializer
|
||||
permission_classes = [auth_permissions.CreateOrganization]
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
@create Organization by user
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data['organization'])
|
||||
|
||||
if serializer.is_valid():
|
||||
organization = serializer.save()
|
||||
|
||||
if 'user_relations' in request.data.keys():
|
||||
user_relations = CustomOperations().custom_create( # create user relations
|
||||
request=request,
|
||||
view=authorize_view.UserRelationViewSet(),
|
||||
data_key='user_relations',
|
||||
additional_data={'organization': organization.id} # noqa
|
||||
)
|
||||
serializer_data = serializer.data
|
||||
serializer_data.update(
|
||||
{'user_relations': user_relations}
|
||||
)
|
||||
return Response(serializer_data, status=status.HTTP_201_CREATED)
|
||||
else:
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
else:
|
||||
return Response(serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
|
||||
|
||||
class BankAccountViewSet(ModelViewSet):
|
||||
""" Crud operations for bank account model """ #
|
||||
queryset = BankAccountInformation.objects.all()
|
||||
serializer_class = BankAccountSerializer
|
||||
|
||||
@@ -1,53 +1,53 @@
|
||||
from apps.authentication.api.v1.serializers.serializer import UserSerializer
|
||||
from rest_framework.pagination import LimitOffsetPagination
|
||||
from rest_framework.viewsets import ModelViewSet, ViewSet
|
||||
from apps.authentication.documents import UserDocument
|
||||
from rest_framework.response import Response
|
||||
from django.http.response import HttpResponse
|
||||
from apps.authentication.models import User
|
||||
from rest_framework.views import APIView
|
||||
from elasticsearch_dsl.query import Q
|
||||
import abc
|
||||
|
||||
|
||||
class PaginatedElasticSearchApiView(APIView, LimitOffsetPagination):
|
||||
"""Base ApiView Class for elasticsearch views with pagination
|
||||
Other ApiView classes should inherit from this class"""
|
||||
serializer_class = None
|
||||
document_class = None
|
||||
|
||||
@abc.abstractmethod
|
||||
def generate_q_expression(self, query):
|
||||
"""This method should be overridden
|
||||
and return a Q() expression."""
|
||||
|
||||
def get(self, request, query):
|
||||
try:
|
||||
q = self.generate_q_expression(query)
|
||||
search = self.document_class.search().query(q)
|
||||
response = search.execute()
|
||||
|
||||
print(f"Found {response.hits.total.value} hit(s) for query: '{query}'")
|
||||
|
||||
results = self.paginate_queryset(response, request, view=self) # noqa
|
||||
serializer = self.serializer_class(results, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
except Exception as e:
|
||||
return HttpResponse(e, status=500)
|
||||
|
||||
|
||||
class SearchUsersApiView(PaginatedElasticSearchApiView): # noqa
|
||||
"""Search in Users"""
|
||||
|
||||
serializer_class = UserSerializer
|
||||
document_class = UserDocument
|
||||
|
||||
def generate_q_expression(self, query):
|
||||
return Q(
|
||||
'multi_match',
|
||||
query=query,
|
||||
fields=[
|
||||
'username',
|
||||
'mobile'
|
||||
], fuzziness='auto'
|
||||
)
|
||||
# from apps.authentication.api.v1.serializers.serializer import UserSerializer
|
||||
# from rest_framework.pagination import LimitOffsetPagination
|
||||
# from rest_framework.viewsets import ModelViewSet, ViewSet
|
||||
# from apps.authentication.document import UserDocument
|
||||
# from rest_framework.response import Response
|
||||
# from django.http.response import HttpResponse
|
||||
# from apps.authentication.models import User
|
||||
# from rest_framework.views import APIView
|
||||
# from elasticsearch_dsl.query import Q
|
||||
# import abc
|
||||
#
|
||||
#
|
||||
# class PaginatedElasticSearchApiView(APIView, LimitOffsetPagination):
|
||||
# """Base ApiView Class for elasticsearch views with pagination,
|
||||
# Other ApiView classes should inherit from this class"""
|
||||
# serializer_class = None
|
||||
# document_class = None
|
||||
#
|
||||
# @abc.abstractmethod
|
||||
# def generate_q_expression(self, query):
|
||||
# """This method should be overridden
|
||||
# and return a Q() expression."""
|
||||
#
|
||||
# def get(self, request, query):
|
||||
# try:
|
||||
# q = self.generate_q_expression(query)
|
||||
# search = self.document_class.search().query(q)
|
||||
# response = search.execute()
|
||||
#
|
||||
# print(f"Found {response.hits.total.value} hit(s) for query: '{query}'")
|
||||
#
|
||||
# results = self.paginate_queryset(response, request, view=self) # noqa
|
||||
# serializer = self.serializer_class(results, many=True)
|
||||
# return self.get_paginated_response(serializer.data)
|
||||
# except Exception as e:
|
||||
# return HttpResponse(e, status=500)
|
||||
#
|
||||
#
|
||||
# class SearchUsersApiView(PaginatedElasticSearchApiView): # noqa
|
||||
# """Search in Users"""
|
||||
#
|
||||
# serializer_class = UserSerializer
|
||||
# document_class = UserDocument
|
||||
#
|
||||
# def generate_q_expression(self, query):
|
||||
# return Q(
|
||||
# 'multi_match',
|
||||
# query=query,
|
||||
# fields=[
|
||||
# 'username',
|
||||
# 'mobile'
|
||||
# ], fuzziness='auto'
|
||||
# )
|
||||
|
||||
@@ -1,11 +1,179 @@
|
||||
from apps.authorization.api.v1.serializers import UserRelationSerializer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import serializers
|
||||
from apps.authentication.models import User
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
Province,
|
||||
Organization,
|
||||
OrganizationType,
|
||||
BankAccountInformation
|
||||
)
|
||||
from apps.authorization import models as authorize_models
|
||||
import typing
|
||||
|
||||
|
||||
class CitySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = City
|
||||
fields = [
|
||||
'id',
|
||||
'name'
|
||||
]
|
||||
|
||||
|
||||
class ProvinceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Province
|
||||
fields = [
|
||||
'id',
|
||||
'name'
|
||||
]
|
||||
|
||||
|
||||
class BankAccountSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = BankAccountInformation
|
||||
fields = [
|
||||
'id',
|
||||
'user',
|
||||
'account',
|
||||
'name',
|
||||
'card',
|
||||
'sheba'
|
||||
]
|
||||
extra_kwargs = {
|
||||
'user': {'required': False},
|
||||
'account': {'required': False},
|
||||
'card': {'required': False},
|
||||
'sheba': {'required': False}
|
||||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user bank account information """
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
instance.account = validated_data.get('account', instance.account)
|
||||
instance.card = validated_data.get('card', instance.card)
|
||||
instance.sheba = validated_data.get('sheba', instance.sheba)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = [
|
||||
'id',
|
||||
'username',
|
||||
'mobile'
|
||||
'password',
|
||||
'first_name',
|
||||
'last_name',
|
||||
'is_active',
|
||||
'mobile',
|
||||
'phone',
|
||||
'national_code',
|
||||
'birthdate',
|
||||
'nationality',
|
||||
'ownership',
|
||||
'address',
|
||||
'photo',
|
||||
'province',
|
||||
'city',
|
||||
'otp_status',
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user instance """
|
||||
instance.username = validated_data.get('username', instance.username)
|
||||
instance.password = validated_data.get('password', instance.password)
|
||||
instance.first_name = validated_data.get('first_name')
|
||||
instance.last_name = validated_data.get('last_name')
|
||||
instance.is_active = validated_data.get('is_active')
|
||||
instance.mobile = validated_data.get('mobile')
|
||||
instance.phone = validated_data.get('phone')
|
||||
instance.national_code = validated_data.get('national_code')
|
||||
instance.birthdate = validated_data.get('birthdate')
|
||||
instance.nationality = validated_data.get('nationality')
|
||||
instance.ownership = validated_data.get('ownership')
|
||||
instance.address = validated_data.get('address')
|
||||
instance.photo = validated_data.get('photo')
|
||||
instance.province = Province.objects.get(id=validated_data.get('province'))
|
||||
instance.city = City.objects.get(id=validated_data.get('city'))
|
||||
instance.otp_status = validated_data.get('otp_status')
|
||||
instance.save()
|
||||
|
||||
return instance
|
||||
|
||||
@staticmethod
|
||||
def update_relations(user: object, relation_data: dict, bank_data: dict) -> typing.Any:
|
||||
"""
|
||||
update user relations & bank account for user
|
||||
"""
|
||||
user_relation = UserRelationSerializer(data=relation_data) # Create user relation
|
||||
if user_relation.is_valid(raise_exception=True):
|
||||
user_relation_obj = user_relation.update(
|
||||
authorize_models.UserRelations.objects.get(user=user),
|
||||
validated_data=relation_data
|
||||
)
|
||||
|
||||
bank_info = BankAccountSerializer(data=bank_data) # Create user bank information
|
||||
if bank_info.is_valid(raise_exception=True):
|
||||
bank_obj = bank_info.update(
|
||||
BankAccountInformation.objects.get(id=bank_data['id']),
|
||||
validated_data=bank_data
|
||||
)
|
||||
|
||||
return user_relation_obj, bank_obj # noqa
|
||||
|
||||
|
||||
class OrganizationTypeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = OrganizationType
|
||||
fields = [
|
||||
'id',
|
||||
'key',
|
||||
'name',
|
||||
]
|
||||
|
||||
|
||||
class OrganizationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Organization
|
||||
fields = [
|
||||
'id',
|
||||
'name',
|
||||
'type',
|
||||
'province',
|
||||
'city',
|
||||
'parent_organization',
|
||||
'national_unique_id'
|
||||
]
|
||||
extra_kwargs = {}
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, Organization):
|
||||
representation['province'] = ProvinceSerializer(instance.province).data
|
||||
representation['city'] = CitySerializer(instance.city).data
|
||||
representation['type'] = OrganizationTypeSerializer(instance.type).data
|
||||
if instance.parent_organization:
|
||||
representation['parent_organization'] = OrganizationSerializer(instance.parent_organization).data
|
||||
|
||||
return representation
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
""" update user organization information """ # noqa
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
if validated_data.get('type'):
|
||||
instance.type = OrganizationType.objects.get(id=validated_data.get('type', instance.type))
|
||||
if validated_data.get('province'):
|
||||
instance.province = Province.objects.get(id=validated_data.get('province', instance.province))
|
||||
if validated_data.get('city'):
|
||||
instance.city = City.objects.get(id=validated_data.get('city', instance.city))
|
||||
if validated_data.get('parent_organization'):
|
||||
instance.parent_organization = Organization.objects.get(
|
||||
id=validated_data.get('parent_organization', instance.parent_organization)
|
||||
)
|
||||
instance.national_unique_id = validated_data.get('national_unique_id', instance.national_unique_id)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
@@ -3,16 +3,29 @@ from rest_framework.routers import DefaultRouter
|
||||
from rest_framework_simplejwt.views import (
|
||||
TokenObtainPairView,
|
||||
TokenRefreshView,
|
||||
TokenVerifyView
|
||||
TokenVerifyView,
|
||||
TokenBlacklistView
|
||||
)
|
||||
from .api import (
|
||||
CustomizedTokenObtainPairView
|
||||
CustomizedTokenObtainPairView,
|
||||
UserViewSet,
|
||||
CityViewSet,
|
||||
ProvinceViewSet,
|
||||
OrganizationViewSet,
|
||||
OrganizationTypeViewSet
|
||||
)
|
||||
from .search_view import SearchUsersApiView
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'user', UserViewSet, basename='user')
|
||||
router.register(r'city', CityViewSet, basename='city')
|
||||
router.register(r'province', ProvinceViewSet, basename='province')
|
||||
router.register(r'organization', OrganizationViewSet, basename='organization')
|
||||
router.register(r'organization-type', OrganizationTypeViewSet, basename='organization_type')
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', CustomizedTokenObtainPairView.as_view(), name='token_obtain_pair'),
|
||||
path('search_user/<str:query>', SearchUsersApiView.as_view(), name='search_user'),
|
||||
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
|
||||
path('token/verify/', TokenVerifyView.as_view(), name='token_verify'),
|
||||
path('token/revoke/', TokenBlacklistView.as_view(), name='revoke_token'),
|
||||
path('', include(router.urls))
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user