-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.py
executable file
·274 lines (203 loc) · 7.98 KB
/
website.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
import datetime
import json
import os
import sys
from collections import OrderedDict
from os.path import abspath, dirname, join
import dateutil
import html2text
import vobject
from dateutil import rrule
from flask import (Flask, Response, render_template, request,
send_from_directory)
from flask_assets import Environment as AssetManager
from flask_flatpages import FlatPages
from flask_frozen import Freezer
from typogrify.templatetags import jinja_filters as typogrify_filters
# Configuration
DEBUG = True
BASE_URL = "http://gtalug.org"
ASSETS_DEBUG = False
ROOT_DIR = dirname(abspath(__file__))
FLATPAGES_AUTO_RELOAD = True
FLATPAGES_EXTENSION = ".markdown"
FLATPAGES_ROOT = join(ROOT_DIR, "pages")
TEMPLATE_ROOT = join(ROOT_DIR, "templates")
MEETINGS_ROOT = join(ROOT_DIR, "meetings")
TIMEZONE = 'America/Toronto'
class MeetingPages(FlatPages):
@property
def root(self):
return MEETINGS_ROOT
app = Flask(__name__, template_folder=TEMPLATE_ROOT)
app.config.from_object(__name__)
app.jinja_env.filters['typogrify'] = typogrify_filters.typogrify
pages = FlatPages(app)
meetings = MeetingPages(app)
freezer = Freezer(app)
asset_manager = AssetManager(app)
@app.route('/')
def index():
meeting = OrderedDict(sorted(meetings._pages.items())).values()[-1]
template = meeting.meta.get('template', 'home.html')
return render_template(template, meeting=meeting)
@app.route('/humans.txt')
@app.route('/robots.txt')
def static_from_root():
return send_from_directory(TEMPLATE_ROOT, request.path[1:])
@app.route('/.htaccess')
def htaccess():
return send_from_directory(TEMPLATE_ROOT, 'htaccess.htaccess')
@app.route('/api/upcoming_meeting.json')
def api_upcoming_meeting():
m = OrderedDict(sorted(meetings._pages.items())).values()[-1]
data = {
'@context': 'http://schema.org',
'@type': 'Event',
'name': m.meta['meeting_title'],
'url': 'http://gtalug.org/meeting/%s/' % m.path,
'sameAs': 'http://gtalug.org/api/meeting/%s.json' % m.path,
'startDate': m.meta['meeting_datetime'].strftime("%v"),
}
return Response(json.dumps(data), mimetype='application/json')
@app.route('/api/meetings.json')
def api_meeting_list():
data = []
for m in meetings:
data += [{
'@context': 'http://schema.org',
'@type': 'Event',
'name': m.meta['meeting_title'],
'url': 'http://gtalug.org/meeting/%s/' % m.path,
'sameAs': 'http://gtalug.org/api/meeting/%s.json' % m.path,
'startDate': m.meta['meeting_datetime'].strftime("%v"),
}, ]
return Response(json.dumps(data), mimetype='application/json')
@app.route('/api/meeting/<path:slug>.json')
def api_meeting_detail(slug):
m = meetings.get_or_404(slug)
data = {
'@context': 'http://schema.org',
'@type': 'Event',
'name': m.meta['meeting_title'],
'startDate': m.meta['meeting_datetime'].strftime("%v"),
'url': 'http://gtalug.org/meeting/%s/' % m.path,
'description': html2text.html2text(m.html),
'offer': {
'@type': 'Offer',
'category': 'http://schema.org/LeisureTimeActivity'
},
'organizer': {
'@type': 'Organization',
'name': 'GTALUG',
'legalName': 'Greater Toronto Area Linux User Group',
'image': 'http://gtalug.org/static/images/gtalug-logo.png',
}
}
if m.meta.get('meeting_location', None):
data['location'] = m.meta['meeting_location']
return Response(json.dumps(data), mimetype='application/json')
@app.route('/twtxt.txt')
def gtalug_twtxt():
meeting_list = list(
reversed(OrderedDict(sorted(meetings._pages.items())).values()))
tweets = []
for m in meeting_list:
context = {
'date': m.meta['meeting_datetime'].isoformat(),
'title': m.meta['meeting_title'],
'path': m.path
}
tweets.append(
"{date}\t{title} <https://gtalug.org/meeting/{path}/>".format(**context))
return Response('\n'.join(tweets), mimetype='text/plain')
@app.route('/gtalug.ics')
def gtalug_ics():
meeting_list = list(
reversed(OrderedDict(sorted(meetings._pages.items())).values()))
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH'
cal.add('VTIMEZONE').tzinfo = dateutil.tz.tzlocal()
for m in meeting_list:
event = cal.add('vevent')
event.add('uid').value = "meeting-%s@GTALUG.org" % m.path
event.add('summary').value = "GTALUG Meeting: %s" % m.meta[
'meeting_title']
event.add('description').value = html2text.html2text(m.html)
event.add('url').value = 'http://gtalug.org/meeting/%s/' % m.path
event.add('status').value = 'CONFIRMED'
event.add('location').value = m.meta['meeting_location']
meeting_datetime = m.meta['meeting_datetime']
event.add('dtstart').value = meeting_datetime
event.add('dtend').value = meeting_datetime + \
datetime.timedelta(hours=2)
last_meeting_date = meeting_list[0].meta['meeting_datetime']
upcoming_meetings = list(rrule.rrule(
freq=rrule.MONTHLY,
dtstart=last_meeting_date + datetime.timedelta(days=1),
count=5,
byweekday=(rrule.TU),
bysetpos=2
))
for m in upcoming_meetings:
event = cal.add('vevent')
event.add(
'uid').value = "meeting-%s@GTALUG.org" % (m.strftime("%Y-%m"))
event.add('summary').value = "GTALUG Meeting"
event.add('description').value = "An upcoming GTALUG meeting."
event.add('url').value = 'http://gtalug.org/'
event.add('status').value = 'TENTATIVE'
event.add('dtstart').value = m
event.add('dtend').value = m + datetime.timedelta(hours=2)
return Response(cal.serialize(), mimetype='text/calendar')
@app.route('/meeting/')
def meeting_list():
page = pages.get_or_404('meeting')
template = page.meta.get('template', 'meeting_list.html')
meeting_list = reversed(
OrderedDict(sorted(meetings._pages.items())).values())
return render_template(template, page=page, meetings=meeting_list)
@app.route('/meeting/<path:slug>/')
def meeting_detail(slug):
meeting = meetings.get_or_404(slug)
template = meeting.meta.get('template', 'meeting_detail.html')
return render_template(template, meeting=meeting)
@app.route('/meeting/<path:slug>/gtalug-meeting.ics')
def meeting_detail_ics(slug):
meeting = meetings.get_or_404(slug)
cal = vobject.iCalendar()
cal.add('method').value = 'PUBLISH'
cal.add('VTIMEZONE').tzinfo = dateutil.tz.tzlocal()
event = cal.add('vevent')
event.add('uid').value = "meeting-%s@GTALUG.org" % meeting.path
event.add('summary').value = "GTALUG Meeting: %s" % meeting.meta[
'meeting_title']
event.add('description').value = html2text.html2text(meeting.html)
event.add('url').value = 'http://gtalug.org/meeting/%s/' % meeting.path
event.add('status').value = 'CONFIRMED'
event.add('location').value = meeting.meta['meeting_location']
meeting_datetime = meeting.meta['meeting_datetime']
event.add('dtstart').value = meeting_datetime
event.add('dtend').value = meeting_datetime + datetime.timedelta(hours=2)
return Response(cal.serialize(), mimetype='text/calendar')
@app.route('/<path:path>/')
def page(path):
page = pages.get_or_404(path)
template = page.meta.get('template', 'page.html')
return render_template(template, page=page)
@freezer.register_generator
def page_list():
for p in pages:
yield 'page', {'path': p.path}
for m in meetings:
yield 'meeting_detail', {'slug': m.path}
yield 'meeting_detail_ics', {'slug': m.path}
yield 'api_meeting_detail', {'slug': m.path}
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == "build":
app.testing = True
freezer.freeze()
else:
port = int(os.environ.get('PORT', 8000))
app.run(port=port)