fix --> filter tag distributions/batch by is_closed/ edit tag distributiosn & batch
This commit is contained in:
@@ -79,16 +79,16 @@ class TagDistributionService:
|
|||||||
|
|
||||||
return {'tag_distributions': distributions, 'distributions_batch': distributions_batch}
|
return {'tag_distributions': distributions, 'distributions_batch': distributions_batch}
|
||||||
|
|
||||||
def edit_distribution(self, batch: TagDistributionBatch = None, data: dict = None, org: Organization = None):
|
def edit_distribution(self, dist_batch: TagDistributionBatch = None, data: dict = None, org: Organization = None):
|
||||||
"""
|
"""
|
||||||
edit record of distributed tags
|
edit record of distributed tags
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# clear and hard delete of distributions
|
# clear and hard delete of distributions
|
||||||
distributions = batch.distributions.all()
|
distributions = dist_batch.distributions.all()
|
||||||
for dist in distributions:
|
for dist in distributions: # free distributed tags from reserve
|
||||||
pass
|
dist.tag.all().update(status='F')
|
||||||
batch.distributions.clear()
|
dist_batch.distributions.clear()
|
||||||
distributions.delete()
|
distributions.delete()
|
||||||
|
|
||||||
# create new distributions and update batch
|
# create new distributions and update batch
|
||||||
@@ -139,3 +139,11 @@ class TagDistributionService:
|
|||||||
distributions.append(dist)
|
distributions.append(dist)
|
||||||
|
|
||||||
total_counted_tags += distribution.get('count')
|
total_counted_tags += distribution.get('count')
|
||||||
|
|
||||||
|
# update distribution batch
|
||||||
|
dist_batch.assigned_org = assigned_org
|
||||||
|
dist_batch.total_tag_count = total_counted_tags
|
||||||
|
dist_batch.distribution_type = distribution_type # noqa
|
||||||
|
dist_batch.distributions.add(*distributions)
|
||||||
|
|
||||||
|
return {'tag_distributions': distributions, 'distributions_batch': dist_batch}
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ class TagDistributionViewSet(
|
|||||||
"""
|
"""
|
||||||
list of tag distributions
|
list of tag distributions
|
||||||
"""
|
"""
|
||||||
queryset = self.get_queryset(visibility_by_org_scope=True).order_by('-create_date')
|
queryset = self.get_queryset(visibility_by_org_scope=True).filter(is_closed=False).order_by('-create_date')
|
||||||
|
|
||||||
queryset = self.filter_queryset(self.filter_query(queryset))
|
queryset = self.filter_queryset(self.filter_query(queryset))
|
||||||
|
|
||||||
@@ -392,6 +392,20 @@ class TagDistributionViewSet(
|
|||||||
serializer = self.serializer_class(distribution_data.get('tag_distributions'), many=True)
|
serializer = self.serializer_class(distribution_data.get('tag_distributions'), many=True)
|
||||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def update(self, request, pk=None, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
edit tag distribution with/without batch in random
|
||||||
|
"""
|
||||||
|
|
||||||
|
org = get_organization_by_user(request.user)
|
||||||
|
data = request.data.copy()
|
||||||
|
dist_batch = tag_models.TagDistributionBatch.objects.get(id=pk)
|
||||||
|
|
||||||
|
distribution_data = self.edit_distribution(org=org, data=data, dist_batch=dist_batch)
|
||||||
|
|
||||||
|
serializer = self.serializer_class(distribution_data.get('tag_distributions'), many=True)
|
||||||
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(
|
@action(
|
||||||
methods=['get'],
|
methods=['get'],
|
||||||
detail=True,
|
detail=True,
|
||||||
@@ -403,7 +417,28 @@ class TagDistributionViewSet(
|
|||||||
"""
|
"""
|
||||||
get distributions by batch
|
get distributions by batch
|
||||||
"""
|
"""
|
||||||
pass
|
batch = tag_models.TagDistributionBatch.objects.get(id=pk)
|
||||||
|
distributions = batch.distributions.all()
|
||||||
|
|
||||||
|
page = self.paginate_queryset(distributions)
|
||||||
|
if page is not None: # noqa
|
||||||
|
serializer = self.get_serializer(page, many=True)
|
||||||
|
return self.get_paginated_response(serializer.data)
|
||||||
|
return Response(self.serializer_class(distributions).data)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['post'],
|
||||||
|
detail=True,
|
||||||
|
url_name='close_distribution',
|
||||||
|
url_path='close_distribution',
|
||||||
|
name='close_distribution',
|
||||||
|
)
|
||||||
|
def close_tag_distribution(self, request, pk=None):
|
||||||
|
distribution = self.get_object()
|
||||||
|
distribution.is_closed = True
|
||||||
|
distribution.save()
|
||||||
|
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class TagDistributionBatchViewSet(
|
class TagDistributionBatchViewSet(
|
||||||
@@ -429,7 +464,7 @@ class TagDistributionBatchViewSet(
|
|||||||
list of tag distribution batches
|
list of tag distribution batches
|
||||||
"""
|
"""
|
||||||
|
|
||||||
queryset = self.get_queryset(visibility_by_org_scope=True).order_by('-create_date')
|
queryset = self.get_queryset(visibility_by_org_scope=True).filter(is_closed=False).order_by('-create_date')
|
||||||
|
|
||||||
queryset = self.filter_query(self.filter_queryset(queryset))
|
queryset = self.filter_query(self.filter_queryset(queryset))
|
||||||
|
|
||||||
@@ -438,3 +473,20 @@ class TagDistributionBatchViewSet(
|
|||||||
serializer = self.get_serializer(page, many=True)
|
serializer = self.get_serializer(page, many=True)
|
||||||
return self.get_paginated_response(serializer.data)
|
return self.get_paginated_response(serializer.data)
|
||||||
return Response(self.serializer_class(queryset).data)
|
return Response(self.serializer_class(queryset).data)
|
||||||
|
|
||||||
|
@action(
|
||||||
|
methods=['post'],
|
||||||
|
detail=True,
|
||||||
|
url_name='close_dist_batch',
|
||||||
|
url_path='close_dist_batch',
|
||||||
|
name='close_dist_batch',
|
||||||
|
)
|
||||||
|
def close_tag_dist_batch(self, request, pk=None):
|
||||||
|
dist_batch = self.get_object()
|
||||||
|
|
||||||
|
# close distribution batch
|
||||||
|
dist_batch.is_closed = True
|
||||||
|
dist_batch.save()
|
||||||
|
dist_batch.distributions.all().update(is_closed=True) # close distributions of batch
|
||||||
|
|
||||||
|
return Response(status=status.HTTP_200_OK)
|
||||||
|
|||||||
Reference in New Issue
Block a user