code + related_object + user should be unique together.
Source code in hypha/apply/todo/services.py
| def validate_user_uniqueness(code, user, related_obj):
"""
code + related_object + user should be unique together.
"""
matching_tasks = Task.objects.filter(
code=code,
related_content_type=ContentType.objects.get_for_model(related_obj).id,
related_object_id=related_obj.id,
)
if matching_tasks.filter(user=user).exists():
# if same task already assigned to the same user
# raise ValidationError("Task is already assigned to the user") # :todo: add validation msg as a log msg?
return False
else:
# if same task is already assigned to user's user_group
user_group_matching_tasks = matching_tasks.annotate(
group_count=Count("user_group")
).filter(group_count=len(user.groups.all()))
for group in user.groups.all():
user_group_matching_tasks = user_group_matching_tasks.filter(
user_group__id=group.id
)
if user_group_matching_tasks.exists():
# raise ValidationError("Task is already assigned to user's group")
return False
return True
|