def subscribe(self, limit=25): async_to_sync(self.channel_layer.group_add)(SUBSCRIBED_OPEN_INCIDENTS, self.channel_name) incidents = self.get_open_incidents() serialized = IncidentSerializer(incidents, many=True) self.send_json({ "type": "subscribed", "channel_name": self.channel_name, "start_incidents": serialized.data })
def incidents_filtered_by_notification_profile_view(request, notification_profile_pk): try: # Go through user to ensure that the user owns the requested notification profile notification_profile = request.user.notification_profiles.get( pk=notification_profile_pk) except NotificationProfile.DoesNotExist: raise ValidationError( f"Notification profile with pk={notification_profile_pk} does not exist." ) serializer = IncidentSerializer(notification_profile.filtered_incidents, many=True) return Response(serializer.data)
def post(self, request, format=None): """ POST a filterstring, get a list of filtered incidents back Format: { "sourceSystemIds": [1, ..], "tags": ["some=tag", ..], } Minimal format: { "sourceSystemIds": [], "tags": [], } """ filter_string_dict = request.data validate_filter_string(filter_string_dict) filter_string_json = json.dumps(filter_string_dict) mock_filter = Filter(filter_string=filter_string_json) serializer = IncidentSerializer(mock_filter.filtered_incidents, many=True) return Response(serializer.data)
def notify_on_change_or_create(sender, instance: Incident, created: bool, raw: bool, *args, **kwargs): is_new = created or raw type_str = "created" if is_new else "modified" serializer = IncidentSerializer(instance) channel_layer = get_channel_layer() content = { "type": type_str, "payload": serializer.data, } try: async_to_sync(channel_layer.group_send)(SUBSCRIBED_OPEN_INCIDENTS, { "type": "notify", "content": content }) except ConnectionRefusedError as error: LOG.error("Websockets: Could not push: %s", error)
def setUp(self): disconnect_signals() super().init_test_objects() incident1_json = IncidentSerializer([self.incident1], many=True).data self.incident1_json = JSONRenderer().render(incident1_json) self.timeslot1 = Timeslot.objects.create(user=self.user1, name="Never") self.timeslot2 = Timeslot.objects.create( user=self.user1, name="Never 2: Ever-expanding Void") filter1 = Filter.objects.create( user=self.user1, name="Critical incidents", filter_string=f'{{"sourceSystemIds": [{self.source1.pk}]}}', ) self.notification_profile1 = NotificationProfile.objects.create( user=self.user1, timeslot=self.timeslot1) self.notification_profile1.filters.add(filter1)
class NotificationProfileViewSet(rw_viewsets.ModelViewSet): permission_classes = [IsAuthenticated, IsOwner] serializer_class = ResponseNotificationProfileSerializer read_serializer_class = ResponseNotificationProfileSerializer write_serializer_class = RequestNotificationProfileSerializer queryset = NotificationProfile.objects.none() def get_queryset(self): return self.request.user.notification_profiles.all() def perform_create(self, serializer): serializer.save(user=self.request.user) @extend_schema( responses={200: IncidentSerializer(many=True)}, ) @action(methods=["get"], detail=True) def incidents(self, request, pk, *args, **kwargs): try: notification_profile = request.user.notification_profiles.get( pk=pk) except NotificationProfile.DoesNotExist: raise ValidationError( f"Notification profile with pk={pk} does not exist.") serializer = IncidentSerializer( notification_profile.filtered_incidents, many=True) return Response(serializer.data) @extend_schema( request=FilterPreviewSerializer, responses=IncidentSerializer(many=True), ) @action(methods=["post"], detail=False) def preview(self, request, **_): """ POST a filterstring, get a list of filtered incidents back Format: { "sourceSystemIds": [1, ..], "tags": ["some=tag", ..], } Minimal format: { "sourceSystemIds": [], "tags": [], } Will eventually take over for the filterpreview endpoint """ filter_string_dict = request.data try: validate_filter_string(filter_string_dict) except Exception as e: assert False, e filter_string_json = json.dumps(filter_string_dict) mock_filter = Filter(filter_string=filter_string_json) serializer = IncidentSerializer(mock_filter.filtered_incidents, many=True) return Response(serializer.data)