Skip to content

Commit bfd4427

Browse files
authored
Add files via upload
0 parents  commit bfd4427

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+656
-0
lines changed

subscription/app_setup.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from configure import detect_configuration_mode
2+
from subscription import create_app
3+
from subscription.core import db
4+
5+
6+
configuration_mode = detect_configuration_mode()
7+
8+
application = create_app(configuration_mode)
9+
10+
with application.app_context():
11+
db.init_app(application)
12+
db.Model.metadata.reflect(db.engine) # load existing DB schema
13+
db.create_all()
14+

subscription/app_setup.pyc

552 Bytes
Binary file not shown.

subscription/configure/__init__.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
config
4+
----------------------
5+
A module providing all configuration for the application.
6+
"""
7+
import os
8+
9+
10+
basedir = os.path.abspath(os.path.dirname(__file__))
11+
12+
13+
class BaseConfig(object):
14+
SECRET_KEY = 'your-super-secret-key'
15+
LOG_REQUESTS = True
16+
17+
# pagination
18+
PAGINATION_DEFAULT_PAGE = 1
19+
PAGINATION_DEFAULT_PER_PAGE = 20
20+
21+
DEFAULT_REQUESTS_TIMEOUT = 11.0 # seconds
22+
23+
24+
class DevelopmentConfig(BaseConfig):
25+
DEBUG = True
26+
TESTING = False
27+
28+
29+
class PilotConfig(BaseConfig):
30+
DEBUG = False
31+
TESTING = False
32+
33+
34+
class ProductionConfig(BaseConfig):
35+
DEBUG = False
36+
TESTING = False
37+
38+
39+
class TestConfig(BaseConfig):
40+
DEBUG = False
41+
TESTING = True
42+
43+
# SQLALCHEMY_DATABASE_URI = "sqlite:///subscription_db"
44+
SQLALCHEMY_DATABASE_URI = "sqlite://" # in-memory database
45+
46+
47+
config_modes = {
48+
'development': DevelopmentConfig,
49+
'testing': TestConfig,
50+
'pilot': PilotConfig,
51+
'production': ProductionConfig
52+
}
53+
54+
55+
def detect_configuration_mode():
56+
from subscription.core.constants import ALLOWED_CONFIGURATION_MODES
57+
from subscription.core.errors import ConfigNotFound
58+
59+
config_mode = os.environ['RUNNING_MODE']
60+
if config_mode in ALLOWED_CONFIGURATION_MODES:
61+
print '"%s" configuration mode detected.' % config_mode
62+
return config_mode
63+
64+
error_message = ('Invalid or no configuration mode ("{0}") detected! '
65+
'Aborting...'.format(config_mode))
66+
raise ConfigNotFound(error_message)
67+
68+
69+
def load_configuration(app, mode):
70+
"""
71+
Load the applicable configuration for the application
72+
:param app: the application
73+
:param mode: the mode signalling which configuration object to load
74+
:return: None
75+
"""
76+
77+
configuration = config_modes[mode]
78+
app.config.from_object(configuration) # Load pre-defined by class/object
79+
80+
# Load from environment
81+
instance_config = {
82+
True: 'config.py',
83+
False: 'test_config.py'
84+
}[configuration != TestConfig]
85+
app.config.from_pyfile(instance_config, silent=True)
86+

subscription/configure/__init__.pyc

3.02 KB
Binary file not shown.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import multiprocessing
2+
3+
bind = '0.0.0.0:[PORT]' # sample '0.0.0.0:1234'
4+
workers = multiprocessing.cpu_count() # or a smaller number
5+
user = 'your_linux_username'
6+
accesslog = '/path/to/your_app/logs/gunicorn.log'
7+
errorlog = '/path/to/your_app/logs/gunicorn.err.log'
8+
loglevel = 'debug'
9+
access_log_format = '[%(h)15s] (%(s)s) [%(D)8sms] %(r)s | %(b)sb'
10+
proc_name = 'your_app_name_gunicorn'
11+
timeout=120

subscription/instance/config.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
# You need to replace the next values with the appropriate values for your configuration
4+
5+
basedir = os.path.abspath(os.path.dirname(__file__))
6+
SQLALCHEMY_ECHO = False
7+
SQLALCHEMY_TRACK_MODIFICATIONS = True
8+
SQLALCHEMY_DATABASE_URI = "postgresql://azeez:root@localhost/subscriptions"

subscription/manage.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from flask_script import Manager, Shell, Server
2+
from flask_migrate import Migrate, MigrateCommand
3+
4+
from app_setup import application
5+
from subscription.core.models import db
6+
7+
manager = Manager(application)
8+
migrate = Migrate(application, db)
9+
10+
11+
@manager.shell
12+
def _make_context():
13+
return dict(app=application, db=db)
14+
15+
16+
manager.add_command('db', MigrateCommand)
17+
manager.add_command("runserver", Server())
18+
manager.add_command("shell", Shell(make_context=_make_context))
19+
20+
21+
if __name__ == '__main__':
22+
manager.run()
23+

subscription/migrations/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

subscription/migrations/alembic.ini

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[handler_console]
38+
class = StreamHandler
39+
args = (sys.stderr,)
40+
level = NOTSET
41+
formatter = generic
42+
43+
[formatter_generic]
44+
format = %(levelname)-5.5s [%(name)s] %(message)s
45+
datefmt = %H:%M:%S

subscription/migrations/env.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from __future__ import with_statement
2+
from alembic import context
3+
from sqlalchemy import engine_from_config, pool
4+
from logging.config import fileConfig
5+
import logging
6+
7+
# this is the Alembic Config object, which provides
8+
# access to the values within the .ini file in use.
9+
config = context.config
10+
11+
# Interpret the config file for Python logging.
12+
# This line sets up loggers basically.
13+
fileConfig(config.config_file_name)
14+
logger = logging.getLogger('alembic.env')
15+
16+
# add your model's MetaData object here
17+
# for 'autogenerate' support
18+
# from myapp import mymodel
19+
# target_metadata = mymodel.Base.metadata
20+
from flask import current_app
21+
config.set_main_option('sqlalchemy.url',
22+
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
23+
target_metadata = current_app.extensions['migrate'].db.metadata
24+
25+
# other values from the config, defined by the needs of env.py,
26+
# can be acquired:
27+
# my_important_option = config.get_main_option("my_important_option")
28+
# ... etc.
29+
30+
31+
def run_migrations_offline():
32+
"""Run migrations in 'offline' mode.
33+
34+
This configures the context with just a URL
35+
and not an Engine, though an Engine is acceptable
36+
here as well. By skipping the Engine creation
37+
we don't even need a DBAPI to be available.
38+
39+
Calls to context.execute() here emit the given string to the
40+
script output.
41+
42+
"""
43+
url = config.get_main_option("sqlalchemy.url")
44+
context.configure(url=url)
45+
46+
with context.begin_transaction():
47+
context.run_migrations()
48+
49+
50+
def run_migrations_online():
51+
"""Run migrations in 'online' mode.
52+
53+
In this scenario we need to create an Engine
54+
and associate a connection with the context.
55+
56+
"""
57+
58+
# this callback is used to prevent an auto-migration from being generated
59+
# when there are no changes to the schema
60+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
61+
def process_revision_directives(context, revision, directives):
62+
if getattr(config.cmd_opts, 'autogenerate', False):
63+
script = directives[0]
64+
if script.upgrade_ops.is_empty():
65+
directives[:] = []
66+
logger.info('No changes in schema detected.')
67+
68+
engine = engine_from_config(config.get_section(config.config_ini_section),
69+
prefix='sqlalchemy.',
70+
poolclass=pool.NullPool)
71+
72+
connection = engine.connect()
73+
context.configure(connection=connection,
74+
target_metadata=target_metadata,
75+
process_revision_directives=process_revision_directives,
76+
**current_app.extensions['migrate'].configure_args)
77+
78+
try:
79+
with context.begin_transaction():
80+
context.run_migrations()
81+
finally:
82+
connection.close()
83+
84+
if context.is_offline_mode():
85+
run_migrations_offline()
86+
else:
87+
run_migrations_online()

subscription/migrations/env.pyc

2.46 KB
Binary file not shown.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""empty message
2+
3+
Revision ID: 23c17475112c
4+
Revises: cac1e8da4b7e
5+
Create Date: 2019-09-23 14:37:25.057891
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '23c17475112c'
14+
down_revision = 'cac1e8da4b7e'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('subscriptions',
22+
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
23+
sa.Column('email_address', sa.String(length=50), nullable=True),
24+
sa.Column('added_at', sa.DateTime(), nullable=False),
25+
sa.PrimaryKeyConstraint('id'),
26+
sa.UniqueConstraint('email_address')
27+
)
28+
op.drop_table('api_v1')
29+
# ### end Alembic commands ###
30+
31+
32+
def downgrade():
33+
# ### commands auto generated by Alembic - please adjust! ###
34+
op.create_table('api_v1',
35+
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
36+
sa.Column('email_address', sa.VARCHAR(length=50), autoincrement=False, nullable=True),
37+
sa.Column('added_at', postgresql.TIMESTAMP(), server_default=sa.text(u'CURRENT_TIMESTAMP'), autoincrement=False, nullable=False),
38+
sa.PrimaryKeyConstraint('id', name=u'subscribers_pkey'),
39+
sa.UniqueConstraint('email_address', name=u'subscribers_email_address_key')
40+
)
41+
op.drop_table('subscriptions')
42+
# ### end Alembic commands ###
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""empty message
2+
3+
Revision ID: 4fcb504c8a47
4+
Revises: 23c17475112c
5+
Create Date: 2019-09-24 17:15:18.565747
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '4fcb504c8a47'
14+
down_revision = '23c17475112c'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.add_column('subscriptions', sa.Column('email', sa.String(length=50), nullable=True))
22+
op.drop_constraint(u'subscriptions_email_address_key', 'subscriptions', type_='unique')
23+
op.create_unique_constraint(None, 'subscriptions', ['email'])
24+
op.drop_column('subscriptions', 'email_address')
25+
# ### end Alembic commands ###
26+
27+
28+
def downgrade():
29+
# ### commands auto generated by Alembic - please adjust! ###
30+
op.add_column('subscriptions', sa.Column('email_address', sa.VARCHAR(length=50), autoincrement=False, nullable=True))
31+
op.drop_constraint(None, 'subscriptions', type_='unique')
32+
op.create_unique_constraint(u'subscriptions_email_address_key', 'subscriptions', ['email_address'])
33+
op.drop_column('subscriptions', 'email')
34+
# ### end Alembic commands ###
Binary file not shown.

0 commit comments

Comments
 (0)