/
server.py
205 lines (134 loc) · 5.48 KB
/
server.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
"""
fichier server app oeasc
"""
import json
import re
from flask import Flask, redirect, session, request, url_for, send_from_directory
from jinja2 import evalcontextfilter, Markup, escape
from app.utils.env import DB, mail
from config import config
from flask_cors import CORS
# Configure sentry if var SENTRY_DSN is set in config
try:
sentry_config = config.SENTRY_DSN
if sentry_config:
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init(
sentry_config,
integrations=[FlaskIntegration()],
traces_sample_rate=1.0,
)
except AttributeError:
pass
class ReverseProxied(object):
def __init__(self, app_in, script_name=None, scheme=None, server=None):
self.app = app_in
self.script_name = script_name
self.scheme = scheme
self.server = server
def __call__(self, environ, start_response):
script_name = environ.get("HTTP_X_SCRIPT_NAME", "") or self.script_name
if script_name:
environ["SCRIPT_NAME"] = script_name
path_info = environ["PATH_INFO"]
if path_info.startswith(script_name):
environ["PATH_INFO"] = path_info[len(script_name) :]
scheme = environ.get("HTTP_X_SCHEME", "") or self.scheme
if scheme:
environ["wsgi.url_scheme"] = scheme
server = environ.get("HTTP_X_FORWARDED_SERVER", "") or self.server
if server:
environ["HTTP_HOST"] = server
return self.app(environ, start_response)
app = Flask(__name__, template_folder="app/templates", static_folder="static")
cors = CORS(app, resources={r"*": {"origins": "*"}}, supports_credentials=True)
# app.wsgi_app = ReverseProxied(app.wsgi_app)
app.secret_key = "dfsdbegerbnergfbergqbqerg"
app.config.from_pyfile("config/config.py", silent=True)
mail.init_app(app)
DB.init_app(app)
app.config["DB"] = DB
app.config["MAIL"] = mail
# app.config['SQLALCHEMY_ECHO'] = True
@app.route("/oeasc/", defaults={"text": ""})
@app.route("/oeasc/<path:text>")
def redirect_front(text):
return redirect("/front/", code=302)
@app.route("/google4b0945b8a2f6425f.html")
def google():
return redirect(url_for("static", filename="google4b0945b8a2f6425f.html"))
with app.app_context():
from app.modules.oeasc.user.mail import function_dict
app.config["after_USERSHUB_request"] = function_dict
# app.config['SQLALCHEMY_ECHO'] = True
from app.modules.oeasc.utils import utils_dict
app.jinja_env.globals["utils"] = utils_dict
@app.after_request
def after_login_method(response):
if not request.cookies.get("token"):
session["current_user"] = None
if request.endpoint == "auth.login" and response.status_code == 200:
if response.get_data().decode("utf-8"):
current_user = json.loads(response.get_data().decode("utf-8"))
session["current_user"] = current_user["user"]
return response
from app.ref_geo import api as ref_geo_api
app.register_blueprint(ref_geo_api.bp, url_prefix="/api/ref_geo")
from app.modules.oeasc.user import api as api_user
app.register_blueprint(api_user.bp, url_prefix="/api/user")
from app.modules.oeasc import api as oeasc_api
app.register_blueprint(oeasc_api.bp, url_prefix="/api/oeasc")
from app.modules.oeasc.declaration import api as declaration_api
app.register_blueprint(declaration_api.bp, url_prefix="/api/declaration")
from app.modules.oeasc.degat_foret import api as degat_foret_api
app.register_blueprint(degat_foret_api.bp, url_prefix="/api/degat_foret")
from app.modules.oeasc.resultat import api as resultats_api
app.register_blueprint(resultats_api.bp, url_prefix="/api/resultat")
from app.modules.oeasc.commons import api as commons_api
app.register_blueprint(commons_api.bp, url_prefix="/api/commons")
from app.modules.oeasc.generic import api as generic_api
app.register_blueprint(generic_api.bp, url_prefix="/api/generic")
from app.modules.oeasc.i_n import api as in_api
app.register_blueprint(in_api.bp, url_prefix="/api/in")
from app.modules.oeasc.chasse import api as chasse_api
app.register_blueprint(chasse_api.bp, url_prefix="/api/chasse")
from pypnusershub import routes
app.register_blueprint(routes.routes, url_prefix="/pypn/auth")
from pypnusershub import routes_register
app.register_blueprint(routes_register.bp, url_prefix="/pypn/register")
from pypnnomenclature.routes import routes
app.register_blueprint(routes, url_prefix="/api/nomenclatures")
from app.modules.oeasc.commands import commands
for cmd in commands:
app.cli.add_command(cmd)
if __name__ == "__main__":
app.run(debug=config.DEBUG, port=config.PORT)
_paragraph_re = re.compile(r"(?:\r\n|\r|\n){2,}")
@app.template_filter()
@evalcontextfilter
def nl2br(eval_ctx, value):
result = "\n\n".join(
"<p>%s</p>" % p.replace("\n", "<br>\n")
for p in _paragraph_re.split(escape(value))
)
if eval_ctx.autoescape:
result = Markup(result)
return result
@app.template_filter()
@evalcontextfilter
def nopar(eval_ctx, value):
if not value:
return ""
s2 = re.sub(r"\(.*\)", "", value)
s2 = s2.strip()
return s2
@app.template_filter()
@evalcontextfilter
def cleanid(eval_ctx, value):
if not value:
return ""
s2 = value.replace(" ", "")
s2 = s2.replace(".", "")
s2 = s2.strip()
return s2