From 72a4c347d8cba15c789177baf77e86153b58d345 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 4 Mar 2020 19:08:20 -0600 Subject: [PATCH] Define custom data model; add alembic scripts; etc. actually our "custom" data model is just rattail + rattail-corepos --- rattail_demo/config.py | 5 +- rattail_demo/db/__init__.py | 0 rattail_demo/db/alembic/env.py | 74 ++++++++++++++++++++++++++ rattail_demo/db/alembic/script.py.mako | 28 ++++++++++ rattail_demo/db/model.py | 10 ++++ rattail_demo/web/views/__init__.py | 2 +- rattail_demo/web/views/vendors.py | 18 +++++++ 7 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 rattail_demo/db/__init__.py create mode 100644 rattail_demo/db/alembic/env.py create mode 100644 rattail_demo/db/alembic/script.py.mako create mode 100644 rattail_demo/db/model.py create mode 100644 rattail_demo/web/views/vendors.py diff --git a/rattail_demo/config.py b/rattail_demo/config.py index 8cd6ccb..0545139 100644 --- a/rattail_demo/config.py +++ b/rattail_demo/config.py @@ -1,10 +1,8 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8; -*- """ Rattail Demo config extension """ -from __future__ import unicode_literals, absolute_import - from rattail.config import ConfigExtension @@ -17,5 +15,6 @@ class DemoConfigExtension(ConfigExtension): def configure(self, config): # tell rattail where our stuff lives + config.setdefault('rattail', 'model', 'rattail_demo.db.model') config.setdefault('rattail.mail', 'emails', 'rattail_demo.emails') config.setdefault('tailbone', 'menus', 'rattail_demo.web.menus') diff --git a/rattail_demo/db/__init__.py b/rattail_demo/db/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rattail_demo/db/alembic/env.py b/rattail_demo/db/alembic/env.py new file mode 100644 index 0000000..5a9183c --- /dev/null +++ b/rattail_demo/db/alembic/env.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8; mode: python; -*- +""" +Alembic environment script +""" + +from alembic import context +from sqlalchemy.orm import configure_mappers + +from rattail.config import make_config +from rattail.db.util import get_default_engine +from rattail.db.continuum import configure_versioning + + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +alembic_config = context.config + +# use same config file for Rattail +rattail_config = make_config(alembic_config.config_file_name, usedb=False, versioning=False) + +# configure Continuum..this is trickier than we want but it works.. +configure_versioning(rattail_config, force=True) +from rattail_demo.db import model +configure_mappers() + +# needed for 'autogenerate' support +target_metadata = model.Base.metadata + + +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. + + """ + engine = get_default_engine(rattail_config) + context.configure( + url=engine.url, + target_metadata=target_metadata) + + 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 = get_default_engine(rattail_config) + 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() diff --git a/rattail_demo/db/alembic/script.py.mako b/rattail_demo/db/alembic/script.py.mako new file mode 100644 index 0000000..4533c7c --- /dev/null +++ b/rattail_demo/db/alembic/script.py.mako @@ -0,0 +1,28 @@ +# -*- coding: utf-8; mode: python; -*- +# -*- coding: utf-8 -*- +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + +from alembic import op +import sqlalchemy as sa +import rattail.db.types +${imports if imports else ""} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/rattail_demo/db/model.py b/rattail_demo/db/model.py new file mode 100644 index 0000000..947595b --- /dev/null +++ b/rattail_demo/db/model.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8; -*- +""" +Rattail Demo data model +""" + +# bring in all the normal stuff from Rattail +from rattail.db.model import * + +# also bring in CORE integration models +from rattail_corepos.db.model import * diff --git a/rattail_demo/web/views/__init__.py b/rattail_demo/web/views/__init__.py index 536c347..4364daf 100644 --- a/rattail_demo/web/views/__init__.py +++ b/rattail_demo/web/views/__init__.py @@ -34,7 +34,7 @@ def includeme(config): config.include('rattail_demo.web.views.tempmon') config.include('rattail_demo.web.views.upgrades') config.include('rattail_demo.web.views.users') - config.include('tailbone.views.vendors') + config.include('rattail_demo.web.views.vendors') # core-pos views config.include('tailbone_corepos.views.corepos') diff --git a/rattail_demo/web/views/vendors.py b/rattail_demo/web/views/vendors.py new file mode 100644 index 0000000..c3b4b86 --- /dev/null +++ b/rattail_demo/web/views/vendors.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8; -*- +""" +Vendor views +""" + +from tailbone.views import vendors as base +# NOTE: the main point of this module is to bring this in +from tailbone_corepos.views.vendors import VendorView + + +def includeme(config): + + # autocomplete + config.add_route('vendors.autocomplete', '/vendors/autocomplete') + config.add_view(base.VendorsAutocomplete, route_name='vendors.autocomplete', + renderer='json', permission='vendors.list') + + VendorView.defaults(config)