forked from certsocietegenerale/fame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
executable file
·170 lines (125 loc) · 4 KB
/
webserver.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
#!/usr/bin/env python2
import os
import jinja2
from json import dumps
from bson import ObjectId
from datetime import datetime
from flask import Flask, redirect, request, url_for
from flask_login import LoginManager
from werkzeug import url_encode
from importlib import import_module
from fame.core import fame_init
from fame.core.user import User
from fame.common.config import fame_config
from fame.common.constants import AVATARS_ROOT
from web.views.files import FilesView
from web.views.analyses import AnalysesView
from web.views.modules import ModulesView
from web.views.search import SearchView
from web.views.configs import ConfigsView
from web.views.users import UsersView
from web.views.helpers import user_if_enabled
try:
fame_init()
except:
print "/!\\ Could not connect to MongoDB database."
app = Flask(__name__, template_folder='web/templates', static_folder='web/static')
app.secret_key = fame_config.secret_key
app.config['TEMPLATES_AUTO_RELOAD'] = True
# Set two tempalte folders (one is for modules)
template_loader = jinja2.ChoiceLoader([
jinja2.FileSystemLoader('web/templates'),
jinja2.FileSystemLoader('fame/modules'),
])
app.jinja_loader = template_loader
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = '/login'
auth_module = import_module('web.auth.{}.views'.format(fame_config.auth))
app.register_blueprint(auth_module.auth)
@login_manager.user_loader
def load_user(user_id):
return user_if_enabled(User.get(_id=ObjectId(user_id)))
@login_manager.token_loader
def token_loader(token):
return user_if_enabled(User.get(auth_token=token))
@login_manager.request_loader
def api_auth(request):
api_key = request.headers.get('X-API-KEY')
user = User.get(api_key=api_key)
if user:
user.is_api = True
return user_if_enabled(user)
# Template Filters
@app.template_filter('date')
def filter_date(value, format='%Y-%m-%d %H:%M'):
return value.strftime(format)
@app.template_filter()
def to_json(value):
return dumps(value, indent=2)
@app.template_filter()
def smart_join(value, separator=", "):
if value is None:
return ''
if isinstance(value, basestring):
return value
else:
return separator.join(value)
@app.template_filter()
def form_value(value):
if value is None:
return ''
else:
return value
@app.template_filter()
def timesince(dt, default="just now"):
"""
Returns string representing "time since" e.g.
3 days ago, 5 hours ago etc.
"""
now = datetime.now()
diff = now - dt
periods = (
(diff.days / 365, "year", "years"),
(diff.days / 30, "month", "months"),
(diff.days / 7, "week", "weeks"),
(diff.days, "day", "days"),
(diff.seconds / 3600, "hour", "hours"),
(diff.seconds / 60, "minute", "minutes"),
(diff.seconds, "second", "seconds"),
)
for period, singular, plural in periods:
if period:
return "%d %s ago" % (period, singular if period == 1 else plural)
return default
@app.template_filter()
def unique(l):
return list(set(l))
@app.template_filter()
def avatar(user_id):
if os.path.exists(os.path.join(AVATARS_ROOT, "{}.png".format(user_id))):
return url_for('static', filename="img/avatars/{}.png".format(user_id))
else:
return url_for('static', filename="img/avatars/default.png")
@app.template_global()
def delete_query(*new_values):
args = request.args.copy()
for key in new_values:
del args[key]
return '{}?{}'.format(request.path, url_encode(args))
@app.template_global()
def modify_query(key, value):
args = request.args.copy()
args[key] = value
return '{}?{}'.format(request.path, url_encode(args))
@app.route('/')
def root():
return redirect('/analyses')
FilesView.register(app)
AnalysesView.register(app)
ModulesView.register(app)
SearchView.register(app)
ConfigsView.register(app)
UsersView.register(app)
if __name__ == '__main__':
app.run(debug=True, port=4200, host="0.0.0.0")