def test_render_theme_template(self): app = Flask(__name__) app.config['THEME_PATHS'] = [join(TESTS, 'morethemes')] setup_themes(app, app_identifier='testing') with app.test_request_context('/'): coolsrc = render_theme_template('cool', 'hello.html').strip() plainsrc = render_theme_template('plain', 'hello.html').strip() assert coolsrc == 'Hello from Cool Blue v2.' assert plainsrc == 'Hello from the application'
def test_active_theme(self): app = Flask(__name__) app.config['THEME_PATHS'] = [join(TESTS, 'morethemes')] setup_themes(app, app_identifier='testing') with app.test_request_context('/'): appdata = render_template('active.html').strip() cooldata = render_theme_template('cool', 'active.html').strip() plaindata = render_theme_template('plain', 'active.html').strip() assert appdata == 'Application, Active theme: none' assert cooldata == 'Cool Blue v2, Active theme: cool' assert plaindata == 'Application, Active theme: plain'
def hour(year, month, day, hour): """ Display image for selected hour if it exists. """ ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"]) path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day) try: image_files = ft.generate_tree(path) except OSError: abort(404) if not image_files: abort(404) kwargs = { "year": year, "month": { "value": month, "name": datetime(int(year), int(month), int(day)).strftime("%B") }, "day": day, "hour": hour, "hour_name": datetime(int(year), int(month), int(day), int(hour)).strftime("%I:00 %p"), "image": image_files[hour].replace(app.config["IMAGE_LOCATION_DIR"], app.config["IMAGE_LOCATION_URL"]), } return render_theme_template(app.config["DEFAULT_THEME"], "hour.html", **kwargs)
def index(): """ Display available years and current day's images. """ ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"]) path = app.config["IMAGE_LOCATION_DIR"] tree = ft.generate_tree(path) utc_time = pytz.utc.localize(datetime.utcnow()) tz_time = utc_time.astimezone(app.config["TIMEZONE"]) - \ timedelta(hours=app.config["OFFSET_HOURS"]) year = str(tz_time.year) month = "0%s" % tz_time.month if tz_time.month < 10 else str(tz_time.month) day = "0%s" % tz_time.day if tz_time.day < 10 else str(tz_time.day) current_day_images = None if tree.get(year, {}).get(month, {}).get(day): current_day_images = [] for hour, image in tree[year][month][day].iteritems(): hour = int(hour) date_hour = datetime(int(year), tz_time.month, tz_time.day, hour) current_day_images.append({ "path": image.replace(path, app.config["IMAGE_LOCATION_URL"]), "name": date_hour.strftime("%I:00 %p"), }) kwargs = { "years": tree, "current_day": current_day_images, } return render_theme_template(app.config["DEFAULT_THEME"], "home.html", **kwargs)
def year(year): """ Display available months in selected year. """ ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"]) path = "%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year) try: tree = ft.generate_tree(path) except OSError: abort(404) if not tree: abort(404) months = tree.keys() month_data = [] for month in months: month_data.append({ "value": month, "name": datetime(year, int(month), 1).strftime("%B"), }) kwargs = { "year": year, "months": month_data, } return render_theme_template(app.config["DEFAULT_THEME"], "year.html", **kwargs)
def month(year, month): """ Display available days in selected month. """ ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"]) path = "%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month) try: tree = ft.generate_tree(path) except OSError: abort(404) if not tree: abort(404) kwargs = { "year": year, "month": { "value": month, "name": datetime(int(year), int(month), 1).strftime("%B"), }, "days": tree.keys(), } return render_theme_template(app.config["DEFAULT_THEME"], "month.html", **kwargs)
def render_template(template, **context): import logging file_handler = logging.FileHandler('app.log') current_app.logger.addHandler(file_handler) current_app.logger.setLevel(logging.INFO) current_app.logger.info('current theme:%s' % get_theme()) current_app.logger.info('template:%s' % template) return render_theme_template(get_theme(), template, **context)
def get_page_by_slug(pagetype, pageref): """Receives Application Metrics transmissions.""" page = Page.query.filter_by(type=pagetype, id=pageref).first_or_404() return render_theme_template(g.theme, 'page.html', page=page, **defaults )
def login(): if request.form.get('username', None) and request.form.get('password', None): user = request.form['username'] passwd = request.form['password'] if user == current_app.config['ADMIN'] and \ passwd == current_app.config['PASS']: session['user'] = user return redirect(request.args.get('next', url_for('flaskext.admin.index'))) else: return themes.render_theme_template("auth", "login.html", bad_login=True) else: if request.method == 'POST': return themes.render_theme_template("auth", "login.html", bad_login=True) else: return themes.render_theme_template("auth", "login.html")
def tag(tag, page): tagged = list() for post in posts: try: if tag in post.meta['tags']: tagged.append(post) except KeyError: pass return render_theme_template(gen.config['THEME'], 'index.html', pagination=paginate(posts, page=page, objects=tagged))
def test_theme_static(self): app = Flask(__name__) app.config['THEME_PATHS'] = [join(TESTS, 'morethemes')] setup_themes(app, app_identifier='testing') with app.test_request_context('/'): coolurl = static_file_url('cool', 'style.css') cooldata = render_theme_template('cool', 'static.html').strip() assert cooldata == 'Cool Blue v2, %s' % coolurl
def tagindex(): tags = [] for post in posts: try: for tag in post.meta['tags']: if tag not in tags: tags.append(tag) except KeyError: pass return render_theme_template(gen.config['THEME'], 'tags.html', tags=sorted(tags))
def static_pages(page_name): """ Enables user to create static content pages by placing partial-HTML files in a provided directory. """ statics = app.config["STATIC_PAGE_DIR"] f = None if os.path.exists("%s/%s.html" % (statics, page_name)): f = open("%s/%s.html" % (statics, page_name)) elif os.path.exists("%s/%s.htm" % (statics, page_name)): f = open("%s/%s.htm" % (statics, page_name)) else: abort(404) html = f.read() f.close() kwargs = { "html": Markup(html), } return render_theme_template(app.config["DEFAULT_THEME"], "static_page.html", **kwargs)
def post(post): post = posts.get(post) return render_theme_template(gen.config['THEME'], 'post.html', element=post)
def render(template, **context): """Theme renderer shortcut. """ _theme = session.get('theme', 'default') return render_theme_template(_theme, template, **context)
def postindex(page): return render_theme_template(gen.config['THEME'], 'index.html', pagination=paginate(posts, page=page))
def page(page): page = pages.get(page) return render_theme_template(gen.config['THEME'], 'page.html', page=page)
def render_template(template, **context): return render_theme_template(get_theme(), template, **context)
def render_templates(template, **context): print template return render_theme_template(get_theme(), template, **context)
def render(template, **context): theme = session.get('theme', app.config['DEFAULT_THEME']) return render_theme_template(theme, template, **context)
def day(year, month, day): """ Display available images in selected day. """ ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"]) path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day) try: image_files = ft.generate_tree(path) except OSError: abort(404) if not image_files: abort(404) images = [] for hour, image in image_files.iteritems(): images.append({ "url": image.replace(app.config["IMAGE_LOCATION_DIR"], app.config["IMAGE_LOCATION_URL"]), "hour": hour, "hour_name": datetime(int(year), int(month), int(day), int(hour)).strftime("%I:00 %p"), }) # find next/previous days days_tree = ft.generate_tree(app.config["IMAGE_LOCATION_DIR"]) days = [] for iter_year, year_data in days_tree.iteritems(): for iter_month, month_data in year_data.iteritems(): for iter_day, day_data in month_data.iteritems(): days.append((iter_year, iter_month, iter_day)) days = sorted(days) prev_day = None next_day = None for index, a_day in enumerate(days): if a_day == (year, month, day): if index - 1 >= 0: prev_day = days[index - 1] prev_day = { "year": prev_day[0], "month": prev_day[1], "day": prev_day[2] } if index + 1 < len(days): next_day = days[index + 1] next_day = { "year": next_day[0], "month": next_day[1], "day": next_day[2] } break kwargs = { "year": year, "month": { "value": month, "name": datetime(int(year), int(month), int(day)).strftime("%B") }, "day": day, "images": images, "prev_day": prev_day, "next_day": next_day, } return render_theme_template(app.config["DEFAULT_THEME"], "day.html", **kwargs)
def render_template(template, **context): flaskg.clock.start('render_template') output = render_theme_template(get_current_theme(), template, **context) flaskg.clock.stop('render_template') return output
def index(): """Receives Application Metrics transmissions.""" return render_theme_template(g.theme, 'index.html', **defaults )
def archive(): return render_theme_template(gen.config['THEME'], 'archive.html', pagination=paginate(posts, page=1, per_page=0))
def render(template, **context): return render_theme_template(get_current_theme(), template, **context)
def render_theme(template_name, **context): """ Overrides the template_loader for theming purposes """ theme return render_theme_template(theme, template_name, **context)