/
diary.py
executable file
·301 lines (239 loc) · 8.2 KB
/
diary.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python2.7
from flask import Flask, request, Response, render_template, redirect, \
url_for, abort, flash
from flask.views import MethodView
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.wtf import Form
from flask.ext.login import LoginManager, login_user, logout_user, \
current_user, login_required
from flask.ext.bcrypt import Bcrypt
from flask.ext.script import Manager
from jinja2.filters import do_mark_safe as safe
from wtforms import fields, validators, ValidationError
from markdown import markdown
from datetime import datetime, date, time
from functools import wraps
import json
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_pyfile('config.py', silent=True)
db = SQLAlchemy(app)
login = LoginManager()
login.setup_app(app)
bcrypt = Bcrypt(app)
manager = Manager(app)
class User(db.Model):
"""The user class is used to store a email/password combination
and determine which entries belong to which user. This makes it
possible to have multiple users use the same diary instance."""
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(30), unique=True)
password = db.Column(db.String(60))
authenticated = db.Column(db.Boolean())
active = db.Column(db.Boolean())
def __init__(self, username, password):
self.username = username
self.password = bcrypt.generate_password_hash(password)
self.active = True
self.authenticated = False
def authenticate(self, password):
return bcrypt.check_password_hash(self.password, password)
def is_authenticated(self):
return self.authenticated
def is_active(self):
return self.active
def is_anonymous(self):
return False
def get_id(self):
return self.id
class Entry(db.Model):
"""The main content class. Every entry in every diary consists
of an entry instance with optional attachments."""
id = db.Column(db.Integer, primary_key=True)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
date = db.Column(db.Date)
time = db.Column(db.Time)
title = db.Column(db.String(255))
markup = db.Column(db.Text)
html = db.Column(db.Text)
public = db.Column(db.Boolean)
def __html__(self):
"""Enable the entry to be used as {{ entry }} in templates. This
property will return the html body of the entry in that case."""
return self.html
Entry.owner = db.relationship('User', backref=db.backref('entry', \
lazy='dynamic'))
class Attachment(db.Model):
"""This class is carrying the attachments. It is always connected to
entries."""
id = db.Column(db.Integer, primary_key=True)
entry_id = db.Column(db.Integer, db.ForeignKey('entry.id'))
filename = db.Column(db.String(255))
Attachment.entry = db.relationship('Entry', backref=db.backref('entry', \
lazy='dynamic'))
class SignUpForm(Form):
username = fields.TextField('Username')
password = fields.PasswordField('Password', [validators.Required()])
confirm = fields.PasswordField('Confirm', [validators.Required()])
def validate_username(form, field):
used = User.query.filter_by(username=form.data['username']).first()
if used:
raise ValidationError('Username is in use already.')
def validate_confirm(form, field):
if not form.data['password'] == form.data['confirm']:
raise ValidationError('The password do not match.')
class LoginForm(Form):
username = fields.TextField('Username')
password = fields.PasswordField('Password')
def validate_username(form, field):
user = User.query.filter_by(username=form.data['username']).first()
if not user:
raise ValidationError('Username does not exist.')
def validate_password(form, field):
user = User.query.filter_by(username=form.data['username']).first()
if not user or not user.authenticate(form.data['password']):
raise ValidationError('Username and password do not match.')
class EntryForm(Form):
title = fields.TextField('Title')
markup = fields.TextAreaField('Markup', [validators.Required()])
public = fields.BooleanField('Make this entry public')
@login.user_loader
def load_user(userid):
return User.query.get(userid)
def render_bs_input(input, disabled=False, override=None, **param):
return safe(render_template('bootstrap_input.html', input=input, \
disabled=disabled, override=override, param=param))
@app.context_processor
def inject_tools():
return dict(render_bs_input=render_bs_input)
def auth_check(username, password):
user = User.query.filter_by(username=username).first()
if not user or not user.authenticate(password):
return False
else:
return True
def auth_missing():
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not auth_check(auth.username, auth.password):
return auth_missing()
return f(*args, **kwargs)
return decorated
@app.route('/signup/', methods=['GET', 'POST'])
def signup():
form = SignUpForm()
if form.validate_on_submit():
user = User(form.data['username'], form.data['password'])
db.session.add(user)
db.session.commit()
user.authenticated = True
login_user(user)
db.session.commit()
flash('Thank you for signing up. You have been logged in already!', \
'success')
return redirect(url_for('index'))
else:
return render_template('signup.html', form=form)
@login.unauthorized_handler
@app.route('/about/')
def about():
return render_template('about.html')
@app.route('/login/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.data['username']).first()
login_user(user)
user.authenticated = True
db.session.commit()
flash('You were logged in successfully.', 'success')
return redirect(url_for('index'))
else:
return render_template('login.html', form=form)
@app.route('/settings/', methods=['GET', 'POST'])
@login_required
def settings():
form = SettingsForm()
pass
@app.route('/logout/')
@login_required
def logout():
logout_user()
current_user.authenticated = False
db.session.commit()
flash('You have been logged out.', 'success')
return redirect(url_for('index'))
@app.route('/')
@login_required
def index():
days = Entry.query.filter_by(owner_id=current_user.id) \
.order_by(db.desc('date')).group_by('date').all()
i = 0
for day in days:
days[i] = {'date': day.date, \
'entries': Entry.query.filter_by(owner_id=current_user.id, \
date=day.date).order_by(db.desc('time')).all()}
i = i+1
return render_template('index.html', entries=days, user=current_user)
@app.route('/view/<int:entryid>/')
@login_required
def view(entryid):
entry = Entry.query.get_or_404(entryid)
if not entry.owner_id == current_user.id:
return abort(403)
return render_template('view.html', entry=entry)
@app.route('/submit/', methods=['GET', 'POST'])
@login_required
def submit():
form = EntryForm()
if form.validate_on_submit():
entry = Entry()
form.populate_obj(entry)
entry.html = markdown(entry.markup, safe_mode="remove")
now = datetime.now()
entry.owner_id = current_user.id
entry.date = now.date()
entry.time = now.time()
db.session.add(entry)
db.session.commit()
flash('Your entry has been submitted.', 'success')
return redirect(url_for('index'))
else:
return render_template('submit.html', form=form)
@app.route('/edit/<int:entryid>/', methods=['GET', 'POST'])
@login_required
def edit(entryid):
entry = Entry.query.get_or_404(entryid)
if not current_user.id == entry.owner_id:
abort(403)
form = EntryForm(obj=entry)
if form.validate_on_submit():
form.populate_obj(entry)
entry.html = markdown(entry.markup)
db.session.commit()
flash('Your entry has been edited.', 'success')
return redirect(url_for('index'))
else:
return render_template('edit.html', form=form, entry=entry)
@app.route('/delete/<int:entryid>/')
@login_required
def delete(entryid):
entry = Entry.query.get_or_404(entryid)
if not current_user.id == entry.owner_id:
abort(403)
db.session.delete(entry)
db.session.commit()
flash('Your entry has been deleted.', 'success')
return redirect(url_for('index'))
@manager.command
def initdb():
"""Initiate all tables in the database."""
db.create_all()
if __name__ == '__main__':
manager.run()