fix --> soft delete distribution batch
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from django.db.models import Sum
|
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
|
||||||
@@ -16,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
|
||||||
@@ -55,7 +61,7 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
parent = tag_dist_batch.parent
|
parent = tag_dist_batch.parent
|
||||||
if parent:
|
if parent:
|
||||||
parent.total_distributed_tag_count = parent.children.aggregate(
|
parent.total_distributed_tag_count = parent.children.aggregate(
|
||||||
total=Sum('total_tag_count')
|
total=Coalesce(Sum('total_tag_count'), 0)
|
||||||
)['total']
|
)['total']
|
||||||
parent.remaining_tag_count = (
|
parent.remaining_tag_count = (
|
||||||
parent.total_tag_count - parent.total_distributed_tag_count
|
parent.total_tag_count - parent.total_distributed_tag_count
|
||||||
@@ -67,113 +73,3 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
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'
|
|
||||||
# ])
|
|
||||||
|
|||||||
@@ -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