def locations_list():
    template_name = 'frontend/location_list.html'
    page_title = _('Locations')

    queryset = services.locations.find()
    queryset_filter = filters.location_filterset()(queryset, request.args)

    args = request.args.copy()
    page = int(args.pop('page', '1'))

    subset = queryset_filter.qs.order_by('location_type')

    if request.args.get('export') and permissions.export_locations.can():
        # Export requested
        dataset = services.locations.export_list(queryset_filter.qs)
        basename = slugify_unicode('%s locations %s' % (
            g.event.name.lower(),
            datetime.utcnow().strftime('%Y %m %d %H%M%S')))
        content_disposition = 'attachment; filename=%s.csv' % basename
        return Response(
            dataset, headers={'Content-Disposition': content_disposition},
            mimetype="text/csv"
        )
    else:
        ctx = dict(
            args=args,
            filter_form=queryset_filter.form,
            page_title=page_title,
            form=DummyForm(),
            locations=subset.paginate(
                page=page, per_page=current_app.config.get('PAGE_SIZE')))

        return render_template(template_name, **ctx)
Exemplo n.º 2
0
def locations_list(view, location_set_id):
    location_set = LocationSet.query.filter(
        LocationSet.id == location_set_id).first_or_404()
    queryset = Location.query.select_from(
        Location, LocationTranslations).filter(
            Location.location_set_id == location_set_id)
    queryset_filter = filters.location_filterset(
        location_set_id=location_set_id)(queryset, request.args)
    extra_fields = [
        field for field in LocationDataField.query.filter(
            LocationDataField.location_set_id == location_set_id)
        if field.visible_in_lists]

    template_name = 'admin/locations_list.html'
    breadcrumbs = [
        {'text': _('Location Sets'), 'url': url_for('locationset.index_view')},
        location_set.name, _('Locations')]

    args = request.args.to_dict(flat=False)
    args.update(location_set_id=location_set_id)
    page = int(args.pop('page', [1])[0])

    # NOTE: this was ordered by location type
    subset = queryset_filter.qs.order_by(Location.code).distinct(Location.code)

    if request.args.get('export') and permissions.export_locations.can():
        # Export requested
        dataset = services.locations.export_list(queryset_filter.qs)
        basename = slugify('%s locations %s' % (
            g.event.name.lower(),
            datetime.utcnow().strftime('%Y %m %d %H%M%S')))
        content_disposition = 'attachment; filename=%s.csv' % basename
        return Response(
            stream_with_context(dataset),
            headers={'Content-Disposition': content_disposition},
            mimetype="text/csv"
        )
    else:
        ctx = dict(
            args=args,
            extra_fields=extra_fields,
            filter_form=queryset_filter.form,
            breadcrumbs=breadcrumbs,
            form=DummyForm(),
            location_set=location_set,
            location_set_id=location_set_id,
            locations=subset.paginate(
                page=page, per_page=current_app.config.get('PAGE_SIZE')))

        return view.render(template_name, **ctx)
Exemplo n.º 3
0
def participant_list(page=1):
    page_title = _('Participants')
    template_name = 'frontend/participant_list.html'

    sortable_columns = {
        'id': 'participant_id',
        'name': 'name',
        'gen': 'gender'
    }

    try:
        extra_fields = filter(
            lambda f: getattr(f, 'listview_visibility', False) is True,
            g.deployment.participant_extra_fields)
    except AttributeError:
        extra_fields = []
    location = None
    if request.args.get('location'):
        location = services.locations.find(
            pk=request.args.get('location')).first()

    for field in extra_fields:
        sortable_columns.update({field.name: field.name})

    queryset = services.participants.find()
    queryset_filter = filters.participant_filterset()(queryset, request.args)

    form = DummyForm(request.form)

    if request.form.get('action') == 'send_message':
        message = request.form.get('message', '')
        recipients = filter(lambda x: x is not '', [
            participant.phone if participant.phone else ''
            for participant in queryset_filter.qs
        ])
        recipients.extend(current_app.config.get('MESSAGING_CC'))

        if message and recipients and permissions.send_messages.can():
            send_messages.delay(str(g.event.pk), message, recipients)
            return 'OK'
        else:
            abort(400)

    if request.args.get('export') and permissions.export_participants.can():
        # Export requested
        dataset = services.participants.export_list(queryset_filter.qs)
        basename = slugify_unicode(
            '%s participants %s' %
            (g.event.name.lower(),
             datetime.utcnow().strftime('%Y %m %d %H%M%S')))
        content_disposition = 'attachment; filename=%s.csv' % basename
        return Response(dataset,
                        headers={'Content-Disposition': content_disposition},
                        mimetype="text/csv")
    else:
        # request.args is immutable, so the .pop() call will fail on it.
        # using .copy() returns a mutable version of it.
        args = request.args.to_dict(flat=False)
        page_spec = args.pop(u'page', None) or [1]
        page = int(page_spec[0])

        sort_by = sortable_columns.get(args.pop('sort_by', ''),
                                       'participant_id')
        subset = queryset_filter.qs.order_by(sort_by)

        # load form context
        context = dict(args=args,
                       extra_fields=extra_fields,
                       filter_form=queryset_filter.form,
                       form=form,
                       location=location,
                       page_title=page_title,
                       location_types=helpers.displayable_location_types(
                           is_administrative=True),
                       participants=subset.paginate(
                           page=page,
                           per_page=current_app.config.get('PAGE_SIZE')))

        return render_template(template_name, **context)