diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-03-20 12:13:52 +0100 |
---|---|---|
committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-03-20 12:13:52 +0100 |
commit | ce12b3c0916958819d3e5cec5c3609e204b28ad8 (patch) | |
tree | c5d4f14e2075306e1ffc6f8d1f98ee07911e64d0 | |
parent | a79bca799a5830b035df818d7e87425c25d081df (diff) |
feat: template for classes
-rw-r--r-- | src/frest/templates/form.rst | 10 | ||||
-rw-r--r-- | src/frest/templates/models.rst | 26 | ||||
-rw-r--r-- | src/frest/templates/route.rst | 88 |
3 files changed, 124 insertions, 0 deletions
diff --git a/src/frest/templates/form.rst b/src/frest/templates/form.rst new file mode 100644 index 0000000..f4b2c14 --- /dev/null +++ b/src/frest/templates/form.rst @@ -0,0 +1,10 @@ +from .models import %%NAME%% +from src.forms import ModelForm + + +class %%NAME%%Form(ModelForm): + model = %%NAME%% + + def __init__(self, data): + super().__init__(self.model) + self.data = data diff --git a/src/frest/templates/models.rst b/src/frest/templates/models.rst new file mode 100644 index 0000000..af80963 --- /dev/null +++ b/src/frest/templates/models.rst @@ -0,0 +1,26 @@ +from database import db +from datetime import datetime +from pytz import timezone +import os + + +class %%NAME%%(db.Model): + %%name%%Id = db.Column(db.Integer, primary_key=True) + %%params_model%% + created_at = db.Column(db.DateTime) + updated_at = db.Column(db.DateTime) + + def __init__(self, **kwargs): + %%params_model_init%% + self.created_at = datetime.now( + timezone(os.getenv("FREST_TIMEZONE", "Europe/Rome")) + ) + self.updated_at = datetime.now( + timezone(os.getenv("FREST_TIMEZONE", "Europe/Rome")) + ) + + def __repr__(self): + return f"<%%NAME%% '{self.%%name%%Id}'>" + + def __str__(self): + return f"{self.%%name%%Id}" diff --git a/src/frest/templates/route.rst b/src/frest/templates/route.rst new file mode 100644 index 0000000..a24a3b1 --- /dev/null +++ b/src/frest/templates/route.rst @@ -0,0 +1,88 @@ +from flask import Blueprint, request, abort +from fapi.utils import http_call, model_serialize +from fapi.decorators import check_token, admin_required +from .models import %%NAME%% +from .forms import %%NAME%%Form +from database import db +import json +from datetime import datetime +from pytz import timezone + +api = Blueprint("%%name%%s", __name__) + + +@api.route("/api/%%name%%") +def all_%%name%%s(): + return http_call( + [ + model_serialize(i, params="%%params%%") + for i in %%NAME%%.query.all() + ], + 200, + ) + + +@api.route("/api/%%name%%/<%%name%%Id>") +def get_%%name%%(%%name%%Id): + %%first_char%% = %%NAME%%.query.filter_by(%%name%%Id=%%name%%Id).first() + if not %%first_char%%: + abort(404) + + return http_call(model_serialize(%%first_char%%, params="%%params%%"), 200) + + +@api.route("/api/%%name%%/<%%name%%Id>", methods=["DELETE"]) +@check_token +def delete_%%name%%(%%name%%Id): + %%first_char%% = %%NAME%%.query.filter_by(%%name%%Id=%%name%%Id) + if not %%first_char%%: + abort(404) + + deleted = %%first_char%%.delete() + db.session.commit() + + return http_call({"delete": deleted}, 200) + + +@api.route("/api/%%name%%", methods=["POST"]) +@check_token +def new_%%name%%(): + if not request.json: + abort(400) + + form = %%NAME%%Form(request.json) + + if form.is_valid(): + %%first_char%% = %%NAME%%( + %%params_form%% + ) + db.session.add(%%first_char%%) + + db.session.commit() + + return http_call({"%%name%%Id": %%first_char%%.%%name%%Id}, 201) + + abort(400) + + +@api.route("/api/%%name%%/<%%name%%Id>", methods=["PUT"]) +@check_token +def edit_%%name%%(%%name%%Id): + if not request.json: + abort(400) + + form = %%NAME%%Form(request.json) + + if form.is_valid(): + %%first_char%% = %%NAME%%.query.filter_by(%%name%%Id=%%name%%Id).first() + if not %%first_char%%: + abort(404) + + %%params_put%% + %%first_char%%.updated_at = datetime.now(timezone("Europe/Rome")) + + db.session.commit() + + return http_call({"%%name%%Id": %%first_char%%.%%name%%Id}, 200) + + abort(400) |