diff --git a/apps/authentication/api/v1/api.py b/apps/authentication/api/v1/api.py index 895b89c..b7c36b2 100644 --- a/apps/authentication/api/v1/api.py +++ b/apps/authentication/api/v1/api.py @@ -332,17 +332,24 @@ class OrganizationViewSet(BaseViewSet, SoftDeleteMixin, ModelViewSet, DynamicSea def get_organizations_by_province(self, request): """ list of organizations by province """ - 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) + org = get_organization_by_user(request.user) - if 'exclude' in request.GET.keys(): - queryset = queryset.exclude(type__key=request.GET['exclude']) + # if user organization activity is on country level show all + # 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) - 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: serializer = self.serializer_class(page, many=True) diff --git a/apps/authentication/migrations/0044_remove_username_unique.py b/apps/authentication/migrations/0044_remove_username_unique.py deleted file mode 100644 index b3910cc..0000000 --- a/apps/authentication/migrations/0044_remove_username_unique.py +++ /dev/null @@ -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), - ), - ] diff --git a/apps/authentication/migrations/0045_alter_user_username.py b/apps/authentication/migrations/0045_alter_user_username.py deleted file mode 100644 index 15a95c2..0000000 --- a/apps/authentication/migrations/0045_alter_user_username.py +++ /dev/null @@ -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'), - ), - ] diff --git a/apps/product/models.py b/apps/product/models.py index 036dc51..59d2bbb 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -11,6 +11,7 @@ from apps.authorization.models import UserRelations from apps.core.models import BaseModel from apps.herd.models import Rancher from apps.livestock.models import LiveStockType +from apps.product.services.distribution_child import get_all_distribution_child class LivestockGroup(models.TextChoices): @@ -441,12 +442,19 @@ class Quota(BaseModel): return self.quota_weight - distributed_weight 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() persian_date = jdatetime.datetime.fromgregorian(datetime=now) 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): if not self.quota_id: self.quota_id = self.generate_quota_id() @@ -737,6 +745,17 @@ class QuotaDistribution(BaseModel): def __str__(self): 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): if not self.distribution_id: self.distribution_id = self.generate_distribution_id() diff --git a/apps/product/services/distribution_child.py b/apps/product/services/distribution_child.py new file mode 100644 index 0000000..0de171a --- /dev/null +++ b/apps/product/services/distribution_child.py @@ -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 diff --git a/logs/django_requests.log b/logs/django_requests.log index 9178665..51bece5 100644 --- a/logs/django_requests.log +++ b/logs/django_requests.log @@ -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: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: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.