<commit_before>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


from rapidsms.webui.utils import render_to_response
from django.http import HttpResponse
from django.template.loader import render_to_string
from xformmanager.models import Case
from datetime import datetime, date, timedelta

'''Define custom reports in this file.  The rules are one module per
   domain and the module name must match _lowercase_ the domain name.  
   Each function in the module will then be listed as a custom report
   that should return html formatted text that will be displayed
   as the report. The doc string of the method is what will be 
   displayed in the UI (self documentation!).'''
   # This is still a bit of a work in progress.  

def monitoring(request):
    '''Safe Pregnancy Monitoring Report'''
    context = { }
    # use the cases we've put together for this
    safe_preg_case_name = "MVP Safe Pregnancies"
    try:
        case = Case.objects.get(name=safe_preg_case_name)
    except Case.DoesNotExist:
        return '''Sorry, it doesn't look like the forms that this report 
                  depends on have been uploaded.'''
    
    data_maps = case.get_all_data_maps()
    all_moms = []
    healthy_moms = []
    very_pregnant_moms = []
    moms_needing_followup = []
    moms_with_open_referrals = []
    for id, map in data_maps.items():
        mom = Mother(id, map)
        all_moms.append(mom)
        prev_list_size = len(moms_needing_followup) +\
                         len(very_pregnant_moms) +\
                         len(moms_with_open_referrals)
        if mom.needs_followup:
            moms_needing_followup.append(mom)
        if mom.months_pregnant >= 7:
            very_pregnant_moms.append(mom)
        if mom.has_open_referral:
            moms_with_open_referrals.append(mom)
        new_list_size = len(moms_needing_followup) +\
                        len(very_pregnant_moms) +\
                        len(moms_with_open_referrals)
        if new_list_size == prev_list_size:
            # we didn't add her to any lists so put her in
            # healthy moms
            healthy_moms.append(mom) 
        
        
        
    context["all_moms"] = all_moms
    context["healthy_moms"] = healthy_moms
    context["open_referrals"] = moms_with_open_referrals
    context["very_pregnant"] = very_pregnant_moms
    context["need_followup"] = moms_needing_followup
    context["empty_data_holder"] = "<b>???</b>"
    return render_to_string("reports/mvp/monitoring.html", context)


class Mother(object):
    
    def __init__(self, id, data_map):
        self.id = id
        self.data_map = data_map 
        # calculate some properties and set them for easy access
        # these are totally hard coded to the forms.  
        # most of these depend on registration and will not display
        # very well if there is no registration
        
        # czue - i don't like that the sequence ids are hard-coded, but using the form
        # ids would just be way too long.  we could key these again by form, but
        # leaving that as an open-ended possibility.  a triple dictionary might
        # be a bit too much to deal with.
        self.mother_name = data_map["sampledata_mother_name_1"]
        chw_cols = ["meta_username_1","meta_username_2","meta_username_3",<commit_msg>Allow the creation of a blacklist of users in the mvp reports<commit_after>#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4


from rapidsms.webui.utils import render_to_response
from django.http import HttpResponse
from django.template.loader import render_to_string
from xformmanager.models import Case
from datetime import datetime, date, timedelta

'''Define custom reports in this file.  The rules are one module per
   domain and the module name must match _lowercase_ the domain name.  
   Each function in the module will then be listed as a custom report
   that should return html formatted text that will be displayed
   as the report. The doc string of the method is what will be 
   displayed in the UI (self documentation!).'''
   # This is still a bit of a work in progress.  

def monitoring(request):
    '''Safe Pregnancy Monitoring Report'''
    context = { }
    # use the cases we've put together for this
    safe_preg_case_name = "MVP Safe Pregnancies"
    try:
        case = Case.objects.get(name=safe_preg_case_name)
    except Case.DoesNotExist:
        return '''Sorry, it doesn't look like the forms that this report 
                  depends on have been uploaded.'''
    
    data_maps = case.get_all_data_maps()
    
    # allow a list of usernames whose submissions don't show up
    # in the report. 
    blacklist = ["teddy", "admin", "demo_user"]
    blacklist_columns = ["meta_username_1","meta_username_2",
                         "meta_username_3","meta_username_4"]
        
    all_moms = []
    healthy_moms = []
    very_pregnant_moms = []
    moms_needing_followup = []
    moms_with_open_referrals = []
    for id, map in data_maps.items():
        # check blacklist
        if _is_blacklisted(map, blacklist, blacklist_columns):
            continue
        mom = Mother(id, map)
        all_moms.append(mom)
        prev_list_size = len(moms_needing_followup) +\
                         len(very_pregnant_moms) +\
                         len(moms_with_open_referrals)
        if mom.needs_followup:
            moms_needing_followup.append(mom)
        if mom.months_pregnant >= 7:
            very_pregnant_moms.append(mom)
        if mom.has_open_referral:
            moms_with_open_referrals.append(mom)
        new_list_size = len(moms_needing_followup) +\
                        len(very_pregnant_moms) +\
                        len(moms_with_open_referrals)
        if new_list_size == prev_list_size:
            # we didn't add her to any lists so put her in
            # healthy moms
            healthy_moms.append(mom) 
        
        
        
    context["all_moms"] = all_moms
    context["healthy_moms"] = healthy_moms
    context["open_referrals"] = moms_with_open_referrals
    context["very_pregnant"] = very_pregnant_moms
    context["need_followup"] = moms_needing_followup
    context["empty_data_holder"] = "<b>???</b>"
    return render_to_string("reports/mvp/monitoring.html", context)

def _is_blacklisted(data, blacklist, blacklist_columns):
    '''Checks a set of columns and values, and if any of the
       columns contains one of the values, returns true'''
    for column in blacklist_columns:
        if data[column] in blacklist:
            return True
    return False

class Mother(object):
    
    def __init__(self, id, data_map):
        self.id = id
        self.data_map = data_map 
        # calculate some properties and set them for easy access
        # these are totally hard coded to the forms.  
        # most of these depend on registration and will not display
        # very well if there is no registration
        
        # czue - i don't like that the sequence ids are hard-coded, but using the form
        # ids would just be way too long.  we could key these again by form, but
        # leaving that as an open-ended possibility.  a triple dictionary might
        # be a bit too much to deal with.
        self.mother_name = data_map["sampledata_mother_name_1"]
        chw_cols = ["meta_username_1","meta_username_2","meta_username_3",
{'fraction_non_alphanumeric': 0.07335228041683584, 'fraction_numerical': 0.002706726214643389, 'mean_word_length': 2.5769603097773475, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '6190433', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2155, 'n_words': 685}