<commit_before>from .models import Alarm
from rest_framework import viewsets
from .serializers import AlarmSerializer, PatientAlarmSerializer
from api.permissions import IsHealthProfessional
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import ParseError
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from motivation_text.models import MotivationText
from rest_framework.exceptions import ParseError


class AlarmViewSet(viewsets.ModelViewSet):
    """
    API endpoint that shows all alarms
    """
    permission_classes = (IsAuthenticated, IsHealthProfessional,)

    def get_serializer_class(self):
        patient_id = self.request.QUERY_PARAMS.get('patient_id', None)
        if patient_id is None:
            return AlarmSerializer
        else:
            return PatientAlarmSerializer

    def get_queryset(self):
        patient_id = self.request.QUERY_PARAMS.get('patient_id', None)
        if patient_id is None:
            return Alarm.objects.all()
        if not patient_id.isdigit():
            raise ParseError(detail="patient_id is not numeric")

        return Alarm.objects.filter(measurement__patient_id=patient_id)

    @detail_route(methods=['post'])
    def handle(self, request, pk=None):
        try:
            alarm_dict = request.DATA['alarm']
            motivation_text_dict = request.DATA['motivation_text']

            alarm = self.get_object()
            alarm.is_treated = alarm_dict['is_treated']
            alarm.treated_text = alarm_dict['treated_text']
            alarm.save()

            motivation_text = None
            if 'text' in motivation_text_dict and motivation_text_dict['text']:
                motivation_text = MotivationText.objects.create(
                    patient=alarm.measurement.patient,
                    text=motivation_text_dict['text']
                )

            serializer = PatientAlarmSerializer(instance=alarm)
            return Response(serializer.data)

        except KeyError:
            raise ParseError()

<commit_msg>Improve performance of alarm endpoint<commit_after>from .models import Alarm
from rest_framework import viewsets
from .serializers import AlarmSerializer, PatientAlarmSerializer
from api.permissions import IsHealthProfessional
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from motivation_text.models import MotivationText
from rest_framework.exceptions import ParseError


class AlarmViewSet(viewsets.ModelViewSet):
    """
    API endpoint that shows all alarms
    """
    permission_classes = (IsAuthenticated, IsHealthProfessional,)

    def get_serializer_class(self):
        patient_id = self.request.QUERY_PARAMS.get('patient_id', None)
        if patient_id is None:
            return AlarmSerializer
        else:
            return PatientAlarmSerializer

    def get_queryset(self):
        patient_id = self.request.QUERY_PARAMS.get('patient_id', None)
        if patient_id is None:
            return Alarm.objects.all().select_related('measurement__patient__user')
        if not patient_id.isdigit():
            raise ParseError(detail="patient_id is not numeric")

        return Alarm.objects.filter(measurement__patient_id=patient_id).select_related('measurement')

    @detail_route(methods=['post'])
    def handle(self, request, pk=None):
        try:
            alarm_dict = request.DATA['alarm']
            motivation_text_dict = request.DATA['motivation_text']

            alarm = self.get_object()
            alarm.is_treated = alarm_dict['is_treated']
            alarm.treated_text = alarm_dict['treated_text']
            alarm.save()

            motivation_text = None
            if 'text' in motivation_text_dict and motivation_text_dict['text']:
                motivation_text = MotivationText.objects.create(
                    patient=alarm.measurement.patient,
                    text=motivation_text_dict['text']
                )

            serializer = PatientAlarmSerializer(instance=alarm)
            return Response(serializer.data)

        except KeyError:
            raise ParseError()

{'fraction_non_alphanumeric': 0.07309048178613396, 'fraction_numerical': 0.0, 'mean_word_length': 2.9590697674418607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1917829', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1105, 'n_words': 248}