Пример #1
0
def _incident_csv(form_pk, location_type_pk, location_pk=None):
    """Given an incident form id, a location type id, and optionally
    a location id, return a CSV file of the number of incidents of each
    type (form field tag) that has occurred, either for the entire
    deployment or under the given location for each location of the
    specified location type. Only submissions sent in by participants
    are used for generating the data.

    Sample output would be:

    LOC | A | B | ... | Z | TOT
    NY  | 2 | 0 | ... | 5 |  7

    `param form_pk`: a `class`Form id
    `param location_type_pk`: a `class`LocationType id
    `param location_pk`: an optional `class`Location id. if given, only
    submissions under that location will be queried.

    `returns`: a string of bytes (str) containing the CSV data.
    """
    form = services.forms.get_or_404(pk=form_pk, form_type='INCIDENT')
    location_type = services.location_types.objects.get_or_404(
        pk=location_type_pk)
    if location_pk:
        location = services.locations.get_or_404(pk=location_pk)
        qs = services.submissions.find(submission_type='O', form=form) \
            .filter_in(location)
    else:
        qs = services.submissions.find(submission_type='O', form=form)

    event = get_event()
    tags = [fi.name for group in form.groups for fi in group.fields]
    qs = qs(created__lte=event.end_date, created__gte=event.start_date)
    df = qs.dataframe()
    ds = Dataset()
    ds.headers = ['LOC'] + tags + ['TOT']

    for summary in incidents_csv(df, location_type.name, tags):
        ds.append([summary.get(heading) for heading in ds.headers])

    return ds.csv
Пример #2
0
 def __init__(self, *args, **kwargs):
     event = kwargs.pop("default_event", get_event())
     super(BaseSubmissionFilterSet, self).__init__(*args, **kwargs)
     self.declared_filters["event"] = EventFilter(widget=widgets.HiddenInput(), default=unicode(event.id))
Пример #3
0
def index():
    args = request.args.copy()

    # get variables from query params, or use (hopefully) sensible defaults
    group = args.pop('group', None)
    location_type_id = args.pop('locationtype', None)

    page_title = _('Dashboard')
    template_name = 'frontend/dashboard.html'

    event = get_event()

    if not args.get('checklist_form'):
        form = forms.find(
            events=event, form_type='CHECKLIST').order_by('name').first()
    else:
        form = forms.get_or_404(pk=args.get('checklist_form'))

    if form:
        args.setdefault('checklist_form', unicode(form.id))

    queryset = submissions.find(
        form=form,
        submission_type='M'
    )
    filter_ = dashboard_filterset()(queryset, data=args)

    obs_queryset = submissions.find(
        form=form,
        submission_type='O'
    )
    obs_filter_ = dashboard_filterset()(obs_queryset, data=args)

    location = None
    if args.get('location'):
        location = locations.get_or_404(pk=args.get('location'))

    # activate sample filter
    filter_form = filter_.form
    queryset = filter_.qs
    obs_queryset = obs_filter_.qs
    next_location_type = False

    if not group:
        data = get_coverage(queryset)
        obs_data = get_coverage(obs_queryset)
    else:
        page_title = page_title + u' · {}'.format(group)
        if not location_type_id:
            location_type = location_types.find(
                is_administrative=True).order_by('ancestor_count').first()
        else:
            location_type = LocationType.objects.get_or_404(
                pk=location_type_id)

        # get the requisite location type - the way the aggregation
        # works, passing in a 'State' location type won't retrieve
        # data for the level below the 'State' type, it will retrieve
        # it for the 'State' type. in general, this isn't the behaviour
        # we want, so we need to find the lower level types and get the
        # one we want (the first of the children)
        le_temp = [lt for lt in location_type.children
                   if lt.is_administrative]

        try:
            next_location_type = le_temp[0]
        except IndexError:
            next_location_type = None

        data = get_coverage(queryset, group, location_type)
        obs_data = get_coverage(obs_queryset, group, location_type)

    # load the page context
    location_id = args.pop('location', '')
    context = {
        'args': args,
        'location_id': location_id,
        'next_location': bool(next_location_type),
        'data': data,
        'obs_data': obs_data,
        'filter_form': filter_form,
        'page_title': page_title,
        'location': location,
        'locationtype': getattr(next_location_type, 'id', ''),
        'group': group or ''
    }

    return render_template(
        template_name,
        **context
    )
Пример #4
0
 def __init__(self, *args, **kwargs):
     event = kwargs.pop('default_event', get_event())
     super(BaseSubmissionFilterSet, self).__init__(*args, **kwargs)
     self.declared_filters['event'] = EventFilter(
         widget=widgets.HiddenInput(), default=unicode(event.id))
Пример #5
0
def main_dashboard(form_id=None):
    args = request.args.copy()

    # get variables from query params, or use (hopefully) sensible defaults
    group = args.pop('group', None)
    location_type_id = args.pop('locationtype', None)

    template_name = 'frontend/dashboard.html'

    event = get_event()

    if not form_id:
        form = forms.find(events=event,
                          form_type='CHECKLIST').order_by('name').first()
    else:
        form = forms.get_or_404(pk=form_id, form_type="CHECKLIST")

    if form is not None:
        page_title = _(u'Dashboard · %(name)s', name=form.name)
    else:
        page_title = _(u'Dashboard')

    queryset = submissions.find(form=form, submission_type='M')
    filter_ = dashboard_filterset()(queryset, data=args)

    obs_queryset = submissions.find(form=form, submission_type='O')
    obs_filter_ = dashboard_filterset()(obs_queryset, data=args)

    location = None
    if args.get('location'):
        location = locations.get_or_404(pk=args.get('location'))

    # activate sample filter
    filter_form = filter_.form
    queryset = filter_.qs
    obs_queryset = obs_filter_.qs
    next_location_type = False

    if not group:
        data = get_coverage(queryset)
        obs_data = get_coverage(obs_queryset)
    else:
        page_title = page_title + u' · {}'.format(group)
        if not location_type_id:
            location_type = location_types.find(
                is_administrative=True).order_by('ancestor_count').first()
        else:
            location_type = LocationType.objects.get_or_404(
                pk=location_type_id)

        # get the requisite location type - the way the aggregation
        # works, passing in a 'State' location type won't retrieve
        # data for the level below the 'State' type, it will retrieve
        # it for the 'State' type. in general, this isn't the behaviour
        # we want, so we need to find the lower level types and get the
        # one we want (the first of the children)
        le_temp = [lt for lt in location_type.children if lt.is_administrative]

        try:
            next_location_type = le_temp[0]
        except IndexError:
            next_location_type = None

        data = get_coverage(queryset, group, location_type)
        obs_data = get_coverage(obs_queryset, group, location_type)

    # load the page context
    location_id = args.pop('location', '')
    context = {
        'args': args,
        'location_id': location_id,
        'next_location': bool(next_location_type),
        'data': data,
        'obs_data': obs_data,
        'filter_form': filter_form,
        'page_title': page_title,
        'location': location,
        'locationtype': getattr(next_location_type, 'id', ''),
        'group': group or '',
        'form_id': unicode(form.pk) if form else None
    }

    return render_template(template_name, **context)