Skip to content

Commit

Permalink
Merge pull request #1 from mikeroll/sqlalchemy
Browse files Browse the repository at this point in the history
Jinja2 + fixed generation of models
  • Loading branch information
SamuelMarks committed Jun 13, 2015
2 parents f376b7c + 430ac68 commit 6d5adba
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 72 deletions.
24 changes: 12 additions & 12 deletions flask_swagger_codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
6 changes: 5 additions & 1 deletion flask_swagger_codegen/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
File renamed without changes.
24 changes: 24 additions & 0 deletions flask_swagger_codegen/templates/app.py.j2
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 0 additions & 33 deletions flask_swagger_codegen/templates/app.tpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions flask_swagger_codegen/templates/models.py.j2
Original file line number Diff line number Diff line change
@@ -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 %}

20 changes: 0 additions & 20 deletions flask_swagger_codegen/templates/models.tpl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 }}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() %}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.

0 comments on commit 6d5adba

Please sign in to comment.