Exemplo n.º 1
0
def view_stat(environ, start_response):
    if environ['REQUEST_METHOD'].lower() == 'get':
        with connection(DB_NAME) as conn:
            with open('html/region_comment.html', 'rt') as stat_rows_template:
                stat_rows_template_str = stat_rows_template.read()
            with get_cursor(conn) as cur:
                regions_ids = cur.execute('''select region_id from City 
                                             where id in (select city_id from Comment 
                                                          group by city_id having count(*)>5);'''
                                          ).fetchall()
            regions_ids = set(region_id[0] for region_id in regions_ids)
            comments_count = {
                region_id: len(tuple(Comment.get_by_region_id(region_id,
                                                              conn)))
                for region_id in regions_ids
            }
            stat_rows = '\n'.join(
                stat_rows_template_str.replace('{region_id}', str(region_id)).
                replace('{name}',
                        Region.get_by_id(region_id, conn).name).replace(
                            '{comments_count}', str(comments_count[region_id]))
                for region_id in regions_ids)
            with open('html/stat.html', 'rt') as response:
                start_response('200 OK,',
                               [('Content-Type', 'text/html; charset=UTF-8')])
                return response.read().replace('{rows}', stat_rows)
Exemplo n.º 2
0
def view_region(environ, start_response):
    if environ['REQUEST_METHOD'].lower() == 'get':
        region_id = int(parse_qs(environ['QUERY_STRING']).get('regionId')[0])
        with connection(DB_NAME) as db_conn:
            region = Region.get_by_id(region_id, db_conn)
            cities = City.get_by_region_id(region_id, db_conn)
            with open('html/city.html', 'rt') as city_rows_template:
                city_rows_template_str = city_rows_template.read()
            city_rows = '\n'.join(
                city_rows_template_str.replace('{city_name}', city.name)
                for city in cities)
        with open('html/region_view.html', 'rt') as response:
            start_response('200 OK,',
                           [('Content-Type', 'text/html; charset=UTF-8')])
            return response.read().replace('{region_name}',
                                           region.name).replace(
                                               '{city_rows}', city_rows)