def generate_location_update_mapping_form(deployment, headers, *args, **kwargs): default_choices = [['', _('Select column')]] + [(v, v) for v in headers] attributes = { '_headers': headers, } for location_type in location_types.find(): name = location_type.name slug = slugify(name, separator='_').lower() attributes['{}_name'.format(slug)] = SelectField( _('%(label)s Name', label=name), choices=default_choices) attributes['{}_code'.format(slug)] = SelectField( _('%(label)s Code', label=name), choices=default_choices) if location_type.has_political_code: attributes['{}_pcode'.format(slug)] = SelectField( _('%(label)s Geopolitical Code', label=name), choices=default_choices) if location_type.has_other_code: attributes['{}_ocode'.format(slug)] = SelectField( _('%(label)s Other Code', label=name), choices=default_choices) if location_type.has_registered_voters: attributes['{}_rv'.format(slug)] = SelectField( _('%(label)s Registered Voters', label=name), choices=default_choices) LocationUpdateMappingForm = type('LocationUpdateMappingForm', (WTSecureForm, ), attributes) return LocationUpdateMappingForm(*args, **kwargs)
class ChecklistInitForm(WTSecureForm): form = SelectField(_('Form'), choices=_make_choices( forms.find(events=g.event, form_type='CHECKLIST').scalar( 'id', 'name'), _('Select form')), validators=[validators.input_required()]) role = SelectField(_('Role'), choices=_make_choices( participant_roles.find().scalar('id', 'name'), _('Select role')), validators=[validators.input_required()]) location_type = SelectField( _('Location type'), choices=_make_choices(location_types.find().scalar('id', 'name'), _('Select location type')), validators=[validators.input_required()])
def generate_location_update_mapping_form( deployment, headers, *args, **kwargs ): default_choices = [['', _('Select column')]] + [(v, v) for v in headers] attributes = { '_headers': headers, } for location_type in location_types.find(): name = location_type.name slug = slugify(name, separator='_').lower() attributes['{}_name'.format(slug)] = SelectField( _('%(label)s Name', label=name), choices=default_choices ) attributes['{}_code'.format(slug)] = SelectField( _('%(label)s Code', label=name), choices=default_choices ) if location_type.has_political_code: attributes['{}_pcode'.format(slug)] = SelectField( _('%(label)s Geopolitical Code', label=name), choices=default_choices ) if location_type.has_other_code: attributes['{}_ocode'.format(slug)] = SelectField( _('%(label)s Other Code', label=name), choices=default_choices ) if location_type.has_registered_voters: attributes['{}_rv'.format(slug)] = SelectField( _('%(label)s Registered Voters', label=name), choices=default_choices ) LocationUpdateMappingForm = type( 'LocationUpdateMappingForm', (WTSecureForm,), attributes ) return LocationUpdateMappingForm(*args, **kwargs)
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 )
def _process_analysis(form_id, location_id=None, tag=None): form = forms.get_or_404(pk=form_id) location = locations.get_or_404(pk=location_id) \ if location_id else locations.root() template_name = '' tags = [] page_title = _(u'%(form)s Analysis', form=form.name) grouped = False display_tag = None filter_class = filters.generate_submission_analysis_filter(form) # set the correct template and fill out the required data if form.form_type == 'CHECKLIST': if tag: template_name = 'process_analysis/checklist_summary_breakdown.html' tags.append(tag) display_tag = tag grouped = True else: template_name = 'process_analysis/checklist_summary.html' tags.extend([ field.name for group in form.groups for field in group.fields if field.analysis_type == 'PROCESS' ]) grouped = False queryset = submissions.find( form=form, submission_type='M', quarantine_status__nin=['A']).filter_in(location) else: grouped = True queryset = submissions.find(form=form).filter_in(location) template_name = 'process_analysis/critical_incident_summary.html' if tag: # a slightly different filter, one prefiltering # on the specified tag display_tag = tag template_name = 'process_analysis/critical_incidents_locations.html' filter_class = \ filters.generate_critical_incident_location_filter(tag) # create data filter filter_set = filter_class(queryset, request.args) # set up template context context = {} context['dataframe'] = filter_set.qs.to_dataframe() context['page_title'] = page_title context['display_tag'] = display_tag context['filter_form'] = filter_set.form context['form'] = form context['location'] = location context['field_groups'] = OrderedDict() context['breadcrumb_data'] = analysis_breadcrumb_data( form, location, display_tag) context['navigation_data'] = analysis_navigation_data( form, location, display_tag) for group in form.groups: process_fields = sorted([ field for field in group.fields if field.analysis_type == 'PROCESS' ], key=lambda x: x.name) context['field_groups'][group.name] = process_fields # processing for incident forms if form.form_type == 'INCIDENT': if display_tag: context['form_field'] = form.get_field_by_tag(display_tag) context['location_types'] = location_types.find(is_political=True) context['incidents'] = filter_set.qs else: incidents_summary = generate_incidents_data( form, filter_set.qs, location, grouped, tags) context['incidents_summary'] = incidents_summary else: process_summary = generate_process_data(form, filter_set.qs, location, grouped=True, tags=tags) context['process_summary'] = process_summary return render_template(template_name, **context)
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)
def _process_analysis(event, form_id, location_id=None, tag=None): form = forms.fget_or_404(id=form_id) location = locations.fget_or_404(id=location_id) \ if location_id else locations.root(event.location_set_id) template_name = '' tags = [] breadcrumbs = [_('Process Data'), form.name] grouped = False display_tag = None event = g.event filter_class = filters.make_submission_analysis_filter(event, form) location_ids = models.LocationPath.query.with_entities( models.LocationPath.descendant_id).filter_by( ancestor_id=location.id, location_set_id=event.location_set_id) # set the correct template and fill out the required data if form.form_type in ['CHECKLIST', 'SURVEY']: if tag: template_name = 'process_analysis/checklist_summary_breakdown.html' tags.append(tag) display_tag = tag grouped = True else: template_name = 'process_analysis/checklist_summary.html' form._populate_field_cache() tags.extend([ f['tag'] for f in form._field_cache.values() if f['analysis_type'] != 'N/A' ]) grouped = False query_kwargs = {'event': event, 'form': form} if not form.untrack_data_conflicts and form.form_type == 'CHECKLIST': query_kwargs['submission_type'] = 'M' else: query_kwargs['submission_type'] = 'O' queryset = submissions.find(**query_kwargs).filter( models.Submission.location_id.in_(location_ids), models.Submission.quarantine_status != 'A') else: grouped = True queryset = submissions.find(event=event, form=form).filter( models.Submission.location_id.in_(location_ids)) template_name = 'process_analysis/critical_incident_summary.html' if tag: # a slightly different filter, one prefiltering # on the specified tag display_tag = tag template_name = 'process_analysis/critical_incidents_locations.html' # noqa filter_class = \ filters.make_incident_location_filter(event, form, tag) # create data filter filter_set = filter_class(queryset, request.args) breadcrumb_data = analysis_breadcrumb_data(form, location, display_tag) if breadcrumb_data['tag']: breadcrumbs.extend([{ 'text': location.name, 'url': url_for('process_analysis.process_analysis_with_location_and_tag', form_id=breadcrumb_data['form'].id, location_id=location.id, tag=breadcrumb_data['tag']) if idx < len(breadcrumb_data.get('locations', [])) - 1 else '' } for idx, location in enumerate(breadcrumb_data.get('locations', [])) ]) else: breadcrumbs.extend([{ 'text': location.name, 'url': url_for('process_analysis.process_analysis_with_location', form_id=breadcrumb_data['form'].id, location_id=location.id) if idx < len(breadcrumb_data.get('locations', [])) - 1 else '' } for idx, location in enumerate(breadcrumb_data.get('locations', [])) ]) # set up template context context = {} context['dataframe'] = make_submission_dataframe(filter_set.qs, form) context['breadcrumbs'] = breadcrumbs context['display_tag'] = display_tag context['filter_form'] = filter_set.form context['form'] = form context['location'] = location context['field_groups'] = OrderedDict() context['navigation_data'] = analysis_navigation_data( form, location, display_tag) # processing for incident forms if form.form_type == 'INCIDENT': if display_tag: context['form_field'] = form.get_field_by_tag(display_tag) context['location_types'] = location_types.find(is_political=True) context['incidents'] = filter_set.qs else: incidents_summary = generate_incidents_data( form, filter_set.qs, location, grouped, tags) context['incidents_summary'] = incidents_summary detail_visible = False for group in form.data['groups']: process_fields = sorted([ field for field in group['fields'] if field['analysis_type'] != 'N/A' and field['type'] != 'boolean' ], key=itemgetter('tag')) context['field_groups'][group['name']] = process_fields if process_fields: detail_visible = True context['detail_visible'] = detail_visible else: for group in form.data['groups']: process_fields = sorted([ field for field in group['fields'] if field['analysis_type'] != 'N/A' ], key=itemgetter('tag')) context['field_groups'][group['name']] = process_fields process_summary = generate_process_data(form, filter_set.qs, location, grouped=True, tags=tags) context['process_summary'] = process_summary return render_template(template_name, **context)
def _process_analysis(form_id, location_id=None, tag=None): form = forms.get_or_404(pk=form_id) location = locations.get_or_404(pk=location_id) \ if location_id else locations.root() template_name = '' tags = [] page_title = _(u'%(form)s Analysis', form=form.name) grouped = False display_tag = None filter_class = filters.generate_submission_analysis_filter(form) # set the correct template and fill out the required data if form.form_type == 'CHECKLIST': if tag: template_name = 'process_analysis/checklist_summary_breakdown.html' tags.append(tag) display_tag = tag grouped = True else: template_name = 'process_analysis/checklist_summary.html' tags.extend([ field.name for group in form.groups for field in group.fields if field.analysis_type == 'PROCESS' ]) grouped = False queryset = submissions.find( form=form, submission_type='M', quarantine_status__nin=['A']).filter_in(location) else: grouped = True queryset = submissions.find(form=form).filter_in(location) template_name = 'process_analysis/critical_incident_summary.html' if tag: # a slightly different filter, one prefiltering # on the specified tag display_tag = tag template_name = 'process_analysis/critical_incidents_locations.html' filter_class = \ filters.generate_critical_incident_location_filter(tag) # create data filter filter_set = filter_class(queryset, request.args) # set up template context context = {} context['dataframe'] = filter_set.qs.to_dataframe() context['page_title'] = page_title context['display_tag'] = display_tag context['filter_form'] = filter_set.form context['form'] = form context['location'] = location context['field_groups'] = OrderedDict() context['breadcrumb_data'] = analysis_breadcrumb_data( form, location, display_tag) context['navigation_data'] = analysis_navigation_data( form, location, display_tag) for group in form.groups: process_fields = sorted([ field for field in group.fields if field.analysis_type == 'PROCESS'], key=lambda x: x.name) context['field_groups'][group.name] = process_fields # processing for incident forms if form.form_type == 'INCIDENT': if display_tag: context['form_field'] = form.get_field_by_tag(display_tag) context['location_types'] = location_types.find( is_political=True) context['incidents'] = filter_set.qs else: incidents_summary = generate_incidents_data( form, filter_set.qs, location, grouped, tags) context['incidents_summary'] = incidents_summary else: process_summary = generate_process_data( form, filter_set.qs, location, grouped=True, tags=tags) context['process_summary'] = process_summary return render_template(template_name, **context)