Exemplo n.º 1
0
def view(indicator_id):

    indicator = mongo.db.indicators.find_one_or_404({'id': indicator_id})
    aichi_indicator_keys = _load_json("../refdata/aichi_indicator_keys.json")
    aichi_order = _load_json("../refdata/aichi_indicator_keys_order.json")

    goal_description = None
    if indicator.get('goal'):
        goal_description = mongo.db.goals.find_one({'short_title': indicator['goal']})

    main_target_description = None
    if indicator.get('relevant_target'):
        main_target_description = mongo.db.targets.find_one({'id': indicator['relevant_target']})

    other_targets_descriptions = []
    if indicator.get('other_targets'):
        other_targets_descriptions = [target['description'] for target in
                mongo.db.targets.find({'id': {'$in': indicator.get('other_targets')}})]

    if goal_description is not None:
        goal_description = goal_description['description']

    return {
        "indicator": indicator,
        "transit_dict": aichi_indicator_keys,
        "order": aichi_order['order'],
        "data": schema.refdata.indicator_data,
        "goal_description": goal_description,
        "main_target_description": main_target_description['description'],
        "other_targets_descriptions": other_targets_descriptions
    }
Exemplo n.º 2
0
def homepage_indicators():
    page = int(flask.request.args.get('page', 1))

    if page not in range(1, 6):
        return flask.abort(404)

    # Use some math to generate the index intervals from the page number.
    get_start_index = lambda (x): 20 * x - 19

    # Render 20 indicators per page.
    start_index = get_start_index(page)
    end_index = start_index + 19

    indicators = [i for i in mongo.db.indicators.find({'$and': [{'id': {'$gte': start_index}},
                                                                 {'id': {'$lte': end_index}}]
                                                     }).sort('id')]

    goals = [g for g in mongo.db.goals.find()]
    targets = [t for t in mongo.db.targets.find()]
    mapping = schema.refdata.mapping
    aichi_indicator_keys = _load_json("../refdata/aichi_indicator_keys.json")
    aichi_order = _load_json("../refdata/aichi_indicator_keys_order.json")

    return {
             'indicators': indicators,
             'goals': goals,
             'targets': targets,
             'transit_dict': aichi_indicator_keys,
             'order': aichi_order['order'],
             'data': schema.refdata.indicator_data,
             'mapping': mapping
           }
Exemplo n.º 3
0
def participants_csv(meeting_id):
    f = StringIO()
    writer = sugar.UnicodeWriter(f)

    # get valid schema with usersdatabase
    person_schema = schema.PersonSchema()
    headers = [(p.label, 'personal_%s' % p.name)
               for p in person_schema['personal'].values()]
    headers_representing = [(p.label, 'representing_%s' % p.name) for p in
                            person_schema['representing'].values()]
    headers.extend(headers_representing)
    meeting_headers = [(p.label, 'meeting_flags_%s' % p.name) for p in
                       person_schema['meeting_flags'].values()]

    photo_header = [(u'Picture URL', person_schema['photo'].name)]
    # write headers to CSV file
    headers_list = [head[0] for head in headers]
    headers_list.extend([head[0] for head in meeting_headers])
    headers_list.extend([head[0] for head in photo_header])

    writer.writerow(headers_list)

    # write content to CSV file
    db_generator = database.find('person', meeting_id=str(meeting_id))
    users = [user for user in db_generator]

    languages = _load_json('../refdata/languages.json')
    categories = _load_json('../refdata/categories.json')
    category = {c['id']: c['name'] for c in categories}
    fees = _load_json('../refdata/fee.json')
    fee = {f['id']: f['name'] for f in fees}
    regions = _load_json('../refdata/regions.json')
    region = {r['id']: r['name'] for r in regions}
    tf_dict = {'1': 'Y', '0': 'N'}

    for user in users:
        row_list = []

        for field in headers:
            db_field = field[1]
            value_field = user.get(field[1])
            if value_field is None:
                    value_field = u''

            if db_field == 'personal_language' and value_field != '':
                value_field = languages[value_field]
            elif db_field == 'personal_country' and value_field != '':
                value_field = country[value_field]
            elif db_field == 'representing_country' and value_field != '':
                value_field = country[value_field]
            elif db_field == 'personal_category' and value_field != '':
                value_field = category[value_field][user['personal_language']]
            elif db_field == 'personal_fee' and value_field != '':
                value_field = fee[value_field][user['personal_language']]
            elif db_field == 'representing_region' and \
                    value_field != '' and value_field != '0':
                    value_field = region[value_field][user['personal_language']]
            elif db_field == 'representing_organization_show' and value_field != '':
                value_field = tf_dict[value_field]

            row_list.append(value_field)

        for field in meeting_headers:
            db_field = field[1]
            value_field = user[field[1]]
            if db_field == 'meeting_flags_acknowledged':
                row_list.append(value_field)
                continue
            try:
                value_field = tf_dict[value_field]
            except KeyError:
                value_field = ''
                pass

            row_list.append(value_field)

        for field in photo_header:
            db_field = field[1]
            photo_url = u''

            try:
                value_field = user[field[1]]
            except KeyError:
                pass

            if value_field:
                photo_url = flask.url_for('static',
                    filename='photos/%s/%s' % (meeting_id, value_field),
                    _external=True)

            row_list.append(photo_url)

        writer.writerow(row_list)

    data = f.getvalue()
    f.close()
    return flask.Response(data, mimetype='text/csv')