Skip to content

Commit

Permalink
Merge pull request #44 from AI4Bharat/unassign_task_changes
Browse files Browse the repository at this point in the history
Refactored unassign_task endpoints
  • Loading branch information
aparna-aa authored Mar 15, 2024
2 parents 4bfd8a8 + c85d85b commit 1dcae3d
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 184 deletions.
81 changes: 81 additions & 0 deletions backend/projects/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import ast
from typing import Tuple
from dateutil.parser import parse as date_parse
import re
import nltk
from projects.models import Project
from rest_framework.response import Response
from rest_framework import status
from tasks.models import Annotation as Annotation_model
from users.models import User

from dataset.models import Instruction, Interaction
from tasks.models import Annotation, REVIEWER_ANNOTATION, SUPER_CHECKER_ANNOTATION
Expand Down Expand Up @@ -293,3 +299,78 @@ def assign_attributes_and_save_dataitem(
data_item.save()
task.output_data = data_item
task.save()


def get_user_from_query_params(
request,
user_type,
pk,
):
user_id_key = user_type + "_id"
if user_id_key in dict(request.query_params).keys():
user_id = request.query_params[user_id_key]
project = Project.objects.get(pk=pk)
user = User.objects.get(pk=user_id)
workspace = project.workspace_id
if request.user in workspace.managers.all():
return user, None
else:
response = Response(
{
"message": f"Only workspace managers can unassign tasks from other {user_type}s."
},
status=status.HTTP_403_FORBIDDEN,
)
return None, response
else:
user = request.user
return user, None


def get_status_from_query_params(request, status_type):
status_key = status_type + "_status"
if status_key in dict(request.query_params).keys():
status_value = request.query_params[status_key]
return ast.literal_eval(status_value)
return None


def get_task_ids(request):
try:
task_ids = request.data.get("task_ids", None)
return task_ids, None
except Exception as e:
return None, Response(
{"message": f"Failed to get the task ids : {e}"},
status=status.HTTP_400_BAD_REQUEST,
)


def get_annotations_for_project(
flag, pk, user, status_value, task_ids, annotation_type
):
project_id = pk
if project_id:
try:
project_obj = Project.objects.get(pk=project_id)
except Project.DoesNotExist:
final_result = {"message": "Project does not exist!"}
ret_status = status.HTTP_404_NOT_FOUND
return None, Response(final_result, status=ret_status)
if project_obj:
ann = Annotation_model.objects.filter(
task__project_id=project_id,
annotation_type=annotation_type,
)
if flag == True:
ann = ann.filter(
completed_by=user.id,
annotation_status__in=status_value,
)
elif task_ids != None:
ann = ann.filter(task__id__in=task_ids)

return ann, None
return None, Response(
{"message": "Project id not provided"}, status=status.HTTP_400_BAD_REQUEST
)
Loading

0 comments on commit 1dcae3d

Please sign in to comment.