<commit_before>from django.conf import settings
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse_lazy
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, Http404

from djsani.core.utils import get_content_type, get_manager
from djsani.core.models import SPORTS_WOMEN, SPORTS_MEN, StudentMedicalManager
from djsani.core.models import StudentMedicalLogEntry
from djsani.core.models import ADDITION, CHANGE
from djsani.insurance.models import StudentHealthInsurance
from djsani.medical_history.waivers.models import Meni, Privacy, Reporting
from djsani.medical_history.waivers.models import Risk, Sicklecell
from djsani.medical_history.models import StudentMedicalHistory
from djsani.medical_history.models import AthleteMedicalHistory
from djsani.core.sql import STUDENT_VITALS
from djzbar.utils.informix import get_engine, get_session
from djtools.utils.date import calculate_age
from djtools.utils.users import in_group
from djtools.fields import TODAY

import datetime


import logging
logger = logging.getLogger(__name__)

"""
table names are the key, base model classes are the value
"""
BASES = {
    "cc_student_health_insurance": StudentHealthInsurance,
    "cc_student_medical_manager": StudentMedicalManager,
    "cc_student_medical_history": StudentMedicalHistory,
    "cc_athlete_medical_history": AthleteMedicalHistory,
    "cc_student_meni_waiver": Meni,
    "cc_athlete_privacy_waiver": Privacy,
    "cc_athlete_reporting_waiver": Reporting,
    "cc_athlete_risk_waiver": Risk,
    "cc_athlete_sicklecell_waiver": Sicklecell,
}

PERSISTENT_TABLES = (
    "cc_athlete_sicklecell_waiver",
    "cc_student_health_insurance",
)

EARL = settings.INFORMIX_EARL

@csrf_exempt
@login_required
def set_type(request):
    """
    Ajax POST. Locations in use:

    Student home
        1) choose student or athlete
        2) if athlete, choose sport(s)
    Dashboard home
        1) immunization
    Dashboard student detail
        1) student or athlete
        2) if athlete, choose sport(s)
        3) insurance opt-out
        4) all waivers
    """
    staff = in_group(request.user, "MedicalStaff")

    # we need a college ID and insure no funny stuff
    cid = request.POST.get("college_id")
    if not cid:
        return HttpResponse("Error")
    if not staff and int(cid) != request.user.id:
        return HttpResponse("Not staff")

    field = request.POST.get("field")
    table = request.POST.get("table")
    switch = request.POST.get("switch")

    # create our dictionary to hold name/value pairs
    dic = {}

    # if student has sickle cell test results then
    # proof is True and waive is False
    if field[0:7] == "results":
        field = "results"
        dic["proof"] = 1
        dic["waive"] = 0
        dic["updated_at"] = datetime.datetime.now()

    # sports field is a list
    if field == "sports":
        switch = ','.join(request.POST.getlist("switch[]"))


    # if we switch from athlete then remove sports
    if field == "athlete" and switch == "0":
        dic["sports"] = ""

    # set name/value
    dic[field] = switch

    # create database session
    session = get_session(EARL)

    # retrieve student manager record
    man = get_manager(session, cid)

    # default action for Entry Log is a database update
    action_flag = CHANGE
    if table:
        # retrieve the object based on table name
        if table in PERSISTENT_TABLES:
            obj = session.query(BASES[table]).filter_by(college_id=cid).first()
        else:
            obj = session.query(BASES[table]).\
                filter_by(college_id=cid).\
                filter(BASES[table].current(settings.START_DATE)).first()

        if not obj:
            dic["college_id"] = cid
            # insert/create new object
            action_flag = ADDITION
            obj = BASES[table](**dic)
            session.add(obj)
            session.flush()
        else:
            # update existing object
            for key, value in dic.iteritems():
                setattr(obj, key, value)

        # update the log entry for staff modifications
        if staff:
            message = ""
            for n,v in dic.items():
                message += "{} = {}\n".format(n,v)
            log = {
                "college_id": request.user.id,
                "content_type_id": get_content_type(session, table).id,
                "object_id": obj.id,
                "object_repr": "{}".format(obj),
                "action_flag": action_flag,
                "action_message": message
            }
            obj = StudentMedicalLogEntry(**log)<commit_msg>removed the PERSISTENT_TABLES tuple since the remaining tables are no longer persistent from year to year<commit_after>from django.conf import settings
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse_lazy
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, Http404

from djsani.core.utils import get_content_type, get_manager
from djsani.core.models import SPORTS_WOMEN, SPORTS_MEN, StudentMedicalManager
from djsani.core.models import StudentMedicalLogEntry
from djsani.core.models import ADDITION, CHANGE
from djsani.insurance.models import StudentHealthInsurance
from djsani.medical_history.waivers.models import Meni, Privacy, Reporting
from djsani.medical_history.waivers.models import Risk, Sicklecell
from djsani.medical_history.models import StudentMedicalHistory
from djsani.medical_history.models import AthleteMedicalHistory
from djsani.core.sql import STUDENT_VITALS
from djzbar.utils.informix import get_engine, get_session
from djtools.utils.date import calculate_age
from djtools.utils.users import in_group
from djtools.fields import TODAY

import datetime


import logging
logger = logging.getLogger(__name__)

"""
table names are the key, base model classes are the value
"""
BASES = {
    "cc_student_health_insurance": StudentHealthInsurance,
    "cc_student_medical_manager": StudentMedicalManager,
    "cc_student_medical_history": StudentMedicalHistory,
    "cc_athlete_medical_history": AthleteMedicalHistory,
    "cc_student_meni_waiver": Meni,
    "cc_athlete_privacy_waiver": Privacy,
    "cc_athlete_reporting_waiver": Reporting,
    "cc_athlete_risk_waiver": Risk,
    "cc_athlete_sicklecell_waiver": Sicklecell,
}

EARL = settings.INFORMIX_EARL

@csrf_exempt
@login_required
def set_type(request):
    """
    Ajax POST. Locations in use:

    Student home
        1) choose student or athlete
        2) if athlete, choose sport(s)
    Dashboard home
        1) immunization
        2) Status (sitrep)
    Dashboard student detail
        1) student or athlete
        2) if athlete, choose sport(s)
        3) insurance opt-out
        4) all waivers
    """
    staff = in_group(request.user, "MedicalStaff")

    # we need a college ID and insure no funny stuff
    cid = request.POST.get("college_id")
    if not cid:
        return HttpResponse("Error")
    if not staff and int(cid) != request.user.id:
        return HttpResponse("Not staff")

    field = request.POST.get("field")
    table = request.POST.get("table")
    switch = request.POST.get("switch")

    # create our dictionary to hold name/value pairs
    dic = {}

    # if student has sickle cell test results then
    # proof is True and waive is False
    if field[0:7] == "results":
        field = "results"
        dic["proof"] = 1
        dic["waive"] = 0
        dic["updated_at"] = datetime.datetime.now()

    # sports field is a list
    if field == "sports":
        switch = ','.join(request.POST.getlist("switch[]"))


    # if we switch from athlete then remove sports
    if field == "athlete" and switch == "0":
        dic["sports"] = ""

    # set name/value
    dic[field] = switch

    # create database session
    session = get_session(EARL)

    # retrieve student manager record
    man = get_manager(session, cid)

    # default action for Entry Log is a database update
    action_flag = CHANGE
    if table:
        # retrieve the object based on table name
        if table == "cc_athlete_sicklecell_waiver" \
          and getattr(man, "cc_athlete_sicklecell_waiver"):
            obj = session.query(Sicklecell).\
                filter_by(college_id=cid).filter(\
                    (Sicklecell.proof == 1) | \
                    (Sicklecell.created_at > settings.START_DATE)\
                ).first()
        else:
            obj = session.query(BASES[table]).\
                filter_by(college_id=cid).\
                filter(BASES[table].current(settings.START_DATE)).first()

        if not obj:
            dic["college_id"] = cid
            # insert/create new object
            action_flag = ADDITION
            obj = BASES[table](**dic)
            session.add(obj)
            session.flush()
        else:
            # update existing object
            for key, value in dic.iteritems():
                setattr(obj, key, value)

        # update the log entry for staff modifications
        if staff:
            message = ""
            for n,v in dic.items():
                message += "{} = {}\n".format(n,v)
            log = {
                "college_id": request.user.id,
                "content_type_id": get_content_type(session, table).id,
                "object_id": obj.id,
                "object_repr": "{}".format(obj),
                "action_flag": action_flag,
                "action_message": message
            }
            obj = StudentMedicalLogEntry(**log)
{'fraction_non_alphanumeric': 0.08901534396910883, 'fraction_numerical': 0.003251702062798496, 'mean_word_length': 2.8044066486277544, 'pattern_counts': {'":': 36, '<': 3, '<?xml version=': 0, '>': 4, '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': '3668516', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2864, 'n_words': 776}