getunicode returns None time to time. Catched that.
This commit is contained in:
parent
cfb0b52ec2
commit
3e4e3854d4
32
main.py
32
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/<username>')
|
||||
@app.route('/user/<username>', method=['OPTIONS', 'PUT'])
|
||||
def create_user (username):
|
||||
user = login(request)
|
||||
if user['_privilege'] > 0:
|
||||
|
@ -6,8 +6,8 @@
|
||||
</head>
|
||||
<body>
|
||||
<div id="contact-mailer-message"></div>
|
||||
<form action="https://mailer.jean-cloud.net/submit" method="POST" id="contact-mailer-form">
|
||||
<input type="hidden" name="token" value="s0y6WANzU1XnYERoJxMwekP9pqilSVLK5Gbf3hmZadHB2rQ4u8" />
|
||||
<form action="http://localhost:8080/submit" method="POST" id="contact-mailer-form">
|
||||
<input type="hidden" name="token" value="M9zyPf4sm6opGe58vbNql0S2UktQIdBOYAaL1VJhKXFwxnRZug" />
|
||||
<div>
|
||||
<label for="nom">Votre nom :</label>
|
||||
<input type="text" name="nom" required="required"/>
|
||||
|
Loading…
Reference in New Issue
Block a user