forked from sigurdga/samklang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
393 lines (348 loc) · 14.1 KB
/
app.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python
# coding: utf-8
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, jsonify, session, g, flash
from werkzeug import secure_filename
from flask.ext.mongoengine import MongoEngine
from flask_mail import Mail, Message
from flask.ext.babel import Babel, format_datetime, format_date, format_time
from flask.ext.babel import gettext as _
from models import Site, File, Menu, MenuLink, LoginToken
from utils import save_file
from blog import blog
from pages import pages
from portfolio import portfolio
from personnel import personnel
from hashlib import sha1
app = Flask(__name__, instance_relative_config=True)
app.register_blueprint(blog)
app.register_blueprint(portfolio)
app.register_blueprint(personnel)
app.register_blueprint(pages)
app.config.from_object('config')
app.config.from_pyfile('config.cfg', silent=True)
db = MongoEngine(app)
mail = Mail(app)
babel = Babel(app)
app.jinja_env.filters['datetime'] = format_datetime
app.jinja_env.filters['date'] = format_date
app.jinja_env.filters['time'] = format_time
import logging
file_handler = logging.FileHandler(app.config.get("LOGFILE", 'samklang.log'))
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
admins = app.config.get("ADMINS", [])
error_sender = app.config.get("MAIL_ERROR_SENDER", "no-reply@localhost")
smtp_server = app.config.get("MAIL_SERVER", "localhost")
if admins and error_sender and smtp_server and not app.debug:
recipients = []
for admin in admins:
if isinstance(admin, tuple):
recipients.append("%s <%s>" % admin)
else:
recipients.append(admin)
import logging
from logging.handlers import SMTPHandler
mail_handler = SMTPHandler(smtp_server, error_sender, recipients,
_('Samklang Error'))
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
@babel.localeselector
def get_locale():
# if a user is logged in, use the locale from the user settings
#user = getattr(g, 'user', None)
#if user is not None:
#return user.locale
# otherwise try to guess the language from the user accept
return request.accept_languages.best_match(['en', 'nb'])
@babel.timezoneselector
def get_timezone():
#user = getattr(g, 'user', None)
#if user is not None:
#return user.timezone
pass
@app.before_request
def add_site():
g.site = Site.get_by_hostname(request.host, app.config.get("DOMAIN_ROOT"))
if g.site is None:
if not app.config.get("DOMAIN_ROOT"):
return _("Config is missing a DOMAIN_ROOT: the domain name of your "
"main site.")
if not app.config.get("ADMINS"):
return _("Config is missing ADMINS: used for login to your "
"first site and recipient of error messages.")
name = app.config.get("ROOT_SITE_NAME", _("Your new site"))
description = _("This is your new site. This is also your root site, "
"and this can be used to create other sites.")
root_site, created = Site.objects.get_or_create(
domain=app.config.get("DOMAIN_ROOT"),
defaults={
"name": name,
"description" :"<h1>%s</h1><p>%s</p>" % (
name, description),
"owner_email": None,
"verified_email": True
})
port = app.config.get("PORT", None)
url = "//%s" % root_site.domain
if port:
url += ":%d" % port
if created:
return redirect(url)
else:
return redirect("%s%s" % (url, url_for("sites")))
g.user = session.get("username", None)
if "menu" in g.site.active_modules:
g.menu, created = Menu.objects.get_or_create(site=g.site.domain)
else:
g.menu = None
# fake login
@app.route("/login", methods=["POST", "GET"])
def login():
if not g.site.verified_email:
#flash("You should set up a verified email address first.")
#return redirect(url_for("add_email"))
# log in without security
flash(
_('Everybody can log in to your site and do changes until you '
'add an <a href="%(url)s">owner email address</a>.',
url=url_for("add_email")),
'secondary')
session["username"] = g.site.domain
return redirect(url_for("index"))
if request.method == "POST":
email = request.form.get("email", None)
admin_found = False
if not g.site.owner_email and g.site.domain == app.config.get("DOMAIN_ROOT"):
for admin in app.config.get("ADMINS"):
if email == admin[1]:
admin_found = True
break
if email != g.site.owner_email and not admin_found:
flash(
_("That email address (%(email)s) does not match the owner "
"email address of this domain",
email=email))
return redirect(url_for("login"))
root_domain = app.config.get("DOMAIN_ROOT")
port = app.config.get("PORT", None)
code = sha1()
code.update(g.site.domain)
code.update(email)
code.update(app.config["SECRET_KEY"])
token = LoginToken()
token.site = g.site.domain
token.code = code.hexdigest()
token.save()
host = g.site.domain
if root_domain:
host += ".%s" % root_domain
if port:
host += ":%d" % port
message = Message(_("Log in to %(host)s", host=g.site.domain),
recipients=[email],
)
message.body = _(
"Log in to samklang by clicking the following link:\r\n\r\n"
"http://%(host)s%(url)s\r\n\r\nKthxbye\r\n",
host=host,
url=url_for(
'email_login',
verification_code=code.hexdigest()
),
)
mail.send(message)
flash(_(
"You should soon get an email soon. Click the link inside it to log in."
))
return redirect(url_for("index"))
return render_template("login.html")
@app.route("/logout")
def logout():
session.pop('username', None)
return redirect(url_for('index'))
@app.route("/admin/email/add", methods=["POST", "GET"])
def add_email():
if request.method == "POST":
root_domain = app.config.get("DOMAIN_ROOT")
port = app.config.get("PORT", None)
email = request.form.get("email", None)
if email:
g.site.owner_email = email
g.site.save()
code = sha1()
code.update(g.site.domain)
code.update(g.site.owner_email)
code.update(app.config["SECRET_KEY"])
# TODO; Add some timing requirements as well
host = g.site.domain
if root_domain:
host += ".%s" % root_domain
if port:
host += ":%d" % port
message = Message(_("Verify your email"),
recipients=[email],
)
message.body = _(
"Thanks for trying Samklang\r\n\r\n"
"Click the following link to verify that this address worked:\r\n"
"http://%(host)s%(url)s\r\n\r\nKthxbye\r\n",
host=host,
url=url_for('email_verify', verification_code=code.hexdigest()))
mail.send(message)
flash(_("You should soon get an email soon. Click the link inside"
" it to verify the address worked."))
return redirect(url_for("index"))
else:
if g.site.verified_email and "username" not in session:
return _("Email already set, you need to log in using the old email"
" address before you can set a new email address.")
return render_template("add_email.html")
@app.route("/login/<verification_code>")
def email_login(verification_code):
try:
LoginToken.objects.get(code=verification_code, site=g.site.domain)
# TODO: add time limit check on previous line
except:
return _("The verification code is wrong or too old")
else:
session["username"] = g.site.domain
flash(_("You are now logged in"))
return redirect(url_for('index'))
@app.route("/admin/keep/<verification_code>")
def email_verify(verification_code):
code = sha1()
code.update(g.site.domain)
code.update(g.site.owner_email)
code.update(app.config["SECRET_KEY"])
if verification_code == code.hexdigest():
g.site.verified_email = True
g.site.save()
session["username"] = g.site.domain
flash(_("Your site is now verified and you are logged in"))
return redirect(url_for('index'))
else:
return _("The verification code is wrong")
@app.route("/sites", methods=["POST", "GET"])
def sites():
root_domain = app.config.get("DOMAIN_ROOT")
port = app.config.get("PORT", None)
if request.method == "POST":
domain = request.form["domain"]
name = request.form["name"]
if domain and name:
site = Site()
site.name = name.strip()
site.domain = domain.strip()
conflicting_site = Site.objects.filter(domain=site.domain).first()
if not conflicting_site:
site.description = _("<h1>Introductory text</h1><p>This should "
"contain simple help about what is changeable and how."
"</p>")
site.save()
url = "//%s" % site.domain
if root_domain:
url += ".%s" % root_domain
if port:
url += ":%d" % port
return redirect(url)
return redirect(url_for("sites"))
if root_domain == g.site.domain:
sites = Site.objects.all()
else:
sites = Site.objects.filter(domain__endswith=g.site.domain)
return render_template("sites.html", sites=sites, root_domain=root_domain, port=port)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/edit", methods=["POST", "GET"])
def edit():
site = g.site
if request.method == "POST" and g.site.domain == g.user:
if request.files["header_image"]:
site.header_image, length = save_file(request.files["header_image"], app.config["UPLOAD_FOLDER"])
site.footers = []
footers = request.form.getlist("footer")
for footer in footers:
if footer:
site.footers.append(footer)
site.name = request.form["name"]
site.description = request.form["description"]
site.save()
return redirect(url_for("index"))
return render_template('edit.html')
@app.route("/media/<path:filename>")
@app.route("/media/<path:filename>.<suffix>")
def media(filename, suffix=None):
if suffix:
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
else:
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
@app.route("/files/delete/<id>", methods=["POST"])
def files_delete(id):
if g.site.domain == g.user:
f = File.objects.get(id=id)
f.delete()
return jsonify(status=True)
else:
return jsonify(status=False)
@app.route("/files/", methods=["POST", "GET"])
def files():
if request.method == "POST" and g.site.domain == g.user:
reqfile = request.files['file']
f = File()
f.site = g.site.domain
f.name = reqfile.filename
f.slug = secure_filename(f.name)
f.content_type = reqfile.mimetype
f.slug, f.content_length = save_file(reqfile, app.config["UPLOAD_FOLDER"])
f.save()
files = File.objects(site=g.site.domain)
return render_template('files.html', files=files)
@app.route("/a/l/", methods=["POST", "GET"])
def menu():
if "menu" in g.site.active_modules:
menu = g.menu
if request.method == "POST" and g.site.domain == g.user:
if "username" in session and g.user == g.site.domain:
texts = request.form.getlist("text")
links = request.form.getlist("link")
menu.links = []
for text, link in zip(texts, links):
if text and link:
menu.links.append(MenuLink(text=text, link=link))
simple = request.form.get("simple", False)
print simple
if simple:
menu.simple = True
else:
menu.simple = False
menu.text_color = request.form.get("text_color", None)
menu.active_text_color = request.form.get("active_text_color", None)
menu.hover_text_color = request.form.get("hover_text_color", None)
menu.background_color = request.form.get("background_color", None)
menu.active_background_color = request.form.get("active_background_color", None)
menu.hover_background_color = request.form.get("hover_background_color", None)
menu.save()
return redirect(url_for("index"))
return render_template("menu.html")
@app.route("/a/m/", methods=["POST", "GET"])
def modules():
available_modules = ["menu", "pages", "blog", "portfolio", "personnel"]
if request.method == "POST" and g.user == g.site.domain:
site_changed = False
for module in available_modules:
module_active = request.form.get("%s_active" % module)
if module_active:
if not module in g.site.active_modules:
g.site.active_modules.append(module)
site_changed = True
else:
if module in g.site.active_modules:
g.site.active_modules.remove(module)
site_changed = True
if site_changed:
g.site.save()
return redirect(url_for("modules"))
return render_template("modules.html", modules=available_modules)
if __name__ == "__main__":
app.run()