fix - soft delete in quota binary tree & CO level of show organizations

This commit is contained in:
2025-11-01 10:12:58 +03:30
parent 364a15acf8
commit fc67266d64
6 changed files with 60 additions and 44 deletions

View File

@@ -332,17 +332,24 @@ class OrganizationViewSet(BaseViewSet, SoftDeleteMixin, ModelViewSet, DynamicSea
def get_organizations_by_province(self, request): def get_organizations_by_province(self, request):
""" list of organizations by province """ """ list of organizations by province """
if 'province' in request.GET.keys(): org = get_organization_by_user(request.user)
queryset = self.get_queryset(show_my_org=True).filter(province=int(request.GET['province']))
else:
queryset = self.get_queryset().filter(province=request.user.province)
if 'exclude' in request.GET.keys(): # if user organization activity is on country level show all
queryset = queryset.exclude(type__key=request.GET['exclude']) # else show by province
if org.field_of_activity != 'CO':
if 'province' in request.GET.keys():
queryset = self.get_queryset(show_my_org=True).filter(province=int(request.GET['province']))
else:
queryset = self.get_queryset().filter(province=request.user.province)
if 'exclude' in request.GET.keys():
queryset = queryset.exclude(type__key=request.GET['exclude'])
else:
queryset = self.get_queryset()
filtered_query = self.filter_query(queryset) filtered_query = self.filter_query(queryset)
page = self.paginate_queryset(filtered_query.order_by('-create_date')) # paginate queryset page = self.paginate_queryset(filtered_query.order_by('-modify_date')) # paginate queryset
if page is not None: if page is not None:
serializer = self.serializer_class(page, many=True) serializer = self.serializer_class(page, many=True)

View File

@@ -1,17 +0,0 @@
# Generated by Django 5.0 on 2025-10-29 13:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authentication', '0043_alter_user_options_alter_user_managers'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=models.CharField(max_length=150, unique=False),
),
]

View File

@@ -1,19 +0,0 @@
# Generated by Django 5.0 on 2025-11-01 05:12
import django.contrib.auth.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('authentication', '0044_remove_username_unique'),
]
operations = [
migrations.AlterField(
model_name='user',
name='username',
field=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'),
),
]

View File

@@ -11,6 +11,7 @@ from apps.authorization.models import UserRelations
from apps.core.models import BaseModel from apps.core.models import BaseModel
from apps.herd.models import Rancher from apps.herd.models import Rancher
from apps.livestock.models import LiveStockType from apps.livestock.models import LiveStockType
from apps.product.services.distribution_child import get_all_distribution_child
class LivestockGroup(models.TextChoices): class LivestockGroup(models.TextChoices):
@@ -441,12 +442,19 @@ class Quota(BaseModel):
return self.quota_weight - distributed_weight return self.quota_weight - distributed_weight
def is_in_valid_time(self): def is_in_valid_time(self):
""" check if quota allowed time for distribute, sale, etc is expired """ """ check if quota allowed time for distribute, sale and... is expired """
now = datetime.now() now = datetime.now()
persian_date = jdatetime.datetime.fromgregorian(datetime=now) persian_date = jdatetime.datetime.fromgregorian(datetime=now)
return persian_date.month in self.sale_license return persian_date.month in self.sale_license
def soft_delete(self):
self.trash = True
self.save(update_fields=['trash'])
for dist in self.distributions_assigned.all():
dist.soft_delete()
def save(self, calculate_final_price=None, *args, **kwargs): def save(self, calculate_final_price=None, *args, **kwargs):
if not self.quota_id: if not self.quota_id:
self.quota_id = self.generate_quota_id() self.quota_id = self.generate_quota_id()
@@ -737,6 +745,17 @@ class QuotaDistribution(BaseModel):
def __str__(self): def __str__(self):
return f"{self.distribution_id}-" return f"{self.distribution_id}-"
def soft_delete(self):
self.trash = True
self.save(update_fields=['trash'])
childs = get_all_distribution_child(self) # noqa
for child in childs:
child.soft_delete()
for entry in self.inventory_entry.all():
entry.soft_delete()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if not self.distribution_id: if not self.distribution_id:
self.distribution_id = self.generate_distribution_id() self.distribution_id = self.generate_distribution_id()

View File

@@ -0,0 +1,13 @@
import typing
def get_all_distribution_child(distribution: object = None) -> typing.Any:
"""
get all child of an distribution
"""
descendants = []
children = distribution.children.all() # noqa
for child in children:
descendants.append(child)
descendants.extend(get_all_distribution_child(child))
return descendants

View File

@@ -598,3 +598,16 @@ AssertionError: .validate() should return the validated data
[2025-10-29 17:41:23,861] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\models.py changed, reloading. [2025-10-29 17:41:23,861] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\models.py changed, reloading.
[2025-10-29 17:41:27,025] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader [2025-10-29 17:41:27,025] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 08:44:37,286] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader [2025-11-01 08:44:37,286] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 08:44:41,864] INFO django.server | IP: - | Path: - | "POST /captcha/ HTTP/1.1" 200 714
[2025-11-01 08:45:21,617] INFO django.server | IP: - | Path: - | "POST /auth/api/v1/login/ HTTP/1.1" 200 681
[2025-11-01 08:55:40,967] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\models.py changed, reloading.
[2025-11-01 08:55:49,079] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 08:57:54,993] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\models.py changed, reloading.
[2025-11-01 08:57:57,325] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 09:41:30,637] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\product\models.py changed, reloading.
[2025-11-01 09:41:34,447] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 10:02:43,730] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\product\models.py changed, reloading.
[2025-11-01 10:02:46,022] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 10:02:53,718] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\product\services\distribution_child.py changed, reloading.
[2025-11-01 10:02:59,031] INFO django.utils.autoreload | IP: - | Path: - | Watching for file changes with StatReloader
[2025-11-01 10:12:17,522] INFO django.utils.autoreload | IP: - | Path: - | D:\Project\Rasaddam_Backend\apps\authentication\api\v1\api.py changed, reloading.