<commit_before>from rest_framework.views import APIView
from rest_framework.response import Response
from rdrf.models.definition.models import Registry, RegistryForm, Section, RegistryYaml
from rdrf.models.definition.models import CommonDataElement
from rdrf.models.proms.models import Survey
from rdrf.models.proms.models import SurveyAssignment
from rdrf.models.proms.models import SurveyRequest
from rdrf.models.proms.models import SurveyRequestStates
from rdrf.models.proms.models import SurveyStates
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.shortcuts import render
from django.conf import settings
from django.db import transaction
from django.http import HttpResponseBadRequest
from rest_framework import status
from rdrf.services.io.defs.exporter import Exporter
from rdrf.services.io.defs.importer import Importer
from rdrf.services.rest.serializers import SurveyAssignmentSerializer, RegistryYamlSerializer
from rdrf.services.rest.auth import PromsAuthentication
from rest_framework.permissions import AllowAny
import requests
import json
import sys


import logging
logger = logging.getLogger(__name__)


def multicde(cde_model):
    datatype = cde_model.datatype.lower().strip()
    return datatype == "range" and cde_model.allow_multiple


@method_decorator(csrf_exempt, name='dispatch')
class SurveyEndpoint(View):

    def post(self, request):
        data = json.loads(request.body)
        patient_token = data.get("patient_token")
        survey_answers = data.get("answers")
        registry_code = data.get("registry_code")
        survey_name = data.get("survey_name")

        try:
            registry_model = Registry.objects.get(code=registry_code)

            survey_model = Survey.objects.get(registry=registry_model,
                                              name=survey_name)

            survey_assignment = SurveyAssignment.objects.get(registry=survey_model.registry,
                                                             survey_name=survey_model.name,
                                                             patient_token=patient_token,
                                                             state=SurveyStates.REQUESTED)
        except (Registry.DoesNotExist, Survey.DoesNotExist, SurveyAssignment.DoesNotExist):
            return HttpResponseBadRequest("Invalid survey request")

        survey_assignment.response = json.dumps(survey_answers)
        survey_assignment.state = SurveyStates.COMPLETED
        survey_assignment.save()
        return render(request, "proms/proms_completed.html", {})


class SurveyAssignments(APIView):
    queryset = SurveyAssignment.objects.all()
    authentication_classes = (PromsAuthentication,)
    permission_classes = (AllowAny,)

    @method_decorator(csrf_exempt)<commit_msg>rdrf #1904 SurveyEndPoint - cached registry<commit_after>from rest_framework.views import APIView
from rest_framework.response import Response
from rdrf.models.definition.models import Registry, RegistryForm, Section, RegistryYaml
from rdrf.models.definition.models import CommonDataElement
from rdrf.models.proms.models import Survey
from rdrf.models.proms.models import SurveyAssignment
from rdrf.models.proms.models import SurveyRequest
from rdrf.models.proms.models import SurveyRequestStates
from rdrf.models.proms.models import SurveyStates
from django.views.generic.base import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from django.shortcuts import render
from django.conf import settings
from django.core.cache import caches
from django.db import transaction
from django.http import HttpResponseBadRequest
from rest_framework import status
from rdrf.services.io.defs.exporter import Exporter
from rdrf.services.io.defs.importer import Importer
from rdrf.services.rest.serializers import SurveyAssignmentSerializer, RegistryYamlSerializer
from rdrf.services.rest.auth import PromsAuthentication
from rest_framework.permissions import AllowAny
import requests
import json
import sys


import logging
logger = logging.getLogger(__name__)


def multicde(cde_model):
    datatype = cde_model.datatype.lower().strip()
    return datatype == "range" and cde_model.allow_multiple


def get_registry(code):
    query_cache = caches["queries"]
    key = f"Registry_{code}"
    if key in query_cache:
        return query_cache.get(key)
    else:
        registry = Registry.objects.get(code=code)
        query_cache.set(key, registry)
        return registry


@method_decorator(csrf_exempt, name='dispatch')
class SurveyEndpoint(View):

    def post(self, request):
        data = json.loads(request.body)
        patient_token = data.get("patient_token")
        survey_answers = data.get("answers")
        registry_code = data.get("registry_code")
        survey_name = data.get("survey_name")

        try:
            registry_model = get_registry(registry_code)
            survey_assignment = SurveyAssignment.objects.get(registry=registry_model,
                                                             survey_name=survey_name,
                                                             patient_token=patient_token,
                                                             state=SurveyStates.REQUESTED)
        except (Registry.DoesNotExist, Survey.DoesNotExist, SurveyAssignment.DoesNotExist):
            return HttpResponseBadRequest("Invalid survey request")

        survey_assignment.response = json.dumps(survey_answers)
        survey_assignment.state = SurveyStates.COMPLETED
        survey_assignment.save()
        return render(request, "proms/proms_completed.html", {})


class SurveyAssignments(APIView):
    queryset = SurveyAssignment.objects.all()
    authentication_classes = (PromsAuthentication,)
    permission_classes = (AllowAny,)

    @method_decorator(csrf_exempt)
{'fraction_non_alphanumeric': 0.07085427135678392, 'fraction_numerical': 0.0006700167504187605, 'mean_word_length': 3.4001473839351513, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '965705', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1569, 'n_words': 351}