Skip to content

Commit

Permalink
updated the changes in backend till the changes from shoonya
Browse files Browse the repository at this point in the history
  • Loading branch information
Pursottam6003 committed Mar 30, 2024
1 parent 64a43b8 commit 728de7d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/dataset/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import resource
# import resource
from django.contrib import admin
from import_export.admin import ImportExportActionModelAdmin
from .resources import *
Expand Down
35 changes: 24 additions & 11 deletions backend/notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,38 @@


def delete_excess_Notification(user):
user_notifications_count = len(Notification.objects.filter(reciever_user_id=user))
if user_notifications_count >= user.notification_limit:
excess_notifications = Notification.objects.filter(
reciever_user_id=user
).order_by("created_at")[: user_notifications_count - user.notification_limit]
for excess_notification in excess_notifications:
excess_notification.reciever_user_id.remove(user)
if len(excess_notification.reciever_user_id.all()) == 0:
excess_notification.delete()
if user.notification_limit is not None:
user_notifications_count = len(Notification.objects.filter(reciever_user_id=user))
if user_notifications_count >= user.notification_limit:
excess_notifications = Notification.objects.filter(
reciever_user_id=user
).order_by("created_at")[: user_notifications_count - user.notification_limit]
for excess_notification in excess_notifications:
excess_notification.reciever_user_id.remove(user)
if len(excess_notification.reciever_user_id.all()) == 0:
excess_notification.delete()
return 0


# @shared_task
def create_notification_handler(title, notification_type, users_ids):
def create_notification_handler(
title, notification_type, users_ids, project_id=None, task_id=None
):
if not notification_aggregated(title, notification_type, users_ids):
notitification_url = (
f"/projects/{project_id}/task/{task_id}"
if project_id and task_id
else f"/projects/{project_id}"
if project_id
else f"/task/{task_id}"
if task_id
else None
)
new_notif = Notification(
notification_type=notification_type,
title=title,
metadata_json="null",
on_click=notitification_url,
)
try:
with transaction.atomic():
Expand All @@ -38,7 +51,7 @@ def create_notification_handler(title, notification_type, users_ids):
new_notif.reciever_user_id.add(receiver_user)
delete_excess_Notification(receiver_user)
except Exception as e:
print(NOTIFICATION_CREATION_FAILED)
print(e,NOTIFICATION_CREATION_FAILED)
print(NOTIFICATION_CREATED)
else:
print(NOTIFICATION_CREATED)
Expand Down
8 changes: 6 additions & 2 deletions backend/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
NOTIFICATION_CHANGED_STATE = {"message": "Notification changed state"}


def createNotification(title, notification_type, users_ids):
def createNotification(
title, notification_type, users_ids, project_id=None, task_id=None
):
"""calling shared task of notification creation from tasks"""
create_notification_handler(title, notification_type, users_ids)
create_notification_handler(
title, notification_type, users_ids, project_id, task_id
)
print(f"Creating notifications title- {title} for users_ids- {users_ids}")
return 0

Expand Down
16 changes: 8 additions & 8 deletions backend/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ def remove_annotator(self, request, pk=None, freeze_user=True):
)
notification_ids.extend(ids)
notification_ids_set = list(set(notification_ids))
createNotification(title, notification_type, notification_ids_set)
createNotification(title, notification_type, notification_ids_set,pk)
return Response(
{"message": "User removed from project"},
status=status.HTTP_201_CREATED,
Expand Down Expand Up @@ -1542,7 +1542,7 @@ def remove_reviewer(self, request, pk=None, freeze_user=True):
)
notification_ids.extend(ids)
notification_ids_set = list(set(notification_ids))
createNotification(title, notification_type, notification_ids_set)
createNotification(title, notification_type, notification_ids_set,project.id)

return Response(
{"message": "User removed from the project"}, status=status.HTTP_200_OK
Expand Down Expand Up @@ -1622,7 +1622,7 @@ def remove_superchecker(self, request, pk=None, freeze_user=True):
)
notification_ids.extend(ids)
notification_ids_set = list(set(notification_ids))
createNotification(title, notification_type, notification_ids_set)
createNotification(title, notification_type, notification_ids_set,pk)

return Response(
{"message": "User removed from the project"}, status=status.HTTP_200_OK
Expand Down Expand Up @@ -1955,7 +1955,7 @@ def update(self, request, pk=None, *args, **kwargs):
super_checkers_bool=True,
project_manager_bool=True,
)
createNotification(title, notification_type, notification_ids)
createNotification(title, notification_type, notification_ids,pk)
return super().update(request, *args, **kwargs)

@is_project_editor
Expand Down Expand Up @@ -3375,7 +3375,7 @@ def add_project_annotators(self, request, pk=None, *args, **kwargs):
project_manager_bool=True,
)

createNotification(title, notification_type, notification_ids)
createNotification(title, notification_type, notification_ids,pk)

return Response(
{"message": "Annotator added to the project"}, status=status.HTTP_200_OK
Expand Down Expand Up @@ -3439,7 +3439,7 @@ def add_project_reviewers(self, request, pk, *args, **kwargs):
project_manager_bool=True,
)

createNotification(title, notification_type, notification_ids)
createNotification(title, notification_type, notification_ids, pk)

return Response({"message": "Reviewers added"}, status=status.HTTP_200_OK)
except Project.DoesNotExist:
Expand Down Expand Up @@ -3500,7 +3500,7 @@ def add_project_supercheckers(self, request, pk, *args, **kwargs):
super_checkers_bool=True,
project_manager_bool=True,
)
createNotification(title, notification_type, notification_ids)
createNotification(title, notification_type, notification_ids,pk)

return Response(
{"message": "SuperCheckers added"}, status=status.HTTP_200_OK
Expand Down Expand Up @@ -4072,7 +4072,7 @@ def project_publish(self, request, pk=None, *args, **kwargs):
super_checkers_bool=True,
project_manager_bool=True,
)
createNotification(title, notification_type, notification_ids)
createNotification(title, notification_type, notification_ids,pk)
return Response(PROJECT_IS_PUBLISHED_ERROR, status=status.HTTP_200_OK)
serializer = ProjectUsersSerializer(project, many=False)
# ret_dict = serializer.data
Expand Down
64 changes: 64 additions & 0 deletions backend/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
TaskAnnotationSerializer,
)
from tasks.utils import compute_meta_stats_for_instruction_driven_chat, query_flower
from notifications.views import createNotification
from notifications.utils import get_userids_from_project_id

from users.models import User
from projects.models import Project, REVIEW_STAGE, ANNOTATION_STAGE, SUPERCHECK_STAGE
Expand Down Expand Up @@ -1149,6 +1151,41 @@ def find_and_replace_words_in_annotation(self, request, pk=None):
)


def update_notification(annotation_obj, task):
project_id = task.project_id.id
project_name = task.project_id
notification_type = "task_update"

if annotation_obj.annotation_status == TO_BE_REVISED:
title = f"{project_name} : {project_id} Some tasks annotated by you in this project have been sent back for revision"
try:
notification_ids = get_userids_from_project_id(
project_id=project_id,
reviewers_bool=True,
project_manager_bool=True,
)
notification_ids_set = list(set(notification_ids))
createNotification(
title, notification_type, notification_ids_set, project_id, task.id
)
except Exception as e:
print(f"Error in creating notification: {e}")

elif annotation_obj.annotation_status == REJECTED:
title = f"{project_name} : {project_id} Some tasks reviewed by you in this project have been rejected by superchecker"
try:
notification_ids = get_userids_from_project_id(
project_id=project_id,
reviewers_bool=True,
project_manager_bool=True,
)
notification_type = "rejected task"
notification_ids_set = list(set(notification_ids))
createNotification(title, notification_type, notification_ids_set)
except Exception as e:
print(f"Error in creating notification: {e}")


class AnnotationViewSet(
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
Expand Down Expand Up @@ -1299,6 +1336,21 @@ def partial_update(self, request, pk=None):
final_result = {"message": "annotation object does not exist!"}
ret_status = status.HTTP_404_NOT_FOUND
return Response(final_result, status=ret_status)
try:
if str(task.id) != str(request.data["task_id"]):
return Response(
{"message": "Task Id does not match the annotation's task id."},
status=status.HTTP_400_BAD_REQUEST,
)
except:
return Response(
{"message": "Missing parameter task_id."},
status=status.HTTP_400_BAD_REQUEST,
)
try:
task = Task.objects.get(pk=request.data["task_id"])
except:
print("task not found")

auto_save = False
if "auto_save" in request.data:
Expand All @@ -1307,10 +1359,22 @@ def partial_update(self, request, pk=None):
if annotation_obj.annotation_type == REVIEWER_ANNOTATION:
is_revised = False
if annotation_obj.annotation_status == TO_BE_REVISED:
update_notification(annotation_obj, task)
is_revised = True
print(annotation_obj)
if "ids" in dict(request.data):
pass

else:
return Response(
{"message": "key doesnot match"},
status=status.HTTP_400_BAD_REQUEST,
)

elif annotation_obj.annotation_type == SUPER_CHECKER_ANNOTATION:
is_rejected = False
if annotation_obj.annotation_type == REJECTED:
update_notification(annotation_obj, task)
is_rejected = True

is_acoustic_project_type = (
Expand Down

0 comments on commit 728de7d

Please sign in to comment.