Esempio n. 1
0
def get_non_archived_facilities_below(location):
    child_ids = (
        location.sql_location.get_descendants(include_self=True)
        .filter(is_archived=False, location_type__name="FACILITY")
        .values_list("location_id", flat=True)
    )
    return [Location.wrap(doc) for doc in get_docs(Location.get_db(), child_ids)]
Esempio n. 2
0
def get_non_archived_facilities_below(location):
    child_ids = location.sql_location.get_descendants(
        include_self=True).filter(is_archived=False,
                                  location_type__name='FACILITY').values_list(
                                      'location_id', flat=True)
    return [
        Location.wrap(doc) for doc in get_docs(Location.get_db(), child_ids)
    ]
Esempio n. 3
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            form_xmlnses = [form['xmlns'] for form in self.all_relevant_forms.values()]
            spoint_loc_map = {
                doc['_id']: doc['location_id']
                for doc in iter_docs(SupplyPointCase.get_db(), sp_ids)
            }
            locations = {
                doc['_id']: Location.wrap(doc)
                for doc in iter_docs(Location.get_db(), spoint_loc_map.values())
            }

            for spoint_id, loc_id in spoint_loc_map.items():
                loc = locations[loc_id]

                form_ids = StockReport.objects.filter(
                    stocktransaction__case_id=spoint_id
                ).exclude(
                    date__lte=self.start_date
                ).exclude(
                    date__gte=self.end_date
                ).values_list(
                    'form_id', flat=True
                ).order_by('-date').distinct()  # not truly distinct due to ordering
                matched = False
                for form_id in form_ids:
                    try:
                        if XFormInstance.get(form_id).xmlns in form_xmlnses:
                            yield {
                                'loc_id': loc._id,
                                'loc_path': loc.path,
                                'name': loc.name,
                                'type': loc.location_type,
                                'reporting_status': 'reporting',
                                'geo': loc._geopoint,
                            }
                            matched = True
                            break
                    except ResourceNotFound:
                        logging.error('Stock report for location {} in {} references non-existent form {}'.format(
                            loc._id, loc.domain, form_id
                        ))
                if not matched:
                    yield {
                        'loc_id': loc._id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type,
                        'reporting_status': 'nonreporting',
                        'geo': loc._geopoint,
                    }
Esempio n. 4
0
def location_dump(request, domain):
    loc_ids = [row['id'] for row in Location.view('commtrack/locations_by_code', startkey=[domain], endkey=[domain, {}])]
    
    resp = HttpResponse(content_type='text/csv')
    resp['Content-Disposition'] = 'attachment; filename="locations_%s.csv"' % domain

    w = csv.writer(resp)
    w.writerow(['UUID', 'Location Type', 'SMS Code'])
    for raw in iter_docs(Location.get_db(), loc_ids):
        loc = Location.wrap(raw)
        w.writerow([loc._id, loc.location_type, loc.site_code])
    return resp
Esempio n. 5
0
def location_dump(request, domain):
    loc_ids = [row['id'] for row in Location.view('commtrack/locations_by_code', startkey=[domain], endkey=[domain, {}])]
    
    resp = HttpResponse(content_type='text/csv')
    resp['Content-Disposition'] = 'attachment; filename="locations_%s.csv"' % domain

    w = csv.writer(resp)
    w.writerow(['UUID', 'Location Type', 'SMS Code'])
    for raw in iter_docs(Location.get_db(), loc_ids):
        loc = Location.wrap(raw)
        w.writerow([loc._id, loc.location_type, loc.site_code])
    return resp
Esempio n. 6
0
 def _gen():
     location_ids = [sp.location_id for sp in self.get_linked_supply_points()]
     for doc in iter_docs(Location.get_db(), location_ids):
         yield Location.wrap(doc)