Example #1
0
def user_name(username):
    user_agent = request.headers.get('User-Agent')
    return render_template(
        'vars_and_filters.html',
        president=President(26),
        browser=user_agent,
        username=username.replace('+',' '),
        fruits=FRUITS,
    )
Example #2
0
def format_html_for_president(term_num):
    """Return HTML for one president"""
    p = President(term_num)
    return '''
    <h1>{}: {} {}</h1>
    <h2>Born in: {}</h2>
    <h2>Lived: {} to {}</h2>
    <h2>Party: {}</h2>
    '''.format(term_num, p.first_name, p.last_name, p.birth_state,
               p.birth_date, p.death_date, p.party)
Example #3
0
def president_by_term(termnum):
    """Retrieve president information for a specified term number"""
    term = int(termnum)
    if 0 < term < 46:
        presidents_list = [President(term)]
        return render_template('president_results.html', presidents=presidents_list)
    else:
#        abort(404)
        html_content = '<h2>Sorry,  {} is not a valid term number</h2>'.format(term)
        return html_content, 404
def pres_by_termnum(termnum):
    accept_type = request.headers.get('ACCEPT')
    p = President(termnum)

    if accept_type == 'application/xml':
        response = pres_to_xml(p)
    else:
        #     elif accept_type == 'application/json':
        president_as_dict = pres_to_dict(p)
        response = jsonify(president=president_as_dict)

    return response
Example #5
0
def president_by_last_name(last_name):
    """Retrieve president information for a specified last name;
        May return info for more than one president
    """
    html_content = ''
    for i in range(1, 45):
        p = President(i)
        if p.last_name.lower() == last_name.lower():
            html_content += format_html_for_president(i)

    if html_content:
        return html_content
    else:
        return '<h2>Sorry,  {} not found</h2>'.format(last_name)
Example #6
0
def president_by_last_name(last_name):
    """Retrieve president information for a specified last name;
        May return info for more than one president
    """
    html_content = ''
    presidents = []
    for i in xrange(1, 46):
        p = President(i)
        if p.last_name.lower() == last_name.lower():
            presidents.append(p)

    if presidents:
        return render_template('president_results.html', presidents=presidents)
    else:
        return '<h2>Sorry,  {} not found</h2>'.format(last_name)
def index():
    """Main page; returns list of all presidents"""
    presidents = []
    for i in xrange(1, 46):
        presidents.append(President(i))
    accept_type = request.headers.get('ACCEPT')
    response = ''
    print "Accept type:", accept_type
    if accept_type.startswith('text/html'):
        response = render_template('president_list_bs.html',
                                   presidents=presidents)
    elif accept_type == 'application/xml':
        response = pres_list_to_xml(presidents)
    elif accept_type == 'application/json':
        presidents_as_dicts = [pres_to_dict(p) for p in presidents]
        response = jsonify(presidents=presidents_as_dicts)
    # handle error here if non-expected type

    return response