blob: d71c094213df8b10217e13944f2cd8dccd9e13de [file] [log] [blame]
Akronca9bd982016-12-06 16:59:57 +01001from functools import wraps
2import json
3
4from werkzeug.utils import redirect
5
6import APIFactory
7from app import user
8import config
9from models import UserEncoder
10import providers
11
12
13__author__ = 'hanl'
14
15from flask import Flask, render_template, request, session, url_for, flash, current_app, Blueprint
16from flask_email import LocmemMail
17from flask_babel import Babel
18from flask_login import LoginManager, current_user
19
20app = Flask(__name__)
21
22lm = LoginManager(app)
23babel = Babel(app)
24# admin = Admin(app)
25mail = LocmemMail(app)
26
27router = Blueprint('app', __name__,
28 template_folder='templates')
29
30
31@router.route('/lang/<language>')
32def set_locale(lang=None):
33 setattr(session, 'lang', lang)
34 return render_template(url_for('index'), lang_code=lang)
35
36
37@babel.localeselector
38def get_locale():
39 browser_locale = request.accept_languages.best_match(app.config.get('LANGUAGES'), default="de")
40 lang = session.get('lang', browser_locale)
41 setattr(session, 'lang', lang)
42 return lang
43
44
45def secured(func):
46 '''
47 overriding login_required, but redirects to login, not to unauthorized page
48 :param func:
49 :return:
50 '''
51
52 @wraps(func)
53 def decorated_view(*args, **kwargs):
54 print "args %s" % str(args)
55 print "kwargs %s" % str(kwargs)
56 print "func %s" % str(func)
57 if current_app.login_manager._login_disabled:
58 return func(*args, **kwargs)
59 elif not current_user.is_authenticated():
60 # fixme: how to set next?
61 # request.args.get('next') or
62 return redirect(url_for('.login'))
63 return func(*args, **kwargs)
64
65 return decorated_view
66
67
68@lm.user_loader
69def load_user(username):
70 # either from session of via rest; cache if possible
71 return providers.PROVIDER.get_user(session)
72
73
74# used to override the cookie encryption behaviour
75# @lm.token_loader
76def load_token(token):
77 return providers.PROVIDER.get_user()
78
79
80@router.route('/')
81def index():
82 return render_template('index.html', lang_code=get_locale())
83
84
85@router.route('/search', methods=['GET'])
86@secured
87def search():
88 if request.method == "GET":
89 data = request.args
90 print "has query %s" % str(data)
91 if 'q' in data:
92 # auth=APIFactory.Oauth2Auth(session['access_token'])
93 response = APIFactory.get("search", params=data)
94 if response is None:
95 pass
96 elif providers.NotificationHandler.isError(response):
97 providers.NotificationHandler.notify(response.json(), flash)
98 else:
99 raw_json = response.json()
100 return render_template('search.html', lang=app.config.get('QUERY_LANGUAGES'),
101 q=data['q'], ql=data['ql'], result=raw_json,
102 result_string=json.dumps(raw_json, indent=4))
103 return render_template('search.html', lang=app.config.get('QUERY_LANGUAGES'),
104 q=None, ql=None,
105 result=None)
106
107
108@router.route('/serialize', methods=['GET', 'POST'])
109@secured
110def serialize():
111 return render_template('search.html')
112
113
114# @router.app_errorhandler(404)
115def page_not_found(e):
116 return render_template('404.html'), 404
117
118
119if __name__ == '__main__':
120 import admin
121
122 app.config.from_object(config)
123 app.json_encoder = UserEncoder
124 app.debug = config.DEBUG
125 app.register_blueprint(router, url_prefix='/app')
126 app.register_blueprint(user.router, url_prefix='/user')
127 app.register_blueprint(admin.router, url_prefix='/admin')
128 providers.init_app(app.config)
129 app.run()
130
131