This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (64 loc) · 2.72 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
import flask
from flask import request, send_from_directory
from flask_sqlalchemy import SQLAlchemy
from ghdb import db, get_ghu_page, get_ghu_total
from flask_caching import Cache
from flask_paginate import Pagination, get_page_args
import os
def create_app():
'''Create app with sqlalchemy (without events) and simple cache.'''
app = flask.Flask(__name__, template_folder='templates')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///clidata.sqlite3'
app.config['SECRET_KEY'] = "aa20ff7c-8ad5-44a7-b0c3-8060b126e186"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # disable sqlalchemy events
app.config['CACHE_TYPE'] = "SimpleCache"
app.config['CACHE_DEFAULT_TIMEOUT']= 300
return app
app = create_app()
db=SQLAlchemy(app)
cache = Cache(app)
@app.route('/favicon.ico')
def favicon():
'''Returns the favicon.'''
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/', methods=['GET'])
@cache.cached(timeout=10, query_string=True)
def main():
'''Displays a specific `page` with `total` users'''
total=get_ghu_total()
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='total')
pagination = Pagination(page=page, per_page=per_page, total=total)
data = get_ghu_page(page, per_page)
user_list = data[0]
total_pages = data[1]
return(flask.render_template('main.html', user_list=user_list, total_pages=total_pages,
total=total,
page=page,
per_page=per_page,
pagination=pagination))
@app.route('/all', methods=['POST', 'GET'])
@cache.cached(timeout=10, query_string=True)
def all_users():
'''JSON response with all users.'''
total = get_ghu_total()
username = request.args.get('username')
order_by = request.args.get('order_by')
if request.args.get('page') is None and request.args.get('total') is None:
data = get_ghu_page(username=username)
else:
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='total')
pagination = Pagination(page=page, per_page=per_page, total=total)
data = get_ghu_page(page, per_page, username=username, order_by=order_by)
user_list = data[0]
# total_pages = data[1]
user_list = [o.serialize() for o in user_list]
return flask.json.jsonify(user_list)
@app.route('/dt', methods=['GET'])
def show_datatable():
'''Renders datatable.'''
return flask.render_template('datatable.html')
if __name__ == '__main__':
app.run(debug=True)