diff --git a/backend/dataset/views.py b/backend/dataset/views.py index 050a1bfda..5c9e1fa21 100644 --- a/backend/dataset/views.py +++ b/backend/dataset/views.py @@ -305,7 +305,7 @@ def download(self, request, pk): URL: /data/instances//download/ Accepted methods: GET """ - export_type = request.GET.get("type", "csv") + export_type = request.GET.get("export_type", "csv").lower() try: # Get the dataset instance for the id dataset_instance = DatasetInstance.objects.get(instance_id=pk) @@ -314,6 +314,19 @@ def download(self, request, pk): dataset_model = apps.get_model("dataset", dataset_instance.dataset_type) data_items = dataset_model.objects.filter(instance_id=pk) + field_names = set([field.name for field in dataset_model._meta.get_fields()]) + for key, value in request.GET.items(): + if key in field_names: + kwargs = {f"{key}__icontains": value} + data_items = data_items.filter(**kwargs) + elif key == "export_type": + continue + else: + return Response( + {"message": "The corresponding column does not exist"}, + status=status.HTTP_400_BAD_REQUEST, + ) + dataset_resource = resources.RESOURCE_MAP[dataset_instance.dataset_type] exported_items = dataset_resource().export_as_generator(export_type, data_items) if export_type == "tsv": diff --git a/backend/notifications/tasks.py b/backend/notifications/tasks.py index a785394c7..34388cb75 100644 --- a/backend/notifications/tasks.py +++ b/backend/notifications/tasks.py @@ -22,13 +22,25 @@ def delete_excess_Notification(user): return 0 -@shared_task -def create_notification_handler(title, notification_type, users_ids): +# @shared_task +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(): diff --git a/backend/notifications/views.py b/backend/notifications/views.py index c78abdad9..063106bfd 100644 --- a/backend/notifications/views.py +++ b/backend/notifications/views.py @@ -20,9 +20,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.delay(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/tasks.py b/backend/projects/tasks.py index 2652ac83a..8f0bd51c1 100644 --- a/backend/projects/tasks.py +++ b/backend/projects/tasks.py @@ -379,7 +379,7 @@ def create_parameters_for_task_creation( tasks = create_tasks_from_dataitems(sampled_items, project) -# @shared_task +@shared_task def export_project_in_place( annotation_fields, project_id, project_type, get_request_data ) -> None: diff --git a/backend/projects/views.py b/backend/projects/views.py index 759cc2b92..579e26ee1 100644 --- a/backend/projects/views.py +++ b/backend/projects/views.py @@ -1528,7 +1528,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"}, @@ -1728,7 +1728,9 @@ 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 @@ -1808,7 +1810,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 @@ -2172,7 +2174,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 @@ -3594,7 +3596,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 @@ -3661,7 +3663,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: @@ -3724,7 +3726,7 @@ def add_project_supercheckers(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": "SuperCheckers added"}, status=status.HTTP_200_OK @@ -4327,7 +4329,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) ret_dict = {"message": "This project is published"} ret_status = status.HTTP_200_OK except Exception as e: diff --git a/backend/shoonya_backend/settings.py b/backend/shoonya_backend/settings.py index e8f6e71d7..2915ce894 100644 --- a/backend/shoonya_backend/settings.py +++ b/backend/shoonya_backend/settings.py @@ -41,6 +41,7 @@ "shoonya.ai4bharat.org", "0.0.0.0", "backend.shoonya.ai4bharat.org", + "backend.shoonya2.ai4bharat.org", ] # Application definition @@ -186,12 +187,13 @@ # Email Settings -EMAIL_BACKEND = "django_smtp_ssl.SSLEmailBackend" +EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" EMAIL_HOST = os.getenv("EMAIL_HOST") -EMAIL_PORT = 465 +EMAIL_PORT = os.getenv("EMAIL_PORT") EMAIL_HOST_USER = os.getenv("SMTP_USERNAME") EMAIL_HOST_PASSWORD = os.getenv("SMTP_PASSWORD") EMAIL_USE_TLS = True +EMAIL_USE_SSL = False DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL") DOMAIN = "shoonya.ai4bharat.org" diff --git a/backend/tasks/views.py b/backend/tasks/views.py index b2c0ac395..43517ac06 100644 --- a/backend/tasks/views.py +++ b/backend/tasks/views.py @@ -34,6 +34,8 @@ TaskAnnotationSerializer, ) from tasks.utils import 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 @@ -1510,6 +1512,41 @@ def get_audio_file(self, request): return Response(data=encoded_audio_data, status=status.HTTP_200_OK) +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, @@ -1673,6 +1710,11 @@ def partial_update(self, request, pk=None): 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: auto_save = True @@ -1696,10 +1738,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 = (