Find file
Fetching contributors…
Cannot retrieve contributors at this time
97 lines (77 sloc) 3.18 KB
from flask import Flask, request, redirect, flash, get_flashed_messages, render_template, session
from optparse import OptionParser
import time
import requests
from StringIO import StringIO
import unicodecsv
from jinja2 import Markup
import sys
app = Flask(__name__)
def get_google_csv_url(key,sheet=None):
url = "" % key
if sheet is not None:
url += "&gid=%s" % sheet
return url
def fetch_perpage_microcopy():
"""Fetch the microcopy deck and return a dict whose keys are the value in the first column
and whose values are a dict of values for that (keys are column headers)"""
if not resp.ok: resp.raise_for_status()
d = {}
reader = unicodecsv.DictReader(StringIO(resp.content))
for row in reader:
# have to convert keys from unicode to str to satisfy python's insistence on strs for kwarg keys
d2 = dict((str(k),v) for k,v in row.items())
row_key = row[reader.fieldnames[0]]
d[row_key] = d2
return d
def fetch_frontpage_microcopy(include_features=False):
"""Fetch the microcopy deck and return a dict whose keys are a front page content ID and whose value is the second column"""
if not resp.ok: resp.raise_for_status()
d = {}
for x in unicodecsv.DictReader(StringIO(resp.content)):
k,v = (x['id'],x['copy']) # choose other names, or document these, or...?
k = str(k)
d[k] = v
if include_features:
d['features'] = fetch_perpage_microcopy(key_mode=key_mode)
return d
def process_microcopy_textblock(text):
if text:
content = "<br>".join(text.strip().splitlines())
content = ''
return Markup(content)
def init_default_context():
return {}
def default():
return redirect('/index.html')
def preview(template,key_mode=False):
if not template.endswith('.html'):
return app.send_static_file(template)
template_key = template[:-5]
context = init_default_context()
# populate with copy from google docs?
# find alternate template name based on key?
except AttributeError: pass
return render_template("%s" % template,**context)
def parse_args():
parser = OptionParser()
parser.add_option("--host", dest="host", default='localhost',
help="override the 'host' value so that one can access the local server from other machines.")
(options, args) = parser.parse_args()
return options
if __name__ == '__main__':
opts = parse_args()
kwargs = { 'debug': True, 'host': }
app.config['HOSTNAME'] =**kwargs)