-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchicagolug.py
157 lines (134 loc) · 4.76 KB
/
chicagolug.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
146
147
148
149
150
151
152
153
154
155
156
157
# https://icecreamcode.org/posts/python/markdown/
from flask import Flask, render_template, Markup, abort, redirect, url_for, request
import yaml
import os
import datetime
import unicodedata
import misaka as m
from uritools import urijoin
from datetime import timedelta
from werkzeug.utils import secure_filename
from feedwerk.atom import AtomFeed
app = Flask(__name__)
app.config.from_pyfile('settings.py')
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)
path = os.path.abspath(os.path.join(os.path.dirname(__file__), directory, filename))
try:
file_contents = open(path).read()
file_contents = unicodedata(file_contents, 'utf-8')
except:
return None
data, text = file_contents.split('---\n', 1)
page = yaml.load(data)
page['content'] = Markup(m.html(text))
page['path'] = file
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['datetime'] = datetime.datetime.strptime(meeting['datetime'], '%Y-%m-%d %H:%M')
return meeting
def get_meetings():
"""Return a list of all meetings"""
files = os.listdir(os.path.abspath(os.path.join(os.path.dirname(__file__), app.config['MEETINGS_DIR'])))
meetings = [meeting for meeting in [get_meeting(file) for file in files] if meeting is not None]
return sorted(meetings, key=lambda item: item['datetime'])
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]
def get_location(path):
"""Get a location from the filesystem"""
location = get_page(app.config['LOCATIONS_DIR'], path)
if location is not None:
location['location'] = location
return location
def get_locations():
"""Return a list of all locations"""
files = os.listdir(os.path.abspath(os.path.join(os.path.dirname(__file__), app.config['LOCATIONS_DIR'])))
locations = [location for location in [get_location(file) for file in files] if location is not None]
return (locations)
def all_locations():
locsDict = {}
locs = []
for l in get_locations():
locsDict[l["locorder"]] = l
for l in locsDict:
locs.append(locsDict[l])
return locs
@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('ChicagoLUG Meetings and 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'],
str(meeting['content']),
author=meeting['speaker'],
url=urijoin(request.url_root, url_for('meeting', date=meeting['path'])),
updated=date,
published=date
)
return feed.get_response()
@app.route('/locations/')
def locations():
return render_template(
'locations.html',
location_list=all_locations()
)
@app.route('/locations/<path>/')
def location(path):
location = get_location(path)
if location is None:
abort(404)
return render_template('location.html', location=location)
@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)
@app.before_request
def redirect_from_epio():
"""Redirect www to naked domain"""
if "www" in request.host:
return redirect('http://chicagolug.org' + request.path, 301)
if __name__ == '__main__':
app.debug = False
app.run()