def index(): context = make_context() speeches = [] for speech in commencement_data.load(): speech['share_url'] = 'http://%s/%s/speech/%s/' % (app_config.PRODUCTION_S3_BUCKET['bucket_name'], app_config.PROJECT_SLUG, speech['slug']) speech['money_quote_image'] = '%s/quote-images/%s.png' % (app_config.S3_BASE_URL, speech['slug']) speech['share_text'] = '%(name)s, %(year)s. From NPR\'s The Best Commencement Speeches, Ever.' % speech speeches.append(speech) speeches = sorted(speeches, key=lambda x: x['name']) context['speeches'] = speeches context['speeches_json'] = json.dumps(speeches) return make_response(render_template('index.html', **context))
def index(): context = make_context() speeches = [] for speech in commencement_data.load(): speech['share_url'] = 'http://%s/%s/speech/%s/' % ( app_config.PRODUCTION_S3_BUCKET['bucket_name'], app_config.PROJECT_SLUG, speech['slug']) speech['money_quote_image'] = '%s/quote-images/%s.png' % ( app_config.S3_BASE_URL, speech['slug']) speech[ 'share_text'] = '%(name)s, %(year)s. From NPR\'s The Best Commencement Speeches, Ever.' % speech speeches.append(speech) speeches = sorted(speeches, key=lambda x: x['name']) context['speeches'] = speeches context['speeches_json'] = json.dumps(speeches) return make_response(render_template('index.html', **context))
def render_speeches(compiled_includes={}): """ Render the speech pages. """ from flask import g, url_for local('rm -rf www/speech') speeches = commencement_data.load() for speech in speeches: slug = speech['slug'] with app.app.test_request_context(): path = '%sindex.html' % url_for('_speech', slug=slug) with app.app.test_request_context(path=path): print 'Rendering %s' % path g.compile_includes = True g.compiled_includes = compiled_includes view = app.__dict__['_speech'] content = view(slug).data compiled_includes = g.compiled_includes path = 'www%s' % path # Ensure path exists head = os.path.split(path)[0] try: os.makedirs(head) except OSError: pass with open(path, 'w') as f: f.write(content)
def _speech(slug): context = make_context() speeches = commencement_data.load() context['speech'] = next(s for s in speeches if s['slug'] == slug) context['share_url'] = 'http://%s/%s/speech/%s/' % ( app_config.PRODUCTION_S3_BUCKET['bucket_name'], app_config.PROJECT_SLUG, slug) context['money_quote_image'] = '%s/quote-images/%s.png' % ( app_config.S3_BASE_URL, slug) context[ 'share_text'] = '%(name)s, %(year)s. From NPR\'s The Best Commencement Speeches, Ever.' % context[ 'speech'] # Fancy web source credit line, e.g., See full text at graduationwisdom.com. if context['speech'].get('full_text_link', None): url = context['speech']['full_text'] elif context['speech'].get('full_text', None): url = context['speech']['full_text'] else: url = context['speech']['source_url'] context['speech']['web_source_credit'] = urlparse(url).netloc.replace( 'www.', '') # Horizontal nav. context['tags'] = [] speech_tags = defaultdict(list) # Build a dictionary of tags; for each tag, get me a list of the speeches. # Loop over all speeches. for speech in speeches: # Loop over the tags in this speech. for tag in speech['tags']: # If this speech shares a tag with the page we're on, append it to the list. if tag in context['speech']['tags']: speech_tags[tag].append(speech) # Get the list of speeches that share tags with this speech on a tag-by-tag basis. # Sort the list by name. Find the speech that follows this one in the list. for tag in context['speech']['tags']: speech_tags[tag] = sorted(speech_tags[tag], key=lambda x: x['name']) for index, speech in enumerate(speech_tags[tag]): if speech['slug'] == context['speech']['slug']: # The next speech should just be the one following this one in the list. try: next_speech = speech_tags[tag][index + 1] except IndexError: next_speech = speech_tags[tag][0] # There is one exception to this rule. # Loop over the speeches we've already grabbed using this logic # and make sure we don't have the same speech showing up twice. # Duplicates are evil. for obj in context['tags']: if obj['speech']['slug'] == next_speech['slug']: try: next_speech = speech_tags[tag][index + 2] except IndexError: next_speech = speech_tags[tag][0] context['tags'].append({ 'tag': app_config.TAGS[tag], 'speech': next_speech }) break return make_response(render_template('speech.html', **context))
def _speech(slug): context = make_context() speeches = commencement_data.load() context['speech'] = next(s for s in speeches if s['slug'] == slug) context['share_url'] = 'http://%s/%s/speech/%s/' % (app_config.PRODUCTION_S3_BUCKET['bucket_name'], app_config.PROJECT_SLUG, slug) context['money_quote_image'] = '%s/quote-images/%s.png' % (app_config.S3_BASE_URL, slug) context['share_text'] = '%(name)s, %(year)s. From NPR\'s The Best Commencement Speeches, Ever.' % context['speech'] # Fancy web source credit line, e.g., See full text at graduationwisdom.com. if context['speech'].get('full_text_link', None): url = context['speech']['full_text'] elif context['speech'].get('full_text', None): url = context['speech']['full_text'] else: url = context['speech']['source_url'] context['speech']['web_source_credit'] = urlparse(url).netloc.replace('www.', '') # Horizontal nav. context['tags'] = [] speech_tags = defaultdict(list) # Build a dictionary of tags; for each tag, get me a list of the speeches. # Loop over all speeches. for speech in speeches: # Loop over the tags in this speech. for tag in speech['tags']: # If this speech shares a tag with the page we're on, append it to the list. if tag in context['speech']['tags']: speech_tags[tag].append(speech) # Get the list of speeches that share tags with this speech on a tag-by-tag basis. # Sort the list by name. Find the speech that follows this one in the list. for tag in context['speech']['tags']: speech_tags[tag] = sorted(speech_tags[tag], key=lambda x: x['name']) for index, speech in enumerate(speech_tags[tag]): if speech['slug'] == context['speech']['slug']: # The next speech should just be the one following this one in the list. try: next_speech = speech_tags[tag][index + 1] except IndexError: next_speech = speech_tags[tag][0] # There is one exception to this rule. # Loop over the speeches we've already grabbed using this logic # and make sure we don't have the same speech showing up twice. # Duplicates are evil. for obj in context['tags']: if obj['speech']['slug'] == next_speech['slug']: try: next_speech = speech_tags[tag][index + 2] except IndexError: next_speech = speech_tags[tag][0] context['tags'].append({ 'tag': app_config.TAGS[tag], 'speech': next_speech }) break return make_response(render_template('speech.html', **context))