Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 166 lines (139 sloc) 4.88 kb
3d467594 »
2011-10-13 Initial commit, baby
1 import flask
3d656b71 »
2011-10-24 Redirect from /setup-first to / with flash message. Add support for a…
2 from flask import Flask, g, abort, flash, redirect, url_for, request, render_template
3d467594 »
2011-10-13 Initial commit, baby
3 import flaskext.redis
4 import datetime
74889b7f »
2011-10-24 Add support for debug argument
5 import sys
1a38df85 »
2011-10-24 Add current directory to the path of the secret_key.txt
6 import os
eba74190 »
2011-10-24 Use a Python file
7 import secret_key
02f5cb81 »
2011-10-28 Replace fixed data with YAML fixture
8 import yaml
3d467594 »
2011-10-13 Initial commit, baby
9
10 app = Flask(__name__)
eba74190 »
2011-10-24 Use a Python file
11 app.secret_key = secret_key.key
3d656b71 »
2011-10-24 Redirect from /setup-first to / with flash message. Add support for a…
12
3d467594 »
2011-10-13 Initial commit, baby
13 db = flaskext.redis.init_redis(app)
14
41e60b67 »
2011-10-24 Add ability to attend/unattend for other dates
15 def get_date():
16 today = request.args.get('date', '')
17 try:
18 datetime.datetime.strptime(today, '%Y-%m-%d')
19 except:
20 if len(today) > 0:
21 flash("Format of date '%s' is incorrect, should be YYYY-mm-dd" % (today,))
22 today = datetime.datetime.now().strftime('%Y-%m-%d')
23 return today
3d467594 »
2011-10-13 Initial commit, baby
24
02f5cb81 »
2011-10-28 Replace fixed data with YAML fixture
25 def get_fixture_data():
26 with open("fixture.yaml", "r") as f:
27 return yaml.load(f.read())
28
3d467594 »
2011-10-13 Initial commit, baby
29 @app.route("/setup-first")
3d656b71 »
2011-10-24 Redirect from /setup-first to / with flash message. Add support for a…
30 def setup_first():
3d467594 »
2011-10-13 Initial commit, baby
31 """Set up a starting dataset.
32 """
02f5cb81 »
2011-10-28 Replace fixed data with YAML fixture
33 fixture = get_fixture_data()
34 for c in fixture['circles']:
3d467594 »
2011-10-13 Initial commit, baby
35 did_add = db.sadd('circles', c['mnemonic'])
36 if did_add:
37 circle_key = 'circle:%s' % c['mnemonic']
38 db.hset(circle_key, 'id', c['id'])
39 db.hset(circle_key, 'name', c['name'])
40 db.hset(circle_key, 'mnemonic', c['mnemonic'])
41
02f5cb81 »
2011-10-28 Replace fixed data with YAML fixture
42 for p in fixture['people']:
3d467594 »
2011-10-13 Initial commit, baby
43 db.sadd('nicks', p)
44
45 db.save()
46
3d656b71 »
2011-10-24 Redirect from /setup-first to / with flash message. Add support for a…
47 flash('First! Database is go \'enna!')
48
49 return redirect(url_for('index'))
50
3d467594 »
2011-10-13 Initial commit, baby
51 @app.route('/')
52 def index():
53 """GUI to state which people are in attendance tonight.
54 """
55 circles = []
56 for mnemonic in db.smembers('circles'):
57 circle = dict(
58 name= db.hget('circle:%s' % mnemonic, 'name'),
59 mnemonic= mnemonic
60 )
61 circles.append(circle)
d8cc6ce9 »
2011-10-24 Redirect to /setup-first if there are no circles (i.e. uninitialized db)
62 if len(circles) == 0:
63 return redirect(url_for('setup_first'))
64
3d467594 »
2011-10-13 Initial commit, baby
65 people = db.smembers('nicks')
41e60b67 »
2011-10-24 Add ability to attend/unattend for other dates
66 today = get_date()
3d467594 »
2011-10-13 Initial commit, baby
67 members = []
68 for person in people:
69 otherkey = '%s:%s' % (person, today)
70 circle = db.get(otherkey) or ''
71 members.append(dict(name=person, circle=circle))
56497f30 »
2011-10-24 Add reports and add nick
72
73 reports = set()
74 for candidate_month in db.keys('meetups:*'):
75 junk, _, month = candidate_month.partition(":")
76 reports.add(month)
77
41e60b67 »
2011-10-24 Add ability to attend/unattend for other dates
78 return render_template('index.html', today=today, members=members, circles=circles, reports=reports)
3d467594 »
2011-10-13 Initial commit, baby
79
10baa511 »
2011-11-15 add test for unique visit of a nick in a circle
80 def person_should_be_added(nick, today, circle):
81 if not nick and circle:
82 return False
83 # Has this Nick been registered in a circle today? If so, return False
84 for circle_name in db.keys("circle:*:%s" % today):
85 if nick in db.smembers(circle_name):
86 return False
87 return True
88
3d467594 »
2011-10-13 Initial commit, baby
89 @app.route("/attend")
90 def attend():
91 """Ajax callback to record a person as attending a circle.
92 """
41e60b67 »
2011-10-24 Add ability to attend/unattend for other dates
93 today = get_date()
94 this_month = today[0:-3]
3d467594 »
2011-10-13 Initial commit, baby
95 nick = request.args.get("nick", "")
96 circle = request.args.get("circle", "")
10baa511 »
2011-11-15 add test for unique visit of a nick in a circle
97 if person_should_be_added(nick, today, circle):
3d467594 »
2011-10-13 Initial commit, baby
98 key = "circle:%s:%s" % (circle, today)
99 db.sadd(key, nick)
100 otherkey = "%s:%s" % (nick, today)
101 db.set(otherkey, circle)
102
103 # Meetup dates are added incrementally
104 db.sadd("meetups", today)
105 db.sadd("meetups:%s" % this_month, today)
106 db.sadd("circles:%s" % this_month, circle)
107 return '1'
108 return '0'
109
110 @app.route("/unattend")
111 def unattend():
112 """Ajax callback to cancel attendance for one person.
113 """
41e60b67 »
2011-10-24 Add ability to attend/unattend for other dates
114 today = get_date()
115 this_month = today[0:-3]
3d467594 »
2011-10-13 Initial commit, baby
116 nick = request.args.get("nick")
117 circle = request.args.get("circle")
118 if nick and circle:
119 db.srem("circle:%s:%s" % (circle, today), nick)
120 db.delete("%s:%s" % (nick, today))
121 return '1'
122 return '0'
123
124 @app.route("/join")
125 def join_forskningsavdelningen():
126 """Add given nick to the database.
127 """
128 nick = request.args.get("nick", "")
369c43c3 »
2011-10-24 Flash on adding nick
129 message = "Hm, what?"
3d467594 »
2011-10-13 Initial commit, baby
130 if nick:
131 db.sadd("nicks", nick)
369c43c3 »
2011-10-24 Flash on adding nick
132 message = "Tack, tillagd."
133 flash(message)
134 return redirect(url_for('index'))
135
3d467594 »
2011-10-13 Initial commit, baby
136
137 @app.route("/report")
138 def report_one_month():
139 """Render a monthly report of attendance of the circles.
140 """
141
142 def get_circles():
143 return db.smembers("circles")
144 report = []
145
146 chosen_month = request.args.get("month", datetime.datetime.now().strftime('%Y-%m'))
147
148 dates_this_month = db.smembers("meetups:%s" % chosen_month)
149 circles_this_month = db.smembers("circles:%s" % chosen_month)
150
26602fb0 »
2011-10-25 sort report by date, closes #5
151 for iso_day in sorted(dates_this_month):
3d467594 »
2011-10-13 Initial commit, baby
152 day = {'date': iso_day, 'circles': []}
153 for circle_name in circles_this_month:
154 circle = {'name': circle_name, 'attendees': []}
155 for nick in db.smembers("circle:%s:%s" % (circle_name, iso_day)):
156 circle['attendees'].append(nick)
157 day['circles'].append(circle)
158 report.append(day)
159 return render_template('report.html', report=report)
160
161 if __name__ == "__main__":
74889b7f »
2011-10-24 Add support for debug argument
162 if 'debug' in sys.argv:
163 app.debug = True;
164
cae90c85 »
2011-10-19 Remove the debug marker
165 app.run()
Something went wrong with that request. Please try again.