forked from mitsuhiko/flask-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback.py
157 lines (124 loc) · 4.6 KB
/
feedback.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
# -*- coding: utf-8 -*-
"""
Flask Feedback
~~~~~~~~~~~~~~
We want to know what where we should improve. This website should
help us achieve that goal.
Inspired by what the Mozilla guys did for Firefox.
:copyright: (c) Copyright 2011 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from datetime import datetime
from random import SystemRandom
from flask import Flask, render_template, session, request, \
url_for, redirect, Response, jsonify
from flaskext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['FEEDBACK_PER_PAGE'] = 30
app.config.from_pyfile('settings.cfg')
db = SQLAlchemy(app)
rnd = SystemRandom()
class Feedback(db.Model):
id = db.Column('feedback_id', db.Integer, primary_key=True)
kind = db.Column(db.Integer)
text = db.Column(db.String(1000))
version = db.Column(db.String(40))
pub_date = db.Column(db.DateTime)
UNHAPPY = -1
HAPPY = 1
KINDS = {'unhappy': UNHAPPY, 'happy': HAPPY}
def __init__(self, kind, text, version):
assert kind in (self.UNHAPPY, self.HAPPY)
self.kind = kind
self.text = u' '.join(text.split())
self.version = version
self.pub_date = datetime.utcnow()
@property
def kind_symbol(self):
if self.kind == self.HAPPY:
return '+'
elif self.kind == self.UNHAPPY:
return '-'
return '?'
@property
def is_happy(self):
return self.kind == self.HAPPY
def to_json(self):
return {
'kind': self.kind_symbol,
'text': self.text,
'version': self.version,
'pub_date': self.pub_date.strftime('%Y-%m-%dT%H:%M:%SZ')
}
def show_feedback(kind, page):
"""Renders returned feedback."""
pagination = Feedback.query \
.filter_by(kind=Feedback.KINDS[kind]) \
.order_by(Feedback.pub_date.desc()) \
.paginate(page, app.config['FEEDBACK_PER_PAGE'])
return render_template('show_feedback.html', kind=kind,
pagination=pagination)
def get_challenge():
"""Return a random challenge"""
return rnd.randrange(1 << 32)
def challenge_response_accepted(challenge, response):
"""Simple check if a valid response for the challenge was provided."""
try:
response = int(response)
except ValueError:
return False
expected = int((challenge / 2.0) + (challenge / 3.0) - (challenge / 4.0))
return expected == response
@app.route('/', methods=['GET', 'POST'])
def give_feedback():
if request.method == 'POST':
challenge = session.pop('challenge', None)
kind = Feedback.KINDS.get(request.form['kind'])
text = request.form['feedback']
version = request.form['version']
if challenge and challenge_response_accepted(
challenge, request.form['response']) and \
kind is not None and len(text) <= 140 and len(version) <= 20:
feedback = Feedback(kind, text, version)
db.session.add(feedback)
db.session.commit()
return redirect(url_for('show_message', id=feedback.id))
return redirect(url_for('give_feedback'))
session['challenge'] = challenge = get_challenge()
return render_template('give_feedback.html', challenge=challenge)
@app.route('/message/<int:id>')
def show_message(id):
feedback = Feedback.query.get_or_404(id)
return render_template('show_message.html', feedback=feedback)
@app.route('/happy', defaults={'page': 1})
@app.route('/happy/page/<int:page>')
def happy(page):
return show_feedback('happy', page)
@app.route('/unhappy', defaults={'page': 1})
@app.route('/unhappy/page/<int:page>')
def unhappy(page):
return show_feedback('unhappy', page)
@app.route('/faq')
def faq():
return render_template('faq.html')
@app.route('/feedback.txt', defaults={'version': 'all'})
@app.route('/feedback-<version>.txt')
def export_text(version):
q = Feedback.query
if version != 'all':
q = q.filter_by(version=version)
messages = q.order_by(Feedback.pub_date).all()
return Response(u'\n'.join('[%s] %s: %s (Flask-%s)' % (
fb.kind_symbol,
fb.pub_date.strftime('%Y-%m-%dT%H:%M:%SZ'),
fb.text,
fb.version or 'unknown'
) for fb in messages), mimetype='text/plain')
@app.route('/feedback.json', defaults={'version': 'all'})
@app.route('/feedback-<version>.json')
def export_json(version):
q = Feedback.query
if version != 'all':
q = q.filter_by(version=version)
messages = q.order_by(Feedback.pub_date).all()
return jsonify(messages=[fb.to_json() for fb in messages])