Changeset - af24f82995c9
[Not reviewed]
0 15 0
Lance Edgar (lance) - 11 years ago 2013-12-17 07:41:07
lance@edbob.org
Refactored model imports, etc.

This is in preparation for using database models only from `rattail` (i.e. no
`edbob`). Mostly the model and enum imports were affected.
15 files changed with 112 insertions and 110 deletions:
0 comments (0 inline, 0 general)
rattail/batches/providers/__init__.py
Show inline comments
 
@@ -30,14 +30,14 @@ import datetime
 

	
 
import edbob
 

	
 
import rattail
 
from ...core import Object
 
from rattail import sil
 

	
 

	
 
__all__ = ['BatchProvider']
 

	
 

	
 
class BatchProvider(edbob.Object):
 
class BatchProvider(Object):
 

	
 
    name = None
 
    description = None
 
@@ -87,9 +87,11 @@ class BatchProvider(edbob.Object):
 
        raise NotImplementedError
 

	
 
    def make_batch(self, session, data, progress=None):
 
        from ...db import model
 

	
 
        self.session = session
 

	
 
        batch = rattail.Batch()
 
        batch = model.Batch()
 
        batch.provider = self.name
 
        batch.source = self.source
 
        batch.id = sil.consume_batch_id(batch.source)
rattail/batches/providers/labels.py
Show inline comments
 
@@ -7,6 +7,7 @@
 
from sqlalchemy.orm import object_session
 

	
 
import rattail
 
from ...db import model
 
from rattail.batches.providers import BatchProvider
 

	
 

	
 
@@ -29,8 +30,8 @@ class PrintLabels(BatchProvider):
 
    def add_rows_begin(self, batch, data):
 
        session = object_session(batch)
 
        if not self.default_profile:
 
            q = session.query(rattail.LabelProfile)
 
            q = q.order_by(rattail.LabelProfile.ordinal)
 
            q = session.query(model.LabelProfile)
 
            q = q.order_by(model.LabelProfile.ordinal)
 
            self.default_profile = q.first()
 
            assert self.default_profile
 
        else:
 
@@ -60,14 +61,14 @@ class PrintLabels(BatchProvider):
 

	
 
            profile = profiles.get(row.F95)
 
            if not profile:
 
                q = session.query(rattail.LabelProfile)
 
                q = q.filter(rattail.LabelProfile.code == row.F95)
 
                q = session.query(model.LabelProfile)
 
                q = q.filter(model.LabelProfile.code == row.F95)
 
                profile = q.one()
 
                profile.labels = []
 
                profiles[row.F95] = profile
 

	
 
            q = session.query(rattail.Product)
 
            q = q.filter(rattail.Product.upc == row.F01)
 
            q = session.query(model.Product)
 
            q = q.filter(model.Product.upc == row.F01)
 
            product = q.one()
 

	
 
            profile.labels.append((product, row.F94))
 
@@ -90,8 +91,8 @@ class PrintLabels(BatchProvider):
 
    def set_params(self, session, **params):
 
        profile = params.get('profile')
 
        if profile:
 
            q = session.query(rattail.LabelProfile)
 
            q = q.filter(rattail.LabelProfile.code == profile)
 
            q = session.query(model.LabelProfile)
 
            q = q.filter(model.LabelProfile.code == profile)
 
            self.default_profile = q.one()
 

	
 
        quantity = params.get('quantity')
rattail/commands.py
Show inline comments
 
@@ -33,7 +33,9 @@ import edbob
 
from edbob import commands
 
from edbob.commands import Subcommand
 

	
 
import rattail
 
from ._version import __version__
 
from .db import Session
 
from .db import model
 

	
 

	
 
class Command(commands.Command):
 
@@ -42,7 +44,7 @@ class Command(commands.Command):
 
    """
 
    
 
    name = 'rattail'
 
    version = rattail.__version__
 
    version = __version__
 
    description = "Retail Software Framework"
 
    long_description = """
 
Rattail is a retail software framework.
 
@@ -158,8 +160,8 @@ class Dump(Subcommand):
 
        from .db.dump import dump_data
 
        from .console import Progress
 

	
 
        if hasattr(edbob, args.model):
 
            model = getattr(edbob, args.model)
 
        if hasattr(model, args.model):
 
            cls = getattr(model, args.model)
 
        else:
 
            sys.stderr.write("Unknown model: {0}\n".format(args.model))
 
            sys.exit(1)
 
@@ -174,7 +176,7 @@ class Dump(Subcommand):
 
            output = sys.stdout
 

	
 
        session = Session()
 
        dump_data(session, model, output, progress=progress)
 
        dump_data(session, cls, output, progress=progress)
 
        session.close()
 

	
 
        if output is not sys.stdout:
 
@@ -298,7 +300,7 @@ class LoadHostDataCommand(Subcommand):
 
    description = "Load data from host database"
 

	
 
    def run(self, args):
 
        from edbob.console import Progress
 
        from .console import Progress
 
        from rattail.db import load
 

	
 
        edbob.init_modules(['edbob.db'])
 
@@ -440,7 +442,7 @@ class PurgeBatchesCommand(Subcommand):
 
        print "Purging batches from database:"
 
        print "    %s" % edbob.engine.url
 

	
 
        session = edbob.Session()
 
        session = Session()
 
        purged = purge_batches(session, purge_everything=args.all)
 
        session.commit()
 
        session.close()
rattail/db/api/products.py
Show inline comments
 
@@ -28,8 +28,7 @@
 

	
 
from sqlalchemy.orm.exc import NoResultFound
 

	
 
import rattail
 
from rattail.db.model import Product, ProductCode
 
from .. import model
 

	
 

	
 
__all__ = ['get_product_by_upc', 'get_product_by_code']
 
@@ -41,8 +40,8 @@ def get_product_by_upc(session, upc):
 
    ``None``.
 
    """
 

	
 
    q = session.query(rattail.Product)
 
    q = q.filter(rattail.Product.upc == upc)
 
    q = session.query(model.Product)
 
    q = q.filter(model.Product.upc == upc)
 
    try:
 
        return q.one()
 
    except NoResultFound:
 
@@ -65,7 +64,7 @@ def get_product_by_code(session, code):
 
       if multiple products would have matched the search.
 
    """
 

	
 
    q = session.query(Product)\
 
        .outerjoin(ProductCode)\
 
        .filter(ProductCode.code == code)
 
    q = session.query(model.Product)\
 
        .outerjoin(model.ProductCode)\
 
        .filter(model.ProductCode.code == code)
 
    return q.first()
rattail/db/batches/util.py
Show inline comments
 
@@ -33,8 +33,7 @@ from sqlalchemy import MetaData
 
import edbob
 
from edbob.time import local_time
 

	
 
import rattail
 
from rattail.db.extension.model import Batch
 
from .. import model
 

	
 

	
 
def purge_batches(session, effective_date=None, purge_everything=False):
 
@@ -61,10 +60,10 @@ def purge_batches(session, effective_date=None, purge_everything=False):
 

	
 
    purged = 0
 

	
 
    q = session.query(Batch)
 
    q = session.query(model.Batch)
 
    if not purge_everything:
 
        q = q.filter(Batch.purge != None)
 
        q = q.filter(Batch.purge < effective_date)
 
        q = q.filter(model.Batch.purge != None)
 
        q = q.filter(model.Batch.purge < effective_date)
 
    for batch in q:
 
        batch.drop_table()
 
        session.delete(batch)
 
@@ -78,7 +77,7 @@ def purge_batches(session, effective_date=None, purge_everything=False):
 
    batch_pattern = re.compile(r'^batch\.[0-9a-f]{32}$')
 

	
 
    current_batches = []
 
    for batch in session.query(Batch):
 
    for batch in session.query(model.Batch):
 
        current_batches.append('batch.%s' % batch.uuid)
 

	
 
    def orphaned_batches(name, metadata):
rattail/db/cache.py
Show inline comments
 
@@ -28,15 +28,13 @@
 

	
 
from sqlalchemy.orm import joinedload
 

	
 
import edbob
 
from edbob.util import requires_impl
 

	
 
import rattail
 
from rattail.db.model import (
 
    Department, Subdepartment, Family, Brand, Vendor, Product, CustomerGroup, Customer)
 
from ..core import Object
 
from . import model
 

	
 

	
 
class DataCacher(edbob.Object):
 
class DataCacher(Object):
 

	
 
    def __init__(self, session=None, **kwargs):
 
        super(DataCacher, self).__init__(session=session, **kwargs)
 
@@ -83,7 +81,7 @@ class DataCacher(edbob.Object):
 

	
 
class DepartmentCacher(DataCacher):
 

	
 
    class_ = Department
 
    class_ = model.Department
 

	
 
    def cache_instance(self, dept):
 
        self.instances[dept.number] = dept
 
@@ -91,7 +89,7 @@ class DepartmentCacher(DataCacher):
 

	
 
class SubdepartmentCacher(DataCacher):
 

	
 
    class_ = Subdepartment
 
    class_ = model.Subdepartment
 

	
 
    def cache_instance(self, subdept):
 
        self.instances[subdept.number] = subdept
 
@@ -99,7 +97,7 @@ class SubdepartmentCacher(DataCacher):
 

	
 
class FamilyCacher(DataCacher):
 

	
 
    class_ = Family
 
    class_ = model.Family
 

	
 
    def cache_instance(self, family):
 
        self.instances[family.code] = family
 
@@ -107,7 +105,7 @@ class FamilyCacher(DataCacher):
 

	
 
class BrandCacher(DataCacher):
 

	
 
    class_ = Brand
 
    class_ = model.Brand
 

	
 
    def cache_instance(self, brand):
 
        self.instances[brand.name] = brand
 
@@ -115,7 +113,7 @@ class BrandCacher(DataCacher):
 

	
 
class VendorCacher(DataCacher):
 

	
 
    class_ = Vendor
 
    class_ = model.Vendor
 

	
 
    def cache_instance(self, vend):
 
        self.instances[vend.id] = vend
 
@@ -123,14 +121,14 @@ class VendorCacher(DataCacher):
 

	
 
class ProductCacher(DataCacher):
 

	
 
    class_ = Product
 
    class_ = model.Product
 
    with_costs = False
 

	
 
    def query(self):
 
        q = self.session.query(Product)
 
        q = self.session.query(model.Product)
 
        if self.with_costs:
 
            q = q.options(joinedload(Product.costs))
 
            q = q.options(joinedload(Product.cost))
 
            q = q.options(joinedload(model.Product.costs))
 
            q = q.options(joinedload(model.Product.cost))
 
        return q
 

	
 
    def cache_instance(self, prod):
 
@@ -151,7 +149,7 @@ def get_product_cache(session, with_costs=False, progress=None):
 

	
 
class CustomerGroupCacher(DataCacher):
 

	
 
    class_ = CustomerGroup
 
    class_ = model.CustomerGroup
 

	
 
    def cache_instance(self, group):
 
        self.instances[group.id] = group
 
@@ -159,7 +157,7 @@ class CustomerGroupCacher(DataCacher):
 

	
 
class CustomerCacher(DataCacher):
 

	
 
    class_ = Customer
 
    class_ = model.Customer
 

	
 
    def cache_instance(self, customer):
 
        self.instances[customer.id] = customer
rattail/db/changes.py
Show inline comments
 
@@ -29,7 +29,7 @@ Data Changes
 
from sqlalchemy.event import listen
 
from sqlalchemy.orm import object_mapper, RelationshipProperty
 

	
 
from .model import Change, Batch, BatchColumn, BatchRow, Role, UserRole
 
from . import model
 
from ..core import get_uuid
 

	
 

	
 
@@ -103,7 +103,7 @@ class ChangeRecorder(object):
 
            return False
 

	
 
        # No need to record changes for batch data.
 
        if isinstance(instance, (Batch, BatchColumn, BatchRow)):
 
        if isinstance(instance, (model.Batch, model.BatchColumn, model.BatchRow)):
 
            return False
 

	
 
        # Ignore instances which don't use UUID.
 
@@ -111,17 +111,17 @@ class ChangeRecorder(object):
 
            return False
 

	
 
        # Ignore Role instances, if so configured.
 
        if self.ignore_role_changes and isinstance(instance, (Role, UserRole)):
 
        if self.ignore_role_changes and isinstance(instance, (model.Role, model.UserRole)):
 
            return False
 

	
 
        # Provide an UUID value, if necessary.
 
        self.ensure_uuid(instance)
 

	
 
        # Record the change.
 
        change = session.query(Change).get(
 
        change = session.query(model.Change).get(
 
            (instance.__class__.__name__, instance.uuid))
 
        if not change:
 
            change = Change(
 
            change = model.Change(
 
                class_name=instance.__class__.__name__,
 
                uuid=instance.uuid)
 
            session.add(change)
rattail/db/load.py
Show inline comments
 
@@ -29,19 +29,20 @@
 
from sqlalchemy.orm import joinedload
 

	
 
import edbob
 
import edbob.db
 

	
 
import rattail
 
from ..core import Object
 
from . import Session
 
from . import model
 

	
 

	
 
class LoadProcessor(edbob.Object):
 
class LoadProcessor(Object):
 

	
 
    def load_all_data(self, host_engine, progress=None):
 

	
 
        edbob.init_modules(['edbob.db', 'rattail.db'])
 

	
 
        self.host_session = edbob.Session(bind=host_engine)
 
        self.session = edbob.Session()
 
        self.host_session = Session(bind=host_engine)
 
        self.session = Session()
 

	
 
        cancel = False
 
        for cls in self.relevant_classes():
 
@@ -86,48 +87,48 @@ class LoadProcessor(edbob.Object):
 
        return not cancel
 

	
 
    def relevant_classes(self):
 
        yield edbob.Person
 
        yield edbob.User
 
        yield rattail.Store
 
        yield rattail.Department
 
        yield rattail.Subdepartment
 
        yield rattail.Category
 
        yield rattail.Brand
 
        yield rattail.Vendor
 
        yield rattail.Product
 
        yield rattail.CustomerGroup
 
        yield rattail.Customer
 
        yield rattail.Employee
 
        yield model.Person
 
        yield model.User
 
        yield model.Store
 
        yield model.Department
 
        yield model.Subdepartment
 
        yield model.Category
 
        yield model.Brand
 
        yield model.Vendor
 
        yield model.Product
 
        yield model.CustomerGroup
 
        yield model.Customer
 
        yield model.Employee
 

	
 
        classes = edbob.config.get('rattail.db', 'load.extra_classes')
 
        if classes:
 
            for cls in classes.split():
 
                yield getattr(edbob, cls)
 
                yield getattr(model, cls)
 

	
 
    def query_Customer(self, q):
 
        q = q.options(joinedload(rattail.Customer.phones))
 
        q = q.options(joinedload(rattail.Customer.emails))
 
        q = q.options(joinedload(rattail.Customer._people))
 
        q = q.options(joinedload(rattail.Customer._groups))
 
        q = q.options(joinedload(model.Customer.phones))
 
        q = q.options(joinedload(model.Customer.emails))
 
        q = q.options(joinedload(model.Customer._people))
 
        q = q.options(joinedload(model.Customer._groups))
 
        return q
 

	
 
    def query_CustomerPerson(self, q):
 
        q = q.options(joinedload(rattail.CustomerPerson.person))
 
        q = q.options(joinedload(model.CustomerPerson.person))
 
        return q
 

	
 
    def query_Employee(self, q):
 
        q = q.options(joinedload(rattail.Employee.phones))
 
        q = q.options(joinedload(rattail.Employee.emails))
 
        q = q.options(joinedload(model.Employee.phones))
 
        q = q.options(joinedload(model.Employee.emails))
 
        return q
 

	
 
    def query_Person(self, q):
 
        q = q.options(joinedload(edbob.Person.phones))
 
        q = q.options(joinedload(edbob.Person.emails))
 
        q = q.options(joinedload(model.Person.phones))
 
        q = q.options(joinedload(model.Person.emails))
 
        return q
 

	
 
    def query_Product(self, q):
 
        q = q.options(joinedload(rattail.Product.costs))
 
        q = q.options(joinedload(rattail.Product.prices))
 
        q = q.options(joinedload(model.Product.costs))
 
        q = q.options(joinedload(model.Product.prices))
 
        return q
 

	
 
    def merge_Product(self, host_product):
 
@@ -145,12 +146,12 @@ class LoadProcessor(edbob.Object):
 
            product.current_price = self.session.merge(host_product.current_price)
 

	
 
    def query_Store(self, q):
 
        q = q.options(joinedload(rattail.Store.phones))
 
        q = q.options(joinedload(rattail.Store.emails))
 
        q = q.options(joinedload(model.Store.phones))
 
        q = q.options(joinedload(model.Store.emails))
 
        return q
 

	
 
    def query_Vendor(self, q):
 
        q = q.options(joinedload(rattail.Vendor._contacts))
 
        q = q.options(joinedload(rattail.Vendor.phones))
 
        q = q.options(joinedload(rattail.Vendor.emails))
 
        q = q.options(joinedload(model.Vendor._contacts))
 
        q = q.options(joinedload(model.Vendor.phones))
 
        q = q.options(joinedload(model.Vendor.emails))
 
        return q
rattail/labels.py
Show inline comments
 
@@ -33,12 +33,15 @@ import shutil
 
from cStringIO import StringIO
 

	
 
import edbob
 
from edbob.util import OrderedDict, requires_impl
 
from edbob.util import requires_impl
 

	
 
from rattail.exceptions import LabelPrintingError
 
from .core import Object
 
from .util import OrderedDict
 
from .files import temp_path
 
from .exceptions import LabelPrintingError
 

	
 

	
 
class LabelPrinter(edbob.Object):
 
class LabelPrinter(Object):
 
    """
 
    Base class for all label printers.
 

	
 
@@ -115,7 +118,7 @@ class CommandFilePrinter(CommandPrinter):
 
        if not output_dir:
 
            raise LabelPrintingError("Printer does not have an output folder defined")
 

	
 
        labels_path = edbob.temp_path(prefix='rattail.', suffix='.labels')
 
        labels_path = temp_path(prefix='rattail.', suffix='.labels')
 
        labels_file = open(labels_path, 'w')
 

	
 
        header = self.batch_header_commands()
 
@@ -202,7 +205,7 @@ class CommandNetworkPrinter(CommandPrinter):
 
            data.close()
 

	
 

	
 
class LabelFormatter(edbob.Object):
 
class LabelFormatter(Object):
 
    """
 
    Base class for all label formatters.
 
    """
rattail/palm.py
Show inline comments
 
@@ -35,7 +35,6 @@ import logging
 

	
 
import edbob
 

	
 
import rattail
 
from rattail.csvutil import DictWriter
 

	
 
try:
rattail/sil/columns.py
Show inline comments
 
@@ -26,10 +26,10 @@
 
``rattail.sil.columns`` -- SIL Columns
 
"""
 

	
 
import edbob
 
from edbob.util import entry_point_map
 

	
 
from rattail.sil.exceptions import SILColumnNotFound
 
from ..core import Object
 
from .exceptions import SILColumnNotFound
 

	
 

	
 
__all__ = ['get_column']
 
@@ -38,13 +38,13 @@ __all__ = ['get_column']
 
supported_columns = None
 

	
 

	
 
class SILColumn(edbob.Object):
 
class SILColumn(Object):
 
    """
 
    Represents a column for use with SIL.
 
    """
 

	
 
    def __init__(self, name, data_type, description, display_name=None, **kwargs):
 
        edbob.Object.__init__(self, **kwargs)
 
        Object.__init__(self, **kwargs)
 
        self.name = name
 
        self.data_type = data_type
 
        self.description = description
rattail/sil/writer.py
Show inline comments
 
@@ -31,17 +31,18 @@ from decimal import Decimal
 

	
 
import edbob
 

	
 
import rattail
 
from rattail.gpc import GPC
 
from .._version import __version__
 
from ..core import Object
 
from ..gpc import GPC
 

	
 

	
 
__all__ = ['Writer']
 

	
 

	
 
class Writer(edbob.Object):
 
class Writer(Object):
 

	
 
    def __init__(self, path, **kwargs):
 
        edbob.Object.__init__(self, **kwargs)
 
        Object.__init__(self, **kwargs)
 
        self.sil_path = path
 
        self.fileobj = self.get_fileobj()
 

	
 
@@ -199,7 +200,7 @@ class Writer(edbob.Object):
 

	
 
            # Provide default for H20 (Software Revision) if none specified.
 
            if 'H20' not in kw:
 
                kw['H20'] = rattail.__version__[:4]
 
                kw['H20'] = __version__[:4]
 

	
 
        # Provide default (current local time) values H07 and H08 (Origin Date /
 
        # Time) if none was specified.
rattail/wince.py
Show inline comments
 
@@ -31,8 +31,8 @@ import os.path
 

	
 
import edbob
 

	
 
from rattail import files
 
from rattail.csvutil import DictWriter
 
from . import files
 
from .csvutil import DictWriter
 

	
 

	
 
def collect_batch(path, device='Default'):
 
@@ -61,7 +61,7 @@ def collect_batch(path, device='Default'):
 
            ctime.strftime('%Y-%m-%d %H-%M-%S'),
 
            os.path.basename(path)))
 

	
 
    csv_path = edbob.temp_path(prefix='rattail.', suffix='.csv')
 
    csv_path = files.temp_path(suffix='.csv')
 
    csv_file = open(csv_path, 'wb')
 
    writer = DictWriter(csv_file, ['upc', 'cases', 'units'])
 
    writer.writeheader()
tests/db/__init__.py
Show inline comments
 
@@ -3,7 +3,8 @@ import unittest
 

	
 
from sqlalchemy import create_engine
 

	
 
from edbob.db import Session, Base
 
from rattail.db import Session
 
from rattail.db.model import Base
 

	
 

	
 
__all__ = ['DataTestCase']
tests/db/test_changes.py
Show inline comments
 
@@ -86,11 +86,7 @@ class TestChangeRecorder(TestCase):
 
        # mock up session to force new change creation
 
        session.query.return_value = session
 
        session.get.return_value = None
 
        with patch('rattail.db.changes.Change') as MockChange:
 
            new_change = Mock()
 
            MockChange.return_value = new_change
 
            self.assertTrue(recorder.record_change(session, Product()))
 
            session.add.assert_called_once_with(new_change)
 
        self.assertTrue(recorder.record_change(session, Product()))
 

	
 
    @patch.multiple('rattail.db.changes', get_uuid=DEFAULT, object_mapper=DEFAULT)
 
    def test_ensure_uuid(self, get_uuid, object_mapper):
0 comments (0 inline, 0 general)