save point
This commit is contained in:
parent
c9d3a2c064
commit
d46ae258fc
3 changed files with 122 additions and 65 deletions
|
@ -147,9 +147,14 @@ class RattailBatchTerminal(BatchTerminal):
|
||||||
}
|
}
|
||||||
|
|
||||||
target_columns = {
|
target_columns = {
|
||||||
|
'DEPT_DCT': [
|
||||||
|
'F03',
|
||||||
|
'F238',
|
||||||
|
],
|
||||||
'ITEM_DCT': [
|
'ITEM_DCT': [
|
||||||
'F01',
|
'F01',
|
||||||
'F02',
|
'F02',
|
||||||
|
'F155',
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,21 +183,52 @@ class RattailBatchTerminal(BatchTerminal):
|
||||||
|
|
||||||
# return batch
|
# return batch
|
||||||
|
|
||||||
|
def add_departments(self, session, batch):
|
||||||
|
for row in batch.provide_rows():
|
||||||
|
dept = rattail.Department()
|
||||||
|
dept.number = row.F03
|
||||||
|
dept.name = row.F238
|
||||||
|
session.add(dept)
|
||||||
|
session.flush()
|
||||||
|
|
||||||
|
def add_products(self, session, batch):
|
||||||
|
q = session.query(rattail.Brand)
|
||||||
|
brands = {}
|
||||||
|
for brand in q:
|
||||||
|
brands[brand.name] = brand
|
||||||
|
|
||||||
|
for row in batch.provide_rows():
|
||||||
|
if row.F155 in brands:
|
||||||
|
brand = brands[row.F155]
|
||||||
|
else:
|
||||||
|
brand = rattail.Brand(name=row.F155)
|
||||||
|
session.add(brand)
|
||||||
|
session.flush()
|
||||||
|
brands[brand.name] = brand
|
||||||
|
prod = rattail.Product()
|
||||||
|
prod.upc = row.F01
|
||||||
|
prod.description = row.F02
|
||||||
|
prod.brand = brand
|
||||||
|
session.add(prod)
|
||||||
|
session.flush()
|
||||||
|
|
||||||
def execute_batch(self, batch):
|
def execute_batch(self, batch):
|
||||||
"""
|
"""
|
||||||
Executes ``batch``, which should be a :class:`rattail.Batch` instance.
|
Executes ``batch``, which should be a :class:`rattail.Batch` instance.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert batch.action_type == rattail.BATCH_ADD
|
|
||||||
assert batch.dictionary.name == 'ITEM_DCT'
|
|
||||||
|
|
||||||
session = object_session(batch)
|
session = object_session(batch)
|
||||||
for row in batch.provide_rows():
|
|
||||||
prod = rattail.Product()
|
if batch.action_type == rattail.BATCH_ADD:
|
||||||
prod.upc = row.F01
|
|
||||||
prod.description = row.F02
|
if batch.dictionary.name == 'DEPT_DCT':
|
||||||
session.add(prod)
|
self.add_departments(session, batch)
|
||||||
session.flush()
|
return
|
||||||
|
if batch.dictionary.name == 'ITEM_DCT':
|
||||||
|
self.add_products(session, batch)
|
||||||
|
return
|
||||||
|
|
||||||
|
assert False, "FIXME"
|
||||||
|
|
||||||
|
|
||||||
# def make_batch(source, elements, session, batch_id=None, **kwargs):
|
# def make_batch(source, elements, session, batch_id=None, **kwargs):
|
||||||
|
|
|
@ -41,6 +41,9 @@ def init_database(session):
|
||||||
columns = [
|
columns = [
|
||||||
('F01', 'UPC', 'GPC(14)'),
|
('F01', 'UPC', 'GPC(14)'),
|
||||||
('F02', 'Description', 'CHAR(20)'),
|
('F02', 'Description', 'CHAR(20)'),
|
||||||
|
('F03', 'Department Number', 'NUMBER(4,0)'),
|
||||||
|
('F155', 'Brand', 'CHAR(30)'),
|
||||||
|
('F238', 'Department Name', 'CHAR(30)'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for name, disp, dtype in columns:
|
for name, disp, dtype in columns:
|
||||||
|
@ -49,16 +52,20 @@ def init_database(session):
|
||||||
session.flush()
|
session.flush()
|
||||||
|
|
||||||
dictionaries = [
|
dictionaries = [
|
||||||
# ('CLASS_GROUP', 'Scale Class / Group', []),
|
('DEPT_DCT', 'Department', [
|
||||||
# ('DEPT_DCT', 'Department', []),
|
('F03', True),
|
||||||
# ('FCOST_DCT', 'Future Cost', []),
|
'F238',
|
||||||
# ('FSPRICE_DCT', 'Future Sale Price', []),
|
]),
|
||||||
('ITEM_DCT', 'Product', [
|
('ITEM_DCT', 'Product', [
|
||||||
('F01', True),
|
('F01', True),
|
||||||
'F02',
|
'F02',
|
||||||
|
'F155',
|
||||||
]),
|
]),
|
||||||
# ('NUTRITION', 'Scale Nutrition', []),
|
|
||||||
# ('PRICE_DCT', 'Price', []),
|
# ('PRICE_DCT', 'Price', []),
|
||||||
|
# ('FCOST_DCT', 'Future Cost', []),
|
||||||
|
# ('FSPRICE_DCT', 'Future Sale Price', []),
|
||||||
|
# ('CLASS_GROUP', 'Scale Class / Group', []),
|
||||||
|
# ('NUTRITION', 'Scale Nutrition', []),
|
||||||
# ('SCALE_TEXT', 'Scale Text', []),
|
# ('SCALE_TEXT', 'Scale Text', []),
|
||||||
# ('VENDOR_DCT', 'Vendor', []),
|
# ('VENDOR_DCT', 'Vendor', []),
|
||||||
]
|
]
|
||||||
|
@ -73,6 +80,6 @@ def init_database(session):
|
||||||
q = q.filter(rattail.SilColumn.sil_name == col)
|
q = q.filter(rattail.SilColumn.sil_name == col)
|
||||||
col = q.one()
|
col = q.one()
|
||||||
bd.columns.append(
|
bd.columns.append(
|
||||||
rattail.BatchDictionaryColumn(column=col, key=key))
|
rattail.BatchDictionaryColumn(sil_column=col, key=key))
|
||||||
session.add(bd)
|
session.add(bd)
|
||||||
session.flush()
|
session.flush()
|
||||||
|
|
114
rattail/model.py
114
rattail/model.py
|
@ -29,7 +29,7 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from sqlalchemy import (Column, String, Integer, Date, DateTime,
|
from sqlalchemy import (Column, String, Integer, Date, DateTime,
|
||||||
Boolean, Text, ForeignKey, BigInteger)
|
Boolean, Text, ForeignKey, BigInteger, Numeric)
|
||||||
from sqlalchemy.orm import relationship, object_session
|
from sqlalchemy.orm import relationship, object_session
|
||||||
|
|
||||||
import edbob
|
import edbob
|
||||||
|
@ -37,10 +37,10 @@ from edbob.db.model import Base, uuid_column
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['SilColumn', 'BatchDictionaryColumn', 'BatchDictionary',
|
__all__ = ['SilColumn', 'BatchDictionaryColumn', 'BatchDictionary',
|
||||||
'BatchTerminalSourceColumn', 'BatchTerminalTargetColumn',
|
'BatchTerminalColumn', 'BatchTerminal', 'BatchColumn',
|
||||||
'BatchTerminal', 'BatchColumn', 'Batch', 'Brand', 'Product']
|
'Batch', 'Brand', 'Department', 'Product']
|
||||||
|
|
||||||
sil_type_pattern = re.compile(r'^(CHAR)\((\d+)\)$')
|
sil_type_pattern = re.compile(r'^(CHAR|NUMBER)\((\d+(?:\,\d+)?)\)$')
|
||||||
|
|
||||||
|
|
||||||
class SilColumn(Base):
|
class SilColumn(Base):
|
||||||
|
@ -71,16 +71,16 @@ class BatchDictionaryColumn(Base):
|
||||||
|
|
||||||
uuid = uuid_column()
|
uuid = uuid_column()
|
||||||
dictionary_uuid = Column(String(32), ForeignKey('batch_dictionaries.uuid'))
|
dictionary_uuid = Column(String(32), ForeignKey('batch_dictionaries.uuid'))
|
||||||
column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
sil_column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
||||||
key = Column(Boolean)
|
key = Column(Boolean)
|
||||||
|
|
||||||
column = relationship(SilColumn)
|
sil_column = relationship(SilColumn)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<BatchDictionaryColumn: %s>" % self.column
|
return "<BatchDictionaryColumn: %s>" % self.sil_column
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.column or '')
|
return str(self.sil_column or '')
|
||||||
|
|
||||||
|
|
||||||
class BatchDictionary(Base):
|
class BatchDictionary(Base):
|
||||||
|
@ -106,52 +106,30 @@ class BatchDictionary(Base):
|
||||||
return str(self.description or '')
|
return str(self.description or '')
|
||||||
|
|
||||||
|
|
||||||
class BatchTerminalSourceColumn(Base):
|
class BatchTerminalColumn(Base):
|
||||||
"""
|
"""
|
||||||
Represents a "source column" supported by a :class:`BatchTerminal`.
|
Represents a column supported by a :class:`BatchTerminal`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = 'batch_terminal_source_columns'
|
__tablename__ = 'batch_terminal_columns'
|
||||||
|
|
||||||
uuid = uuid_column()
|
uuid = uuid_column()
|
||||||
terminal_uuid = Column(String(32), ForeignKey('batch_terminals.uuid'))
|
terminal_uuid = Column(String(32), ForeignKey('batch_terminals.uuid'))
|
||||||
dictionary_uuid = Column(String(32), ForeignKey('batch_dictionaries.uuid'))
|
dictionary_uuid = Column(String(32), ForeignKey('batch_dictionaries.uuid'))
|
||||||
column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
sil_column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
||||||
ordinal = Column(Integer)
|
ordinal = Column(Integer)
|
||||||
|
source = Column(Boolean)
|
||||||
|
target = Column(Boolean)
|
||||||
|
|
||||||
dictionary = relationship(BatchDictionary)
|
dictionary = relationship(BatchDictionary)
|
||||||
column = relationship(SilColumn)
|
sil_column = relationship(SilColumn)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<BatchTerminalSourceColumn: %s, %s, %s>" % (
|
return "<BatchTerminalColumn: %s, %s, %s>" % (
|
||||||
self.terminal, self.dictionary, self.name)
|
self.terminal, self.dictionary, self.sil_column)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.name or '')
|
return str(self.sil_column or '')
|
||||||
|
|
||||||
|
|
||||||
class BatchTerminalTargetColumn(Base):
|
|
||||||
"""
|
|
||||||
Represents a "target column" supported by a :class:`BatchTerminal`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__tablename__ = 'batch_terminal_target_columns'
|
|
||||||
|
|
||||||
uuid = uuid_column()
|
|
||||||
terminal_uuid = Column(String(32), ForeignKey('batch_terminals.uuid'))
|
|
||||||
dictionary_uuid = Column(String(32), ForeignKey('batch_dictionaries.uuid'))
|
|
||||||
column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
|
||||||
ordinal = Column(Integer)
|
|
||||||
|
|
||||||
dictionary = relationship(BatchDictionary)
|
|
||||||
column = relationship(SilColumn)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return "<BatchTerminalTargetColumn: %s, %s, %s>" % (
|
|
||||||
self.terminal, self.dictionary, self.name)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return str(self.name or '')
|
|
||||||
|
|
||||||
|
|
||||||
class BatchTerminal(Base):
|
class BatchTerminal(Base):
|
||||||
|
@ -171,11 +149,8 @@ class BatchTerminal(Base):
|
||||||
source_kwargs = Column(Text)
|
source_kwargs = Column(Text)
|
||||||
target_kwargs = Column(Text)
|
target_kwargs = Column(Text)
|
||||||
|
|
||||||
source_columns = relationship(
|
columns = relationship(
|
||||||
BatchTerminalSourceColumn,
|
BatchTerminalColumn,
|
||||||
backref='terminal')
|
|
||||||
target_columns = relationship(
|
|
||||||
BatchTerminalTargetColumn,
|
|
||||||
backref='terminal')
|
backref='terminal')
|
||||||
|
|
||||||
_terminal = 'not_got_yet'
|
_terminal = 'not_got_yet'
|
||||||
|
@ -186,6 +161,11 @@ class BatchTerminal(Base):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.description or '')
|
return str(self.description or '')
|
||||||
|
|
||||||
|
def source_columns(self, dictionary):
|
||||||
|
for col in self.columns:
|
||||||
|
if col.dictionary is dictionary:
|
||||||
|
yield col
|
||||||
|
|
||||||
def get_terminal(self):
|
def get_terminal(self):
|
||||||
"""
|
"""
|
||||||
Returns the :class:`rattail.batches.BatchTerminal` instance which is
|
Returns the :class:`rattail.batches.BatchTerminal` instance which is
|
||||||
|
@ -211,11 +191,11 @@ class BatchColumn(Base):
|
||||||
uuid = uuid_column()
|
uuid = uuid_column()
|
||||||
batch_uuid = Column(String(32), ForeignKey('batches.uuid'))
|
batch_uuid = Column(String(32), ForeignKey('batches.uuid'))
|
||||||
ordinal = Column(Integer)
|
ordinal = Column(Integer)
|
||||||
column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
sil_column_uuid = Column(String(32), ForeignKey('sil_columns.uuid'))
|
||||||
source_uuid = Column(String(32), ForeignKey('batch_terminals.uuid'))
|
source_uuid = Column(String(32), ForeignKey('batch_terminals.uuid'))
|
||||||
targeted = Column(Boolean)
|
targeted = Column(Boolean)
|
||||||
|
|
||||||
column = relationship(SilColumn)
|
sil_column = relationship(SilColumn)
|
||||||
|
|
||||||
source = relationship(
|
source = relationship(
|
||||||
BatchTerminal,
|
BatchTerminal,
|
||||||
|
@ -224,7 +204,7 @@ class BatchColumn(Base):
|
||||||
)
|
)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<BatchColumn: %s, %s>" % (self.batch, self.column)
|
return "<BatchColumn: %s, %s>" % (self.batch, self.sil_column)
|
||||||
|
|
||||||
|
|
||||||
# def get_sil_column(name):
|
# def get_sil_column(name):
|
||||||
|
@ -265,8 +245,18 @@ def get_sil_type(data_type):
|
||||||
m = sil_type_pattern.match(data_type)
|
m = sil_type_pattern.match(data_type)
|
||||||
if m:
|
if m:
|
||||||
data_type, precision = m.groups()
|
data_type, precision = m.groups()
|
||||||
|
if precision.isdigit():
|
||||||
|
precision = int(precision)
|
||||||
|
scale = 0
|
||||||
|
else:
|
||||||
|
precision, scale = precision.split(',')
|
||||||
|
precision = int(precision)
|
||||||
|
scale = int(scale)
|
||||||
if data_type == 'CHAR':
|
if data_type == 'CHAR':
|
||||||
return String(int(precision))
|
assert not scale, "FIXME"
|
||||||
|
return String(precision)
|
||||||
|
if data_type == 'NUMBER':
|
||||||
|
return Numeric(precision, scale)
|
||||||
|
|
||||||
assert False, "FIXME"
|
assert False, "FIXME"
|
||||||
|
|
||||||
|
@ -418,7 +408,7 @@ class Batch(Base):
|
||||||
'uuid': uuid_column(),
|
'uuid': uuid_column(),
|
||||||
}
|
}
|
||||||
for col in self.columns:
|
for col in self.columns:
|
||||||
kwargs[col.column.sil_name] = Column(get_sil_type(col.column.data_type))
|
kwargs[col.sil_column.sil_name] = Column(get_sil_type(col.sil_column.data_type))
|
||||||
self._row_classes[self.uuid] = type('BatchRow', (Base,), kwargs)
|
self._row_classes[self.uuid] = type('BatchRow', (Base,), kwargs)
|
||||||
return self._row_classes[self.uuid]
|
return self._row_classes[self.uuid]
|
||||||
|
|
||||||
|
@ -512,6 +502,30 @@ class Brand(Base):
|
||||||
uuid = uuid_column()
|
uuid = uuid_column()
|
||||||
name = Column(String(100))
|
name = Column(String(100))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Brand: %s>" % self.name
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.name or '')
|
||||||
|
|
||||||
|
|
||||||
|
class Department(Base):
|
||||||
|
"""
|
||||||
|
Represents an organizational department.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__tablename__ = 'departments'
|
||||||
|
|
||||||
|
uuid = uuid_column()
|
||||||
|
number = Column(Integer)
|
||||||
|
name = Column(String(30))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Department: %s>" % self.name
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.name)
|
||||||
|
|
||||||
|
|
||||||
class Product(Base):
|
class Product(Base):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue