<commit_before>from casexml.apps.case.models import CommCareCase
from fluff.filters import ANDFilter, ORFilter, NOTFilter
from dimagi.utils.decorators.memoized import memoized
from corehq.apps.groups.models import Group
from corehq.fluff.calculators.xform import IN_MULTISELECT, IntegerPropertyReference
from couchforms.models import XFormInstance
from .filters import ALL_CVSU_GROUP
import fluff
from corehq.fluff.calculators import xform as xcalc
from corehq.fluff.calculators import case as ccalc
from fluff.models import SimpleCalculator

REPORT_INCIDENT_XMLNS = 'http://openrosa.org/formdesigner/A12E46B1-7ED8-4DE3-B7BB-358219CC6994'
FOLLOWUP_FORM_XMLNS = 'http://openrosa.org/formdesigner/9457DE46-E640-4F6E-AD9A-F9AC9FDA35E6'
IGA_FORM_XMLNS = 'http://openrosa.org/formdesigner/B4BAF20B-4337-409D-A446-FD4A0C8D5A9A'
OUTREACH_FORM_XMLNS = 'http://openrosa.org/formdesigner/B5C415BB-456B-49BE-A7AF-C5E7C9669E34'

get_form_user_id = lambda form: form.metadata.userID
get_case_user_id = lambda case: case.user_id

@memoized
def get_group_id(user_id):
    groups = Group.by_user(user_id, wrap=False)
    for g in groups:
        if g != ALL_CVSU_GROUP:
            return g

def get_group_id_gen(user_id_getter):
    def group_id_getter(form):
        user_id = user_id_getter(form)
        return get_group_id(user_id)

    return group_id_getter

def date_reported(form):
    return form.form.get('date_reported', form.received_on)

def date_provided(form):
    return form.form.get('mediation_provided_date', form.received_on)

def date_mediated(form):
    date = form.form.get('mediation_date', form.received_on)
    return date or form.received_on  # some forms empty strings

def date_reported_mediated(form):
    if form.xmlns == FOLLOWUP_FORM_XMLNS:
        return date_mediated(form)
    else:
        return date_reported(form)

def date_reported_provided_mediated(form):
    if form.xmlns == FOLLOWUP_FORM_XMLNS:
        return date_mediated(form)
    elif ORFilter([filter_action('immediate_referral'), filter_action('actions_other')]).filter(form):
        return date_reported(form)
    else:
        return date_provided(form)

def get_age(form):
    return form.form.get('victim_age', None)

def get_sex(form):
    return form.form.get('victim_sex', None)

@memoized
def filter_action(action):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        property_path='form/actions_to_resolve_case',
        property_value=action
    )

@memoized
def filter_service(service):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/immediate_services',
        property_value=service
    )

@memoized
def filter_outcome(outcome, xmlns=None):
    if xmlns:
        return xcalc.FormPropertyFilter(
            xmlns=xmlns,
            property_path='form/mediation_outcome',
            property_value=outcome
        )
    else:
        return ORFilter([
            xcalc.FormPropertyFilter(
                xmlns=REPORT_INCIDENT_XMLNS,
                property_path='form/mediation_outcome',
                property_value=outcome
            ),
            xcalc.FormPropertyFilter(
                xmlns=FOLLOWUP_FORM_XMLNS,
                property_path='form/mediation_outcome',
                property_value=outcome
            )
        ])

@memoized
def filter_immediate_referral_org(org):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/immediate_referral_organisation',
        property_value=org
    )

@memoized
def filter_referral_org(org):
    return ORFilter([
        xcalc.FormPropertyFilter(
            xmlns=REPORT_INCIDENT_XMLNS,
            operator=IN_MULTISELECT,
            property_path='form/mediation_referral',
            property_value=org
        ),
        xcalc.FormPropertyFilter(
            xmlns=FOLLOWUP_FORM_XMLNS,
            operator=IN_MULTISELECT,
            property_path='form/mediation_referral',
            property_value=org
        )
    ])

@memoized
def filter_abuse(category):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/abuse_category',
        property_value=category
    )

class ORCalculator(SimpleCalculator):
    def __init__(self, date_extractor=None, calculators=None, indicator_calculator=None, window=None):
        combined_filter = ORFilter([calc._filter for calc in calculators if calc._filter])
        super(ORCalculator, self).__init__(date_extractor, combined_filter,
                                           indicator_calculator=indicator_calculator, window=window)

class UnicefMalawiFluff(fluff.IndicatorDocument):
    document_class = XFormInstance
    document_filter = ANDFilter([
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://openrosa.org/user-registration')),
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://openrosa.org/user/registration')),
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://code.javarosa.org/devicereport'))
    ])

    domains = ('cvsulive',)
    group_by = (
        'domain',
        fluff.AttributeGetter('user_id', get_form_user_id),
        fluff.AttributeGetter('group_id', get_group_id_gen(get_form_user_id)),
        fluff.AttributeGetter('age', get_age),
        fluff.AttributeGetter('sex', get_sex),
    )
    group_by_type_map = {
        'age': fluff.TYPE_INTEGER
    }

    # ---------------------------------------------------------------------
    # incident resolution
    # ---------------------------------------------------------------------

    resolution_resolved_at_cvsu = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('resolved', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('resolved', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_unresolved = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('unresolved', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('unresolved', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_case_withdrawn = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('case_withdrawn', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('case_withdrawn', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_referred_ta = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ta')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('ta')])
        ])
    )

    resolution_referral_ta_court = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ta_court')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('ta_court')])
        ])
    )

    resolution_referral_police = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('police')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_police')])
        ])
    )

    resolution_referral_social_welfare = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('social_welfare')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_social_welfare')])
        ])
    )

    resolution_referral_ngo = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ngo')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_ngo')])
        ])
    )

    resolution_referral_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('referral_other')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_other')])
        ])
    )

    resolution_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            filter_action('actions_other'),
            filter_outcome('other_mediation_outcome', REPORT_INCIDENT_XMLNS),
            filter_outcome('other', FOLLOWUP_FORM_XMLNS)
        ]),
    )

    resolution_total = ORCalculator(
        date_extractor=date_reported_provided_mediated,
        calculators=[
            resolution_resolved_at_cvsu,
            resolution_referred_ta,
            resolution_referral_ta_court,
            resolution_referral_police,
            resolution_referral_social_welfare,
            resolution_referral_ngo,
            resolution_referral_other,
            resolution_unresolved,
            resolution_other
        ]
    )

    # ---------------------------------------------------------------------
    # services
    # ---------------------------------------------------------------------

    service_referral = SimpleCalculator(
        date_provider=date_reported_mediated,
        filter=ORFilter([
            filter_action('immediate_referral'),
            filter_service('referral_hostpital'),
            filter_outcome('mediation_outcome_referred')
        ])
    )

    service_mediation = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_action('mediation_scheduled'), filter_action('mediation_provided')])
    )

    service_counselling = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_service('counselling'), filter_service('couselling')])
    )

    service_psychosocial_support = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('psychosocial_support')
    )

    service_first_aid = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('first_aid')
    )

    service_shelter = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('shelter')
    )

    service_other = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_action('actions_other'), filter_service('services_other')])
    )

    service_total = ORCalculator(
        date_extractor=date_reported_mediated,
        calculators=[
            service_referral,
            service_mediation,
            service_counselling,
            service_psychosocial_support,
            service_first_aid,
            service_shelter,
            service_other
        ]
    )

    # ---------------------------------------------------------------------
    # outreach
    # ---------------------------------------------------------------------

    incidents = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS)
    )

    outreach = SimpleCalculator(
        date_provider=lambda form: form.form.get('date', form.received_on),
        filter=xcalc.FormPropertyFilter(xmlns=OUTREACH_FORM_XMLNS)
    )

    iga = SimpleCalculator(
        date_provider=lambda form: form.form.get('start_date', form.received_on),
        filter=xcalc.FormPropertyFilter(xmlns=IGA_FORM_XMLNS)
    )

    # ---------------------------------------------------------------------
    # abuse
    # ---------------------------------------------------------------------

    abuse_children_in_household = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/nr_children_in_family', transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_children_abused = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/no_children_abused', transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_category_physical = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('physical')
    )

    abuse_category_sexual = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('sexual')
    )

    abuse_category_psychological = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('psychological')
    )

    abuse_category_exploitation = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('exploitation')
    )

    abuse_category_neglect = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('neglect')
    )

    abuse_category_other = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('abuse_other')
    )

    abuse_category_total = ORCalculator(
        date_extractor=date_reported,
        calculators=[
            abuse_category_physical,
            abuse_category_sexual,
            abuse_category_psychological,
            abuse_category_exploitation,
            abuse_category_neglect,
            abuse_category_other
        ]
    )

    class Meta:
        app_label = 'cvsu'

def case_date_reported(case):
    return case.date_reported

def filter_case_outcome(outcome):
    return ccalc.CasePropertyFilter(
        type='victim',
        property_name='mediation_outcome',
        property_value=outcome
    )

UnicefMalawiFluffPillow = UnicefMalawiFluff.pillow()
<commit_msg>minor cleanups to cvsu fluff<commit_after>from casexml.apps.case.models import CommCareCase
from fluff.filters import ANDFilter, ORFilter, NOTFilter, Filter
from dimagi.utils.decorators.memoized import memoized
from corehq.apps.groups.models import Group
from corehq.fluff.calculators.xform import IN_MULTISELECT, IntegerPropertyReference
from couchforms.models import XFormInstance
from .filters import ALL_CVSU_GROUP
import fluff
from corehq.fluff.calculators import xform as xcalc
from corehq.fluff.calculators import case as ccalc
from fluff.models import SimpleCalculator

REPORT_INCIDENT_XMLNS = 'http://openrosa.org/formdesigner/A12E46B1-7ED8-4DE3-B7BB-358219CC6994'
FOLLOWUP_FORM_XMLNS = 'http://openrosa.org/formdesigner/9457DE46-E640-4F6E-AD9A-F9AC9FDA35E6'
IGA_FORM_XMLNS = 'http://openrosa.org/formdesigner/B4BAF20B-4337-409D-A446-FD4A0C8D5A9A'
OUTREACH_FORM_XMLNS = 'http://openrosa.org/formdesigner/B5C415BB-456B-49BE-A7AF-C5E7C9669E34'

get_user_id = lambda form: form.metadata.userID

@memoized
def get_group_id(form):
    user_id = get_user_id(form)
    groups = Group.by_user(user_id, wrap=False)
    for g in groups:
        if g != ALL_CVSU_GROUP:
            return g

def date_reported(form):
    return form.form.get('date_reported', form.received_on)

def date_provided(form):
    return form.form.get('mediation_provided_date', form.received_on)

def date_mediated(form):
    date = form.form.get('mediation_date', form.received_on)
    return date or form.received_on  # some forms empty strings

def date_reported_mediated(form):
    if form.xmlns == FOLLOWUP_FORM_XMLNS:
        return date_mediated(form)
    else:
        return date_reported(form)

def date_reported_provided_mediated(form):
    if form.xmlns == FOLLOWUP_FORM_XMLNS:
        return date_mediated(form)
    elif ORFilter([filter_action('immediate_referral'), filter_action('actions_other')]).filter(form):
        return date_reported(form)
    else:
        return date_provided(form)

def get_age(form):
    return form.form.get('victim_age', None)

def get_sex(form):
    return form.form.get('victim_sex', None)

@memoized
def filter_action(action):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        property_path='form/actions_to_resolve_case',
        property_value=action
    )

@memoized
def filter_service(service):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/immediate_services',
        property_value=service
    )

@memoized
def filter_outcome(outcome, xmlns=None):
    if xmlns:
        return xcalc.FormPropertyFilter(
            xmlns=xmlns,
            property_path='form/mediation_outcome',
            property_value=outcome
        )
    else:
        return ORFilter([
            xcalc.FormPropertyFilter(
                xmlns=REPORT_INCIDENT_XMLNS,
                property_path='form/mediation_outcome',
                property_value=outcome
            ),
            xcalc.FormPropertyFilter(
                xmlns=FOLLOWUP_FORM_XMLNS,
                property_path='form/mediation_outcome',
                property_value=outcome
            )
        ])

@memoized
def filter_immediate_referral_org(org):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/immediate_referral_organisation',
        property_value=org
    )

@memoized
def filter_referral_org(org):
    return ORFilter([
        xcalc.FormPropertyFilter(
            xmlns=REPORT_INCIDENT_XMLNS,
            operator=IN_MULTISELECT,
            property_path='form/mediation_referral',
            property_value=org
        ),
        xcalc.FormPropertyFilter(
            xmlns=FOLLOWUP_FORM_XMLNS,
            operator=IN_MULTISELECT,
            property_path='form/mediation_referral',
            property_value=org
        )
    ])

@memoized
def filter_abuse(category):
    return xcalc.FormPropertyFilter(
        xmlns=REPORT_INCIDENT_XMLNS,
        operator=IN_MULTISELECT,
        property_path='form/abuse_category',
        property_value=category
    )

class CustomFilter(Filter):
    def __init__(self, filter):
        self._filter = filter

    def filter(self, item):
        return self._filter(item)

class UnicefMalawiFluff(fluff.IndicatorDocument):
    document_class = XFormInstance
    document_filter = ANDFilter([
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://openrosa.org/user-registration')),
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://openrosa.org/user/registration')),
        NOTFilter(xcalc.FormPropertyFilter(xmlns='http://code.javarosa.org/devicereport')),
        CustomFilter(lambda f: get_user_id(f) != 'demo_user'),
        CustomFilter(lambda f: get_group_id(f)),
    ])

    domains = ('cvsulive',)
    group_by = (
        'domain',
        fluff.AttributeGetter('user_id', get_user_id),
        fluff.AttributeGetter('group_id', get_group_id),
        fluff.AttributeGetter('age', get_age),
        fluff.AttributeGetter('sex', get_sex),
    )
    group_by_type_map = {
        'age': fluff.TYPE_INTEGER
    }

    # ---------------------------------------------------------------------
    # incident resolution
    # ---------------------------------------------------------------------

    resolution_resolved_at_cvsu = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('resolved', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('resolved', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_unresolved = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('unresolved', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('unresolved', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_case_withdrawn = SimpleCalculator(
        date_provider=date_provided,
        filter=ORFilter([
            ANDFilter([filter_action('mediation_provided'), filter_outcome('case_withdrawn', REPORT_INCIDENT_XMLNS)]),
            filter_outcome('case_withdrawn', FOLLOWUP_FORM_XMLNS)
        ])
    )

    resolution_referred_ta = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ta')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('ta')])
        ])
    )

    resolution_referral_ta_court = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ta_court')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('ta_court')])
        ])
    )

    resolution_referral_police = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('police')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_police')])
        ])
    )

    resolution_referral_social_welfare = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('social_welfare')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_social_welfare')])
        ])
    )

    resolution_referral_ngo = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('ngo')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_ngo')])
        ])
    )

    resolution_referral_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            ANDFilter([filter_action('immediate_referral'), filter_immediate_referral_org('referral_other')]),
            ANDFilter([filter_outcome('mediation_outcome_referred'), filter_referral_org('med_ref_other')])
        ])
    )

    resolution_other = SimpleCalculator(
        date_provider=date_reported_provided_mediated,
        filter=ORFilter([
            filter_action('actions_other'),
            filter_outcome('other_mediation_outcome', REPORT_INCIDENT_XMLNS),
            filter_outcome('other', FOLLOWUP_FORM_XMLNS)
        ]),
    )

    resolution_total = xcalc.or_calc([
        resolution_resolved_at_cvsu,
        resolution_referred_ta,
        resolution_referral_ta_court,
        resolution_referral_police,
        resolution_referral_social_welfare,
        resolution_referral_ngo,
        resolution_referral_other,
        resolution_unresolved,
        resolution_other],
        date_provider=date_reported_provided_mediated,
    )

    # ---------------------------------------------------------------------
    # services
    # ---------------------------------------------------------------------

    service_referral = SimpleCalculator(
        date_provider=date_reported_mediated,
        filter=ORFilter([
            filter_action('immediate_referral'),
            filter_service('referral_hostpital'),
            filter_outcome('mediation_outcome_referred')
        ])
    )

    service_mediation = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_action('mediation_scheduled'), filter_action('mediation_provided')])
    )

    service_counselling = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_service('counselling'), filter_service('couselling')])
    )

    service_psychosocial_support = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('psychosocial_support')
    )

    service_first_aid = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('first_aid')
    )

    service_shelter = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_service('shelter')
    )

    service_other = SimpleCalculator(
        date_provider=date_reported,
        filter=ORFilter([filter_action('actions_other'), filter_service('services_other')])
    )

    service_total = xcalc.or_calc([
        service_referral,
        service_mediation,
        service_counselling,
        service_psychosocial_support,
        service_first_aid,
        service_shelter,
        service_other],
        date_provider=date_reported_mediated,
    )

    # ---------------------------------------------------------------------
    # outreach
    # ---------------------------------------------------------------------

    incidents = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS)
    )

    outreach = SimpleCalculator(
        date_provider=lambda form: form.form.get('date', form.received_on),
        filter=xcalc.FormPropertyFilter(xmlns=OUTREACH_FORM_XMLNS)
    )

    iga = SimpleCalculator(
        date_provider=lambda form: form.form.get('start_date', form.received_on),
        filter=xcalc.FormPropertyFilter(xmlns=IGA_FORM_XMLNS)
    )

    # ---------------------------------------------------------------------
    # abuse
    # ---------------------------------------------------------------------

    abuse_children_in_household = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/nr_children_in_family', transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_children_abused = SimpleCalculator(
        date_provider=date_reported,
        filter=xcalc.FormPropertyFilter(xmlns=REPORT_INCIDENT_XMLNS),
        indicator_calculator=IntegerPropertyReference(
            'form/no_children_abused', transform=lambda x: 0 if x == 999 else x)  # unknown value = 999
    )

    abuse_category_physical = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('physical')
    )

    abuse_category_sexual = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('sexual')
    )

    abuse_category_psychological = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('psychological')
    )

    abuse_category_exploitation = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('exploitation')
    )

    abuse_category_neglect = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('neglect')
    )

    abuse_category_other = SimpleCalculator(
        date_provider=date_reported,
        filter=filter_abuse('abuse_other')
    )

    abuse_category_total = xcalc.or_calc([
        abuse_category_physical,
        abuse_category_sexual,
        abuse_category_psychological,
        abuse_category_exploitation,
        abuse_category_neglect,
        abuse_category_other],
        date_provider=date_reported
    )

    class Meta:
        app_label = 'cvsu'

def case_date_reported(case):
    return case.date_reported

def filter_case_outcome(outcome):
    return ccalc.CasePropertyFilter(
        type='victim',
        property_name='mediation_outcome',
        property_value=outcome
    )

UnicefMalawiFluffPillow = UnicefMalawiFluff.pillow()
<|endoftext|>


<commit_before>from nalaf.features import FeatureGenerator
from nalaf.structures.data import FeatureDictionary
from nalaf.features import get_spacy_nlp_english
from nalaf.preprocessing.parsers import SpacyParser
from nalaf.preprocessing.spliters import GenericSplitter, NLTK_SPLITTER
from nalaf.preprocessing.tokenizers import GenericTokenizer, NLTK_TOKENIZER
from nalaf.preprocessing.edges import SentenceDistanceEdgeGenerator
from nalaf.features.relations.sentence import NamedEntityCountFeatureGenerator
from nalaf import print_debug
import time

class RelationExtractionPipeline:
    """
    Prepares an instance of a dataset by executing modules in fixed order.
        * Finally executes each feature generator in the order they were provided

    :param class1: the class of entity1
    :type class1: str
    :param class1: the class of entity2
    :type class1: str
    :param rel_type: the relation type between the two entities
    :type rel_type: str
    :param train: if the mode is training or testing
    :type train: bool
    :param feature_set: the feature_set of the original training data
    :type feature_set: str
    :param feature_generators: one or more modules responsible for generating features
    :type feature_generators: collections.Iterable[FeatureGenerator]
    """

    def __init__(self, class1, class2, rel_type, parser=None, splitter=None, tokenizer=None, edge_generator=None, feature_set=None, feature_generators=None):
        self.class1 = class1
        self.class2 = class2
        self.rel_type = rel_type

        if not parser:
            nlp = get_spacy_nlp_english(load_parser=True)
            parser = SpacyParser(nlp)

        self.parser = parser

        if not splitter:
            # if nlp:  # Spacy parser is used, which includes a sentence splitter
            #     splitter = GenericSplitter(lambda string: (sent.text for sent in nlp(string).sents))
            # else:
            #     splitter = NLTK_SPLITTER
            splitter = NLTK_SPLITTER

        self.splitter = splitter

        if not tokenizer:
            if nlp:  # Spacy parser is used, which includes a tokenizer
                tokenizer = GenericTokenizer(lambda string: (tok.text for tok in nlp.tokenizer(string)))
            else:
                tokenizer = NLTK_TOKENIZER

        self.tokenizer = tokenizer

        self.edge_generator = SentenceDistanceEdgeGenerator(self.class1, self.class2, self.rel_type, distance=0) if edge_generator is None else edge_generator

        self.feature_set = FeatureDictionary() if feature_set is None else feature_set

        self.feature_generators = self._verify_feature_generators(feature_generators) if feature_generators else [
            NamedEntityCountFeatureGenerator(self.class1, prefix=1),
            NamedEntityCountFeatureGenerator(self.class2, prefix=2)
        ]

    def execute(self, dataset, train, only_features=False):
        # Note: the order of splitter/tokenizer/edger/parser is important
        # Note: we could avoid the re-splitting & tokenization (see c3d320f08ed8893460d5a68b1b5c87aab6ea0c27)
        #   yet that may later create unforseen problems and re-doing has no significant impact in running time

        start = time.time()

        if not only_features:
            self.splitter.split(dataset)
            self.tokenizer.tokenize(dataset)
            self.edge_generator.generate(dataset)

        # The labels are always re-generated
        dataset.label_edges()
        self.parser.parse(dataset)

        for feature_generator in self.feature_generators:
            feature_generator.generate(dataset, self.feature_set, train, use_gold=self.edge_generator.use_gold, use_pred=self.edge_generator.use_pred)

        end = time.time()
        print_debug("Relation pipeline (only_features: {}), running time: {}".format(only_features, str(end - start)))

    def _verify_feature_generators(self, feature_generators):
        if hasattr(feature_generators, '__iter__'):
            for index, feature_generator in enumerate(feature_generators):
                if not isinstance(feature_generator, FeatureGenerator):
                    raise TypeError('not an instance that implements FeatureGenerator at index {}'.format(index))

            return feature_generators

        elif isinstance(feature_generators, FeatureGenerator):
            return [feature_generators]

        else:
            raise TypeError('not an instance or iterable of instances that implements FeatureGenerator')
<commit_msg>fix order (matters!)<commit_after>from nalaf.features import FeatureGenerator
from nalaf.structures.data import FeatureDictionary
from nalaf.features import get_spacy_nlp_english
from nalaf.preprocessing.parsers import SpacyParser
from nalaf.preprocessing.spliters import GenericSplitter, NLTK_SPLITTER
from nalaf.preprocessing.tokenizers import GenericTokenizer, NLTK_TOKENIZER
from nalaf.preprocessing.edges import SentenceDistanceEdgeGenerator
from nalaf.features.relations.sentence import NamedEntityCountFeatureGenerator
from nalaf import print_debug
import time

class RelationExtractionPipeline:
    """
    Prepares an instance of a dataset by executing modules in fixed order.
        * Finally executes each feature generator in the order they were provided

    :param class1: the class of entity1
    :type class1: str
    :param class1: the class of entity2
    :type class1: str
    :param rel_type: the relation type between the two entities
    :type rel_type: str
    :param train: if the mode is training or testing
    :type train: bool
    :param feature_set: the feature_set of the original training data
    :type feature_set: str
    :param feature_generators: one or more modules responsible for generating features
    :type feature_generators: collections.Iterable[FeatureGenerator]
    """

    def __init__(self, class1, class2, rel_type, parser=None, splitter=None, tokenizer=None, edge_generator=None, feature_set=None, feature_generators=None):
        self.class1 = class1
        self.class2 = class2
        self.rel_type = rel_type

        if not parser:
            nlp = get_spacy_nlp_english(load_parser=True)
            parser = SpacyParser(nlp)

        self.parser = parser

        if not splitter:
            # if nlp:  # Spacy parser is used, which includes a sentence splitter
            #     splitter = GenericSplitter(lambda string: (sent.text for sent in nlp(string).sents))
            # else:
            #     splitter = NLTK_SPLITTER
            splitter = NLTK_SPLITTER

        self.splitter = splitter

        if not tokenizer:
            if nlp:  # Spacy parser is used, which includes a tokenizer
                tokenizer = GenericTokenizer(lambda string: (tok.text for tok in nlp.tokenizer(string)))
            else:
                tokenizer = NLTK_TOKENIZER

        self.tokenizer = tokenizer

        self.edge_generator = SentenceDistanceEdgeGenerator(self.class1, self.class2, self.rel_type, distance=0) if edge_generator is None else edge_generator

        self.feature_set = FeatureDictionary() if feature_set is None else feature_set

        self.feature_generators = self._verify_feature_generators(feature_generators) if feature_generators else [
            NamedEntityCountFeatureGenerator(self.class1, prefix=1),
            NamedEntityCountFeatureGenerator(self.class2, prefix=2)
        ]

    def execute(self, dataset, train, only_features=False):
        # Note: the order of splitter/tokenizer/edger/parser is important
        # Note: we could avoid the re-splitting & tokenization (see c3d320f08ed8893460d5a68b1b5c87aab6ea0c27)
        #   yet that may later create unforseen problems and re-doing has no significant impact in running time

        start = time.time()

        if not only_features:
            self.splitter.split(dataset)
            self.tokenizer.tokenize(dataset)
            self.parser.parse(dataset)  # Note, the percolate_tokens_to_entities should go before the edge generator due to sentences adjustments
            self.edge_generator.generate(dataset)

        # The labels are always re-generated
        dataset.label_edges()

        for feature_generator in self.feature_generators:
            feature_generator.generate(dataset, self.feature_set, train, use_gold=self.edge_generator.use_gold, use_pred=self.edge_generator.use_pred)

        end = time.time()
        print_debug("Relation pipeline (only_features: {}), running time: {}".format(only_features, str(end - start)))

    def _verify_feature_generators(self, feature_generators):
        if hasattr(feature_generators, '__iter__'):
            for index, feature_generator in enumerate(feature_generators):
                if not isinstance(feature_generator, FeatureGenerator):
                    raise TypeError('not an instance that implements FeatureGenerator at index {}'.format(index))

            return feature_generators

        elif isinstance(feature_generators, FeatureGenerator):
            return [feature_generators]

        else:
            raise TypeError('not an instance or iterable of instances that implements FeatureGenerator')
<|endoftext|>


<commit_before>from datetime import datetime

from django.http import JsonResponse
from django.utils import timezone

def dt(orig: [datetime, None]) -> [float, None]:
    return None if not orig else timezone.localtime(orig).timestamp()

def get_object(request):
    model = request.GET["model"].split(".")
    pk = request.GET["pk"]
    d = {}
    app = model[0]
    mdl = model[2]
    if app == "directions":
        import directions.models as directions

        if mdl == "IstochnikiFinansirovaniya":
            if directions.IstochnikiFinansirovaniya.objects.filter(pk=pk).exists():
                n = directions.IstochnikiFinansirovaniya.objects.get(pk=pk)
                d["pk"] = n.pk
                d["title"] = n.title
                d["base"] = n.base_id

        elif mdl == "Napravleniya":
            if directions.Napravleniya.objects.filter(pk=pk).exists():
                n = directions.Napravleniya.objects.get(pk=pk)
                d["pk"] = n.pk
                d["history_num"] = n.history_num
                d["data_sozdaniya"] = dt(n.data_sozdaniya)
                d["client"] = n.client_id
                d["diagnos"] = n.diagnos
                d["doc"] = n.doc_id
                d["doc_who_create"] = n.doc_who_create_id
                d["istochnik_f"] = n.istochnik_f_id
                d["cancel"] = n.cancel
                d["result_rmis_send"] = n.result_rmis_send

        elif mdl == "TubesRegistration":
            if directions.TubesRegistration.objects.filter(pk=pk).exists():
                t = directions.TubesRegistration.objects.get(pk=pk)
                d["pk"] = t.pk
                d["type"] = t.type.tube_id
                d["doc_get"] = None if not t.doc_get else t.doc_get_id
                d["time_get"] = dt(t.time_get)
                d["doc_recive"] = None if not t.doc_recive else t.doc_recive_id
                d["time_recive"] = dt(t.time_recive)
                d["notice"] = t.notice

        elif mdl == "Issledovaniya":
            if directions.Issledovaniya.objects.filter(pk=pk).exists():
                i = directions.Issledovaniya.objects.get(pk=pk)
                d["pk"] = i.pk
                d["direction"] = i.napravleniye_id
                d["research"] = i.research_id
                d["tubes"] = [x.pk for x in i.tubes.all()]
                d["doc_save"] = None if not i.doc_save else i.doc_save_id
                d["time_save"] = dt(i.time_save)
                d["doc_confirmation"] = None if not i.doc_confirmation else i.doc_confirmation_id
                d["time_confirmation"] = dt(i.time_confirmation)
                d["lab_comment"] = i.lab_comment

        elif mdl == "Result":
            if directions.Result.objects.filter(pk=pk):
                r = directions.Result.objects.get(pk=pk)
                d["pk"] = r.pk
                d["issledovaniye"] = r.issledovaniye_id
                d["fraction"] = r.fraction_id
                d["value"] = r.value
                d["units"] = r.units
                d["is_normal"] = r.is_normal

        elif mdl == "ParaclinicResult":
            if directions.ParaclinicResult.objects.filter(pk=pk):
                r = directions.ParaclinicResult.objects.get(pk=pk)
                d["pk"] = r.pk
                d["issledovaniye"] = r.issledovaniye_id
                d["field"] = r.field_id
                d["value"] = r.value

    elif app == "clients":
        import clients.models as clients

        if mdl == "Individual":
            if clients.Individual.objects.filter(pk=pk):
                i = clients.Individual.objects.get(pk=pk)
                d["pk"] = i.pk
                d["family"] = i.family
                d["name"] = i.name
                d["patronymic"] = i.patronymic
                d["birthday"] = i.bd()
                d["sex"] = i.sex

        elif mdl == "Card":
            if clients.Card.objects.filter(pk=pk):
                c = clients.Card.objects.get(pk=pk)
                d["pk"] = c.pk
                d["number"] = c.number
                d["base"] = c.base_id
                d["individual"] = c.individual_id
                d["is_archive"] = c.is_archive

        elif mdl == "CardBase":
            if clients.CardBase.objects.filter(pk=pk):
                c = clients.CardBase.objects.get(pk=pk)
                d["pk"] = c.pk
                d["title"] = c.title
                d["short_title"] = c.short_title

    elif app == "podrazdeleniya":
        import podrazdeleniya.models as podrazdeleniya

        if mdl == "Podrazdeleniya":
            if podrazdeleniya.Podrazdeleniya.objects.filter(pk=pk):
                i = podrazdeleniya.Podrazdeleniya.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["short_title"] = i.short_title
                d["p_type"] = i.p_type

    elif app == "users":
        import users.models as users

        if mdl == "DoctorProfile":
            if users.DoctorProfile.objects.filter(pk=pk):
                i = users.DoctorProfile.objects.get(pk=pk)
                d["pk"] = i.pk
                d["username"] = i.user.username
                d["fio"] = i.fio
                d["podrazdeleniye"] = i.podrazdeleniye_id

    elif app == "researches":
        import researches.models as researches

        if mdl == "Tubes":
            if researches.Tubes.objects.filter(pk=pk):
                i = researches.Tubes.objects.get(pk=pk)
                d["pk"] = i.pk
                d["color"] = i.color
                d["title"] = i.title
                d["short_title"] = i.get_short_title()

    elif app == "directory":
        import directory.models as directory

        if mdl == "Researches":
            if directory.Researches.objects.filter(pk=pk):
                i = directory.Researches.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["short_title"] = i.get_title()
                d["podrazdeleniye"] = i.podrazdeleniye_id
                d["is_paraclinic"] = i.is_paraclinic
                d["code"] = i.code

        elif mdl == "Fractions":
            if directory.Fractions.objects.filter(pk=pk):
                i = directory.Fractions.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["research"] = i.research_id
                d["order"] = i.sort_weight
                d["code"] = i.code

        elif mdl == "ParaclinicInputGroups":
            if directory.ParaclinicInputGroups.objects.filter(pk=pk):
                i = directory.ParaclinicInputGroups.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["research"] = i.research_id
                d["order"] = i.order

        elif mdl == "ParaclinicInputField":
            if directory.ParaclinicInputField.objects.filter(pk=pk):
                i = directory.ParaclinicInputField.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["group"] = i.group_id
                d["order"] = i.order

    return JsonResponse(d)

def get_directory(request):
    import clients.models as clients
    import directions.models as directions
    import podrazdeleniya.models as podrazdeleniya
    import users.models as users
    import researches.models as researches
    import directory.models as directory

    d = {
        "clients.models.CardBase": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.short_title
        } for x in clients.CardBase.objects.all().order_by("pk")],

        "directions.models.IstochnikiFinansirovaniya": [{
            "pk": x.pk,
            "title": x.title,
            "base": x.base_id
        } for x in directions.IstochnikiFinansirovaniya.objects.all().order_by("pk")],

        "podrazdeleniya.models.Podrazdeleniya.TYPES": [{"pk": x[0], "title": x[1]} for x in
                                                       podrazdeleniya.Podrazdeleniya.TYPES],

        "podrazdeleniya.models.Podrazdeleniya": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.get_title(),
            "p_type": x.p_type
        } for x in podrazdeleniya.Podrazdeleniya.objects.all().order_by("pk")],

        "users.models.DoctorProfile": [{
            "pk": x.pk,
            "username": x.user.username,
            "fio": x.fio,
            "podrazdeleniye": x.podrazdeleniye_id
        } for x in users.DoctorProfile.objects.all().order_by("pk")],

        "researches.models.Tubes": [{
            "pk": x.pk,
            "color": x.color,
            "title": x.title,
            "short_title": x.get_short_title()
        } for x in researches.Tubes.objects.all().order_by("pk")],

        "directory.models.Researches": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.get_title(),
            "podrazdeleniye": x.podrazdeleniye_id,
            "is_paraclinic": x.is_paraclinic,
            "code": x.code,
        } for x in directory.Researches.objects.all().order_by("pk")],

        "directory.models.Fractions": [{
            "pk": x.pk,
            "title": x.title,
            "research": x.research_id,
            "order": x.sort_weight,
            "code": x.code,
        } for x in directory.Fractions.objects.all().order_by("pk")],

        "directory.models.ParaclinicInputGroups": [{
            "pk": x.pk,
            "title": x.title,
            "research": x.research_id,
            "order": x.order,
        } for x in directory.ParaclinicInputGroups.objects.all().order_by("pk")],

        "directory.models.ParaclinicInputField": [{
            "pk": x.pk,
            "title": x.title,
            "group": x.group_id,
            "order": x.order,
        } for x in directory.ParaclinicInputField.objects.all().order_by("pk")],
    }

    return JsonResponse(d)
<commit_msg>2.16     + Статистика<commit_after>from datetime import datetime

from django.http import JsonResponse
from django.utils import timezone

def dt(orig: [datetime, None]) -> [float, None]:
    return None if not orig else timezone.localtime(orig).timestamp()

def get_object(request):
    model = request.GET["model"].split(".")
    pk = request.GET["pk"]
    d = {}
    app = model[0]
    mdl = model[2]
    if app == "directions":
        import directions.models as directions

        if mdl == "IstochnikiFinansirovaniya":
            if directions.IstochnikiFinansirovaniya.objects.filter(pk=pk).exists():
                n = directions.IstochnikiFinansirovaniya.objects.get(pk=pk)
                d["pk"] = n.pk
                d["title"] = n.title
                d["base"] = n.base_id

        elif mdl == "Napravleniya":
            if directions.Napravleniya.objects.filter(pk=pk).exists():
                n = directions.Napravleniya.objects.get(pk=pk)
                d["pk"] = n.pk
                d["history_num"] = n.history_num
                d["data_sozdaniya"] = dt(n.data_sozdaniya)
                d["client"] = n.client_id
                d["diagnos"] = n.diagnos
                d["doc"] = n.doc_id
                d["doc_who_create"] = n.doc_who_create_id
                d["istochnik_f"] = n.istochnik_f_id
                d["cancel"] = n.cancel
                d["result_rmis_send"] = n.result_rmis_send

        elif mdl == "TubesRegistration":
            if directions.TubesRegistration.objects.filter(pk=pk).exists():
                t = directions.TubesRegistration.objects.get(pk=pk)
                d["pk"] = t.pk
                d["type"] = t.type.tube_id
                d["doc_get"] = None if not t.doc_get else t.doc_get_id
                d["time_get"] = dt(t.time_get)
                d["doc_recive"] = None if not t.doc_recive else t.doc_recive_id
                d["time_recive"] = dt(t.time_recive)
                d["notice"] = t.notice
                d["issledovaniya"] = [x.pk for x in t.issledovaniya_set.all()]

        elif mdl == "Issledovaniya":
            if directions.Issledovaniya.objects.filter(pk=pk).exists():
                i = directions.Issledovaniya.objects.get(pk=pk)
                d["pk"] = i.pk
                d["direction"] = i.napravleniye_id
                d["research"] = i.research_id
                d["doc_save"] = None if not i.doc_save else i.doc_save_id
                d["time_save"] = dt(i.time_save)
                d["doc_confirmation"] = None if not i.doc_confirmation else i.doc_confirmation_id
                d["time_confirmation"] = dt(i.time_confirmation)
                d["lab_comment"] = i.lab_comment

        elif mdl == "Result":
            if directions.Result.objects.filter(pk=pk):
                r = directions.Result.objects.get(pk=pk)
                d["pk"] = r.pk
                d["issledovaniye"] = r.issledovaniye_id
                d["fraction"] = r.fraction_id
                d["value"] = r.value
                d["units"] = r.units
                d["is_normal"] = r.is_normal

        elif mdl == "ParaclinicResult":
            if directions.ParaclinicResult.objects.filter(pk=pk):
                r = directions.ParaclinicResult.objects.get(pk=pk)
                d["pk"] = r.pk
                d["issledovaniye"] = r.issledovaniye_id
                d["field"] = r.field_id
                d["value"] = r.value

    elif app == "clients":
        import clients.models as clients

        if mdl == "Individual":
            if clients.Individual.objects.filter(pk=pk):
                i = clients.Individual.objects.get(pk=pk)
                d["pk"] = i.pk
                d["family"] = i.family
                d["name"] = i.name
                d["patronymic"] = i.patronymic
                d["birthday"] = i.bd()
                d["sex"] = i.sex

        elif mdl == "Card":
            if clients.Card.objects.filter(pk=pk):
                c = clients.Card.objects.get(pk=pk)
                d["pk"] = c.pk
                d["number"] = c.number
                d["base"] = c.base_id
                d["individual"] = c.individual_id
                d["is_archive"] = c.is_archive

        elif mdl == "CardBase":
            if clients.CardBase.objects.filter(pk=pk):
                c = clients.CardBase.objects.get(pk=pk)
                d["pk"] = c.pk
                d["title"] = c.title
                d["short_title"] = c.short_title

    elif app == "podrazdeleniya":
        import podrazdeleniya.models as podrazdeleniya

        if mdl == "Podrazdeleniya":
            if podrazdeleniya.Podrazdeleniya.objects.filter(pk=pk):
                i = podrazdeleniya.Podrazdeleniya.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["short_title"] = i.short_title
                d["p_type"] = i.p_type

    elif app == "users":
        import users.models as users

        if mdl == "DoctorProfile":
            if users.DoctorProfile.objects.filter(pk=pk):
                i = users.DoctorProfile.objects.get(pk=pk)
                d["pk"] = i.pk
                d["username"] = i.user.username
                d["fio"] = i.fio
                d["podrazdeleniye"] = i.podrazdeleniye_id

    elif app == "researches":
        import researches.models as researches

        if mdl == "Tubes":
            if researches.Tubes.objects.filter(pk=pk):
                i = researches.Tubes.objects.get(pk=pk)
                d["pk"] = i.pk
                d["color"] = i.color
                d["title"] = i.title
                d["short_title"] = i.get_short_title()

    elif app == "directory":
        import directory.models as directory

        if mdl == "Researches":
            if directory.Researches.objects.filter(pk=pk):
                i = directory.Researches.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["short_title"] = i.get_title()
                d["podrazdeleniye"] = i.podrazdeleniye_id
                d["is_paraclinic"] = i.is_paraclinic
                d["code"] = i.code

        elif mdl == "Fractions":
            if directory.Fractions.objects.filter(pk=pk):
                i = directory.Fractions.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["research"] = i.research_id
                d["order"] = i.sort_weight
                d["code"] = i.code

        elif mdl == "ParaclinicInputGroups":
            if directory.ParaclinicInputGroups.objects.filter(pk=pk):
                i = directory.ParaclinicInputGroups.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["research"] = i.research_id
                d["order"] = i.order

        elif mdl == "ParaclinicInputField":
            if directory.ParaclinicInputField.objects.filter(pk=pk):
                i = directory.ParaclinicInputField.objects.get(pk=pk)
                d["pk"] = i.pk
                d["title"] = i.title
                d["group"] = i.group_id
                d["order"] = i.order

    return JsonResponse(d)

def get_directory(request):
    import clients.models as clients
    import directions.models as directions
    import podrazdeleniya.models as podrazdeleniya
    import users.models as users
    import researches.models as researches
    import directory.models as directory

    d = {
        "clients.models.CardBase": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.short_title
        } for x in clients.CardBase.objects.all().order_by("pk")],

        "directions.models.IstochnikiFinansirovaniya": [{
            "pk": x.pk,
            "title": x.title,
            "base": x.base_id
        } for x in directions.IstochnikiFinansirovaniya.objects.all().order_by("pk")],

        "podrazdeleniya.models.Podrazdeleniya.TYPES": [{"pk": x[0], "title": x[1]} for x in
                                                       podrazdeleniya.Podrazdeleniya.TYPES],

        "podrazdeleniya.models.Podrazdeleniya": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.get_title(),
            "p_type": x.p_type
        } for x in podrazdeleniya.Podrazdeleniya.objects.all().order_by("pk")],

        "users.models.DoctorProfile": [{
            "pk": x.pk,
            "username": x.user.username,
            "fio": x.fio,
            "podrazdeleniye": x.podrazdeleniye_id
        } for x in users.DoctorProfile.objects.all().order_by("pk")],

        "researches.models.Tubes": [{
            "pk": x.pk,
            "color": x.color,
            "title": x.title,
            "short_title": x.get_short_title()
        } for x in researches.Tubes.objects.all().order_by("pk")],

        "directory.models.Researches": [{
            "pk": x.pk,
            "title": x.title,
            "short_title": x.get_title(),
            "podrazdeleniye": x.podrazdeleniye_id,
            "is_paraclinic": x.is_paraclinic,
            "code": x.code,
        } for x in directory.Researches.objects.all().order_by("pk")],

        "directory.models.Fractions": [{
            "pk": x.pk,
            "title": x.title,
            "research": x.research_id,
            "order": x.sort_weight,
            "code": x.code,
        } for x in directory.Fractions.objects.all().order_by("pk")],

        "directory.models.ParaclinicInputGroups": [{
            "pk": x.pk,
            "title": x.title,
            "research": x.research_id,
            "order": x.order,
        } for x in directory.ParaclinicInputGroups.objects.all().order_by("pk")],

        "directory.models.ParaclinicInputField": [{
            "pk": x.pk,
            "title": x.title,
            "group": x.group_id,
            "order": x.order,
        } for x in directory.ParaclinicInputField.objects.all().order_by("pk")],
    }

    return JsonResponse(d)
<|endoftext|>


<commit_before>"""
Django settings for superlists project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1_2v2!6#e&i6ha+h2380x52o$87dj4!x+-_%t12sitxrh3382l'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'lists',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'superlists.urls'

WSGI_APPLICATION = 'superlists.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'
<commit_msg>Set STATIC_ROOT in settings and disable admin site<commit_after>"""
Django settings for superlists project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1_2v2!6#e&i6ha+h2380x52o$87dj4!x+-_%t12sitxrh3382l'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = (
    #'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'lists',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'superlists.urls'

WSGI_APPLICATION = 'superlists.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.abspath(os.path.join(BASE_DIR, '../static'))
<|endoftext|>


<commit_before><commit_msg>started on iptables bits for the agent<commit_after>import os
import re

data1 = """Chain INPUT (policy ACCEPT 2606360 packets, 1157611388 bytes)
pkts bytes target prot opt in out source destination
716412 215078536 all -- eth0 * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 2568282 packets, 1334805872 bytes)
pkts bytes target prot opt in out source destination
678334 392273020 all -- * eth0 0.0.0.0/0 0.0.0.0/0
"""

data2 = """Chain INPUT (policy ACCEPT 7176 packets, 488039 bytes)
pkts bytes target prot opt in out source destination
7194 489261 all -- eth0 * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 7161 packets, 5772399 bytes)
pkts bytes target prot opt in out source destination
7161 5772399 all -- * eth0 0.0.0.0/0 0.0.0.0/0
"""

data3 = """Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
25576 1430552 all -- eth0 * 0.0.0.0/0 0.0.0.0/0
78644 4415632 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
499 39920 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
3 360 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
3 156 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
8938 728746 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 25765 packets, 44644747 bytes)
pkts bytes target prot opt in out source destination
"""

data4 = """Chain INPUT (policy ACCEPT 17 packets, 1175 bytes)
    pkts      bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts      bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 12 packets, 9943 bytes)
    pkts      bytes target     prot opt in     out     source               destination
"""

def parse_output(data):

    inbound_text = ""
    outbound_text = ""
    forward_text = ""

    psc = 0
    for line in data.split('\n'):

        if(psc==0):
            if(line): inbound_text += line+"\n"
        if(psc==1):
            if(line): forward_text += line+"\n"
        if(psc==2):
            if(line): outbound_text += line+"\n"

        if(len(line)==0): psc += 1

    #print '-'*10
    #print 'inbound_text', inbound_text
    #print '-'*10
    #print 'forward_text', forward_text
    #print '-'*10
    #print 'outbound_text', outbound_text
    #print '-'*10

    inbound_traffic = {}

    for line in inbound_text.split('\n'):
        if("INPUT" in line):
            input_accept = line.split('policy ACCEPT ')[1]
            input_accept = input_accept.split(' ')
            input_accept_packets = input_accept[0]
            input_accept_bytes = input_accept[2]
            break

    inbound_traffic['input_accept_packets_counted'] = 0
    inbound_traffic['input_accept_bytes_counted'] = 0
    inbound_traffic['input_accept_packets_from_header'] = input_accept_packets
    inbound_traffic['input_accept_bytes_from_header'] = input_accept_bytes

    input_accept_packets = 0
    input_accept_bytes = 0

    # XXX count packets, bytes manually

    # XXX compare values and preferably go for those which are counted

    print 'inbound_traffic', inbound_traffic

    outbound_traffic = {}
    forward_traffic = {}

    return

parse_output(data1)
print '*'*80
parse_output(data2)
print '*'*80
parse_output(data3)
print '*'*80
parse_output(data4)
print '*'*80
<|endoftext|>


<commit_before>from devicehive.info import Info
from devicehive.token import Token
from devicehive.device import Device

class Api(object):
    """Api class."""

    def __init__(self, transport, authentication):
        self._transport = transport
        self._token = Token(transport, authentication)

    def authenticate(self):
        self._token.authenticate()

    def get_info(self):
        info = Info(self._transport)
        return info.get()

    def get_cluster_info(self):
        info = Info(self._transport)
        return info.get_cluster()

    def create_token(self, user_id, expiration=None, actions=None,
                     network_ids=None, device_ids=None):
        return self._token.create(user_id, expiration, actions, network_ids,
                                  device_ids)

    def refresh_token(self):
        self._token.refresh()
        return self._token.access_token()

    def list_devices(self, name=None, name_pattern=None, network_id=None,
                     network_name=None, sort_field=None, sort_order=None,
                     take=None, skip=None):
        # TODO: implement params for websocket when API will be extended.
        url = 'device'
        action = 'device/list'
        request = {}
        params = {'response_key': 'devices', 'params': {}}
        if name:
            params['params']['name'] = name
        if name_pattern:
            params['params']['namePattern'] = name_pattern
        if network_id:
            params['params']['networkId'] = network_id
        if network_name:
            params['params']['networkName'] = network_name
        if sort_field:
            params['params']['sortField'] = sort_field
        if sort_order:
            params['params']['sortOrder'] = sort_order
        if take:
            params['params']['take'] = take
        if skip:
            params['params']['skip'] = skip
        response = self._token.authorized_request(url, action, request,
                                                  **params)
        # TODO: replace assert with exception.
        assert response.success(), 'List devices failure'
        devices = response.response('devices')
        return [Device(self._transport, self._token, device)
                for device in devices]

    def get_device(self, device_id):
        device = Device(self._transport, self._token)
        device.get(device_id)
        return device

    def put_device(self, device_id, name=None, data=None, network_id=None,
                   is_blocked=False):
        if not name:
            name = device_id
        device = {Device.ID_KEY: device_id,
                  Device.NAME_KEY: name,
                  Device.DATA_KEY: data,
                  Device.NETWORK_ID_KEY: network_id,
                  Device.IS_BLOCKED_KEY: is_blocked}
        device = Device(self._transport, self._token, device)
        device.save()
        return device

    def disconnect(self):
        if not self._transport.connected():
            return
        self._transport.disconnect()
<commit_msg>Remove assert<commit_after>from devicehive.info import Info
from devicehive.token import Token
from devicehive.device import Device
from devicehive.api_object import ensure_success_response

class Api(object):
    """Api class."""

    def __init__(self, transport, authentication):
        self._transport = transport
        self._token = Token(transport, authentication)

    def authenticate(self):
        self._token.authenticate()

    def get_info(self):
        info = Info(self._transport)
        return info.get()

    def get_cluster_info(self):
        info = Info(self._transport)
        return info.get_cluster()

    def create_token(self, user_id, expiration=None, actions=None,
                     network_ids=None, device_ids=None):
        return self._token.create(user_id, expiration, actions, network_ids,
                                  device_ids)

    def refresh_token(self):
        self._token.refresh()
        return self._token.access_token()

    def list_devices(self, name=None, name_pattern=None, network_id=None,
                     network_name=None, sort_field=None, sort_order=None,
                     take=None, skip=None):
        # TODO: implement params for websocket when API will be extended.
        url = 'device'
        action = 'device/list'
        request = {}
        params = {'response_key': 'devices', 'params': {}}
        if name:
            params['params']['name'] = name
        if name_pattern:
            params['params']['namePattern'] = name_pattern
        if network_id:
            params['params']['networkId'] = network_id
        if network_name:
            params['params']['networkName'] = network_name
        if sort_field:
            params['params']['sortField'] = sort_field
        if sort_order:
            params['params']['sortOrder'] = sort_order
        if take:
            params['params']['take'] = take
        if skip:
            params['params']['skip'] = skip
        response = self._token.authorized_request(url, action, request,
                                                  **params)
        ensure_success_response(self._transport.name(), response,
                                'List devices failure')
        devices = response.response('devices')
        return [Device(self._transport, self._token, device)
                for device in devices]

    def get_device(self, device_id):
        device = Device(self._transport, self._token)
        device.get(device_id)
        return device

    def put_device(self, device_id, name=None, data=None, network_id=None,
                   is_blocked=False):
        if not name:
            name = device_id
        device = {Device.ID_KEY: device_id,
                  Device.NAME_KEY: name,
                  Device.DATA_KEY: data,
                  Device.NETWORK_ID_KEY: network_id,
                  Device.IS_BLOCKED_KEY: is_blocked}
        device = Device(self._transport, self._token, device)
        device.save()
        return device

    def disconnect(self):
        if not self._transport.connected():
            return
        self._transport.disconnect()
<|endoftext|>


<commit_before># -*- encoding: UTF-8 -*-

from core.tests.helpers import init, clean
from cvn import settings as st_cvn
from django import test
from django.contrib.auth.models import User
from selenium import webdriver
from selenium.common.exceptions import (NoSuchElementException,
                                        NoAlertPresentException)
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest
from pyvirtualdisplay import Display

class LoginCAS(test.LiveServerTestCase):

    def __init__(self, *args, **kwargs):
        super(LoginCAS, self).__init__(*args, **kwargs)
        init()

    def setUp(self):
        display = Display(visible=0, size=(1280, 1024))
        display.start()
        self.display = display
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        #self.driver.set_page_load_timeout(30)
        self.base_url = "https://loginpruebas.ull.es/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME, "page-header"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_login_no_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invNoCas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.ID, "status"))

    def test_upload_cvn_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id(
            "id_cvn_file").send_keys(st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_no_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-NO-FECYT.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "errorlist"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_user_admin(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # Add admin permissions after login CAS
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.CLASS_NAME, 'btn-signout')))
        u = User.objects.get(username="invipas")
        u.is_staff = True
        u.is_superuser = True
        u.save()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-Test.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_download_cvn(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # First upload CVN
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        # Download CVN
        driver.find_element_by_link_text(
            u'Descargar una copia de mi CVN').click()
        # Check if CVN window is open
        self.assertEqual(len(driver.window_handles), 2)
        # Change to CVN window
        driver.switch_to_window(driver.window_handles[-1])
        # Return to 'Main' windows
        driver.switch_to_window(driver.window_handles[0])
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_rrhh(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id(
            "id_cvn_file").send_keys(st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        self.assertFalse(self.is_element_present(By.ID, "dept"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException:  # , e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException:  # , e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)

    @classmethod
    def tearDownClass(cls):
        clean()

if __name__ == "__main__":
    unittest.main()
<commit_msg>Change the wait value to find a dom element so the tests run faster<commit_after># -*- encoding: UTF-8 -*-

from core.tests.helpers import init, clean
from cvn import settings as st_cvn
from django import test
from django.contrib.auth.models import User
from selenium import webdriver
from selenium.common.exceptions import (NoSuchElementException,
                                        NoAlertPresentException)
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import unittest
from pyvirtualdisplay import Display

class LoginCAS(test.LiveServerTestCase):

    def __init__(self, *args, **kwargs):
        super(LoginCAS, self).__init__(*args, **kwargs)
        init()

    def setUp(self):
        display = Display(visible=0, size=(1280, 1024))
        display.start()
        self.display = display
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(8)
        #self.driver.set_page_load_timeout(30)
        self.base_url = "https://loginpruebas.ull.es/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME, "page-header"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_login_no_cas(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invNoCas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        self.assertTrue(self.is_element_present(By.ID, "status"))

    def test_upload_cvn_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id(
            "id_cvn_file").send_keys(st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_no_fecyt(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-NO-FECYT.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "errorlist"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_upload_cvn_user_admin(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invipas")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # Add admin permissions after login CAS
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
            (By.CLASS_NAME, 'btn-signout')))
        u = User.objects.get(username="invipas")
        u.is_staff = True
        u.is_superuser = True
        u.save()
#        driver.find_element_by_id("id_cvn_file").clear()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-Test.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        self.assertTrue(self.is_element_present(By.CLASS_NAME,
                                                "alert-success"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_download_cvn(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        # First upload CVN
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id("id_cvn_file").send_keys(
            st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        #driver.find_element_by_xpath("//button[@type='submit']").click()
        # Download CVN
        driver.find_element_by_link_text(
            u'Descargar una copia de mi CVN').click()
        # Check if CVN window is open
        self.assertEqual(len(driver.window_handles), 2)
        # Change to CVN window
        driver.switch_to_window(driver.window_handles[-1])
        # Return to 'Main' windows
        driver.switch_to_window(driver.window_handles[0])
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def test_user_rrhh(self):
        driver = self.driver
        driver.get(self.base_url +
                   "/cas-1/login?service=http%3A%2F%2Flocalhost%3A8081%2F" +
                   "investigacion%2Faccounts%2Flogin%2F%3Fnext%3D%252F" +
                   "es%252Finvestigacion%252Fcvn%252F")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("invbecario")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("pruebasINV1")
        driver.find_element_by_name("submit").click()
        driver.execute_script(
            'document.getElementById("id_cvn_file").removeAttribute("style")')
        driver.find_element_by_id(
            "id_cvn_file").send_keys(st_cvn.TEST_ROOT + "cvn/CVN-Test_2.pdf")
        self.assertFalse(self.is_element_present(By.ID, "dept"))
        driver.find_element_by_link_text(u"Cerrar sesión").click()

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException:  # , e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to_alert()
        except NoAlertPresentException:  # , e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.display.stop()
        self.assertEqual([], self.verificationErrors)

    @classmethod
    def tearDownClass(cls):
        clean()

if __name__ == "__main__":
    unittest.main()
<|endoftext|>