Files @ 96fdd9b3915c
Branch filter:

Location: rattail-project/rattail/rattail/db/extension/model.py

lance
Backout changeset 14e7e8f7a2e5 (see note)

Apparently PythonService.exe doesn't acknowledge the 'absolute_import' feature
from __future__...? So we can't have a 'rattail.sqlalchemy' module and instead
must keep 'GPCType' within 'rattail.gpc'.
  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#!/usr/bin/env python
# -*- coding: utf-8  -*-
################################################################################
#
#  Rattail -- Retail Software Framework
#  Copyright © 2010-2012 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/>.
#
################################################################################

"""
``rattail.db.extension.model`` -- Schema Definition
"""

from sqlalchemy import Column, ForeignKey
from sqlalchemy import String, Integer, DateTime, Date, Boolean, Numeric, Text
from sqlalchemy import types
from sqlalchemy import and_
from sqlalchemy.orm import relationship, object_session
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.orderinglist import ordering_list

import edbob
from edbob.db.model import Base, uuid_column
from edbob.db.extensions.contact import Person, EmailAddress, PhoneNumber
from edbob.exceptions import LoadSpecError
from edbob.sqlalchemy import getset_factory

from rattail import sil
from rattail import batches
from rattail.gpc import GPCType


__all__ = ['Department', 'Subdepartment', 'Brand', 'Category', 'Vendor',
           'VendorContact', 'VendorPhoneNumber', 'Product', 'ProductCost',
           'ProductPrice', 'Customer', 'CustomerEmailAddress',
           'CustomerPhoneNumber', 'CustomerGroup', 'CustomerGroupAssignment',
           'CustomerPerson', 'Employee', 'EmployeeEmailAddress',
           'EmployeePhoneNumber', 'BatchColumn', 'Batch', 'LabelProfile']


class BatchColumn(Base):
    """
    Represents a :class:`SilColumn` associated with a :class:`Batch`.
    """

    __tablename__ = 'batch_columns'

    uuid = uuid_column()
    batch_uuid = Column(String(32), ForeignKey('batches.uuid'))
    ordinal = Column(Integer, nullable=False)
    name = Column(String(20))
    display_name = Column(String(50))
    sil_name = Column(String(10))
    data_type = Column(String(15))
    description = Column(String(50))
    visible = Column(Boolean, default=True)

    def __init__(self, sil_name=None, **kwargs):
        if sil_name:
            kwargs['sil_name'] = sil_name
            sil_column = sil.get_column(sil_name)
            kwargs.setdefault('name', sil_name)
            kwargs.setdefault('data_type', sil_column.data_type)
            kwargs.setdefault('description', sil_column.description)
            kwargs.setdefault('display_name', sil_column.display_name)
        super(BatchColumn, self).__init__(**kwargs)

    def __repr__(self):
        return "<BatchColumn: %s>" % self.name

    def __unicode__(self):
        return unicode(self.display_name or '')


class BatchRow(edbob.Object):
    """
    Superclass of batch row objects.
    """

    def __unicode__(self):
        return u"Row %d" % self.ordinal


class Batch(Base):
    """
    Represents a SIL-compliant batch of data.
    """

    __tablename__ = 'batches'

    uuid = uuid_column()
    provider = Column(String(50))
    id = Column(String(8))
    source = Column(String(6))
    destination = Column(String(6))
    action_type = Column(String(6))
    description = Column(String(50))
    rowcount = Column(Integer, default=0)
    executed = Column(DateTime)
    purge = Column(Date)

    columns = relationship(BatchColumn, backref='batch',
                           collection_class=ordering_list('ordinal'),
                           order_by=BatchColumn.ordinal,
                           cascade='save-update, merge, delete, delete-orphan')

    _rowclasses = {}

    def __repr__(self):
        return "<Batch: %s>" % self.description

    def __unicode__(self):
        return unicode(self.description or '')

    @property
    def rowclass(self):
        """
        Returns the mapped class for the underlying row (data) table.
        """

        if not self.uuid:
            object_session(self).flush()
            assert self.uuid

        if self.uuid not in self._rowclasses:

            kwargs = {
                '__tablename__': 'batch.%s' % self.uuid,
                'uuid': uuid_column(),
                'ordinal': Column(Integer, nullable=False),
                }

            for column in self.columns:
                data_type = sil.get_sqlalchemy_type(column.data_type)
                kwargs[column.name] = Column(data_type)
            rowclass = type('BatchRow_%s' % str(self.uuid), (Base, BatchRow), kwargs)

            batch_uuid = self.uuid
            def batch(self):
                return object_session(self).query(Batch).get(batch_uuid)
            rowclass.batch = property(batch)

            self._rowclasses[self.uuid] = rowclass

        return self._rowclasses[self.uuid]

    def add_column(self, sil_name=None, **kwargs):
        column = BatchColumn(sil_name, **kwargs)
        self.columns.append(column)

    def add_row(self, row, **kwargs):
        """
        Adds a row to the batch data table.
        """

        session = object_session(self)
        # FIXME: This probably needs to use a func.max() query.
        row.ordinal = self.rowcount + 1
        session.add(row)
        self.rowcount += 1
        session.flush()

    def create_table(self):
        """
        Creates the batch's data table within the database.
        """

        self.rowclass.__table__.create()

    def drop_table(self):
        """
        Drops the batch's data table from the database.
        """

        self.rowclass.__table__.drop()

    def execute(self, progress=None):
        provider = self.get_provider()
        assert provider
        provider.execute(self, progress)
        self.executed = edbob.utc_time(naive=True)
        object_session(self).flush()

    def get_provider(self):
        assert self.provider
        return batches.get_provider(self.provider)

    def iter_rows(self):
        session = object_session(self)
        q = session.query(self.rowclass)
        q = q.order_by(self.rowclass.ordinal)
        return q


class Brand(Base):
    """
    Represents a brand or similar product line.
    """

    __tablename__ = 'brands'

    uuid = uuid_column()
    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 __unicode__(self):
        return unicode(self.name or '')


class Subdepartment(Base):
    """
    Represents an organizational subdepartment.
    """

    __tablename__ = 'subdepartments'

    uuid = uuid_column()
    number = Column(Integer)
    name = Column(String(30))
    department_uuid = Column(String(32), ForeignKey('departments.uuid'))

    department = relationship(Department)

    def __repr__(self):
        return "<Subdepartment: %s>" % self.name

    def __unicode__(self):
        return unicode(self.name or '')


class Category(Base):
    """
    Represents an organizational category for products.
    """

    __tablename__ = 'categories'

    uuid = uuid_column()
    number = Column(Integer)
    name = Column(String(50))
    department_uuid = Column(String(32), ForeignKey('departments.uuid'))

    department = relationship(Department)

    def __repr__(self):
        return "<Category: %s, %s>" % (self.number, self.name)

    def __str__(self):
        return str(self.name or '')

    def __unicode__(self):
        return unicode(self.name or '')


class VendorEmailAddress(EmailAddress):
    """
    Represents an email address associated with a :class:`Vendor`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Vendor'}


class VendorPhoneNumber(PhoneNumber):
    """
    Represents a phone (or fax) number associated with a :class:`Vendor`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Vendor'}


class VendorContact(Base):
    """
    Represents a point of contact (e.g. salesperson) for a vendor, from the
    retailer's perspective.
    """

    __tablename__ = 'vendor_contacts'

    uuid = uuid_column()
    vendor_uuid = Column(String(32), ForeignKey('vendors.uuid'), nullable=False)
    person_uuid = Column(String(32), ForeignKey('people.uuid'), nullable=False)
    preference = Column(Integer, nullable=False)

    person = relationship(Person)

    def __repr__(self):
        return "<VendorContact: %s, %s>" % (self.vendor, self.person)

    def __unicode__(self):
        return unicode(self.person)


class Vendor(Base):
    """
    Represents a vendor from which products are purchased by the store.
    """

    __tablename__ = 'vendors'

    uuid = uuid_column()
    id = Column(String(15))
    name = Column(String(40))

    contacts = relationship(VendorContact, backref='vendor',
                            collection_class=ordering_list('preference', count_from=1),
                            order_by=VendorContact.preference)

    def __repr__(self):
        return "<Vendor: %s>" % self.name

    def __unicode__(self):
        return unicode(self.name or '')

    def add_contact(self, person):
        contact = VendorContact(person=person)
        self.contacts.append(contact)

    def add_email_address(self, address, type='Info'):
        email = VendorEmailAddress(address=address, type=type)
        self.emails.append(email)

    def add_phone_number(self, number, type='Voice'):
        phone = VendorPhoneNumber(number=number, type=type)
        self.phones.append(phone)

Vendor.emails = relationship(
    VendorEmailAddress,
    backref='vendor',
    primaryjoin=VendorEmailAddress.parent_uuid == Vendor.uuid,
    foreign_keys=[VendorEmailAddress.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=VendorEmailAddress.preference,
    cascade='save-update, merge, delete, delete-orphan')

Vendor.email = relationship(
    VendorEmailAddress,
    primaryjoin=and_(
        VendorEmailAddress.parent_uuid == Vendor.uuid,
        VendorEmailAddress.preference == 1),
    foreign_keys=[VendorEmailAddress.parent_uuid],
    uselist=False,
    viewonly=True)

Vendor.phones = relationship(
    VendorPhoneNumber,
    backref='vendor',
    primaryjoin=VendorPhoneNumber.parent_uuid == Vendor.uuid,
    foreign_keys=[VendorPhoneNumber.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=VendorPhoneNumber.preference,
    cascade='save-update, merge, delete, delete-orphan')

Vendor.phone = relationship(
    VendorPhoneNumber,
    primaryjoin=and_(
        VendorPhoneNumber.parent_uuid == Vendor.uuid,
        VendorPhoneNumber.preference == 1),
    foreign_keys=[VendorPhoneNumber.parent_uuid],
    uselist=False,
    viewonly=True)

Vendor.contact = relationship(
    VendorContact,
    primaryjoin=and_(
        VendorContact.vendor_uuid == Vendor.uuid,
        VendorContact.preference == 1),
    uselist=False,
    viewonly=True)


class ProductCost(Base):
    """
    Represents a source from which a product may be obtained via purchase.
    """

    __tablename__ = 'product_costs'

    uuid = uuid_column()
    product_uuid = Column(String(32), ForeignKey('products.uuid'), nullable=False)
    vendor_uuid = Column(String(32), ForeignKey('vendors.uuid'), nullable=False)
    preference = Column(Integer, nullable=False)
    code = Column(String(15))

    case_size = Column(Integer)
    case_cost = Column(Numeric(9,5))
    pack_size = Column(Integer)
    pack_cost = Column(Numeric(9,5))
    unit_cost = Column(Numeric(9,5))
    effective = Column(DateTime)

    vendor = relationship(Vendor)

    def __repr__(self):
        return "<ProductCost: %s : %s>" % (self.product, self.vendor)


class ProductPrice(Base):
    """
    Represents a price for a product.
    """

    __tablename__ = 'product_prices'

    uuid = uuid_column()
    product_uuid = Column(String(32), ForeignKey('products.uuid'), nullable=False)
    type = Column(Integer)
    level = Column(Integer)
    starts = Column(DateTime)
    ends = Column(DateTime)
    price = Column(Numeric(8,3))
    multiple = Column(Integer)
    pack_price = Column(Numeric(8,3))
    pack_multiple = Column(Integer)

    def __repr__(self):
        return "<ProductPrice: %s : %s>" % (self.product, self.price)


class Product(Base):
    """
    Represents a product for sale and/or purchase.
    """

    __tablename__ = 'products'

    uuid = uuid_column()
    upc = Column(GPCType, index=True)
    department_uuid = Column(String(32), ForeignKey('departments.uuid'))
    subdepartment_uuid = Column(String(32), ForeignKey('subdepartments.uuid'))
    category_uuid = Column(String(32), ForeignKey('categories.uuid'))
    brand_uuid = Column(String(32), ForeignKey('brands.uuid'))
    description = Column(String(60))
    description2 = Column(String(60))
    size = Column(String(30))
    unit_of_measure = Column(String(4))

    regular_price_uuid = Column(
        String(32),
        ForeignKey('product_prices.uuid',
                   use_alter=True,
                   name='products_regular_price_uuid_fkey'))

    current_price_uuid = Column(
        String(32),
        ForeignKey('product_prices.uuid',
                   use_alter=True,
                   name='products_current_price_uuid_fkey'))

    department = relationship(Department)
    subdepartment = relationship(Subdepartment)
    category = relationship(Category)
    brand = relationship(Brand)

    costs = relationship(ProductCost, backref='product',
                         collection_class=ordering_list('preference', count_from=1),
                         order_by=ProductCost.preference)

    def __repr__(self):
        return "<Product: %s>" % self.description

    def __unicode__(self):
        return unicode(self.description or '')

Product.prices = relationship(
    ProductPrice, backref='product',
    primaryjoin=ProductPrice.product_uuid == Product.uuid)

Product.regular_price = relationship(
    ProductPrice,
    primaryjoin=Product.regular_price_uuid == ProductPrice.uuid,
    lazy='joined',
    post_update=True)

Product.current_price = relationship(
    ProductPrice,
    primaryjoin=Product.current_price_uuid == ProductPrice.uuid,
    lazy='joined',
    post_update=True)

Product.cost = relationship(
    ProductCost,
    primaryjoin=and_(
        ProductCost.product_uuid == Product.uuid,
        ProductCost.preference == 1,
        ),
    uselist=False,
    viewonly=True)

Product.vendor = relationship(
    Vendor,
    secondary=ProductCost.__table__,
    primaryjoin=and_(
        ProductCost.product_uuid == Product.uuid,
        ProductCost.preference == 1,
        ),
    secondaryjoin=Vendor.uuid == ProductCost.vendor_uuid,
    uselist=False,
    viewonly=True)


class EmployeeEmailAddress(EmailAddress):
    """
    Represents an email address associated with a :class:`Employee`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Employee'}


class EmployeePhoneNumber(PhoneNumber):
    """
    Represents a phone (or fax) number associated with a :class:`Employee`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Employee'}


class Employee(Base):
    """
    Represents an employee within the organization.
    """

    __tablename__ = 'employees'

    uuid = uuid_column()
    id = Column(Integer)
    person_uuid = Column(String(32), ForeignKey('people.uuid'), nullable=False)
    status = Column(Integer)
    display_name = Column(String(100))

    person = relationship(Person)

    first_name = association_proxy('person', 'first_name')
    last_name = association_proxy('person', 'last_name')

    def __repr__(self):
        return "<Employee: %s>" % self.person

    def __unicode__(self):
        return unicode(self.display_name or self.person)

    def add_email_address(self, address, type='Home'):
        email = EmployeeEmailAddress(address=address, type=type)
        self.emails.append(email)

    def add_phone_number(self, number, type='Home'):
        phone = EmployeePhoneNumber(number=number, type=type)
        self.phones.append(phone)

Employee.emails = relationship(
    EmployeeEmailAddress,
    backref='employee',
    primaryjoin=EmployeeEmailAddress.parent_uuid == Employee.uuid,
    foreign_keys=[EmployeeEmailAddress.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=EmployeeEmailAddress.preference,
    cascade='save-update, merge, delete, delete-orphan')

Employee.email = relationship(
    EmployeeEmailAddress,
    primaryjoin=and_(
        EmployeeEmailAddress.parent_uuid == Employee.uuid,
        EmployeeEmailAddress.preference == 1),
    foreign_keys=[EmployeeEmailAddress.parent_uuid],
    uselist=False,
    viewonly=True)

Employee.phones = relationship(
    EmployeePhoneNumber,
    backref='employee',
    primaryjoin=EmployeePhoneNumber.parent_uuid == Employee.uuid,
    foreign_keys=[EmployeePhoneNumber.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=EmployeePhoneNumber.preference,
    cascade='save-update, merge, delete, delete-orphan')

Employee.phone = relationship(
    EmployeePhoneNumber,
    primaryjoin=and_(
        EmployeePhoneNumber.parent_uuid == Employee.uuid,
        EmployeePhoneNumber.preference == 1),
    foreign_keys=[EmployeePhoneNumber.parent_uuid],
    uselist=False,
    viewonly=True)


class CustomerEmailAddress(EmailAddress):
    """
    Represents an email address associated with a :class:`Customer`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Customer'}


class CustomerPhoneNumber(PhoneNumber):
    """
    Represents a phone (or fax) number associated with a :class:`Customer`.
    """

    __mapper_args__ = {'polymorphic_identity': 'Customer'}


class CustomerPerson(Base):
    """
    Represents the association between a :class:`Person` and a customer account
    (:class:`Customer`).
    """

    __tablename__ = 'customers_people'

    uuid = uuid_column()
    customer_uuid = Column(String(32), ForeignKey('customers.uuid'))
    person_uuid = Column(String(32), ForeignKey('people.uuid'))
    ordinal = Column(Integer, nullable=False)

    person = relationship(Person)


class Customer(Base):
    """
    Represents a customer account.  Customer accounts may consist of more than
    one :class:`Person`, in some cases.
    """

    __tablename__ = 'customers'

    uuid = uuid_column()
    id = Column(String(20))
    name = Column(String(255))

    def __repr__(self):
        return "<Customer: %s, %s>" % (self.id, self.name or self.person)

    def __unicode__(self):
        return unicode(self.name or self.person)

    def add_email_address(self, address, type='Home'):
        email = CustomerEmailAddress(address=address, type=type)
        self.emails.append(email)

    def add_phone_number(self, number, type='Home'):
        phone = CustomerPhoneNumber(number=number, type=type)
        self.phones.append(phone)

Customer.emails = relationship(
    CustomerEmailAddress,
    backref='customer',
    primaryjoin=CustomerEmailAddress.parent_uuid == Customer.uuid,
    foreign_keys=[CustomerEmailAddress.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=CustomerEmailAddress.preference,
    cascade='save-update, merge, delete, delete-orphan')

Customer.email = relationship(
    CustomerEmailAddress,
    primaryjoin=and_(
        CustomerEmailAddress.parent_uuid == Customer.uuid,
        CustomerEmailAddress.preference == 1),
    foreign_keys=[CustomerEmailAddress.parent_uuid],
    uselist=False,
    viewonly=True)

Customer.phones = relationship(
    CustomerPhoneNumber,
    backref='customer',
    primaryjoin=CustomerPhoneNumber.parent_uuid == Customer.uuid,
    foreign_keys=[CustomerPhoneNumber.parent_uuid],
    collection_class=ordering_list('preference', count_from=1),
    order_by=CustomerPhoneNumber.preference,
    cascade='save-update, merge, delete, delete-orphan')

Customer.phone = relationship(
    CustomerPhoneNumber,
    primaryjoin=and_(
        CustomerPhoneNumber.parent_uuid == Customer.uuid,
        CustomerPhoneNumber.preference == 1),
    foreign_keys=[CustomerPhoneNumber.parent_uuid],
    uselist=False,
    viewonly=True)

Customer._people = relationship(
    CustomerPerson, backref='customer',
    primaryjoin=CustomerPerson.customer_uuid == Customer.uuid,
    collection_class=ordering_list('ordinal', count_from=1),
    order_by=CustomerPerson.ordinal)

Customer.people = association_proxy(
    '_people', 'person',
    getset_factory=getset_factory,
    creator=lambda x: CustomerPerson(person=x))

Customer._person = relationship(
    CustomerPerson,
    primaryjoin=and_(
        CustomerPerson.customer_uuid == Customer.uuid,
        CustomerPerson.ordinal == 1),
    uselist=False,
    viewonly=True)

Customer.person = association_proxy(
    '_person', 'person',
    getset_factory=getset_factory)


class CustomerGroup(Base):
    """
    Represents an arbitrary group to which :class:`Customer` instances may (or
    may not) belong.
    """

    __tablename__ = 'customer_groups'

    uuid = uuid_column()
    id = Column(String(20))
    name = Column(String(255))

    def __repr__(self):
        return "<CustomerGroup: %s, %s>" % (self.id, self.name)

    def __unicode__(self):
        return unicode(self.name or '')


class CustomerGroupAssignment(Base):
    """
    Represents the assignment of a :class:`Customer` to a
    :class:`CustomerGroup`.
    """

    __tablename__ = 'customers_groups'

    uuid = uuid_column()
    customer_uuid = Column(String(32), ForeignKey('customers.uuid'))
    group_uuid = Column(String(32), ForeignKey('customer_groups.uuid'))
    ordinal = Column(Integer, nullable=False)

    group = relationship(CustomerGroup)

Customer._groups = relationship(
    CustomerGroupAssignment, backref='customer',
    collection_class=ordering_list('ordinal', count_from=1),
    order_by=CustomerGroupAssignment.ordinal)

Customer.groups = association_proxy(
    '_groups', 'group',
    getset_factory=getset_factory,
    creator=lambda x: CustomerGroupAssignment(group=x))


class LabelProfile(Base):
    """
    Represents a "profile" (collection of settings) for product label printing.
    """

    __tablename__ = 'label_profiles'

    uuid = uuid_column()
    ordinal = Column(Integer)
    code = Column(String(3))
    description = Column(String(50))
    printer_spec = Column(String(255))
    formatter_spec = Column(String(255))
    format = Column(Text)

    _printer = None
    _formatter = None

    def __repr__(self):
        return "<LabelProfile: %s>" % self.code

    def __unicode__(self):
        return unicode(self.description or '')

    def get_formatter(self):
        if not self._formatter and self.formatter_spec:
            try:
                formatter = edbob.load_spec(self.formatter_spec)
            except LoadSpecError:
                pass
            else:
                self._formatter = formatter()
        return self._formatter        

    def get_printer(self):
        if not self._printer and self.printer_spec:
            try:
                printer = edbob.load_spec(self.printer_spec)
            except LoadSpecError:
                pass
            else:
                self._printer = printer()
                for name in printer.required_settings:
                    setattr(printer, name, self.get_printer_setting(name))
                self._printer.formatter = self.get_formatter()
        return self._printer

    def get_printer_setting(self, name):
        session = object_session(self)
        if not self.uuid:
            session.flush()
        name = 'labels.%s.printer.%s' % (self.uuid, name)
        return edbob.get_setting(name, session)

    def save_printer_setting(self, name, value):
        session = object_session(self)
        if not self.uuid:
            session.flush()
        name = 'labels.%s.printer.%s' % (self.uuid, name)
        edbob.save_setting(name, value, session)