This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
504 lines (444 loc) · 16.9 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
from flask import Flask, Markup
from flask import (
url_for,
request, redirect,
render_template,
send_from_directory,
)
# Flask extensions
from flask_api import FlaskAPI
from flask_flatpages import FlatPages
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import or_, desc
# Administration area
import flask_admin as admin
from flask_admin.contrib.sqla import ModelView
from flask_admin.contrib.fileadmin import FileAdmin
from flask_admin.form import ImageUploadField
# Data loading support
import geojson, json
from werkzeug import secure_filename
# Project formatting
import arrow
import markdown
MARKDOWN_EXT = ['markdown.extensions.tables']
# Gravatar
from urllib.parse import urlencode
import hashlib, codecs, datetime
import os.path as ospath
from os import urandom
try:
import helper
except:
from . import helper
# Create application
app = FlaskAPI(__name__, static_url_path='')
app.debug = True
app.config.from_pyfile('config.py')
app.jinja_env.add_extension('pypugjs.ext.jinja.PyPugJSExtension')
# TODO: move app to Blueprints
# TODO: move to Rest+ https://flask-restplus.readthedocs.io/
# version='1.0', title='Smart Use API',
# description='An API based on Frictionless Data standards')
db = SQLAlchemy(app)
migrate = Migrate(app, db)
pages = FlatPages(app)
# Various presets
with open(ospath.join(ospath.dirname(__file__), 'templates','presets','project-categories.json'), "r") as f:
project_categories = json.load(f)
screenshot_path = ospath.join(ospath.dirname(__file__), 'screenshots')
upload_path = ospath.join(ospath.dirname(__file__), 'uploads')
DEFAULT_THUMB = '/img/usermap.jpg'
# Create admin
admin = admin.Admin(app, name='SmartUse', template_mode='bootstrap3')
# -------- Models ---------------
class Organisation(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(128), nullable=False)
url = db.Column(db.Unicode(255))
logo = db.Column(db.Unicode(255))
def __repr__(self):
return self.name
def dict(self):
return {
'id': self.id,
'name': self.name,
'url': self.url,
'logo': self.logo
}
class License(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(256), unique=True, nullable=False)
name = db.Column(db.Unicode(256), unique=True, nullable=False, doc="Short, lowercase identifer")
path = db.Column(db.Unicode(2048), doc="Enter URL to the license")
def __repr__(self):
return self.title
def dict(self):
return {
'id': self.id,
'name': self.name,
'title': self.title,
'path': self.path or '',
}
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(128), unique=True, nullable=False)
created = db.Column(db.DateTime(), default=datetime.datetime.now())
updated = db.Column(db.DateTime(), default=datetime.datetime.now())
summary = db.Column(db.UnicodeText(255))
details = db.Column(db.UnicodeText)
category = db.Column(db.String(32), doc="review, labs, collect, present, publish")
organisation_id = db.Column(db.Integer, db.ForeignKey(Organisation.id))
organisation = db.relationship(Organisation,
backref=db.backref('project', single_parent=True))
license_id = db.Column(db.Integer, db.ForeignKey(License.id))
license = db.relationship(License,
backref=db.backref('project', single_parent=True))
screenshot = db.Column(db.String(256), doc="Use the Data tab to upload a screenshot")
is_hidden = db.Column(db.Boolean(), default=False)
is_featured = db.Column(db.Boolean(), default=False)
slug = db.Column(db.String(64), unique=True)
token_edit = db.Column(db.String(64), default=codecs.encode(urandom(12), 'hex').decode())
def thumb(self, as_thumbnail=True):
if not self.screenshot: return DEFAULT_THUMB
name, _ = ospath.splitext(self.screenshot)
if as_thumbnail:
return '/screenshots/' + secure_filename('%s_thumb.jpg' % name)
return '/screenshots/' + secure_filename('%s.jpg' % name)
def __repr__(self):
return self.title
@property
def url(self):
return request.host_url.rstrip('/') + url_for('project_page_by_slug', project_slug=self.slug)
@property
def detail_url(self):
return request.host_url.rstrip('/') + url_for('project_detail', project_id=self.id)
def dict(self):
d = {
'id': self.id,
'hidden': self.is_hidden,
'featured': self.is_featured,
'name': 'smartuse-%d' % self.id,
'text': self.title, 'title': self.title,
'screenshot': self.thumb(False),
'thumbnail': self.thumb(),
'date-created': self.created.strftime("%d.%m.%Y"),
'date-updated': self.updated.strftime("%d.%m.%Y"),
'summary': self.summary,
'url': self.url,
'path': self.detail_url,
'mediatype': "application/vnd.datapackage+json",
'format': helper.media_name('datapackage.json'),
}
if self.category:
d['category'] = self.category
if self.organisation:
d['organisation'] = self.organisation.name
return d
# Many-to-many relationship
projects_users = db.Table(
'projects_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('project_id', db.Integer(), db.ForeignKey('project.id'))
)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.Unicode(16), nullable=False)
fullname = db.Column(db.Unicode(128))
email = db.Column(db.Unicode(128))
phone = db.Column(db.Unicode(32))
notes = db.Column(db.UnicodeText)
biography = db.Column(db.UnicodeText)
organisation_id = db.Column(db.Integer, db.ForeignKey(Organisation.id))
organisation = db.relationship(Organisation,
backref=db.backref('user', single_parent=True))
projects = db.relationship(Project, secondary=projects_users,
backref=db.backref('users', lazy='dynamic'))
def __repr__(self):
return self.username
def gravatar(self):
gr_size = 80
if self.email == "": return "/img/usericon.png"
email = self.email.lower().encode('utf-8')
gravatar_url = "https://www.gravatar.com/avatar/"
gravatar_url += hashlib.md5(email).hexdigest() + "?"
gravatar_url += urlencode({'s':str(gr_size)})
return gravatar_url
def dict(self):
d = {
'id': self.id,
'username': self.username,
'fullname': self.fullname,
'gravatar': self.gravatar(),
}
if not self.organisation is None:
d['organisation'] = self.organisation.dict()
return d
class Source(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(256), unique=True, nullable=False)
path = db.Column(db.Unicode(2048), doc="Enter URL to the data source")
fmt = db.Column(db.String(32))
organisation_id = db.Column(db.Integer, db.ForeignKey(Organisation.id))
organisation = db.relationship(Organisation,
backref=db.backref('source', single_parent=True))
license_id = db.Column(db.Integer, db.ForeignKey(License.id))
license = db.relationship(License,
backref=db.backref('source', single_parent=True))
def __repr__(self):
return self.title
def dict(self):
d = {
'id': self.id,
'title': self.title,
'path': self.path or '',
'format': helper.media_name(self.path, self.fmt),
'mediatype': helper.media_mime(self.path, self.fmt),
}
if not self.organisation is None:
d['organisation'] = self.organisation.dict()
if not self.license is None:
d['license'] = self.license.dict()
return d
# Many-to-many relationship
projects_renderings = db.Table(
'projects_renderings',
db.Column('project_id', db.Integer(), db.ForeignKey('project.id')),
db.Column('rendering_id', db.Integer(), db.ForeignKey('rendering.id'))
)
sources_renderings = db.Table(
'sources_renderings',
db.Column('source_id', db.Integer(), db.ForeignKey('source.id')),
db.Column('rendering_id', db.Integer(), db.ForeignKey('rendering.id'))
)
class Rendering(db.Model):
id = db.Column(db.Integer, primary_key=True)
order = db.Column(db.Integer)
title = db.Column(db.String(128), unique=True, nullable=False)
description = db.Column(db.UnicodeText)
path = db.Column(db.Unicode(2048),
doc="Provide a URL here, use the Data tab to upload files")
projects = db.relationship('Project', secondary=projects_renderings,
backref=db.backref('renderings', lazy='dynamic'))
sources = db.relationship('Source', secondary=sources_renderings,
backref=db.backref('renderings', lazy='dynamic'))
def __repr__(self):
return self.title
def dict(self):
content = ''
if self.description:
try:
content = Markup(markdown.markdown(self.description, extensions=MARKDOWN_EXT))
except Exception as e:
print(e)
return {
'id': self.id,
'title': self.title,
'name': helper.slugify(self.title),
'description': content,
'mediatype': helper.media_mime(self.path),
'format': helper.media_name(self.path),
'path': self.path or '',
'sources': [r.dict() for r in self.sources]
}
# ----------- Admin views -----------
UserView = ModelView(User, db.session, name="Users")
UserView.column_list = ('username', 'fullname', 'organisation')
admin.add_view(UserView)
admin.add_view(ModelView(Organisation, db.session, name="Organisations"))
class ProjectView(ModelView):
column_list = ('title', 'created', 'updated', 'category')
form_excluded_columns = ('slug', 'token_edit')
form_extra_fields = {
'screenshot': ImageUploadField('Screenshot',
base_path=screenshot_path, url_relative_path='/screenshots/',
thumbnail_size=(256, 256, True))
}
inline_models = [Rendering]
can_export = True
def on_model_change(view, form, model, is_created):
model.slug = helper.slugify(form.title.data)
admin.add_view(ProjectView(Project, db.session, name="Projects (Data Packages)"))
class RenderingView(ModelView):
column_list = ('title', 'path')
can_export = True
admin.add_view(RenderingView(Rendering, db.session, name="Renderings (Views)"))
admin.add_view(ModelView(Source, db.session, name="Data sources"))
admin.add_view(ModelView(License, db.session, name="Licenses"))
admin.add_view(FileAdmin(upload_path, '/uploads/', name="Uploads"))
# API views
@app.route("/api/projects", methods=['GET'])
def projects_list():
return [p.dict() for p in Project.query
.filter_by(is_hidden=False,is_featured=False)
.limit(24).all()]
@app.route("/api/projects/featured", methods=['GET'])
def projects_list_featured():
return [p.dict() for p in Project.query
.filter_by(is_hidden=False,is_featured=True)
.limit(6).all()]
@app.route("/api/projects/all", methods=['GET'])
def projects_list_all():
return [p.dict() for p in Project.query
.filter_by(is_hidden=False)
.order_by(Project.category)
.limit(60).all()] # TODO: pagination
@app.route("/api/projects/by/<string:BY_CAT>", methods=['GET'])
def projects_list_by_category(BY_CAT):
return [p.dict() for p in Project.query
.filter_by(is_hidden=False,category=BY_CAT)
.limit(24).all()]
@app.route('/api/projects/search', methods=['GET'])
def projects_search():
q = request.args.get('q')
if not q or len(q.strip()) < 3: return []
q = '%' + q.strip() + '%'
return [p.dict() for p in Project.query
.filter_by(is_hidden=False)
.filter(or_(
Project.title.ilike(q),
Project.details.ilike(q),
Project.summary.ilike(q),
)).limit(60).all()]
@app.route("/api/organisations", methods=['GET'])
def organisations_list():
return [o.dict() for o in Organisation.query.limit(10).all()]
@app.route("/api/project/<int:project_id>", methods=['GET'])
def project_detail(project_id):
project = Project.query.filter_by(id=project_id).first_or_404()
renderings = project.renderings.order_by(Rendering.order).all()
d = {
'data': project.dict(),
'details': project.details,
'renderings': [r.dict() for r in renderings],
}
author = project.users.first()
if author is not None: d['author'] = author.dict()
license = project.license
if license is not None: d['license'] = license.dict()
return d
def get_file(filename):
f = open(ospath.join(
ospath.dirname(__file__),
'content',
'top',
filename
), 'r')
return f.read()
def get_md(filename):
t = get_file('%s.md' % filename)
return Markup(markdown.markdown(t))
# Flask views
@app.route('/about')
def index_about():
return render_template('public/about.pug',
intro=get_md('about-intro'),
report=get_md('about-report'),
logos=get_file('logos.html'),
impressum=get_file('impressum.html'),
)
@app.route('/contact')
def index_contact():
return render_template('public/contact.pug',
participate=get_md('contact-participate'),
impressum=get_file('impressum.html'),
)
@app.route('/legal')
def index_legal():
return render_template('public/legal.pug',
content=get_md('legal'),
impressum=get_file('impressum.html'),
)
@app.route('/faq')
def index_faq():
return render_template('public/faq.pug')
@app.route('/search')
def index_search():
return render_template('public/search.pug')
@app.route('/labs')
def index_labs():
return render_template('public/labs.pug',
content=get_md('labs-about'),
)
@app.route('/studies')
def index_studies():
return render_template('public/studies.pug',
studies=get_md('home-studies'),
)
@app.route('/qr')
def index_qr():
return render_template('public/qr.pug',
studies=get_md('home-qr'),
)
@app.route('/')
def index_root():
return render_template('public/home.pug',
headline=get_md('home-headline'),
studies=get_md('home-studies'),
about=get_md('home-about'),
)
@app.route('/p/<path:path>/')
def flat_page(path):
page = pages.get_or_404(path)
content = Markup(markdown.markdown(page.body))
return render_template('public/page.pug', page=page, content=content)
@app.route("/project/<project_slug>")
def project_page_by_slug(project_slug):
return project_page(Project.query.filter_by(slug=project_slug).first_or_404())
@app.route("/project/<int:project_id>")
def project_page_by_id(project_id):
return project_page(Project.query.filter_by(id=project_id).first_or_404())
def project_page(project):
content = Markup(markdown.markdown(project.details, extensions=MARKDOWN_EXT))
meta = project.dict()
created = arrow.get(meta['date-created'], 'DD.MM.YYYY').humanize()
updated = arrow.get(meta['date-updated'], 'DD.MM.YYYY').format('DD.MM.YYYY')
category = None
if project.category in project_categories:
category = project_categories[project.category]
category['class'] = 'fas fa-' + category['icon']
# version = 1.2
organisation = project.organisation
authors = [author.dict() for author in project.users]
renderings = sorted([res.dict() for res in project.renderings],
key=lambda res: res['id'])
return render_template('public/project.pug', **locals())
# Static paths
@app.route('/img/<path:path>')
def send_static_img(path):
return send_from_directory('./static/img', path)
@app.route('/scripts/<path:path>')
def send_static_scripts(path):
return send_from_directory('./static/scripts', path)
@app.route('/styles/<path:path>')
def send_static_styles(path):
return send_from_directory('./static/styles', path)
@app.route('/tags/<path:path>')
def send_static_tags(path):
return send_from_directory('./static/tags', path)
@app.route('/vendor/<path:path>')
def send_static_vendor(path):
return send_from_directory('./static/vendor', path)
@app.route('/meta/<path:path>')
def send_static_meta(path):
return send_from_directory('./static/meta', path)
@app.route('/theme/<path:path>')
def send_static_theme(path):
return send_from_directory('./static/theme', path)
@app.route('/screenshots/<path:path>')
def send_screenshots(path):
return send_from_directory('./screenshots', path)
# Data serving paths
@app.route('/uploads/<path:path>')
@app.route('/api/project/<int:pid>/<path:path>')
def send_uploads(path):
return send_from_directory('./uploads', path)
# Error paths
@app.errorhandler(404)
def page_not_found(e):
return render_template('public/404.pug'), 404
if __name__ == '__main__':
db.create_all()
# Start app
app.run(debug=True)