def load_reports(state=None, anonymize=True): _loc = u._fac_cache('fug') def extract_report(r): data = u.extract_report(r) fug_id = data['facility'] fug = _loc(fug_id) data['fug'] = fug.name data['facility'] = fug.parent_id return data reports = [extract_report(r) for r in FadamaReport.objects.all().select_related()] facs = facilities_by_id() reports = [r for r in reports if state is None or state == facs[r['facility']]['state']] # todo: these should probably be loaded on-demand for individual reports comments = map_reduce(ReportComment.objects.all(), lambda c: [(c.report_id, c)]) def _ts(r): return datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S') for r in reports: if not anonymize: r['_contact'] = r['contact'] u.anonymize_contact(r) r['month'] = _ts(r).strftime('%b %Y') r['_month'] = _ts(r).strftime('%Y-%m') r['thread'] = [c.json() for c in sorted(comments.get(r['id'], []), key=lambda c: c.date)] r['display_time'] = _ts(r).strftime('%d/%m/%y %H:%M') r['site_name'] = facs[r['facility']]['name'] reports_by_contact = map_reduce((r for r in reports if not r['proxy']), lambda r: [(r['contact'], r)]) for r in reports: r['from_same'] = [k['id'] for k in reports_by_contact.get(r['contact'], []) if k != r and abs(_ts(r) - _ts(k)) <= settings.RECENT_REPORTS_FROM_SAME_PHONE_WINDOW] return reports
def load_reports(user=None, state=None, anonymize=True): FadamaReport = get_model('dashboard', 'FadamaReport') ReportComment = get_model('dashboard', 'ReportComment') ReportCommentView = get_model('dashboard', 'ReportCommentView') facs = facilities_by_id() fugs = u._fac_cache('fug') def extract_report(r): data = u.extract_report(r) loc_id = data['facility'] fug = fugs(loc_id) if fug: data['fug'] = fug.name data['facility'] = fug.parent_id elif facs.get(loc_id): data['fug'] = None data['facility'] = loc_id else: data['fug'] = None data['facility'] = None return data reports = [extract_report(r) for r in FadamaReport.objects.all().select_related()] def filter_state(r, state): if state is None: return True else: reporting_fug = fugs(r['reporting_facility']) if reporting_fug: reporting_fca_id = reporting_fug.parent_id else: reporting_fca_id = r['reporting_facility'] reporting_state = facs[reporting_fca_id]['state'] return state == reporting_state reports = [r for r in reports if filter_state(r, state)] # todo: these should probably be loaded on-demand for individual reports comments = map_reduce(ReportComment.objects.filter(fadama_report__isnull=False), lambda c: [(c.fadama_report_id, c)]) def _ts(r): return datetime.strptime(r['timestamp'], '%Y-%m-%dT%H:%M:%S') views = [] if user: views = ReportCommentView.objects.filter(user=user)\ .values_list('report_comment', flat=True) def _get_json(comment): # Add whether or not the comment has been viewed. json = comment.json() json.update({'viewed': comment.pk in views if user else None}) return json for r in reports: if not anonymize: r['_contact'] = r['contact'] u.anonymize_contact(r) r['month'] = _ts(r).strftime('%b %Y') r['_month'] = _ts(r).strftime('%Y-%m') r['thread'] = [_get_json(c) for c in sorted(comments.get(r['id'], []), key=lambda c: c.date)] r['display_time'] = _ts(r).strftime('%d/%m/%y at %H:%M') r['site_name'] = facs[r['facility']]['name'] if r['for_this_site'] else r['site_other'] reports_by_contact = map_reduce((r for r in reports if not r['proxy']), lambda r: [(r['contact'], r)]) for r in reports: r['from_same'] = [k['id'] for k in reports_by_contact.get(r['contact'], []) if k != r and abs(_ts(r) - _ts(k)) <= settings.RECENT_REPORTS_FROM_SAME_PHONE_WINDOW] return reports