78
78
process_rawdata ,
79
79
request_rawdata ,
80
80
s3_object_exists ,
81
+ send_notification ,
81
82
)
82
83
83
84
if settings .ENABLE_PREDICTION_API :
@@ -983,13 +984,14 @@ class UserNotificationViewSet(ReadOnlyModelViewSet):
983
984
permission_classes = [IsOsmAuthenticated ]
984
985
serializer_class = UserNotificationSerializer
985
986
filter_backends = [DjangoFilterBackend , filters .OrderingFilter ]
986
- filterset_fields = [' is_read' ]
987
- ordering = [' -created_at' ]
988
- ordering_fields = [' created_at' , ' read_at' , ' is_read' ]
987
+ filterset_fields = [" is_read" ]
988
+ ordering = [" -created_at" ]
989
+ ordering_fields = [" created_at" , " read_at" , " is_read" ]
989
990
990
991
def get_queryset (self ):
991
992
return UserNotification .objects .filter (user = self .request .user )
992
993
994
+
993
995
class MarkNotificationAsRead (APIView ):
994
996
authentication_classes = [OsmAuthentication ]
995
997
permission_classes = [IsOsmAuthenticated ]
@@ -999,19 +1001,29 @@ class MarkNotificationAsRead(APIView):
999
1001
)
1000
1002
def post (self , request , notification_id , format = None ):
1001
1003
try :
1002
- notification = UserNotification .objects .get (id = notification_id , user = request .user )
1004
+ notification = UserNotification .objects .get (
1005
+ id = notification_id , user = request .user
1006
+ )
1003
1007
1004
1008
if notification .is_read :
1005
- return Response ({"detail" : "Notification is already marked as read." }, status = status .HTTP_200_OK )
1009
+ return Response (
1010
+ {"detail" : "Notification is already marked as read." },
1011
+ status = status .HTTP_200_OK ,
1012
+ )
1006
1013
1007
1014
notification .is_read = True
1008
1015
notification .read_at = timezone .now ()
1009
1016
notification .save ()
1010
1017
1011
- return Response ({"detail" : "Notification marked as read." }, status = status .HTTP_200_OK )
1018
+ return Response (
1019
+ {"detail" : "Notification marked as read." }, status = status .HTTP_200_OK
1020
+ )
1012
1021
1013
1022
except UserNotification .DoesNotExist :
1014
- return Response ({"detail" : "Notification not found." }, status = status .HTTP_404_NOT_FOUND )
1023
+ return Response (
1024
+ {"detail" : "Notification not found." }, status = status .HTTP_404_NOT_FOUND
1025
+ )
1026
+
1015
1027
1016
1028
class MarkAllNotificationsAsRead (APIView ):
1017
1029
authentication_classes = [OsmAuthentication ]
@@ -1023,20 +1035,29 @@ class MarkAllNotificationsAsRead(APIView):
1023
1035
200 : openapi .Response (
1024
1036
description = "All unread notifications marked as read." ,
1025
1037
examples = {
1026
- "application/json" : {"detail" : "All unread notifications marked as read." }
1038
+ "application/json" : {
1039
+ "detail" : "All unread notifications marked as read."
1040
+ }
1027
1041
},
1028
1042
),
1029
1043
},
1030
1044
)
1031
1045
def post (self , request , format = None ):
1032
- unread_notifications = UserNotification .objects .filter (user = request .user , is_read = False )
1046
+ unread_notifications = UserNotification .objects .filter (
1047
+ user = request .user , is_read = False
1048
+ )
1033
1049
1034
1050
if not unread_notifications .exists ():
1035
- return Response ({"detail" : "No unread notifications found." }, status = status .HTTP_404_NOT_FOUND )
1051
+ return Response (
1052
+ {"detail" : "No unread notifications found." },
1053
+ status = status .HTTP_404_NOT_FOUND ,
1054
+ )
1036
1055
1037
1056
unread_notifications .update (is_read = True , read_at = timezone .now ())
1038
- return Response ({"detail" : "All unread notifications marked as read." }, status = status .HTTP_200_OK )
1039
-
1057
+ return Response (
1058
+ {"detail" : "All unread notifications marked as read." },
1059
+ status = status .HTTP_200_OK ,
1060
+ )
1040
1061
1041
1062
1042
1063
class TerminateTrainingView (APIView ):
@@ -1049,17 +1070,35 @@ def post(self, request, training_id, format=None):
1049
1070
1050
1071
task_id = training_instance .task_id
1051
1072
if not task_id :
1052
- return Response ({"detail" : "No task associated with this training." }, status = status .HTTP_400_BAD_REQUEST )
1073
+ return Response (
1074
+ {"detail" : "No task associated with this training." },
1075
+ status = status .HTTP_400_BAD_REQUEST ,
1076
+ )
1053
1077
1054
- task = AsyncResult (task_id ,app = current_app )
1055
- if task .state in ["PENDING" , "STARTED" , "RETRY" ]:
1078
+ task = AsyncResult (task_id , app = current_app )
1079
+ if (
1080
+ task .state in ["PENDING" , "STARTED" , "RETRY" , "FAILURE" ]
1081
+ and training_instance .status != "FAILED"
1082
+ ):
1056
1083
current_app .control .revoke (task_id , terminate = True )
1057
1084
training_instance .status = "FAILED"
1058
1085
training_instance .finished_at = now ()
1059
1086
training_instance .save ()
1060
- return Response ({"detail" : "Training task cancelled successfully." }, status = status .HTTP_200_OK )
1087
+ send_notification (training_instance , "Cancelled" )
1088
+ return Response (
1089
+ {"detail" : "Training task cancelled successfully." },
1090
+ status = status .HTTP_200_OK ,
1091
+ )
1061
1092
else :
1062
- return Response ({"detail" : f"Task cannot be cancelled. Current state: { task .state } " }, status = status .HTTP_400_BAD_REQUEST )
1093
+ return Response (
1094
+ {
1095
+ "detail" : f"Task cannot be cancelled. Current state: { task .state } "
1096
+ },
1097
+ status = status .HTTP_400_BAD_REQUEST ,
1098
+ )
1063
1099
1064
1100
except Training .DoesNotExist :
1065
- return Response ({"detail" : "Training not found or do not belong to you" }, status = status .HTTP_404_NOT_FOUND )
1101
+ return Response (
1102
+ {"detail" : "Training not found or do not belong to you" },
1103
+ status = status .HTTP_404_NOT_FOUND ,
1104
+ )
0 commit comments