Files
@ 527f617822ba
Branch filter:
Location: rattail-project/rattail/rattail/db/load.py
527f617822ba
5.1 KiB
text/x-python
Update changelog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2014 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Load Data from Host
"""
from __future__ import unicode_literals
from sqlalchemy.orm import joinedload
from . import model
from . import Session
class LoadProcessor(object):
def __init__(self, config):
self.config = config
def load_all_data(self, host_engine, progress=None):
self.host_session = Session(bind=host_engine)
self.session = Session()
cancel = False
for cls in self.relevant_classes():
if not self.load_class_data(cls, progress):
cancel = True
break
self.host_session.close()
if cancel:
self.session.rollback()
else:
self.session.commit()
self.session.close()
return not cancel
def load_class_data(self, cls, progress=None):
query = self.host_session.query(cls)
if hasattr(self, 'query_%s' % cls.__name__):
query = getattr(self, 'query_%s' % cls.__name__)(query)
count = query.count()
if not count:
return True
prog = None
if progress:
prog = progress("Loading %s data" % cls.__name__, count)
cancel = False
for i, instance in enumerate(query, 1):
if hasattr(self, 'merge_%s' % cls.__name__):
getattr(self, 'merge_%s' % cls.__name__)(instance)
else:
self.session.merge(instance)
self.session.flush()
if prog and not prog.update(i):
cancel = True
break
if prog:
prog.destroy()
return not cancel
def relevant_classes(self):
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 = self.config.get('rattail.db', 'load.extra_classes')
if classes:
for cls in classes.split():
yield getattr(model, cls)
def query_Customer(self, q):
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(model.CustomerPerson.person))
return q
def query_Employee(self, q):
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(model.Person.phones))
q = q.options(joinedload(model.Person.emails))
return q
def query_Product(self, q):
q = q.options(joinedload(model.Product.costs))
q = q.options(joinedload(model.Product.prices))
return q
def merge_Product(self, host_product):
# This logic is necessary due to the inter-dependency between Product
# and ProductPrice tables. merge() will cause a flush(); however it
# apparently will not honor the 'post_update=True' flag on the relevant
# relationships.. I'm unclear whether this is a "bug" with SQLAlchemy,
# but the workaround is simple enough that I'm leaving it for now.
product = self.session.merge(host_product)
product.regular_price_uuid = None
product.current_price_uuid = None
if host_product.regular_price_uuid:
product.regular_price = self.session.merge(host_product.regular_price)
if host_product.current_price_uuid:
product.current_price = self.session.merge(host_product.current_price)
def query_Store(self, q):
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(model.Vendor._contacts))
q = q.options(joinedload(model.Vendor.phones))
q = q.options(joinedload(model.Vendor.emails))
return q
|