Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add a flask_sqla template (Flask+SQLAlchemy+WTForms)

  • Loading branch information...
commit dc7dd2d9a117714078d9e55369ab5ac35ef2d72c 1 parent 62a4700
@mattoufoutu authored
Showing with 182 additions and 0 deletions.
  1. +1 −0  README.rst
  2. +1 −0  setup.py
  3. +8 −0 src/flask_project_templates/flasktemplates.py
  4. +16 −0 src/flask_project_templates/templates/flask_sqla/+project+/__init__.py_tmpl
  5. +22 −0 src/flask_project_templates/templates/flask_sqla/+project+/core/__init__.py
  6. +1 −0  src/flask_project_templates/templates/flask_sqla/+project+/forms.py
  7. +30 −0 src/flask_project_templates/templates/flask_sqla/+project+/models.py_tmpl
  8. +3 −0  src/flask_project_templates/templates/flask_sqla/+project+/settings.py_tmpl
  9. 0  src/flask_project_templates/templates/flask_sqla/+project+/static/css/+project+.css
  10. 0  src/flask_project_templates/templates/flask_sqla/+project+/static/js/+project+.js
  11. +12 −0 src/flask_project_templates/templates/flask_sqla/+project+/templates/base.html
  12. +2 −0  src/flask_project_templates/templates/flask_sqla/+project+/templates/index.html
  13. +11 −0 src/flask_project_templates/templates/flask_sqla/+project+/templates/macros.html
  14. 0  src/flask_project_templates/templates/flask_sqla/+project+/tests/__init__.py
  15. +9 −0 src/flask_project_templates/templates/flask_sqla/+project+/urls.py_tmpl
  16. 0  src/flask_project_templates/templates/flask_sqla/+project+/views/__init__.py
  17. +7 −0 src/flask_project_templates/templates/flask_sqla/+project+/views/main.py
  18. +55 −0 src/flask_project_templates/templates/flask_sqla/manage.py_tmpl
  19. +4 −0 src/flask_project_templates/templates/flask_sqla/requirements.txt
View
1  README.rst
@@ -7,6 +7,7 @@ Templates:
----------
* flask_base: Flask basic project
+* flask_sqla: Flask project using SQLAlchemy
Installation:
-------------
View
1  setup.py
@@ -41,5 +41,6 @@
entry_points = """
[paste.paster_create_template]
flask_basic=flask_project_templates.flasktemplates:FlaskBasicTemplate
+ flask_sqla=flask_project_templates.flasktemplates:FlaskSQLAlchemyTemplate
""",
)
View
8 src/flask_project_templates/flasktemplates.py
@@ -31,3 +31,11 @@ def append_secret_key(self):
class FlaskBasicTemplate(FlaskTemplate):
_template_dir = 'templates/flask_basic'
summary = 'Flask basic project'
+
+
+class FlaskSQLAlchemyTemplate(FlaskTemplate):
+ _template_dir = 'templates/flask_sqla'
+ summary = 'Flask project using SQLAlchemy'
+ vars = [
+ var('db_uri', 'Database URI', default='sqlite:///:memory:'),
+ ]
View
16 src/flask_project_templates/templates/flask_sqla/+project+/__init__.py_tmpl
@@ -0,0 +1,16 @@
+from flask import Flask
+from flask.ext.sqlalchemy import SQLAlchemy
+
+from $(project) import settings
+from $(project).core import setup_routing
+
+# setup application
+app = Flask('$(project)')
+app.config.from_object(settings)
+
+# setup database
+db = SQLAlchemy(app)
+
+# register application views and blueprints
+from $(project).urls import routes
+setup_routing(app, routes)
View
22 src/flask_project_templates/templates/flask_sqla/+project+/core/__init__.py
@@ -0,0 +1,22 @@
+
+def setup_routing(app, routes):
+ """
+ Registers blueprint instances and add routes all at once.
+ Routes are defined using the following format:
+ [
+ ((blueprint_or_app_instance, url_prefix),
+ ('/route1/<param>', view_function),
+ ('/route2', ViewClass.as_view('view_name'),
+ ),
+ ...
+ ]
+ """
+ for route in routes:
+ endpoint, rules = route[0], route[1:]
+ for pattern, view in rules:
+ if endpoint is None:
+ app.add_url_rule(pattern, view_func=view)
+ else:
+ endpoint[0].add_url_rule(pattern, view_func=view)
+ if endpoint is not None:
+ app.register_blueprint(endpoint[0], url_prefix=endpoint[1])
View
1  src/flask_project_templates/templates/flask_sqla/+project+/forms.py
@@ -0,0 +1 @@
+from flask.ext.wtf import *
View
30 src/flask_project_templates/templates/flask_sqla/+project+/models.py_tmpl
@@ -0,0 +1,30 @@
+from sqlalchemy.ext.declarative import declared_attr
+
+from $(project) import db
+
+
+class AutoInitMixin(object):
+ """
+ Mixin for populating models columns automatically (no need
+ to define an __init__ method) and set the default value if any.
+ Also sets the model id and __tablename__ automatically.
+ """
+ id = db.Column(db.Integer, primary_key=True)
+
+ # use the lowercased model class name as the __tablename__
+ @declarred_attr
+ def __tablename__(cls):
+ return cls.__name__.lower()
+
+ def __init__(self, *args, **kwargs):
+ for attr in (a for a in dir(self) if not a.startswith('_')):
+ attr_obj = getattr(self, attr)
+ if isinstance(attr_obj, db.Column):
+ if attr in kwargs:
+ setattr(self, attr, kwargs[attr])
+ else:
+ if hasattr(attr_obj, 'default'):
+ if callable(attr_obj.default):
+ setattr(self, attr, attr_obj.default())
+ else:
+ setattr(self, attr, attr_obj.default)
View
3  src/flask_project_templates/templates/flask_sqla/+project+/settings.py_tmpl
@@ -0,0 +1,3 @@
+DEBUG = True
+SECRET_KEY = '$(secret_key)'
+SQLALCHEMY_DATABASE_URI = '$(db_uri)'
View
0  src/flask_project_templates/templates/flask_sqla/+project+/static/css/+project+.css
No changes.
View
0  src/flask_project_templates/templates/flask_sqla/+project+/static/js/+project+.js
No changes.
View
12 src/flask_project_templates/templates/flask_sqla/+project+/templates/base.html
@@ -0,0 +1,12 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title>{% block title %}{% endblock %}</title>
+ <script type="text/javascript" src="/static/js/$(project).js"></script>
+ <link rel="stylesheet" type="text/css" href="/static/css/$(project.css)" />
+</head>
+<body>
+{% block body %}{% endblock %}
+</body>
+</html>
View
2  src/flask_project_templates/templates/flask_sqla/+project+/templates/index.html
@@ -0,0 +1,2 @@
+{% extends 'base.html' %}
+{% block body %}<h1>Hello, {{ name|default('World', boolean=True) }}!</h1>{% endblock %}
View
11 src/flask_project_templates/templates/flask_sqla/+project+/templates/macros.html
@@ -0,0 +1,11 @@
+{% macro render_field(field) %}
+ {{ field.label }}
+ {{ field(**kwargs)|safe }}
+ {% if field.errors %}
+ <ul class='errors'>
+ {% for error in field.errors %}
+ <li class='error'>{{ error }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+{% endmacro %}
View
0  src/flask_project_templates/templates/flask_sqla/+project+/tests/__init__.py
No changes.
View
9 src/flask_project_templates/templates/flask_sqla/+project+/urls.py_tmpl
@@ -0,0 +1,9 @@
+
+from $(project).views.main import main, index
+
+routes = [
+ ((main, ''),
+ ('/', index),
+ ('/<name>', index),
+ )
+]
View
0  src/flask_project_templates/templates/flask_sqla/+project+/views/__init__.py
No changes.
View
7 src/flask_project_templates/templates/flask_sqla/+project+/views/main.py
@@ -0,0 +1,7 @@
+from flask import Blueprint, render_template
+
+main = Blueprint('main', __name__)
+
+
+def index(name=None):
+ return render_template('index.html', name=name)
View
55 src/flask_project_templates/templates/flask_sqla/manage.py_tmpl
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+
+from flask.ext.script import Command, Manager, Shell
+
+from $(project) import app
+
+manager = Manager(app)
+
+
+class SyncDB(Command):
+ """
+ Initializes the database tables.
+ """
+ def run(self):
+ from $(project) import db
+ db.drop_all()
+ db.create_all()
+ db.session.commit()
+
+
+class FixedShell(Shell):
+ """
+ Runs a Python shell inside Flask application context.
+ """
+ def run(self, no_ipython):
+ context = self.get_context()
+ if not no_ipython:
+ try:
+ from IPython.frontend.terminal.embed import InteractiveShellEmbed
+ sh = InteractiveShellEmbedbanner1=self.banner
+ sh(global_ns=dict(), local_ns=context)
+ except ImportError:
+ pass
+ from code import interact
+ interact(banner=self.banner, local=context)
+
+
+class Test(Command):
+ """
+ Runs the application's test suite.
+ """
+ def run(self):
+ import os
+ from unittest import TestLoader, TextTestRunner
+ cur_dir = os.path.dirname(os.path.abspath(__file__))
+ loader = TestLoader()
+ test_suite = loader.discover(cur_dir)
+ runner = TextTestRunner(verbosity=2)
+ runner.run(test_suite)
+
+
+manager._commands['shell'] = FixedShell()
+manager.add_command('syncdb', SyncDB())
+manager.add_command('test', Test())
+manager.run()
View
4 src/flask_project_templates/templates/flask_sqla/requirements.txt
@@ -0,0 +1,4 @@
+Flask
+Flask-Script
+Flask-SQLAlchemy
+Flask-WTF
Please sign in to comment.
Something went wrong with that request. Please try again.