def rows(self): rows = {} if self.config['location_id']: last_period_st, last_period_end = calculate_last_period(self.config['enddate']) supply_points = get_supply_points(self.config['location_id'], self.config['domain']).values_list( 'supply_point_id', flat=True ) complete = 0 incomplete = 0 for sp in supply_points: products_count = len(SQLLocation.objects.get(supply_point_id=sp).products) st = StockTransaction.objects.filter(case_id=sp, report__date__range=[last_period_st, last_period_end] ).distinct('product_id').count() if products_count == st: complete += 1 else: incomplete += 1 rows = dict( total=complete + incomplete, complete=complete, incomplete=incomplete ) return rows
def rows(self): rows = [] if self.config['location_id']: supply_points = get_supply_points(self.config['location_id'], self.config['domain']).values_list( 'supply_point_id', flat=True ) last_period_st, last_period_end = calculate_last_period(self.config['enddate']) reported = StockTransaction.objects.filter(case_id__in=supply_points, report__date__range=[last_period_st, last_period_end] ).values_list('case_id', flat=True) not_reported = SQLLocation.objects.filter(location_type__in=self.location_types, parent__location_id=self.config['location_id'])\ .exclude(supply_point_id__in=reported) for loc in not_reported: url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (loc.location_id, self.config['startdate'], self.config['enddate'])) st = StockTransaction.objects.filter(case_id=loc.supply_point_id).order_by('-report__date') if st: date = st[0].report.date else: date = _('---') rows.append([link_format(loc.name, url), date]) return rows
def rows(self): rows = [] if self.config['location_id']: last_period_st, last_period_end = calculate_last_period(self.config['enddate']) locations = SQLLocation.objects.filter(parent__location_id=self.config['location_id'], location_type__in=self.location_types) for loc in locations: st = StockTransaction.objects.filter(case_id=loc.supply_point_id, report__date__range=[last_period_st, last_period_end] ).order_by('-report__date') st_count = st.distinct('product_id').count() if len(loc.products) != st_count: if st: date = st[0].report.date else: date = '---' url = make_url( StockLevelsReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (loc.location_id, self.config['startdate'], self.config['enddate'])) rows.append([link_format(loc.name, url), date]) return rows
def get_url(cls, domain=None, render_as=None, **kwargs): url = super(MultiReport, cls).get_url(domain=domain, render_as=None, kwargs=kwargs) request = kwargs.get('request') user = getattr(request, 'couch_user', None) dm = user.get_domain_membership(domain) if user else None if dm: if dm.program_id: program_id = dm.program_id else: program_id = 'all' location_id = get_user_location_id(user, domain) if location_id: try: location = SQLLocation.active_objects.get( location_id=location_id) if cls.__name__ == "DashboardReport": if not location.location_type.administrative: location = location.parent location_id = location.location_id except SQLLocation.DoesNotExist: location_id = None start_date, end_date = calculate_last_period() url = '%s?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s&datespan_first=%s' % ( url, location_id or '', program_id if program_id else '', start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'), '%s|%s' % (start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'))) return url
def get_url(cls, domain=None, render_as=None, **kwargs): url = super(MultiReport, cls).get_url(domain=domain, render_as=None, kwargs=kwargs) request = kwargs.get('request') user = getattr(request, 'couch_user', None) dm = user.get_domain_membership(domain) if user else None if dm: if dm.program_id: program_id = dm.program_id else: program_id = 'all' location_id = get_user_location_id(user, domain) if location_id: try: location = SQLLocation.active_objects.get(location_id=location_id) if cls.__name__ == "DashboardReport": if not location.location_type.administrative: location = location.parent location_id = location.location_id except SQLLocation.DoesNotExist: location_id = None start_date, end_date = calculate_last_period() url = '%s?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s&datespan_first=%s' % ( url, location_id or '', program_id if program_id else '', start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'), '%s|%s' % (start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) ) return url
def get_redirect_url(self, *args, **kwargs): domain = kwargs['domain'] url = DashboardReport.get_raw_url(domain, request=self.request) user = self.request.couch_user dm = user.get_domain_membership(domain) if user else None if dm: if dm.program_id: program_id = dm.program_id else: program_id = 'all' loc_id = '' if dm.location_id: location = SQLLocation.objects.get(location_id=dm.location_id) if not location.location_type.administrative: url = StockStatus.get_raw_url(domain, request=self.request) loc_id = location.location_id else: try: extension = EWSExtension.objects.get(domain=domain, user_id=user.get_id) loc_id = extension.location_id if loc_id: url = StockStatus.get_raw_url(domain, request=self.request) except EWSExtension.DoesNotExist: pass start_date, end_date = calculate_last_period() url = '%s?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s' % ( url, loc_id or '', program_id if program_id else '', start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d') ) return url
def get_redirect_url(self, *args, **kwargs): domain = kwargs['domain'] url = DashboardReport.get_raw_url(domain, request=self.request) user = self.request.couch_user if self.request.user.is_authenticated else None dm = user.get_domain_membership(domain) if user else None if dm: if dm.program_id: program_id = dm.program_id else: program_id = 'all' loc_id = '' if dm.location_id: location = SQLLocation.objects.get(location_id=dm.location_id) if not location.location_type.administrative: url = StockStatus.get_raw_url(domain, request=self.request) loc_id = location.location_id else: try: extension = EWSExtension.objects.get(domain=domain, user_id=user.get_id) loc_id = extension.location_id if loc_id: url = StockStatus.get_raw_url(domain, request=self.request) except EWSExtension.DoesNotExist: pass start_date, end_date = calculate_last_period() url = '%s?location_id=%s&filter_by_program=%s&startdate=%s&enddate=%s' % ( url, loc_id or '', program_id if program_id else '', start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d') ) return url
def report_config(self): report_config = super(DashboardReport, self).report_config startdate, enddate = calculate_last_period() report_config.update( dict(startdate=startdate, enddate=enddate, program=None, products=None)) return report_config
def report_config(self): report_config = super(DashboardReport, self).report_config startdate, enddate = calculate_last_period() report_config.update(dict( startdate=startdate, enddate=enddate, program=None, products=None )) return report_config
def report_config(self): startdate, enddate = calculate_last_period(datetime.utcnow()) return dict( domain=self.domain, startdate=startdate, enddate=enddate, location_id=self.request.GET.get('location_id') or get_country_id(self.domain), user=self.request.couch_user, program=None, products=None )
def rows(self): rows = {} if self.config['location_id']: supply_points = get_supply_points(self.config['location_id'], self.config['domain']).values_list( 'supply_point_id', flat=True ) last_period_st, last_period_end = calculate_last_period(self.config['enddate']) reports = StockTransaction.objects.filter(case_id__in=supply_points, report__date__range=[last_period_st, last_period_end] ).distinct('case_id').count() rows = dict( total=len(supply_points), reported=reports, non_reported=len(supply_points) - reports ) return rows
def test_calculation(self): monday = datetime(2016, 1, 11) tuesday = datetime(2016, 1, 12) wednesday = datetime(2016, 1, 13) thurdsay = datetime(2016, 1, 14) friday = datetime(2016, 1, 15) saturday = datetime(2016, 1, 16) sunday = datetime(2016, 1, 17) expected_period = (datetime(2016, 1, 8), datetime(2016, 1, 14, 23, 59, 59, 999999)) self.assertEqual(calculate_last_period(monday), expected_period) self.assertEqual(calculate_last_period(tuesday), expected_period) self.assertEqual(calculate_last_period(wednesday), expected_period) self.assertEqual(calculate_last_period(thurdsay), expected_period) expected_period = (datetime(2016, 1, 15), datetime(2016, 1, 21, 23, 59, 59, 999999)) self.assertEqual(calculate_last_period(friday), expected_period) self.assertEqual(calculate_last_period(saturday), expected_period) self.assertEqual(calculate_last_period(sunday), expected_period)
def rows(self): rows = [] if self.config['location_id']: last_period_st, last_period_end = calculate_last_period(self.config['enddate']) for loc in self.get_locations: supply_points = get_supply_points(loc.location_id, loc.domain).values_list('supply_point_id', flat=True) sites = len(supply_points) reported = StockTransaction.objects.filter(case_id__in=supply_points, report__date__range=[last_period_st, last_period_end] ).distinct('case_id').count() reporting_rates = '%.2f%%' % (reported * 100 / (float(sites) or 1.0)) url = make_url( ReportingRatesReport, self.config['domain'], '?location_id=%s&startdate=%s&enddate=%s', (loc.location_id, self.config['startdate'], self.config['enddate'])) rows.append([link_format(loc.name, url), sites, reported, reporting_rates]) return rows
def default_datespan(self): last_period_st, last_period_end = calculate_last_period(datetime.now()) datespan = DateSpan(startdate=last_period_st, enddate=last_period_end) datespan.is_default = True return datespan
def last_reporting_period(): return calculate_last_period()