-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathbrighton.py
145 lines (107 loc) · 3.88 KB
/
brighton.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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__)
app.config.from_pyfile('settings.py')
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))
try:
file_contents = open(path, encoding='utf-8').read()
except:
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 = datetime.datetime.now()
return [meeting for meeting in meeting_list if meeting['datetime'] < now]
def future_meetings():
meeting_list = get_meetings()
now = datetime.datetime.now()
return [meeting for meeting in meeting_list if meeting['datetime'] > now]
@app.route('/')
def index():
return render_template('homepage.html', future_meeting_list=future_meetings())
@app.route('/archive/')
def archive():
"""Legacy URL"""
return redirect(url_for('meetings'))
@app.route('/meetings/')
def meetings():
return render_template(
'meetings.html',
past_meeting_list=reversed(past_meetings()),
future_meeting_list=future_meetings()
)
@app.route('/meetings/<date>/')
def meeting(date):
meeting = get_meeting(date)
if meeting is None:
abort(404)
return render_template('meeting.html', meeting=meeting)
@app.route('/pages/<path>/')
def page(path):
page = get_page(app.config['PAGES_DIR'], path)
if page is None:
abort(404)
return render_template('page.html', page=page)
@app.route('/meetings.atom')
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)
feed.add(
meeting['title'],
meeting['content'],
author=meeting['speaker'],
url=urljoin(request.url_root, url_for('meeting', date=meeting['path'])),
updated=date,
published=date
)
return feed.get_response()
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
# Add jinja filters
@app.template_filter('datetimeformat')
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))
app.run(debug=True, host='0.0.0.0', port=port)