diff options
-rw-r--r-- | frest/utils.py | 45 |
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 |