diff --git a/backend/dataset/admin.py b/backend/dataset/admin.py index f0722494..1cc90467 100644 --- a/backend/dataset/admin.py +++ b/backend/dataset/admin.py @@ -1,4 +1,4 @@ -import resource +# import resource from django.contrib import admin from import_export.admin import ImportExportActionModelAdmin from .resources import * diff --git a/backend/notifications/tasks.py b/backend/notifications/tasks.py index 4fb177d2..5cb461d1 100644 --- a/backend/notifications/tasks.py +++ b/backend/notifications/tasks.py @@ -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(): @@ -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) diff --git a/backend/notifications/views.py b/backend/notifications/views.py index 40f0ee29..f35c7467 100644 --- a/backend/notifications/views.py +++ b/backend/notifications/views.py @@ -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 diff --git a/backend/projects/views.py b/backend/projects/views.py index 61c70242..99b35c5d 100644 --- a/backend/projects/views.py +++ b/backend/projects/views.py @@ -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, @@ -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 @@ -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 @@ -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 @@ -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 @@ -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: @@ -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 @@ -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 diff --git a/backend/tasks/views.py b/backend/tasks/views.py index 5d251e2c..0b8da625 100644 --- a/backend/tasks/views.py +++ b/backend/tasks/views.py @@ -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 @@ -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, @@ -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: @@ -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 = (