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(
|
parent_tag_distribution = TagDistribution.objects.get(
|
||||||
id=dist_data['parent_tag_distribution']
|
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(
|
tags = Tag.objects.filter(
|
||||||
distributions__tag_distribution_batch=parent_batch,
|
distributions__tag_distribution_batch=parent_batch,
|
||||||
@@ -221,7 +223,7 @@ class TagDistributionService:
|
|||||||
assigner_org=org,
|
assigner_org=org,
|
||||||
assigned_org=assigned_org,
|
assigned_org=assigned_org,
|
||||||
total_tag_count=total_counted_tags,
|
total_tag_count=total_counted_tags,
|
||||||
distribution_type='batch',
|
distribution_type=parent_batch.distribution_type,
|
||||||
dist_batch_identity=generate_unique_code(
|
dist_batch_identity=generate_unique_code(
|
||||||
f"{random.randint(1000, 9999)}"
|
f"{random.randint(1000, 9999)}"
|
||||||
)
|
)
|
||||||
@@ -243,7 +245,7 @@ class TagDistributionService:
|
|||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
|
||||||
if tag_batch.assigner_org != org:
|
if tag_batch.assigner_org != org:
|
||||||
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید")
|
raise PermissionDenied("اجازه ویرایش این توزیع را ندارید") # noqa
|
||||||
|
|
||||||
for dist in tag_batch.distributions.all():
|
for dist in tag_batch.distributions.all():
|
||||||
dist.tag.all().update(
|
dist.tag.all().update(
|
||||||
@@ -266,7 +268,9 @@ class TagDistributionService:
|
|||||||
parent_tag_distribution = TagDistribution.objects.get(
|
parent_tag_distribution = TagDistribution.objects.get(
|
||||||
id=dist_data['parent_tag_distribution']
|
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']
|
count = dist_data['count']
|
||||||
|
|
||||||
tags = Tag.objects.filter(
|
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 m2m_changed
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
@@ -15,14 +17,19 @@ def update_batch_on_distribution_change(
|
|||||||
if not instance.batch:
|
if not instance.batch:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if instance.parent:
|
||||||
|
return
|
||||||
|
|
||||||
batch = instance.batch
|
batch = instance.batch
|
||||||
|
|
||||||
distributions = TagDistribution.objects.filter(batch=batch)
|
distributions = TagDistribution.objects.filter(batch=batch)
|
||||||
|
|
||||||
distributed_tags = Tag.objects.filter(
|
distributed_tags = Tag.objects.filter(
|
||||||
distributions__batch=batch
|
distributions__batch=batch,
|
||||||
|
status__in=['R', 'A'],
|
||||||
).distinct().count()
|
).distinct().count()
|
||||||
|
|
||||||
|
print("distributed_tags", distributed_tags)
|
||||||
batch.total_distributed_tags = distributed_tags
|
batch.total_distributed_tags = distributed_tags
|
||||||
batch.total_remaining_tags = (
|
batch.total_remaining_tags = (
|
||||||
int(batch.request_number) - distributed_tags
|
int(batch.request_number) - distributed_tags
|
||||||
@@ -53,121 +60,16 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
tag_dist_batch = instance
|
tag_dist_batch = instance
|
||||||
parent = tag_dist_batch.parent
|
parent = tag_dist_batch.parent
|
||||||
if parent:
|
if parent:
|
||||||
# parent.total_distributed_tag_count += tag_dist_batch.total_tag_count
|
parent.total_distributed_tag_count = parent.children.aggregate(
|
||||||
parent.remaining_tag_count = 20
|
total=Coalesce(Sum('total_tag_count'), 0)
|
||||||
print(parent.remaining_tag_count)
|
)['total']
|
||||||
parent.save(update_fields=['remaining_tag_count'])
|
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'])
|
||||||
|
|
||||||
|
if not getattr(instance, 'parent_flag', False):
|
||||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
||||||
instance.flag = True
|
instance.flag = True
|
||||||
tag_dist_batch.save(update_fields=['remaining_tag_count'])
|
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'
|
|
||||||
# ])
|
|
||||||
|
|||||||
@@ -478,9 +478,9 @@ class TagDistributionViewSet(
|
|||||||
@action(
|
@action(
|
||||||
methods=['put'],
|
methods=['put'],
|
||||||
detail=True,
|
detail=True,
|
||||||
url_path='distribute_distribution',
|
url_path='edit_distribute_distribution',
|
||||||
url_name='distribute_distribution',
|
url_name='edit_distribute_distribution',
|
||||||
name='distribute_distribution',
|
name='edit_distribute_distribution',
|
||||||
)
|
)
|
||||||
def update_distribute_from_distribution(self, request, pk=None):
|
def update_distribute_from_distribution(self, request, pk=None):
|
||||||
"""
|
"""
|
||||||
@@ -558,9 +558,9 @@ class TagDistributionViewSet(
|
|||||||
|
|
||||||
class TagDistributionBatchViewSet(
|
class TagDistributionBatchViewSet(
|
||||||
BaseViewSet,
|
BaseViewSet,
|
||||||
|
viewsets.ModelViewSet,
|
||||||
SoftDeleteMixin,
|
SoftDeleteMixin,
|
||||||
DynamicSearchMixin,
|
DynamicSearchMixin,
|
||||||
viewsets.ModelViewSet,
|
|
||||||
TagDistributionService
|
TagDistributionService
|
||||||
):
|
):
|
||||||
queryset = tag_models.TagDistributionBatch.objects.all()
|
queryset = tag_models.TagDistributionBatch.objects.all()
|
||||||
@@ -612,6 +612,8 @@ class TagDistributionBatchViewSet(
|
|||||||
dist_batch.is_closed = True
|
dist_batch.is_closed = True
|
||||||
dist_batch.save()
|
dist_batch.save()
|
||||||
dist_batch.distributions.all().update(is_closed=True) # close distributions of batch
|
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)
|
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'))
|
dashboard_data = self.distribution_batch_main_dashboard(org=org, is_closed=params.get('is_closed'))
|
||||||
|
|
||||||
return Response(dashboard_data, status=status.HTTP_200_OK)
|
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