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']
|
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
|
raise PermissionDenied("دسترسی غیرمجاز") # noqa
|
||||||
|
|
||||||
for dist_data in data['dists']:
|
for dist_data in data['dists']:
|
||||||
species = dist_data['species_code']
|
species = dist_data['species_code']
|
||||||
count = dist_data['count']
|
count = dist_data['count']
|
||||||
parent_tag_distribution = TagDistribution.objects.get(
|
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(
|
tags = Tag.objects.filter(
|
||||||
distributions__tag_distribution_batch=parent_batch,
|
distributions__tag_distribution_batch=parent_batch,
|
||||||
species_code=species,
|
species_code=species,
|
||||||
status='R',
|
status='R',
|
||||||
organization=org
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if tags.count() < count:
|
if tags.count() < count:
|
||||||
@@ -199,6 +199,7 @@ class TagDistributionService:
|
|||||||
|
|
||||||
dist = TagDistribution.objects.create(
|
dist = TagDistribution.objects.create(
|
||||||
parent=parent_tag_distribution,
|
parent=parent_tag_distribution,
|
||||||
|
batch=batch,
|
||||||
assigner_org=org,
|
assigner_org=org,
|
||||||
assigned_org=assigned_org,
|
assigned_org=assigned_org,
|
||||||
species_code=species,
|
species_code=species,
|
||||||
@@ -262,13 +263,16 @@ class TagDistributionService:
|
|||||||
|
|
||||||
for dist_data in data['dists']:
|
for dist_data in data['dists']:
|
||||||
species = dist_data['species_code']
|
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']
|
count = dist_data['count']
|
||||||
|
|
||||||
tags = Tag.objects.filter(
|
tags = Tag.objects.filter(
|
||||||
distributions__tag_distribution_batch=parent_batch,
|
distributions__tag_distribution_batch=parent_batch,
|
||||||
species_code=species,
|
species_code=species,
|
||||||
status='R',
|
status='R',
|
||||||
organization=org
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if tags.count() < count:
|
if tags.count() < count:
|
||||||
@@ -278,7 +282,8 @@ class TagDistributionService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
dist = TagDistribution.objects.create(
|
dist = TagDistribution.objects.create(
|
||||||
parent=None,
|
parent=parent_tag_distribution,
|
||||||
|
batch=batch,
|
||||||
assigner_org=org,
|
assigner_org=org,
|
||||||
assigned_org=assigned_org,
|
assigned_org=assigned_org,
|
||||||
species_code=species,
|
species_code=species,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ def update_batch_on_distribution_change(
|
|||||||
distributed_tags = Tag.objects.filter(
|
distributed_tags = Tag.objects.filter(
|
||||||
distributions__batch=batch
|
distributions__batch=batch
|
||||||
).distinct().count()
|
).distinct().count()
|
||||||
print(distributed_tags)
|
|
||||||
|
|
||||||
batch.total_distributed_tags = distributed_tags
|
batch.total_distributed_tags = distributed_tags
|
||||||
batch.total_remaining_tags = (
|
batch.total_remaining_tags = (
|
||||||
@@ -54,8 +53,10 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
tag_dist_batch = instance
|
tag_dist_batch = instance
|
||||||
if tag_dist_batch.parent:
|
if tag_dist_batch.parent:
|
||||||
tag_dist_batch.parent.total_distributed_tag_count += tag_dist_batch.total_tag_count
|
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.parent.save(update_fields=['total_distributed_tag_count', 'remaining_tag_count'])
|
||||||
|
|
||||||
tag_dist_batch.remaining_tag_count = tag_dist_batch.total_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()
|
# total = instance.tag.count()
|
||||||
#
|
#
|
||||||
# instance.total_tag_count = total
|
# instance.total_tag_count = total
|
||||||
# instance.distributed_number = total
|
# instance.distributed_number = total - instance.remaining_number
|
||||||
# instance.remaining_number = 0
|
# instance.remaining_number = total - instance.distributed_number
|
||||||
# instance.save(update_fields=[
|
# instance.save(update_fields=[
|
||||||
# 'total_tag_count',
|
# 'total_tag_count',
|
||||||
# 'distributed_number',
|
# 'distributed_number',
|
||||||
@@ -114,25 +115,6 @@ def calculate_tag_distribution_detail(sender, instance: TagDistributionBatch, **
|
|||||||
# 'status'
|
# '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)
|
# @receiver(m2m_changed, sender=TagDistribution.tag.through)
|
||||||
# def on_tags_removed_from_distribution(sender, instance, action, pk_set, **kwargs):
|
# def on_tags_removed_from_distribution(sender, instance, action, pk_set, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user