import --> distribute from distribution and calculate some values in model
This commit is contained in:
@@ -177,21 +177,21 @@ class TagDistributionService:
|
||||
id=data['parent_distribution_batch']
|
||||
)
|
||||
|
||||
if parent_batch.assigned_org != org:
|
||||
if parent_batch.assigned_org != org and org.type.key != 'ADM':
|
||||
raise PermissionDenied("دسترسی غیرمجاز") # noqa
|
||||
|
||||
for dist_data in data['dists']:
|
||||
species = dist_data['species_code']
|
||||
count = dist_data['count']
|
||||
parent_tag_distribution = TagDistribution.objects.get(
|
||||
id=data['parent_tag_distribution']
|
||||
id=dist_data['parent_tag_distribution']
|
||||
)
|
||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
||||
|
||||
tags = Tag.objects.filter(
|
||||
distributions__tag_distribution_batch=parent_batch,
|
||||
species_code=species,
|
||||
status='R',
|
||||
organization=org
|
||||
)
|
||||
|
||||
if tags.count() < count:
|
||||
@@ -199,6 +199,7 @@ class TagDistributionService:
|
||||
|
||||
dist = TagDistribution.objects.create(
|
||||
parent=parent_tag_distribution,
|
||||
batch=batch,
|
||||
assigner_org=org,
|
||||
assigned_org=assigned_org,
|
||||
species_code=species,
|
||||
@@ -262,13 +263,16 @@ class TagDistributionService:
|
||||
|
||||
for dist_data in data['dists']:
|
||||
species = dist_data['species_code']
|
||||
parent_tag_distribution = TagDistribution.objects.get(
|
||||
id=dist_data['parent_tag_distribution']
|
||||
)
|
||||
batch = TagBatch.objects.get(batch_identity=dist_data.get('batch_identity'))
|
||||
count = dist_data['count']
|
||||
|
||||
tags = Tag.objects.filter(
|
||||
distributions__tag_distribution_batch=parent_batch,
|
||||
species_code=species,
|
||||
status='R',
|
||||
organization=org
|
||||
)
|
||||
|
||||
if tags.count() < count:
|
||||
@@ -278,7 +282,8 @@ class TagDistributionService:
|
||||
)
|
||||
|
||||
dist = TagDistribution.objects.create(
|
||||
parent=None,
|
||||
parent=parent_tag_distribution,
|
||||
batch=batch,
|
||||
assigner_org=org,
|
||||
assigned_org=assigned_org,
|
||||
species_code=species,
|
||||
|
||||
@@ -22,7 +22,6 @@ def update_batch_on_distribution_change(
|
||||
distributed_tags = Tag.objects.filter(
|
||||
distributions__batch=batch
|
||||
).distinct().count()
|
||||
print(distributed_tags)
|
||||
|
||||
batch.total_distributed_tags = distributed_tags
|
||||
batch.total_remaining_tags = (
|
||||
@@ -54,8 +53,10 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
||||
tag_dist_batch = instance
|
||||
if tag_dist_batch.parent:
|
||||
tag_dist_batch.parent.total_distributed_tag_count += tag_dist_batch.total_tag_count
|
||||
tag_dist_batch.parent.remaining_tag_count = tag_dist_batch.parent.total_tag_count - tag_dist_batch.total_tag_count
|
||||
|
||||
tag_dist_batch.parent.remaining_tag_count = (
|
||||
tag_dist_batch.parent.total_tag_count - tag_dist_batch.parent.total_distributed_tag_count
|
||||
)
|
||||
print(tag_dist_batch.parent.remaining_tag_count)
|
||||
tag_dist_batch.parent.save(update_fields=['total_distributed_tag_count', 'remaining_tag_count'])
|
||||
|
||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_tag_count
|
||||
@@ -82,8 +83,8 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
||||
# total = instance.tag.count()
|
||||
#
|
||||
# instance.total_tag_count = total
|
||||
# instance.distributed_number = total
|
||||
# instance.remaining_number = 0
|
||||
# instance.distributed_number = total - instance.remaining_number
|
||||
# instance.remaining_number = total - instance.distributed_number
|
||||
# instance.save(update_fields=[
|
||||
# 'total_tag_count',
|
||||
# 'distributed_number',
|
||||
@@ -114,25 +115,6 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
||||
# 'status'
|
||||
# ])
|
||||
#
|
||||
# for dist_batch in instance.tag_distribution_batch.all():
|
||||
# agg = dist_batch.distributions.aggregate(
|
||||
# total=Count('tag')
|
||||
# )
|
||||
#
|
||||
# total_dist = agg.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'
|
||||
# ])
|
||||
#
|
||||
#
|
||||
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
||||
# def on_tags_removed_from_distribution(sender, instance, action, pk_set, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user