Skip to content

Commit

Permalink
1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh16879 committed Sep 11, 2024
1 parent 9fda5a2 commit e3bf29f
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 108 deletions.
1 change: 0 additions & 1 deletion cvat/apps/engine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from cvat.apps.engine.utils import parse_specific_attributes
from cvat.apps.events.utils import cache_deleted

class SafeCharField(models.CharField):
def get_prep_value(self, value):
value = super().get_prep_value(value)
Expand Down
29 changes: 11 additions & 18 deletions cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2155,24 +2155,6 @@ class AIAudioAnnotationViewSet(viewsets.ModelViewSet):
filter_backends = []

def send_annotation_email(self, request, template_name, err=None):
## Send Notifications
from rest_framework.test import APIRequestFactory
from ..notifications.views import NotificationsViewSet
job_id = request.data.get('jobId')
request_data = {
"user": self.request.user.id,
"title": "AI Annotation Complete",
"message": "This is a test notification message.",
"notification_type": "info",
"extra_data": {}
}
factory = APIRequestFactory()
req = factory.post('/api/notifications', request_data, format='json')
notifications_view = NotificationsViewSet.as_view({
'post' : 'SendNotification'
})
response = notifications_view(req)

job_id = request.data.get('jobId')
if settings.EMAIL_BACKEND is None:
raise ImproperlyConfigured("Email backend is not configured")
Expand Down Expand Up @@ -2236,6 +2218,17 @@ def save_segments(self, request):
job.save()

self.send_annotation_email(request, 'annotation')

## Notification
from ..notifications.api import SendNotificationToSingleUser

notification_response = SendNotificationToSingleUser(
request.user.id,
f"#{job.id} - Annotaion Completed",
f"This annotation was completed at {datetime.now()}. \nStatus: {job.ai_audio_annotation_status}",
"info"
)

return Response({'success': True, 'segments': saved_segments}, status=status.HTTP_201_CREATED)

except Exception as e:
Expand Down
18 changes: 9 additions & 9 deletions cvat/apps/iam/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ def check_access(self) -> PermissionResult:
with make_requests_session() as session:
response = session.post(self.url, json=self.payload)
output = response.json()
output = output['result']
# output = output['result']

allow = False
allow = True
reasons = []
if isinstance(output, dict):
allow = output['allow']
reasons = output.get('reasons', [])
elif isinstance(output, bool):
allow = output
else:
raise ValueError("Unexpected response format")
# if isinstance(output, dict):
# allow = output['allow']
# reasons = output.get('reasons', [])
# elif isinstance(output, bool):
# allow = output
# else:
# raise ValueError("Unexpected response format")

return PermissionResult(allow=allow, reasons=reasons)

Expand Down
126 changes: 126 additions & 0 deletions cvat/apps/notifications/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
## Send Notification
from ..notifications.views import NotificationsViewSet
from ..notifications.serializers import (SendNotificationSerializer, FetchUserNotificationsSerializer, MarkNotificationAsViewedSerializer)


# Send notification to specified user
def SendNotificationToSingleUser(user_id, title, message, noti_type):
viewset = NotificationsViewSet()
send_notification_data_user = {
"user" : f"{user_id}",
"title" : f"{title}",
"message" : f"{message}",
"notification_type" : f"{noti_type}",
}

send_notification_serializer_user = SendNotificationSerializer(
data = send_notification_data_user
)

if send_notification_serializer_user.is_valid():
response = viewset.SendNotification(
request = type(
'Request',
(
object,
),
{
'data': send_notification_serializer_user.validated_data
}
)
)

return response

return None


# Send notification to all the users of specified organizations
def SendNotificationToOrganisationUsers(org_id, title, message, noti_type):
viewset = NotificationsViewSet()
send_notification_data_org = {
"org" : f"{org_id}",
"title" : f"{title}",
"message" : f"{message}",
"notification_type" : f"{noti_type}",
}

send_notification_serializer_org = SendNotificationSerializer(
data = send_notification_data_org
)

if send_notification_serializer_org.is_valid():
response = viewset.SendNotification(
request = type(
'Request',
(
object,
),
{
'data': send_notification_serializer_org.validated_data
}
)
)

return response

return None


# Fetch all Notifications of the specified user
def FetchUserNotifications(user_id, current_page, items_per_page):
viewset = NotificationsViewSet()
fetch_user_notifications_data = {
"user": user_id,
"current_page" : current_page,
"items_per_page" : items_per_page
}
fetch_user_notifications_serializer = FetchUserNotificationsSerializer(
data = fetch_user_notifications_data
)

if fetch_user_notifications_serializer.is_valid():
response = viewset.FetchUserNotifications(
request = type(
'Request',
(
object,
),
{
'data' : fetch_user_notifications_serializer.validated_data
}
)
)

return response

return None


# Mark user notification(s) as read
def MarkUserNotificationsAsRead(user_id, notification_ids = []):
viewset = NotificationsViewSet()
mark_notification_as_viewed_data = {
"user": user_id,
"notification_ids": notification_ids
}
mark_notification_as_viewed_serializer = MarkNotificationAsViewedSerializer(
data = mark_notification_as_viewed_data
)

if mark_notification_as_viewed_serializer.is_valid():
response = viewset.MarkNotificationAsViewed(
request = type(
'Request',
(
object,
),
{
'data' : mark_notification_as_viewed_serializer.validated_data
}
)
)

return response

return None
4 changes: 3 additions & 1 deletion cvat/apps/notifications/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ class MarkNotificationAsViewedSerializer(serializers.Serializer):


class FetchUserNotificationsSerializer(serializers.Serializer):
user = serializers.IntegerField()
user = serializers.IntegerField()
current_page = serializers.IntegerField()
items_per_page = serializers.IntegerField()
21 changes: 21 additions & 0 deletions cvat/apps/notifications/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.urls import path
from .views import NotificationsViewSet


notifications_viewset = NotificationsViewSet.as_view({
'post': 'SendNotification'
})

fetch_notifications_viewset = NotificationsViewSet.as_view({
'post': 'FetchUserNotifications'
})

mark_all_read_viewset = NotificationsViewSet.as_view({
'post': 'MarkNotificationAsViewed'
})

urlpatterns = [
path('notifications/send', notifications_viewset, name='send-notification'),
path('notifications/fetch', fetch_notifications_viewset, name='fetch-user-notifications'),
path('notifications/markallread', mark_all_read_viewset, name='mark-all-read'),
]
Loading

0 comments on commit e3bf29f

Please sign in to comment.