From 3e4e3854d4892b126a9942f617caa2970178449b Mon Sep 17 00:00:00 2001 From: Adrian Amaglio Date: Wed, 16 Sep 2020 11:19:31 +0200 Subject: [PATCH] getunicode returns None time to time. Catched that. --- main.py | 32 +++++++++++++++++++++++++++----- test.html | 4 ++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index cf08db4..4dede47 100755 --- a/main.py +++ b/main.py @@ -12,6 +12,7 @@ from dotenv import load_dotenv import random, string # for tokens import html # for sanitization import datetime # to name unsent mails +from bson.json_util import dumps ##################################################### Bottle stuff ############################################ @@ -26,9 +27,25 @@ class StripPathMiddleware(object): e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') return self.a(e, h) +class EnableCors(object): + name = 'enable_cors' + api = 2 + + def apply(self, fn, context): + def _enable_cors(*args, **kwargs): + # set CORS headers + response.headers['Access-Control-Allow-Origin'] = '*' + response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, OPTIONS' + response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token' + + if bottle.request.method != 'OPTIONS': + # actual request; reply with the actual response + return fn(*args, **kwargs) + + return _enable_cors app = application = bottle.Bottle(catchall=False) - +#app.install(EnableCors()) ##################################################### Configuration ############################################ @@ -85,6 +102,7 @@ else: # mongodb initialization mongodb_client = pymongo.MongoClient("mongodb://{}:{}/".format(mongodb_host, mongodb_port), connect=False, serverSelectionTimeoutMS=10000, connectTimeoutMS=10000) mongodb_database = mongodb_client[mongodb_dbname] +print(mongodb_database) ##################################################### main route: mail submission ############################################ @@ -118,6 +136,8 @@ def submission (): try: subject_fields = fill_fields(request, get_fields(form['subject'])) content_fields = fill_fields(request, get_fields(form['content'])) + print(subject_fields) + print(content_fields) except MissingParameterException as e: response.status = 404 return str(e) @@ -168,7 +188,9 @@ def fill_fields(request, fields): for field in fields: if field in request.forms: fields[field] = request.forms.getunicode(field) - elif fields[field] == None: + if fields[field] is None: # if unicode failed + fields[field] = request.forms.get(field) + elif fields[field] is None: raise MissingParameterException("Le champs {} est obligatoire".format(field)) return fields @@ -282,7 +304,7 @@ def list_forms (): response.status = 400 return 'Privilèges insufisants' data = mongodb_database['forms'].find(filt) - return bottle.template("list.tpl", data=data) + return dumps(list(data)) except pymongo.errors.ServerSelectionTimeoutError as e: response.status = 500 return 'La base de donnée n’est pas accessible' @@ -330,13 +352,13 @@ def list_users (): return 'Privilèges insufisants' try: data = mongodb_database['users'].find() - return bottle.template("list.tpl", data=data) + return dumps(list(data)) except pymongo.errors.ServerSelectionTimeoutError as e: response.status = 500 return 'La base de donnée n’est pas accessible' -@app.put('/user/') +@app.route('/user/', method=['OPTIONS', 'PUT']) def create_user (username): user = login(request) if user['_privilege'] > 0: diff --git a/test.html b/test.html index 94c873d..445e98a 100644 --- a/test.html +++ b/test.html @@ -6,8 +6,8 @@
-
- + +