def get(self): pass_phrase = self.authenticate_request() if pass_phrase: if self.check_setup(): loader = Loader(extension='html', search_dirs=['view', 'view/setup']) renderer = Renderer(file_extension='html', search_dirs=['view/partials', 'view/setup']) template = loader.load_name('setup') html = renderer.render(template, {"pass": pass_phrase}) self.write(html) else: self.write("setup already completed.")
def get(self, article_name): article_name = article_name.lower() if article_name in BaseController.articles: article = BaseController.articles[article_name] # if content has not modified since last request # send a 304 not modified status modified_header_key = "If-Modified-Since" if modified_header_key in self.request.headers: if (self.request.headers["If-Modified-Since"] == article['modified_date']): self.set_status(304) return if (BaseController.settings['enable_caching'] and article_name in BaseController.cached_articles): html = BaseController.cached_articles[article_name] else: view_model = { "article": article, "site_name": BaseController.settings['site_name'] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=[ 'view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('article') html = renderer.render(template, view_model) # cache the html BaseController.cached_articles[article_name] = html # set http caching headers if "http_caching_max_age" in BaseController.settings: max_age = BaseController.settings["http_caching_max_age"] else: max_age = 60 self.set_header("Cache-control", "max-age=%s" % max_age) self.set_header("Last-Modified", article['modified_date']) self.write(html) else: raise tornado.web.HTTPError(404)
def get(self): pass_phrase = self.authenticate_request() if pass_phrase: if self.check_setup(): loader = Loader(extension='html', search_dirs=['view', 'view/setup']) renderer = Renderer( file_extension='html', search_dirs=['view/partials', 'view/setup']) template = loader.load_name('setup') html = renderer.render(template, {"pass": pass_phrase}) self.write(html) else: self.write("setup already completed.")
def generate_page(self, articles): view_model = { "articles": articles, "site_name": BaseController.settings["site_name"] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=['view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('list') html = renderer.render(template, view_model) return html
def get(self, article_name): article_name = article_name.lower() if article_name in BaseController.articles: article = BaseController.articles[article_name] # if content has not modified since last request # send a 304 not modified status modified_header_key = "If-Modified-Since" if modified_header_key in self.request.headers: if (self.request.headers["If-Modified-Since"] == article['modified_date']): self.set_status(304) return if (BaseController.settings['enable_caching'] and article_name in BaseController.cached_articles): html = BaseController.cached_articles[article_name] else: view_model = { "article": article, "site_name": BaseController.settings['site_name'] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=['view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('article') html = renderer.render(template, view_model) # cache the html BaseController.cached_articles[article_name] = html # set http caching headers if "http_caching_max_age" in BaseController.settings: max_age = BaseController.settings["http_caching_max_age"] else: max_age = 60 self.set_header("Cache-control", "max-age=%s" % max_age) self.set_header("Last-Modified", article['modified_date']) self.write(html) else: raise tornado.web.HTTPError(404)
def generate_page(self, articles): view_model = { "articles": articles, "site_name": BaseController.settings["site_name"] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=[ 'view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('list') html = renderer.render(template, view_model) return html
def get(self): if (BaseController.settings['enable_caching'] and BaseController.cached_home): html = BaseController.cached_home self.write(html) else: published_articles = [] for article in BaseController.articles.values(): if article['date'] is not None: published_articles.append(article) articles = sorted(published_articles, key=operator.itemgetter("date"), reverse=True) max_articles_count = BaseController.settings[ "homepage_max_articles"] show_archive = False if len(articles) > max_articles_count: show_archive = True articles = articles[0:max_articles_count] view_model = { "articles": articles, "showArchive": show_archive, "site_name": BaseController.settings["site_name"] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=[ 'view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('home') html = renderer.render(template, view_model) # cache the home page BaseController.cached_home = html self.write(html)
def index(): loader = Loader() template = loader.load_name('index') response = requests.get( 'http://www.unboxedconsulting.com/people/carl-whittaker') status = response.status_code if status == 200: content = response.content soup = BeautifulSoup(content) image = 'http://www.unboxedconsulting.com' + soup.select( 'a.front img')[0]['src'] else: image = '' return pystache.render(template, { 'has_caricature': status == 200, 'image': image })
def index(): loader = Loader() template = loader.load_name('index') response = requests.get('http://www.unboxedconsulting.com/people/carl-whittaker') status = response.status_code if status == 200: content = response.content soup = BeautifulSoup(content) image = 'http://www.unboxedconsulting.com' + soup.select('a.front img')[0]['src'] else: image = '' return pystache.render( template, { 'has_caricature': status == 200, 'image': image } )
def get(self): if (BaseController.settings['enable_caching'] and BaseController.cached_home): html = BaseController.cached_home self.write(html) else: published_articles = [] for article in BaseController.articles.values(): if article['date'] is not None: published_articles.append(article) articles = sorted(published_articles, key=operator.itemgetter("date"), reverse=True) max_articles_count = BaseController.settings["homepage_max_articles"] show_archive = False if len(articles) > max_articles_count: show_archive = True articles = articles[0:max_articles_count] view_model = { "articles": articles, "showArchive": show_archive, "site_name": BaseController.settings["site_name"] } self.attach_meta_data(view_model) loader = Loader(file_encoding='utf8', extension='html', search_dirs=['view', ]) renderer = Renderer(file_encoding='utf8', file_extension='html', search_dirs=['view/partials']) template = loader.load_name('home') html = renderer.render(template, view_model) # cache the home page BaseController.cached_home = html self.write(html)
def test_load_name(self): loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR], extension='txt') template = loader.load_name('template') self.assertEqual(template, 'Test template file\n')
def _fetch_template(self, template_name): loader = Loader(search_dirs=[ROOT_PATH+"/static/template/"]) return loader.load_name(template_name)
import os import pystache import gevent import gevent.monkey from gevent.pywsgi import WSGIServer gevent.monkey.patch_all() from pystache.loader import Loader from flask import Flask, Response, request app = Flask(__name__) loader = Loader() templates = { 'index': loader.load_name('index') } @app.route('/') def index(): r = redis.Redis() ctx = { 'temperature': r.get('temperature'), } return pystache.render(templates['index'], ctx) @app.route('/current-temperature/') def current_temperature():
# # title_partial = {'page-title': 'Some cinder crap | {{title}}'} # renderer = Renderer(partials=title_partial) # renderer.search_dirs.append(TEMPLATE_DIR) # path = os.path.join(TEMPLATE_DIR, "master-template.mustache") # actual = renderer.render_path(path, test_obj) # print actual # step 1: render content in template content_renderer = Renderer() path = os.path.join(CLASS_TEMPLATE_DIR, "main-content-template.mustache") content_renderer.search_dirs.append(TEMPLATE_DIR) rendered_content = content_renderer.render_path(path, class_obj) # step 2: place rendered content into main template # - should have the following custom partials: # - page title (define in object for page templates) # - page content (rendered page content) # - any other common partials that may lie outside the basic content area test_obj = {'title': class_obj["name"]} loader = Loader() # template = loader.read("title") title_partial = loader.load_name(os.path.join(CLASS_TEMPLATE_DIR, "title")) partials = {'page-title': title_partial, 'main-content': rendered_content} renderer = Renderer(partials=partials) path = os.path.join(TEMPLATE_DIR, "master-template.mustache") actual = renderer.render_path(path, test_obj) print actual
else: print "Droplet named `biuVPN` exists" print droplet.name, droplet.id, droplet.ip_address, droplet.status, \ droplet.created_at droplet_id = droplet.id ip_address = droplet.ip_address while not ip_address: print "Waiting Digital Ocean creating droplet ..." droplet.load() ip_address = droplet.ip_address sleep(5) # Generate Ansible configuration files print "Generating Ansible configuration files" loader = Loader(extension="mustache", search_dirs=os.getcwd()) renderer = Renderer() template = loader.load_name(os.getcwd() + "/" + "ansible_hosts") result = renderer.render(template, {"ip_address": ip_address}) with open("ansible_hosts", 'w') as file_handler: file_handler.write(result) template = loader.load_name(os.getcwd() + "/" + "biuVPN.yml") result = renderer.render(template, {"ip_address": ip_address}) with open("biuVPN.yml", 'w') as file_handler: file_handler.write(result)
def _fetch_template(self, template_name): loader = Loader(search_dirs=[self._get_template_directory()]) return loader.load_name(template_name)
from flask import Flask from flask_compress import Compress from flask_talisman import Talisman, GOOGLE_CSP_POLICY app = Flask(__name__) Compress(app) GOOGLE_CSP_POLICY['style-src'] += ' cdnjs.cloudflare.com' Talisman(app, content_security_policy=GOOGLE_CSP_POLICY) from letterboxd import get_letterboxd_most_recently_watched_details cache = SimpleCache() loader = Loader() template = loader.load_name('index') def get_cached_result(func, key): cached_result = None try: cached_result = cache.get(key) if not cached_result: cached_result = func() cache.set(key, cached_result) except: pass return cached_result
import pystache import gevent import gevent.monkey import redis from gevent.pywsgi import WSGIServer gevent.monkey.patch_all() from pystache.loader import Loader from flask import Flask, Response, request app = Flask(__name__) loader = Loader() templates = { 'comic': loader.load_name('comic') } @app.route('/') def index(): if request.headers.get('accept') == 'text/event-stream': r = redis.Redis() pubsub = r.pubsub() pubsub.subscribe(['comic']) def images(): for msg in pubsub.listen(): if msg['type'] == 'message': yield 'event: comic\n' yield 'data: %s\n\n' % msg['data']
# title_partial = {'page-title': 'Some cinder crap | {{title}}'} # renderer = Renderer(partials=title_partial) # renderer.search_dirs.append(TEMPLATE_DIR) # path = os.path.join(TEMPLATE_DIR, "master-template.mustache") # actual = renderer.render_path(path, test_obj) # print actual # step 1: render content in template content_renderer = Renderer() path = os.path.join(CLASS_TEMPLATE_DIR, "main-content-template.mustache") content_renderer.search_dirs.append(TEMPLATE_DIR) rendered_content = content_renderer.render_path(path, class_obj) # step 2: place rendered content into main template # - should have the following custom partials: # - page title (define in object for page templates) # - page content (rendered page content) # - any other common partials that may lie outside the basic content area test_obj = {"title": class_obj["name"]} loader = Loader() # template = loader.read("title") title_partial = loader.load_name(os.path.join(CLASS_TEMPLATE_DIR, "title")) partials = {"page-title": title_partial, "main-content": rendered_content} renderer = Renderer(partials=partials) path = os.path.join(TEMPLATE_DIR, "master-template.mustache") actual = renderer.render_path(path, test_obj) print actual
from pystache.loader import Loader from pystache import render from flask import Flask, abort, url_for from flask_compress import Compress from arguments import arguments app = Flask(__name__) Compress(app) loader = Loader() home_template = loader.load_name('templates/home') def slugify(string): return string.lower().replace(' ', '-') routes = {} for challengers in arguments: for perm in itertools.permutations(challengers): slugs = tuple(slugify(c) for c in perm) routes[slugs] = perm
def get(self): loader = Loader(extension='html', search_dirs=['view', 'view/setup']) template = loader.load_name('success') renderer = Renderer(file_extension='html', search_dirs=['view/partials', 'view/setup']) self.write(renderer.render(template, ""))
def test_load_name(self): loader = Loader(search_dirs=[DATA_DIR, LOADER_DATA_DIR], extension="txt") template = loader.load_name("template") self.assertEqual(template, "Test template file\n")