Skip to content

Commit

Permalink
Flask project base structure
Browse files Browse the repository at this point in the history
  • Loading branch information
xen committed Jul 11, 2014
0 parents commit 3425976
Show file tree
Hide file tree
Showing 52 changed files with 1,242 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.py[co]
__pycache__

# Translations compiled files
*.mo

# Local development ignores
venv
data.sqlite

# Production config
local.cfg

# Static
static/libs/*

# Other stuff here
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Fresh installation test
#
# To run this: sudo docker build .

FROM stackbrew/ubuntu:saucy
MAINTAINER Flask developers

RUN apt-get update

ADD ./ /code/

ENV DEBIAN_FRONTEND noninteractive
RUN cat /code/packages.txt | xargs apt-get -y --force-yes install
RUN npm install -g bower
RUN ldconfig

RUN cd /code/ && make setup

RUN cd /code/src/ && python manage.py test
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
all: setup

venv/bin/activate:
virtualenv-2.7 venv

run: venv/bin/activate requirements.txt
. venv/bin/activate; python manage.py runserver

static/bower.json:
cd static && bower install

setup: venv/bin/activate requirements.txt static/bower.json
. venv/bin/activate; pip install -Ur requirements.txt

init: venv/bin/activate requirements.txt
. venv/bin/activate; python manage.py init

babel: venv/bin/activate
. venv/bin/activate; pybabel extract -F babel.cfg -o project/translations/messages.pot project

# lazy babel scan
lazybabel: venv/bin/activate
. venv/bin/activate; pybabel extract -F babel.cfg -k lazy_gettext -o project/translations/messages.pot project

# run $LANG=ru
addlang: venv/bin/activate
. venv/bin/activate; pybabel init -i project/translations/messages.pot -d project/translations -l $(LANG)

updlang: venv/bin/activate
. venv/bin/activate; pybabel update -i project/translations/messages.pot -d project/translations
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Flask project template

Flask project template.
3 changes: 3 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[python: **.py]
[jinja2: **.html]
extensions=jinja2.ext.autoescape,jinja2.ext.with_
26 changes: 26 additions & 0 deletions local.example.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# production mode, yopta
DEBUG = False

SQLALCHEMY_ECHO = True
SQLALCHEMY_RECORD_QUERIES = False

# this is example deployment configuration

# run python and execute this code
# >>> import os; os.urandom(24)
# value place here as result

# SECRET_KEY = '<key>'

# BROKER_URL = "mongodb://localhost"
# CELERY_RESULT_BACKEND = 'mongodb'
# CELERY_IMPORTS = ('backend.tasks',)
# CELERY_MONGODB_BACKEND_SETTINGS = {
# 'host': 'localhost',
# 'port': 27017,
# 'database': 'digdata_celery',
# #'user': user,
# #'password': password,
# 'taskmeta_collection': 'teskmeta'
# }
# CELERY_ANNOTATIONS = {"*": {"rate_limit": "10/s"}}
42 changes: 42 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import json

from flask.ext.script import Manager
from flask.ext.migrate import MigrateCommand

from project import create_app
from project.extensions import db

from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.serializer import dumps, loads

manager = Manager(create_app)
# Add Flask-Migrate commands under `db` prefix, for example:
# $ python manage.py db init
#
# $ python manage.py db migrate
#

manager.add_command('db', MigrateCommand)


@manager.command
def init():
"""Run in local machine."""
syncdb()


@manager.command
def syncdb():
"""Init/reset database."""
db.drop_all()
db.create_all()


manager.add_option('-c', '--config', dest="config", required=False,
help="config file")

if __name__ == "__main__":
manager.run()
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
48 changes: 48 additions & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

[alembic:exclude]
tables =
90 changes: 90 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def exclude_tables_from_config(config_):
tables_ = config_.get("tables", None)
if tables_ is not None:
tables = tables_.split(",")
return tables

exclude_tables = exclude_tables_from_config(config.get_section('alembic:exclude'))


def include_object(object, name, type_, reflected, compare_to):
if type_ == "table" and name in exclude_tables:
return False
else:
return True


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(url=url)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata
)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
22 changes: 22 additions & 0 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision}
Create Date: ${create_date}

"""

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
16 changes: 16 additions & 0 deletions packages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
build-essential
python
python-software-properties
python-pip
python-setuptools
python-dev
python-virtualenv
python-pip
python-psycopg2
postgresql
curl
git
git-core
nodejs
openssl
libssl-dev
1 change: 1 addition & 0 deletions project/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .app import create_app
3 changes: 3 additions & 0 deletions project/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from .views import *
15 changes: 15 additions & 0 deletions project/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-

from flask import (Blueprint, render_template, current_app)

from ..extensions import manager
# from ..models import MyModel


def initialize_api():
# List all Flask-Restless APIs here
# model_api = manager.create_api(MyModel, methods=['GET'])
pass


api = Blueprint('api', __name__, url_prefix='/api')
Loading

0 comments on commit 3425976

Please sign in to comment.