diff --git a/flask_swagger_codegen/generator.py b/flask_swagger_codegen/generator.py index a8e3e77..8eff619 100644 --- a/flask_swagger_codegen/generator.py +++ b/flask_swagger_codegen/generator.py @@ -16,41 +16,41 @@ def render(self, template, *args, **kwargs): return template.render(*args, **kwargs) def generate_requirements(self): - return self.render('requirements.tpl') + return self.render('requirements.py.j2') def generate_routes(self): return self.render( - 'routes.tpl', + 'routes.py.j2', routes=self.model.routes, resources=self.model.resources) def generate_schemas(self): - return self.render('schemas.tpl', schemas=self.model.schemas) + return self.render('schemas.py.j2', schemas=self.model.schemas) def generate_models(self): - return self.render('models.tpl', schemas=self.model.schemas) + return self.render('models.py.j2', schemas=self.model.schemas) def generate_validators(self): - return self.render('validators.tpl', validators=self.model.validators) + return self.render('validators.py.j2', validators=self.model.validators) def generate_filters(self): - return self.render('filters.tpl', filters=self.model.filters) + return self.render('filters.py.j2', filters=self.model.filters) def generate_views(self): for view, ins in self.model.resources_group.iteritems(): - yield (view, self.render('views.tpl', resources=ins)) + yield (view, self.render('views.py.j2', resources=ins)) def generate_init(self): - return self.render('init.tpl') + return self.render('init.py.j2') def generate_api(self): - return self.render('api.tpl') + return self.render('api.py.j2') def generate_app(self): - return self.render('app.tpl', model=self.model) + return self.render('app.py.j2', model=self.model) def generate_blueprint(self): - return self.render('blueprint.tpl', model=self.model) + return self.render('blueprint.py.j2', model=self.model) def generate_view_tests(self): for view, ins in self.model.resources_group.iteritems(): - yield (view, self.render('view_test.tpl', resources=ins)) + yield (view, self.render('view_test.py.j2', resources=ins)) diff --git a/flask_swagger_codegen/resolver.py b/flask_swagger_codegen/resolver.py index 8582765..59d65fb 100644 --- a/flask_swagger_codegen/resolver.py +++ b/flask_swagger_codegen/resolver.py @@ -40,9 +40,13 @@ class FieldResolver(object): 'int64': 'Integer', 'float': 'Float', 'double': 'Decimal', + 'byte': 'Integer', + 'date': 'Date', + 'date-time': 'DateTime', + 'password': 'String', + 'decimal': 'Decimal', 'formattedString': 'FormattedString', - 'date': 'Date', 'time': 'Time', 'datetime': 'DateTime', 'timedelta': 'TimeDelta', diff --git a/flask_swagger_codegen/templates/_do_not_change.tpl b/flask_swagger_codegen/templates/_do_not_change.j2 similarity index 100% rename from flask_swagger_codegen/templates/_do_not_change.tpl rename to flask_swagger_codegen/templates/_do_not_change.j2 diff --git a/flask_swagger_codegen/templates/api.tpl b/flask_swagger_codegen/templates/api.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/api.tpl rename to flask_swagger_codegen/templates/api.py.j2 diff --git a/flask_swagger_codegen/templates/app.py.j2 b/flask_swagger_codegen/templates/app.py.j2 new file mode 100644 index 0000000..bfdef94 --- /dev/null +++ b/flask_swagger_codegen/templates/app.py.j2 @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +from flask import Flask + + +def create_app(): + app = Flask(__name__) + + from {{ model.blueprint }} import bp + app.register_blueprint(bp, url_prefix='/{{ model.blueprint }}') + + from {{ model.blueprint }}.models import db + db.init_app(app) + + from {{ model.blueprint }}.schemas import ma + ma.init_app(app) + + return app + + +app = create_app() + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/flask_swagger_codegen/templates/app.tpl b/flask_swagger_codegen/templates/app.tpl deleted file mode 100644 index d1452f6..0000000 --- a/flask_swagger_codegen/templates/app.tpl +++ /dev/null @@ -1,33 +0,0 @@ -# -*- coding: utf-8 -*- - -from collections import namedtuple - -from flask import Flask, Blueprint -from flask_sqlalchemy import SQLAlchemy -from flask_marshmallow import Marshmallow - -import {{ model.blueprint }} - -_last_res = None - - -def create_app(): - global _last_res - if _last_res: - return _last_res - _app = Flask(__name__) - _app.register_blueprint( - {{ model.blueprint }}.bp, - url_prefix='/v1') - _db = SQLAlchemy(_app) - _ma = Marshmallow(_app) - - _last_res = namedtuple('Result', 'app db ma')(_app, _db, _ma) # Cache - return _last_res - - -app, db, ma = create_app() - - -if __name__ == '__main__': - app.run(debug=True) diff --git a/flask_swagger_codegen/templates/blueprint.tpl b/flask_swagger_codegen/templates/blueprint.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/blueprint.tpl rename to flask_swagger_codegen/templates/blueprint.py.j2 diff --git a/flask_swagger_codegen/templates/filters.tpl b/flask_swagger_codegen/templates/filters.py.j2 similarity index 96% rename from flask_swagger_codegen/templates/filters.tpl rename to flask_swagger_codegen/templates/filters.py.j2 index c543fd5..47177b3 100644 --- a/flask_swagger_codegen/templates/filters.tpl +++ b/flask_swagger_codegen/templates/filters.py.j2 @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -{% include '_do_not_change.tpl' %} +{% include '_do_not_change.j2' %} from functools import wraps from flask import request, Response diff --git a/flask_swagger_codegen/templates/init.tpl b/flask_swagger_codegen/templates/init.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/init.tpl rename to flask_swagger_codegen/templates/init.py.j2 diff --git a/flask_swagger_codegen/templates/models.py.j2 b/flask_swagger_codegen/templates/models.py.j2 new file mode 100644 index 0000000..7cec973 --- /dev/null +++ b/flask_swagger_codegen/templates/models.py.j2 @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +{% include '_do_not_change.j2' %} + +# models are for SQL-alchemy + +from flask_sqlalchemy import SQLAlchemy +db = SQLAlchemy() + + +{%- for name, schema in schemas.iteritems() %} + + +class {{ name }}(db.Model): + __tablename__ = '{{ name | lower }}s' + id = db.Column(db.Integer(), primary_key=True) + {%- for n, field in schema.fields.iteritems() %} + {{n}} = db.Column(db.{{ field | replace('fields.', '') }}) + {%- endfor %} + +{%- endfor %} + diff --git a/flask_swagger_codegen/templates/models.tpl b/flask_swagger_codegen/templates/models.tpl deleted file mode 100644 index bcd071d..0000000 --- a/flask_swagger_codegen/templates/models.tpl +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- - -{% include '_do_not_change.tpl' %} - -# models are for SQL-alchemy - -from marshmallow import fields -from app import db - - -{%- for name, schema in schemas.iteritems() %} - - -class {{ name }}(db.Model): - {%- for n, field in schema.fields.iteritems() %} - {{n}} = db.Column(db.{{ field }}) - {%- endfor %} - -{%- endfor %} - diff --git a/flask_swagger_codegen/templates/requirements.tpl b/flask_swagger_codegen/templates/requirements.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/requirements.tpl rename to flask_swagger_codegen/templates/requirements.py.j2 diff --git a/flask_swagger_codegen/templates/routes.tpl b/flask_swagger_codegen/templates/routes.py.j2 similarity index 86% rename from flask_swagger_codegen/templates/routes.tpl rename to flask_swagger_codegen/templates/routes.py.j2 index 4b715d1..6c37896 100644 --- a/flask_swagger_codegen/templates/routes.tpl +++ b/flask_swagger_codegen/templates/routes.py.j2 @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -{% include '_do_not_change.tpl' %} +{% include '_do_not_change.j2' %} {% for _, res in resources.iteritems() -%} from .api.{{ res.root_path }} import {{ res.class_name }} diff --git a/flask_swagger_codegen/templates/schemas.tpl b/flask_swagger_codegen/templates/schemas.py.j2 similarity index 69% rename from flask_swagger_codegen/templates/schemas.tpl rename to flask_swagger_codegen/templates/schemas.py.j2 index 32e0320..93de443 100644 --- a/flask_swagger_codegen/templates/schemas.tpl +++ b/flask_swagger_codegen/templates/schemas.py.j2 @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- -{% include '_do_not_change.tpl' %} +{% include '_do_not_change.j2' %} -from app import ma from models import {{', '.join(schemas.keys())}} - +from flask_marshmallow import Marshmallow +ma = Marshmallow() {%- for name, schema in schemas.iteritems() %} diff --git a/flask_swagger_codegen/templates/validators.tpl b/flask_swagger_codegen/templates/validators.py.j2 similarity index 96% rename from flask_swagger_codegen/templates/validators.tpl rename to flask_swagger_codegen/templates/validators.py.j2 index f7096c4..f7ff64f 100644 --- a/flask_swagger_codegen/templates/validators.tpl +++ b/flask_swagger_codegen/templates/validators.py.j2 @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -{% include '_do_not_change.tpl' %} +{% include '_do_not_change.j2' %} from functools import wraps from werkzeug.datastructures import MultiDict diff --git a/flask_swagger_codegen/templates/view_test.tpl b/flask_swagger_codegen/templates/view_test.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/view_test.tpl rename to flask_swagger_codegen/templates/view_test.py.j2 diff --git a/flask_swagger_codegen/templates/views.tpl b/flask_swagger_codegen/templates/views.py.j2 similarity index 100% rename from flask_swagger_codegen/templates/views.tpl rename to flask_swagger_codegen/templates/views.py.j2