def get_data_below(organization, startdate, enddate, depth): """Do a lookukp of the organizations children and flattens the recursive return into a simple array of items in the format: [recursiondepth, descriptor, item, report_rowdata] """ fullret = [] for child_org in organization.children.all(): # add the child organization itself fullret.append(get_single_item(None, child_org, startdate, enddate, depth)) members, supervisors = utils.get_members_and_supervisors(child_org) # add supervisor/member counts is_first = True for supervisor in supervisors: # leaving the hack in that everything after the first is expected # to have a "None" description. This should be revisited if is_first: fullret.append(get_single_item("Supervisors", supervisor, startdate, enddate, depth + 1)) is_first = False else: fullret.append(get_single_item(None, supervisor, startdate, enddate, depth + 1)) is_first = True for member in members: if is_first: fullret.append(get_single_item("Members", member, startdate, enddate, depth + 1)) is_first = False else: fullret.append(get_single_item(None, member, startdate, enddate, depth + 1)) # and don't forget to recurse through this child's children, if they # exist fullret += get_data_below(child_org, startdate, enddate, depth + 1) # CZUE: 6/9/2009 this was in the previous implementation. I don't exactly # know how the cache works, but I'm leaving it in. if depth == 0: username_datecount_cache.clear() return fullret
def get_single_org_data(organization, startdate, enddate): fullret = [] fullret.append(get_single_item(None, organization, startdate, enddate, 0)) members, supervisors = utils.get_members_and_supervisors(organization) is_first = True for supervisor in supervisors: # leaving the hack in that everything after the first is expected # to have a "None" description. This should be revisited if is_first: fullret.append(get_single_item("Supervisors", supervisor, startdate, enddate, 1)) is_first = False else: fullret.append(get_single_item(None, supervisor, startdate, enddate, 1)) is_first = True #reset the first of the supervisors for member in members: if is_first: fullret.append(get_single_item("Members", member, startdate, enddate, 1)) is_first = False else: fullret.append(get_single_item(None, member, startdate, enddate, 1)) return fullret
def get_org_reportdata(organization, startdate, enddate): """returns a full domain of data reportage information""" fullret = [] members, supervisors = utils.get_members_and_supervisors(organization) is_first = True fdefs = get_formdefs_for_domain(organization.domain) for supervisor in supervisors: # leaving the hack in that everything after the first is expected # to have a "None" description. This should be revisited try: username = ReporterProfile.objects.get(reporter=supervisor).chw_username usercount = get_username_count(fdefs,[username], startdate, enddate)[username] except Exception, e: logging.error("Error with reporter profile query: " + str(supervisor) + " :: " + str(e)) continue if is_first: fullret.append([1,"Supervisors", supervisor, usercount]) is_first = False else: fullret.append([1,None, supervisor, usercount])
def testGetAllMembers(self): orgs = Organization.objects.all() for org in orgs: print utils.get_members_and_supervisors(org)
def get_aggregate_count(content_obj, startdate, enddate,forms_to_filter=None): """For a given content object, presumably it's a organization or a user, it'll query all the xforms in its domain and see what the aggregate counts of all submissions it has under itself""" if not content_obj: return 0 usernames_to_filter = [] totalspan = enddate-startdate master_date_hash = {} for day in range(0,totalspan.days+1): delta = timedelta(days=day) target_date = startdate + delta master_date_hash[target_date.strftime('%m/%d/%Y')] = 0 is_supervisor = False is_org = False is_member = False domain = None if isinstance(content_obj, Organization): is_org = True domain = content_obj.domain (members, supervisors) = utils.get_members_and_supervisors(content_obj) for member in members: # only include these if we have a valid mapping to a chw_id try: usernames_to_filter.append(member.profile.get().report_identity) except ReporterProfile.DoesNotExist: # this is an okay error - just don't include them pass for supervisor in supervisors: try: usernames_to_filter.append(supervisor.profile.get().report_identity) except ReporterProfile.DoesNotExist: # this is an okay error - just don't include them pass elif isinstance(content_obj, User): # CZUE: this could very likely blow up on us. There is no # guarantee that this object exists domain = content_obj.domain_membership.filter(is_active=True)[0] supervising_orgs = utils.get_supervisor_roles(content_obj) usernames_to_filter.append(content_obj.report_identity) is_member = True for user in usernames_to_filter: if not username_datecount_cache.has_key(user): username_datecount_cache[user] = get_user_forms_count(domain, user, startdate, enddate,forms_to_filter=forms_to_filter) for target_date in username_datecount_cache[user].keys(): master_date_hash[target_date] += username_datecount_cache[user][target_date] row = [] for day in range(0,totalspan.days+1): delta = timedelta(days=day) target_date = startdate + delta val = master_date_hash[target_date.strftime('%m/%d/%Y')] row.append(val) return row
def run_reports(run_frequency): """Entry point for all reports in ReportSchedule to run For a given frequency, ALL corresponding reports for that frequency will be queried and executed.""" (startdate, enddate) = get_daterange(run_frequency) logging.debug("running reports for " + run_frequency) for report in ReportSchedule.objects.all().filter(active=True, report_frequency=run_frequency): try: logging.debug("running report " + str(report)) # dmyung - note, this needs to be refactored ASAP to reflect the metadata based reports. if report.report_class == "siteadmin": # get the user id, then, get the report function. usr = report.recipient_user organization = report.organization if organization != None: data = get_data_for_organization(run_frequency, report.report_delivery, organization) logging.debug("got report data") params = {} heading = "Report for: " + startdate.strftime("%m/%d/%Y") + " - " + enddate.strftime("%m/%d/%Y") params["heading"] = heading if report.report_delivery == "email": subject = ( "[CommCare HQ] " + run_frequency + " report " + startdate.strftime("%m/%d/%Y") + "-" + enddate.strftime("%m/%d/%Y") + " :: " + str(organization) ) rendered_text = render_direct_email( data, startdate, enddate, "hq/reports/email_hierarchy_report.txt", params ) transport_email( rendered_text, usr, params={"startdate": startdate, "enddate": enddate, "email_subject": subject}, ) else: rendered_text = render_direct_sms( data, startdate, enddate, "hq/reports/sms_organization.txt", params ) transport_sms(rendered_text, usr, params) elif report.report_class == "supervisor" or report.report_class == "member": # get the organization field and run the hierarchical report org = report.organization (members, supervisors) = utils.get_members_and_supervisors(org) orgdata = metadata.get_org_reportdata(org, startdate, enddate) params = {} if report.report_delivery == "email": params["heading"] = ( "User report for period: " + startdate.strftime("%m/%d/%Y") + " - " + enddate.strftime("%m/%d/%Y") ) rendering_template = "hq/reports/email_hierarchy_report.txt" renderfunc = render_direct_email delivery_func = transport_email else: params["heading"] = "Report " + startdate.strftime("%m/%d") + " - " + enddate.strftime("%m/%d/%Y") rendering_template = "hq/reports/sms_organization.txt" renderfunc = render_direct_sms delivery_func = transport_sms data = metadata.get_org_reportdata(org, startdate, enddate) rendered_message = renderfunc(data, startdate, enddate, rendering_template, params) raise Exception("Delivery of metadata based supervisor/member reports not completed yet") if report.report_class == "member": for member in members: delivery_func(member, rendered_text, params) else: for super in supervisors: delivery_func(rendered_text, [super.email], params) logging.debug("report delivered") elif report.report_class == "other": # Custom function. We will check the attr for that function's existence and execute. funcname = report.report_function _debug_and_print("Activating custom report function " + str(funcname)) if hasattr(custom, funcname): func = getattr(custom, funcname) func(report, run_frequency) logging.debug("custom report complete") else: logging.error("Error, report custom function " + str(funcname) + " does not exist") elif report.report_class == "domain": # added this enum for the custom domained reports defined in # apps/reports/<domain>.py if not report.domain or not report.report_function: raise Exception("Domain report %s must have domain and function set." % report) _debug_and_print( "Activating custom domain report function %s for domain %s." % (report.report_function, report.domain) ) report_method = get_report_method(report.domain, report.report_function) if not report_method: raise Exception("No report named %s found for %s" % (report.report_function, report.domain)) # alrighty, we found the method we want to run. # HACK! For now try manually setting the language to swahili # as a proof of concept. Eventually this will come from the # attached reporter profile. # So in django all management commands default to english for # some reason, so we have to manually activate the language rather than # just assuming it'll work with the settings. translation.activate("sw") try: # TODO: not all reports may be okay with us passing in an empty # request object. For the time being we assume that they are # pretty moderate hackiness here - if the report takes in a domain # pass it in. if "domain" in inspect.getargspec(report_method)[0]: report_body = report_method(None, domain=report.domain) else: report_body = report_method(None) finally: # make sure we set the default back. translation.activate(settings.LANGUAGE_CODE) # _debug_and_print(report_body) transport_email( report_body, report.recipient_user, params={"startdate": startdate, "enddate": enddate, "email_subject": report_method.__doc__}, ) except Exception, e: logging.error("Error running " + run_frequency + " report: " + str(report) + " Exception: " + str(e))