Ejemplo n.º 1
0
def display_report(report, format):
    report = CadorsReport.query.get_or_404(report)

    if format == 'atom':
        reports = process_report_atom([report])
        feed_timestamp = generate(report.last_updated, accept_naive=True)
        response = make_response(
            render_template('feed.xml',
                            reports=reports,
                            feed_timestamp=feed_timestamp))
        response.mimetype = "application/atom+xml"
    elif format == 'json':
        response = make_response(json.dumps(
                {'report': report},
                indent=None if request.is_xhr else 2,
                default=json_default))
        response.mimetype = "application/json"
    elif format == 'html':
        response = make_response(
            render_template('single_report.html',
                            report=report))
    elif format == 'kml':
        response = make_response(render_template('kml.xml', report=report))
        response.mimetype = "application/vnd.google-earth.kml+xml"
        
    response.last_modified = report.last_updated
    return prepare_response(response, 43200)
Ejemplo n.º 2
0
def do_daily_report(year, month, day, format):
    try:
        ts = datetime(year, month, day)
    except ValueError:
        abort(400)

    daily_report = DailyReport.query.filter(
        DailyReport.report_date == ts).first_or_404()


    navlinks = make_navlinks(daily_report)

    if format == 'atom':
        reports = process_report_atom(daily_report.reports)
        feed_timestamp = generate(daily_report.last_updated, accept_naive=True)
        title = "Daily Report for %s" % ts.strftime("%Y-%m-%d")
        
        first=navlinks['first'].link(format='atom', _external=True)
        prev=navlinks['prev'].link(format='atom', _external=True) \
            if navlinks['prev'] is not None else None
        next=navlinks['next'].link(format='atom', _external=True) \
            if navlinks['next'] is not None else None
        last=navlinks['last'].link(format='atom', _external=True)

        response = make_response(
            render_template('feed.xml',
                            feed_timestamp=feed_timestamp,
                            reports=reports,
                            title=title,
                            first=first, next=next, prev=prev, last=last)) 
        response.mimetype = "application/atom+xml"
    elif format == 'html':
        occurrence_type = CadorsReport.occurrence_type
        type_count = func.count(CadorsReport.region)

        types = db.session.query(occurrence_type, type_count).group_by(
            occurrence_type).with_parent(
            daily_report).order_by(type_count.desc()).all()

        region = CadorsReport.region
        region_count = func.count(CadorsReport.region)

        regions = db.session.query(region, region_count).group_by(
            region).with_parent(
            daily_report).order_by(region_count.desc()).all()

        response = make_response(
            render_template('daily_report.html',
                            daily_report=daily_report,
                            reports=daily_report.reports,
                            types=types,
                            regions=regions,
                            **navlinks))
    elif format == 'json':
        response = make_response(json.dumps(
                {'reports': daily_report.reports},
                indent=None if request.is_xhr else 2,
                default=json_default))
        response.mimetype = "application/json"

    response.last_modified = daily_report.last_updated
    return prepare_response(response, 43200)