From 4a49b7aad064c5a0b896b8f76b40da28272e4aa6 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 20 Mar 2020 10:19:47 +0100 Subject: feat: utils file --- frest/utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 frest/utils.py 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 -- cgit v1.2.3-18-g5258