Compare commits
4 Commits
main
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
| 3258991014 | |||
| fb58e6c3aa | |||
| c5b87e8591 | |||
| 0dd145131f |
@@ -186,7 +186,9 @@ class TagDistributionService:
|
||||
parent_tag_distribution = TagDistribution.objects.get(
|
||||
id=dist_data['parent_tag_distribution']
|
||||
)
|
||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
||||
batch = TagBatch.objects.get(
|
||||
batch_identity=dist_data.get('batch_identity')
|
||||
) if dist_data.get('batch_identity') else None
|
||||
|
||||
tags = Tag.objects.filter(
|
||||
distributions__tag_distribution_batch=parent_batch,
|
||||
@@ -221,7 +223,7 @@ class TagDistributionService:
|
||||
assigner_org=org,
|
||||
assigned_org=assigned_org,
|
||||
total_tag_count=total_counted_tags,
|
||||
distribution_type='batch',
|
||||
distribution_type=parent_batch.distribution_type,
|
||||
dist_batch_identity=generate_unique_code(
|
||||
f"{random.randint(1000, 9999)}"
|
||||
)
|
||||
@@ -243,7 +245,7 @@ class TagDistributionService:
|
||||
with transaction.atomic():
|
||||
|
||||
if tag_batch.assigner_org != org:
|
||||
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید")
|
||||
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید") # noqa
|
||||
|
||||
for dist in tag_batch.distributions.all():
|
||||
dist.tag.all().update(
|
||||
@@ -266,7 +268,9 @@ class TagDistributionService:
|
||||
parent_tag_distribution = TagDistribution.objects.get(
|
||||
id=dist_data['parent_tag_distribution']
|
||||
)
|
||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
||||
batch = TagBatch.objects.get(
|
||||
batch_identity=dist_data.get('batch_identity')
|
||||
) if dist_data.get('batch_identity') else None
|
||||
count = dist_data['count']
|
||||
|
||||
tags = Tag.objects.filter(
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from django.db.models import Sum
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.db.models.signals import m2m_changed
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
@@ -15,14 +17,19 @@ def update_batch_on_distribution_change(
|
||||
if not instance.batch:
|
||||
return
|
||||
|
||||
if instance.parent:
|
||||
return
|
||||
|
||||
batch = instance.batch
|
||||
|
||||
distributions = TagDistribution.objects.filter(batch=batch)
|
||||
|
||||
distributed_tags = Tag.objects.filter(
|
||||
distributions__batch=batch
|
||||
distributions__batch=batch,
|
||||
status__in=['R', 'A'],
|
||||
).distinct().count()
|
||||
|
||||
print("distributed_tags", distributed_tags)
|
||||
batch.total_distributed_tags = distributed_tags
|
||||
batch.total_remaining_tags = (
|
||||
int(batch.request_number) - distributed_tags
|
||||
@@ -53,121 +60,16 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
||||
tag_dist_batch = instance
|
||||
parent = tag_dist_batch.parent
|
||||
if parent:
|
||||
# parent.total_distributed_tag_count += tag_dist_batch.total_tag_count
|
||||
parent.remaining_tag_count = 20
|
||||
print(parent.remaining_tag_count)
|
||||
parent.save(update_fields=['remaining_tag_count'])
|
||||
parent.total_distributed_tag_count = parent.children.aggregate(
|
||||
total=Coalesce(Sum('total_tag_count'), 0)
|
||||
)['total']
|
||||
parent.remaining_tag_count = (
|
||||
parent.total_tag_count - parent.total_distributed_tag_count
|
||||
)
|
||||
parent.parent_flag = True
|
||||
parent.save(update_fields=['remaining_tag_count', 'total_distributed_tag_count'])
|
||||
|
||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
||||
instance.flag = True
|
||||
tag_dist_batch.save(update_fields=['remaining_tag_count'])
|
||||
|
||||
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
||||
# def on_tags_added_to_distribution(sender, instance, action, pk_set, **kwargs):
|
||||
# if action != 'post_add':
|
||||
# return
|
||||
#
|
||||
# if not pk_set:
|
||||
# return
|
||||
#
|
||||
# with transaction.atomic():
|
||||
#
|
||||
# Tag.objects.filter(
|
||||
# id__in=pk_set
|
||||
# ).update(
|
||||
# status='R',
|
||||
# organization=instance.assigned_org
|
||||
# )
|
||||
#
|
||||
# total = instance.tag.count()
|
||||
#
|
||||
# instance.total_tag_count = total
|
||||
# instance.distributed_number = total - instance.remaining_number
|
||||
# instance.remaining_number = total - instance.distributed_number
|
||||
# instance.save(update_fields=[
|
||||
# 'total_tag_count',
|
||||
# 'distributed_number',
|
||||
# 'remaining_number'
|
||||
# ])
|
||||
#
|
||||
# if instance.batch:
|
||||
# batch = instance.batch
|
||||
#
|
||||
# distributed_tags = Tag.objects.filter(
|
||||
# batches=batch,
|
||||
# status__in=['R', 'A']
|
||||
# ).count()
|
||||
#
|
||||
# total_tags = batch.tag.count()
|
||||
#
|
||||
# batch.total_distributed_tags = distributed_tags
|
||||
# batch.total_remaining_tags = total_tags - distributed_tags
|
||||
# batch.status = (
|
||||
# 'distributed'
|
||||
# if batch.total_remaining_tags == 0
|
||||
# else 'created'
|
||||
# )
|
||||
#
|
||||
# batch.save(update_fields=[
|
||||
# 'total_distributed_tags',
|
||||
# 'total_remaining_tags',
|
||||
# 'status'
|
||||
# ])
|
||||
#
|
||||
#
|
||||
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
||||
# def on_tags_removed_from_distribution(sender, instance, action, pk_set, **kwargs):
|
||||
# if action not in ['post_remove', 'post_clear']:
|
||||
# return
|
||||
#
|
||||
# if action == 'post_clear':
|
||||
# pk_set = list(instance.tag.values_list('id', flat=True))
|
||||
#
|
||||
# if not pk_set:
|
||||
# return
|
||||
#
|
||||
# with transaction.atomic():
|
||||
#
|
||||
# Tag.objects.filter(id__in=pk_set).update(
|
||||
# status='R',
|
||||
# organization=instance.assigner_org
|
||||
# )
|
||||
#
|
||||
# total = instance.tag.count()
|
||||
# instance.total_tag_count = total
|
||||
# instance.distributed_number = total
|
||||
# instance.remaining_number = 0
|
||||
# instance.save(update_fields=[
|
||||
# 'total_tag_count',
|
||||
# 'distributed_number',
|
||||
# 'remaining_number'
|
||||
# ])
|
||||
#
|
||||
# if instance.batch:
|
||||
# batch = instance.batch
|
||||
# distributed_tags = Tag.objects.filter(
|
||||
# batches=batch,
|
||||
# status__in=['R', 'A']
|
||||
# ).count()
|
||||
# total_tags = batch.tag.count()
|
||||
# batch.total_distributed_tags = distributed_tags
|
||||
# batch.total_remaining_tags = total_tags - distributed_tags
|
||||
# batch.status = 'distributed' if batch.total_remaining_tags == 0 else 'created'
|
||||
# batch.save(update_fields=[
|
||||
# 'total_distributed_tags',
|
||||
# 'total_remaining_tags',
|
||||
# 'status'
|
||||
# ])
|
||||
#
|
||||
# for dist_batch in instance.tag_distribution_batch.all():
|
||||
# total_dist = dist_batch.distributions.aggregate(
|
||||
# total=Count('tag')
|
||||
# ).get('total', 0)
|
||||
# dist_batch.total_distributed_tag_count = total_dist
|
||||
# dist_batch.remaining_tag_count = dist_batch.total_tag_count - total_dist
|
||||
# dist_batch.is_closed = dist_batch.remaining_tag_count == 0
|
||||
# dist_batch.save(update_fields=[
|
||||
# 'total_distributed_tag_count',
|
||||
# 'remaining_tag_count',
|
||||
# 'is_closed'
|
||||
# ])
|
||||
if not getattr(instance, 'parent_flag', False):
|
||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
||||
instance.flag = True
|
||||
tag_dist_batch.save(update_fields=['remaining_tag_count'])
|
||||
|
||||
@@ -478,9 +478,9 @@ class TagDistributionViewSet(
|
||||
@action(
|
||||
methods=['put'],
|
||||
detail=True,
|
||||
url_path='distribute_distribution',
|
||||
url_name='distribute_distribution',
|
||||
name='distribute_distribution',
|
||||
url_path='edit_distribute_distribution',
|
||||
url_name='edit_distribute_distribution',
|
||||
name='edit_distribute_distribution',
|
||||
)
|
||||
def update_distribute_from_distribution(self, request, pk=None):
|
||||
"""
|
||||
@@ -558,9 +558,9 @@ class TagDistributionViewSet(
|
||||
|
||||
class TagDistributionBatchViewSet(
|
||||
BaseViewSet,
|
||||
viewsets.ModelViewSet,
|
||||
SoftDeleteMixin,
|
||||
DynamicSearchMixin,
|
||||
viewsets.ModelViewSet,
|
||||
TagDistributionService
|
||||
):
|
||||
queryset = tag_models.TagDistributionBatch.objects.all()
|
||||
@@ -612,6 +612,8 @@ class TagDistributionBatchViewSet(
|
||||
dist_batch.is_closed = True
|
||||
dist_batch.save()
|
||||
dist_batch.distributions.all().update(is_closed=True) # close distributions of batch
|
||||
for distribute in dist_batch.distributions.all():
|
||||
distribute.tag.all().update(status='F')
|
||||
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
||||
@@ -670,3 +672,19 @@ class TagDistributionBatchViewSet(
|
||||
dashboard_data = self.distribution_batch_main_dashboard(org=org, is_closed=params.get('is_closed'))
|
||||
|
||||
return Response(dashboard_data, status=status.HTTP_200_OK)
|
||||
|
||||
def destroy(self, request, pk=None, *args, **kwargs):
|
||||
"""
|
||||
delete tag distribution batch and free their tag from distribute
|
||||
"""
|
||||
|
||||
dist_batch = self.get_object()
|
||||
|
||||
for distribute in dist_batch.distributions.all():
|
||||
distribute.tag.all().update(status='F')
|
||||
distribute.tag.clear()
|
||||
distribute.soft_delete()
|
||||
|
||||
dist_batch.soft_delete()
|
||||
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
||||
Reference in New Issue
Block a user