organization limit for quota - get org childs
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import typing
|
||||
from rest_framework.permissions import AllowAny
|
||||
from apps.authentication.api.v1.serializers.jwt import CustomizedTokenObtainPairSerializer
|
||||
from rest_framework.decorators import action, permission_classes
|
||||
from apps.authentication import permissions as auth_permissions
|
||||
@@ -16,7 +14,9 @@ from apps.core.pagination import CustomPageNumberPagination
|
||||
from apps.authorization.api.v1 import api as authorize_view
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from apps.authentication.tools import get_token_jti
|
||||
from common.helpers import get_organization_by_user
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
from rest_framework.permissions import AllowAny
|
||||
from apps.authentication.models import (
|
||||
User,
|
||||
City,
|
||||
@@ -35,6 +35,7 @@ from rest_framework import status
|
||||
from django.db import transaction
|
||||
from common.sms import send_sms
|
||||
import random
|
||||
import typing
|
||||
|
||||
|
||||
class CustomizedTokenObtainPairView(TokenObtainPairView):
|
||||
@@ -197,6 +198,14 @@ class OrganizationViewSet(ModelViewSet):
|
||||
queryset = Organization.objects.all()
|
||||
serializer_class = OrganizationSerializer
|
||||
|
||||
def get_all_org_child(self, org):
|
||||
descendants = []
|
||||
children = org.parents.all()
|
||||
for child in children:
|
||||
descendants.append(child)
|
||||
descendants.extend(self.get_all_org_child(child))
|
||||
return descendants
|
||||
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
@@ -274,6 +283,24 @@ class OrganizationViewSet(ModelViewSet):
|
||||
serializer = self.serializer_class(queryset, many=True)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=False,
|
||||
url_path='child_organizations',
|
||||
url_name='child_organizations',
|
||||
name='child_organizations'
|
||||
)
|
||||
@transaction.atomic
|
||||
def get_child_organizations(self, request):
|
||||
organization = get_organization_by_user(request.user)
|
||||
child_organizations = self.get_all_org_child(organization)
|
||||
|
||||
page = self.paginate_queryset(child_organizations) # paginate queryset
|
||||
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
|
||||
class BankAccountViewSet(ModelViewSet):
|
||||
""" Crud operations for bank account model """ #
|
||||
|
||||
@@ -10,6 +10,7 @@ from apps.authentication.models import (
|
||||
Province,
|
||||
Organization,
|
||||
OrganizationType,
|
||||
OrganizationStats,
|
||||
BankAccountInformation
|
||||
)
|
||||
from apps.authorization import models as authorize_models
|
||||
@@ -220,3 +221,9 @@ class OrganizationSerializer(serializers.ModelSerializer):
|
||||
instance.national_unique_id = validated_data.get('national_unique_id', instance.national_unique_id)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
|
||||
class OrganizationStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = OrganizationStats
|
||||
fields = '__all__'
|
||||
|
||||
@@ -4,3 +4,6 @@ from django.apps import AppConfig
|
||||
class AuthenticationConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.authentication'
|
||||
|
||||
def ready(self):
|
||||
import apps.authentication.signals
|
||||
|
||||
37
apps/authentication/migrations/0026_organizationstats.py
Normal file
37
apps/authentication/migrations/0026_organizationstats.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Generated by Django 5.0 on 2025-07-16 07:41
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('authentication', '0025_alter_organizationtype_name'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='OrganizationStats',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('create_date', models.DateTimeField(auto_now_add=True)),
|
||||
('modify_date', models.DateTimeField(auto_now=True)),
|
||||
('creator_info', models.CharField(max_length=100, null=True)),
|
||||
('modifier_info', models.CharField(max_length=100, null=True)),
|
||||
('trash', models.BooleanField(default=False)),
|
||||
('total_quota_received', models.PositiveBigIntegerField(default=0)),
|
||||
('total_distributed', models.PositiveBigIntegerField(default=0)),
|
||||
('total_inventory_in', models.PositiveBigIntegerField(default=0)),
|
||||
('total_sold', models.PositiveBigIntegerField(default=0)),
|
||||
('total_buyers', models.PositiveBigIntegerField(default=0)),
|
||||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_createddby', to=settings.AUTH_USER_MODEL)),
|
||||
('modified_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(class)s_modifiedby', to=settings.AUTH_USER_MODEL)),
|
||||
('organization', models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='stats', to='authentication.organization')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -135,6 +135,26 @@ class Organization(BaseModel):
|
||||
super(Organization, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class OrganizationStats(BaseModel):
|
||||
organization = models.OneToOneField(
|
||||
Organization,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='stats',
|
||||
null=True
|
||||
)
|
||||
total_quota_received = models.PositiveBigIntegerField(default=0)
|
||||
total_distributed = models.PositiveBigIntegerField(default=0)
|
||||
total_inventory_in = models.PositiveBigIntegerField(default=0)
|
||||
total_sold = models.PositiveBigIntegerField(default=0)
|
||||
total_buyers = models.PositiveBigIntegerField(default=0)
|
||||
|
||||
def __str__(self):
|
||||
return f'Organization: {self.organization.name}'
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
return super(OrganizationStats, self).save(*args, **kwargs)
|
||||
|
||||
|
||||
class BankAccountInformation(BaseModel):
|
||||
user = models.ForeignKey(
|
||||
User,
|
||||
|
||||
11
apps/authentication/signals.py
Normal file
11
apps/authentication/signals.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.db.models import Sum
|
||||
from apps.product.models import QuotaDistribution
|
||||
from apps.warehouse.models import InventoryQuotaSaleTransaction
|
||||
from django.db.models.signals import post_save, post_delete
|
||||
from django.dispatch import receiver
|
||||
|
||||
|
||||
@receiver([post_save, post_delete], sender=QuotaDistribution)
|
||||
@receiver([post_save, post_delete], sender=InventoryQuotaSaleTransaction)
|
||||
def update_organization_stats(sender, instance, **kwargs):
|
||||
pass
|
||||
Reference in New Issue
Block a user