getunicode returns None time to time. Catched that.

This commit is contained in:
Adrian Amaglio 2020-09-16 11:19:31 +02:00
parent cfb0b52ec2
commit 3e4e3854d4
2 changed files with 29 additions and 7 deletions

32
main.py
View File

@ -12,6 +12,7 @@ from dotenv import load_dotenv
import random, string # for tokens import random, string # for tokens
import html # for sanitization import html # for sanitization
import datetime # to name unsent mails import datetime # to name unsent mails
from bson.json_util import dumps
##################################################### Bottle stuff ############################################ ##################################################### Bottle stuff ############################################
@ -26,9 +27,25 @@ class StripPathMiddleware(object):
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
return self.a(e, h) 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 = application = bottle.Bottle(catchall=False)
#app.install(EnableCors())
##################################################### Configuration ############################################ ##################################################### Configuration ############################################
@ -85,6 +102,7 @@ else:
# mongodb initialization # mongodb initialization
mongodb_client = pymongo.MongoClient("mongodb://{}:{}/".format(mongodb_host, mongodb_port), connect=False, serverSelectionTimeoutMS=10000, connectTimeoutMS=10000) mongodb_client = pymongo.MongoClient("mongodb://{}:{}/".format(mongodb_host, mongodb_port), connect=False, serverSelectionTimeoutMS=10000, connectTimeoutMS=10000)
mongodb_database = mongodb_client[mongodb_dbname] mongodb_database = mongodb_client[mongodb_dbname]
print(mongodb_database)
##################################################### main route: mail submission ############################################ ##################################################### main route: mail submission ############################################
@ -118,6 +136,8 @@ def submission ():
try: try:
subject_fields = fill_fields(request, get_fields(form['subject'])) subject_fields = fill_fields(request, get_fields(form['subject']))
content_fields = fill_fields(request, get_fields(form['content'])) content_fields = fill_fields(request, get_fields(form['content']))
print(subject_fields)
print(content_fields)
except MissingParameterException as e: except MissingParameterException as e:
response.status = 404 response.status = 404
return str(e) return str(e)
@ -168,7 +188,9 @@ def fill_fields(request, fields):
for field in fields: for field in fields:
if field in request.forms: if field in request.forms:
fields[field] = request.forms.getunicode(field) 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)) raise MissingParameterException("Le champs {} est obligatoire".format(field))
return fields return fields
@ -282,7 +304,7 @@ def list_forms ():
response.status = 400 response.status = 400
return 'Privilèges insufisants' return 'Privilèges insufisants'
data = mongodb_database['forms'].find(filt) data = mongodb_database['forms'].find(filt)
return bottle.template("list.tpl", data=data) return dumps(list(data))
except pymongo.errors.ServerSelectionTimeoutError as e: except pymongo.errors.ServerSelectionTimeoutError as e:
response.status = 500 response.status = 500
return 'La base de donnée nest pas accessible' return 'La base de donnée nest pas accessible'
@ -330,13 +352,13 @@ def list_users ():
return 'Privilèges insufisants' return 'Privilèges insufisants'
try: try:
data = mongodb_database['users'].find() data = mongodb_database['users'].find()
return bottle.template("list.tpl", data=data) return dumps(list(data))
except pymongo.errors.ServerSelectionTimeoutError as e: except pymongo.errors.ServerSelectionTimeoutError as e:
response.status = 500 response.status = 500
return 'La base de donnée nest pas accessible' return 'La base de donnée nest pas accessible'
@app.put('/user/<username>') @app.route('/user/<username>', method=['OPTIONS', 'PUT'])
def create_user (username): def create_user (username):
user = login(request) user = login(request)
if user['_privilege'] > 0: if user['_privilege'] > 0:

View File

@ -6,8 +6,8 @@
</head> </head>
<body> <body>
<div id="contact-mailer-message"></div> <div id="contact-mailer-message"></div>
<form action="https://mailer.jean-cloud.net/submit" method="POST" id="contact-mailer-form"> <form action="http://localhost:8080/submit" method="POST" id="contact-mailer-form">
<input type="hidden" name="token" value="s0y6WANzU1XnYERoJxMwekP9pqilSVLK5Gbf3hmZadHB2rQ4u8" /> <input type="hidden" name="token" value="M9zyPf4sm6opGe58vbNql0S2UktQIdBOYAaL1VJhKXFwxnRZug" />
<div> <div>
<label for="nom">Votre nom&nbsp;:</label> <label for="nom">Votre nom&nbsp;:</label>
<input type="text" name="nom" required="required"/> <input type="text" name="nom" required="required"/>