<commit_before>import os
import datetime

from rest_framework import status
from rest_framework.response import Response

from django.conf import settings
from django.utils import timezone

from .models import State
from .utils import get_question, save_answer

from schools.models import School
from common.views import KLPAPIView


PRI = "08039236431"
PRE = "08039510414"
GKA_DEV = "08039510185"
GKA_SMS = "08039514048"
GKA_SERVER = "08039591332"


class SMSView(KLPAPIView):
    def get(self, request):
        status_code = status.HTTP_200_OK

        date = request.QUERY_PARAMS.get('Date', None)
        data = request.QUERY_PARAMS.get('Body', None)
        ivrs_type = request.QUERY_PARAMS.get('To', None)
        telephone = request.QUERY_PARAMS.get('From', None)
        session_id = request.QUERY_PARAMS.get('SmsSid', None)

        state, created = State.objects.get_or_create(
            session_id=session_id
        )
        state.telephone = telephone
        state.date_of_visit = get_date(date)
        state.answers = []
        # Ignoring index 0 since question_numbers start from 1
        state.answers.append('IGNORED_INDEX')

        data = data.split(',')
        school_id = data.pop(0)

        # Checking if school_id has been entered and whether the
        # entered ID is valid.
        school_type = None
        if not school_id:
            status_code = status.HTTP_404_NOT_FOUND
        elif School.objects.filter(id=school_id.strip('"')).exists():
            state.school_id = school_id.strip('"')
            school_type = School.objects.filter(
                id=school_id.strip('"')
            ).values(
                'admin3__type__name'
            )[0]['admin3__type__name']
        else:
            status_code = status.HTTP_404_NOT_FOUND

        # Validating whether the entered school ID corresponds to the
        # correct school_type. Assigns the ivrs_type based on the call
        # number as well.
        if ivrs_type == GKA_SERVER or ivrs_type == GKA_DEV:
            if school_type != u'Primary School':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'gka-v3'
            for i in range(0,10): # Initializing answer slots 1 to 10 with NA
                state.answers.append('NA')
        elif ivrs_type == PRI:
            if school_type != u'Primary School':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'ivrs-pri'
            for i in range(0,6): # Initializing answer slots 1 to 6 with NA
                state.answers.append('NA')
        else: # ivrs_type == PRE
            if school_type != u'PreSchool':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'ivrs-pre'
            for i in range(0,6): # Initializing answer slots 1 to 6 with NA
                state.answers.append('NA')

        state.save()


# This view is on hold for now.
class DynamicResponse(KLPAPIView):
    def get(self, request):
        sound_file_paths = ""

        if settings.IVRS_VOICE_FILES_DIR:
            status_code = status.HTTP_200_OK

            digits = request.QUERY_PARAMS.get('digits', None)

            digits = digits.strip('"')
            path = settings.IVRS_VOICE_FILES_DIR
            path = os.path.join(path, '') # Adds a trailing slash if not present.

            for digit in digits:
                file_name = digit + ".wav\n"
                sound_file_paths += path + file_name
        else:
            status_code = status.HTTP_404_NOT_FOUND

        return Response(
            sound_file_paths,
            status=status_code,
            content_type="text/plain"
        )


class CheckSchool(KLPAPIView):
    def get(self, request):
        status_code = status.HTTP_200_OK

        ivrs_type = request.QUERY_PARAMS.get('To', None)
        telephone = request.QUERY_PARAMS.get('From', None)
        date = request.QUERY_PARAMS.get('StartTime', None)
        school_id = request.QUERY_PARAMS.get('digits', None)
        session_id = request.QUERY_PARAMS.get('CallSid', None)

        state, created = State.objects.get_or_create(
            session_id=session_id
        )
        state.telephone = telephone
        state.date_of_visit = get_date(date)
        state.answers = []
        state.answers.append('IGNORED_INDEX') # Ignoring index 0 since question_numbers start from 1

        # Checking if school_id has been entered and whether the
        # entered ID is valid.
        school_type = None
        if not school_id:
            status_code = status.HTTP_404_NOT_FOUND
        elif School.objects.filter(id=school_id.strip('"')).exists():
            state.school_id = school_id.strip('"')
            school_type = School.objects.filter(
                id=school_id.strip('"')
            ).values(
                'admin3__type__name'
            )[0]['admin3__type__name']
        else:
            status_code = status.HTTP_404_NOT_FOUND

        # Validating whether the entered school ID corresponds to the
        # correct school_type. Assigns the ivrs_type based on the call
        # number as well.
        if ivrs_type == GKA_SERVER or ivrs_type == GKA_DEV:
            if school_type != u'Primary School':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'gka-v3'
            for i in range(0,10): # Initializing answer slots 1 to 10 with NA
                state.answers.append('NA')
        elif ivrs_type == PRI:
            if school_type != u'Primary School':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'ivrs-pri'
            for i in range(0,6): # Initializing answer slots 1 to 6 with NA
                state.answers.append('NA')
        else: # ivrs_type == PRE
            if school_type != u'PreSchool':
                status_code = status.HTTP_404_NOT_FOUND

            state.ivrs_type = 'ivrs-pre'
            for i in range(0,6): # Initializing answer slots 1 to 6 with NA
                state.answers.append('NA')

        state.save()

        return Response("", status=status_code)


class ReadSchool(KLPAPIView):
    def get(self, request):
        session_id = request.QUERY_PARAMS.get('CallSid', None)
        if State.objects.filter(session_id=session_id).exists():
            state = State.objects.get(session_id=session_id)

            status_code = status.HTTP_200_OK
            school = School.objects.get(id=state.school_id)
            # Removes special characters from the school name. Apparently,
            # that was causing Exotel to break.
            school_name = ''.join(<commit_msg>Replaces the check_school code with the check_school function<commit_after>import os
import datetime

from rest_framework import status
from rest_framework.response import Response

from django.conf import settings
from django.utils import timezone

from .models import State
from .utils import get_question, save_answer, check_school

from schools.models import School
from common.views import KLPAPIView

# Exotel numbers. Find them at http://my.exotel.in/viamentis/apps#installed-apps
PRI = "08039236431"
PRE = "08039510414"
GKA_DEV = "08039510185"
GKA_SMS = "08039514048"
GKA_SERVER = "08039591332"


class SMSView(KLPAPIView):
    def get(self, request):
        status_code = status.HTTP_200_OK

        date = request.QUERY_PARAMS.get('Date', None)
        data = request.QUERY_PARAMS.get('Body', None)
        ivrs_type = request.QUERY_PARAMS.get('To', None)
        telephone = request.QUERY_PARAMS.get('From', None)
        session_id = request.QUERY_PARAMS.get('SmsSid', None)

        state, created = State.objects.get_or_create(
            session_id=session_id
        )
        state.telephone = telephone
        state.date_of_visit = get_date(date)
        state.answers = []
        # Ignoring index 0 since question_numbers start from 1
        state.answers.append('IGNORED_INDEX')

        data = data.split(',')
        school_id = data.pop(0)

        state, status_code, message = check_school(state, school_id)


# This view is on hold for now.
class DynamicResponse(KLPAPIView):
    def get(self, request):
        sound_file_paths = ""

        if settings.IVRS_VOICE_FILES_DIR:
            status_code = status.HTTP_200_OK

            digits = request.QUERY_PARAMS.get('digits', None)

            digits = digits.strip('"')
            path = settings.IVRS_VOICE_FILES_DIR
            path = os.path.join(path, '') # Adds a trailing slash if not present.

            for digit in digits:
                file_name = digit + ".wav\n"
                sound_file_paths += path + file_name
        else:
            status_code = status.HTTP_404_NOT_FOUND

        return Response(
            sound_file_paths,
            status=status_code,
            content_type="text/plain"
        )


class CheckSchool(KLPAPIView):
    def get(self, request):
        status_code = status.HTTP_200_OK

        ivrs_type = request.QUERY_PARAMS.get('To', None)
        telephone = request.QUERY_PARAMS.get('From', None)
        date = request.QUERY_PARAMS.get('StartTime', None)
        school_id = request.QUERY_PARAMS.get('digits', None)
        session_id = request.QUERY_PARAMS.get('CallSid', None)

        state, created = State.objects.get_or_create(
            session_id=session_id
        )
        state.telephone = telephone
        state.date_of_visit = get_date(date)
        state.answers = []
        state.answers.append('IGNORED_INDEX') # Ignoring index 0 since question_numbers start from 1

        if school_id:
            school_id = school_id.strip('"')

        state, status_code, message = check_school(state, school_id)

        return Response("", status=status_code)


class ReadSchool(KLPAPIView):
    def get(self, request):
        session_id = request.QUERY_PARAMS.get('CallSid', None)
        if State.objects.filter(session_id=session_id).exists():
            state = State.objects.get(session_id=session_id)

            status_code = status.HTTP_200_OK
            school = School.objects.get(id=state.school_id)
            # Removes special characters from the school name. Apparently,
            # that was causing Exotel to break.
            school_name = ''.join(
{'fraction_non_alphanumeric': 0.0903075126508369, 'fraction_numerical': 0.021019852082522383, 'mean_word_length': 2.2939102564102565, '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': '475552', 'n_tokens_mistral': 3291, 'n_tokens_neox': 3045, 'n_words': 719}