summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-03-20 10:19:47 +0100
committerSanto Cariotti <dcariotti24@gmail.com>2020-03-20 10:19:47 +0100
commit4a49b7aad064c5a0b896b8f76b40da28272e4aa6 (patch)
tree756e65b2f7311e6d4ef825756ee53442ccdbec79
parent22dc19db42b71998efc305a4495940661957b57b (diff)
feat: utils file
-rw-r--r--frest/utils.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/frest/utils.py b/frest/utils.py
new file mode 100644
index 0000000..fbfa3bf
--- /dev/null
+++ b/frest/utils.py
@@ -0,0 +1,45 @@
+from flask import make_response, jsonify, abort, request, render_template
+from flask_mail import Message
+from mail import mail
+from functools import wraps
+from datetime import datetime
+import os
+
+
+def send_email(sender, email, activation_code, title, template):
+ msg = Message(title, sender=sender, recipients=[email])
+ rest_link = os.getenv("FREST_URL", "http://localhost:8080/app")
+ msg.html = render_template(template, link=rest_link, code=activation_code)
+ mail.send(msg)
+
+
+def http_call(data, status):
+ return make_response(jsonify({"status": status, "result": data}), status)
+
+
+def model_serialize(obj, params="", extend_model_for=[]):
+ fields = {}
+ params = params.split(",")
+
+ for i in [f for f in dir(obj) if f in params]:
+ if isinstance(obj.__getattribute__(i), datetime):
+ fields[i] = obj.__getattribute__(i).strftime("%d/%m/%Y %H:%M")
+ else:
+ fields[i] = obj.__getattribute__(i)
+
+ if len(extend_model_for) > 0:
+ for key, value in fields.items():
+ if isinstance(value, list):
+ _l = []
+ for v in value:
+ for i in extend_model_for:
+ if isinstance(v, i):
+ _l.append(v.as_json())
+
+ fields[key] = _l
+ else:
+ for i in extend_model_for:
+ if isinstance(value, i):
+ fields[key] = value.as_json()
+
+ return fields