Find file
069634b Jun 9, 2014
146 lines (107 sloc) 3.88 KB
import os
import yaml
import logging
import datetime
import markdown
from urllib.parse import urljoin
from datetime import timedelta
from werkzeug import secure_filename
from werkzeug.contrib.atom import AtomFeed
from flask import Flask, render_template, Markup, abort, redirect, url_for, request
app = Flask(__name__)
logger = logging.getLogger(__name__)
cache = {}
def get_page(directory, file):
"""Load and parse a page from the filesystem. Returns the page, or None if not found"""
filename = secure_filename(file)
if filename in cache:
return cache[filename]
path = os.path.abspath(os.path.join(os.path.dirname(__file__), directory, filename))
file_contents = open(path, encoding='utf-8').read()
logger.exception("Failed to open file at path: %s", path)
return None
data, text = file_contents.split('---\n', 1)
page = yaml.load(data)
page['content'] = Markup(markdown.markdown(text))
page['path'] = file
cache[filename] = page
return page
def get_meeting(path):
"""Get a meeting from the filesystem"""
meeting = get_page(app.config['MEETINGS_DIR'], path)
if meeting is not None:
meeting = meeting.copy()
meeting['datetime'] = datetime.datetime.strptime(meeting['datetime'], '%Y-%m-%d %H:%M')
return meeting
def get_meetings():
"""Return a list of all meetings"""
if 'meeting_list' in cache:
return cache['meeting_list']
files = os.listdir(os.path.abspath(os.path.join(os.path.dirname(__file__), app.config['MEETINGS_DIR'])))
meetings = filter(lambda meeting: meeting is not None, [get_meeting(file) for file in files])
result = sorted(meetings, key=lambda item: item['datetime'])
cache['meeting_list'] = result
return result
def past_meetings():
meeting_list = get_meetings()
now =
return [meeting for meeting in meeting_list if meeting['datetime'] < now]
def future_meetings():
meeting_list = get_meetings()
now =
return [meeting for meeting in meeting_list if meeting['datetime'] > now]
def index():
return render_template('homepage.html', future_meeting_list=future_meetings())
def archive():
"""Legacy URL"""
return redirect(url_for('meetings'))
def meetings():
return render_template(
def meeting(date):
meeting = get_meeting(date)
if meeting is None:
return render_template('meeting.html', meeting=meeting)
def page(path):
page = get_page(app.config['PAGES_DIR'], path)
if page is None:
return render_template('page.html', page=page)
def feed():
feed = AtomFeed('BrightonPy Events', feed_url=request.url, url=request.url_root)
for meeting in reversed(get_meetings()):
# A little hack
date = meeting['datetime'] - timedelta(weeks=1)
url=urljoin(request.url_root, url_for('meeting', date=meeting['path'])),
return feed.get_response()
def page_not_found(error):
return render_template('404.html'), 404
# Add jinja filters
def format_datetime(datetime_object, format):
"""Format a datetime object for display, used in Jinja2 templates"""
return datetime_object.strftime(format)
if __name__ == '__main__':
port = int(os.getenv('PORT', 5000)), host='', port=port)