Beispiel #1
0
 def parent_id(cls):
     table_name = plural_underscored(cls.__name__)
     return db.Column(
         db.Integer,
         db.ForeignKey("{}.id".format(table_name),
                       ondelete="CASCADE",
                       onupdate="CASCADE"))
Beispiel #2
0
    def closure(cls):
        class_name = cls.__name__ + 'Localized'
        tablename = plural_underscored(class_name)

        if db.metadata.tables.get(tablename) is not None:
            return cls

        cls_columns = cls.__table__.get_children()
        columns = dict([(c.name, c.copy()) for c in cls_columns
                        if isinstance(c.type, (db.Unicode, db.UnicodeText))])
        localized_names = columns.keys()

        columns.update({
            'parent_id':
            db.Column(db.Integer,
                      db.ForeignKey(cls.__tablename__ + '.id',
                                    ondelete="CASCADE",
                                    onupdate="CASCADE"),
                      nullable=True),
            'parent':
            db.relationship(cls, backref='localized_ref'),
            'locale':
            db.Column(db.Unicode(255), default=lang, index=True)
        })

        cls_localized = type(class_name, (db.Model, CRUDMixin), columns)

        for field in localized_names:
            create_property(cls, cls_localized, columns, field)

        return cls
Beispiel #3
0
class Address(db.Model, CRUDMixin):
    """ Represents address data for users
        By default model inherits id and created_at fields from the CRUDMixin
    """
    __mapper_args__ = {
        'order_by': ['country_id', 'city']
    }
    city = db.Column(db.Unicode(255), nullable=False)
    street = db.Column(db.Unicode(255), nullable=False)
    apartment = db.Column(db.Unicode(20))
    zip_code = db.Column(db.String(20))
    customer_id = db.Column(db.Integer, db.ForeignKey('customers.id',
                                                      ondelete='CASCADE',
                                                      use_alter=True,
                                                      name='fk_customer_id'))
    country_id = db.Column(db.Integer, db.ForeignKey('countries.id'))
    country = db.relationship('Country')

    def __repr__(self):
        return "<Address:('%s','%s')>" % (self.city, self.street)

    def save(self, commit=True):
        instance = super(Address, self).save(commit)
        if (instance.customer_id is not None and
                    instance.id == instance.customer.billing_address_id):
            billing_data_changed.send(self, user_id=instance.customer.user_id)
        return instance

    def _type_get(self):
        if self.id == self.customer.billing_address_id:
            return 'billing'
        elif self.id == self.customer.delivery_address_id:
            return 'delivery'
        else:
            return None

    type = property(_type_get, lambda x, y: None)

    def as_dict(self, include=None, exclude=None):
        include = include or []
        include.extend(['type'])

        return super(Address, self).as_dict(include, exclude)
Beispiel #4
0
class SocialConnection(db.Model, CRUDMixin):
    user_id = db.Column(db.Integer(), db.ForeignKey('users.id'))
    user = db.relationship('User', backref=db.backref('connections',
                           lazy='dynamic'), cascade='all')
    provider_id = db.Column(db.String(255))
    provider_user_id = db.Column(db.String(255))
    access_token = db.Column(db.String(255))
    secret = db.Column(db.String(255))
    display_name = db.Column(db.Unicode(255))
    profile_url = db.Column(db.String(512))
    image_url = db.Column(db.String(512))
    rank = db.Column(db.Integer)
Beispiel #5
0
class ProductDelivery(db.Model, CRUDMixin):

    delivery_type = db.Column(db.String(128))
    variant_id = db.Column(db.String(255), nullable=True, index=True)
    cost = db.Column(db.Numeric(precision=18, scale=2))

    country_id = db.Column(db.Integer,
                           db.ForeignKey('countries.id'),
                           nullable=True)
    country = db.relationship('Country')

    __table_args__ = (db.UniqueConstraint('delivery_type',
                                          'variant_id',
                                          'country_id',
                                          name='uq_type_variant_country'), )
Beispiel #6
0
class BankAccount(db.Model, CRUDMixin):
    bank_name = db.Column(db.Unicode(512))
    iban = db.Column(db.String(256))
    swift = db.Column(db.String(256))
    updated_at = db.Column(db.DateTime, onupdate=datetime.now)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    user = db.relationship('User', backref=db.backref('accounts',
                           lazy='dynamic'))

    def check_owner(self, user):
        return user.id == self.user_id

    def save(self, commit=True):
        instance = super(BankAccount, self).save(commit)
        billing_data_changed.send(self, user_id=instance.user_id)
        return instance
Beispiel #7
0
class Customer(db.Model, CRUDMixin):
    MIN_BIRTHDATE_YEAR = 1900

    sex = db.Column(db.Unicode(1), index=True)
    birthdate = db.Column(db.DateTime, index=True)
    first_name = db.Column(db.Unicode(255), default=u'')
    last_name = db.Column(db.Unicode(255), default=u'')
    email = db.Column(db.String(80), index=True)
    phone = db.Column(db.String(80), default='')
    fax = db.Column(db.String(80), default='')
    gender = db.Column(db.String(1), default='')
    company = db.Column(db.Unicode(255), default=u'')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow)
    notes = db.Column(db.UnicodeText)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    user = db.relationship("User", backref=db.backref("customer",
                                                      uselist=False))
    addresses = db.relationship('Address', backref=db.backref('customer'),
                        primaryjoin="Address.customer_id==Customer.id",
                        cascade='all, delete', lazy='dynamic')
    billing_address_id = db.Column(db.Integer, db.ForeignKey('addresses.id',
                        use_alter=True, name='fk_billing_address'))
    _billing_address = db.relationship("Address", cascade='all, delete',
                        primaryjoin="Customer.billing_address_id==Address.id")
    delivery_address_id = db.Column(db.Integer, db.ForeignKey('addresses.id',
                        use_alter=True, name='fk_delivery_address'))
    _delivery_address = db.relationship("Address", cascade='all, delete',
                        primaryjoin="Customer.delivery_address_id==Address.id")


    @validates('birthdate')
    def validate_birthdate(self, key, value):
        min_date = datetime(self.MIN_BIRTHDATE_YEAR, 1, 1)
        if value < min_date:
            raise CustomerIsTooOldError()

        return value

    def __unicode__(self):
        return u"{0.first_name} {0.last_name}".format(self)

    @property
    def __addresses_ids(self):
        return map(attrgetter('id'), self.addresses)

    def set_address(self, addr_type, value):
        """
        :param addr_type: Either `billing` or `delivery` to describe type the
                          address will be used for
        :param value:     Instance of the Address model
        """
        if value.id not in self.__addresses_ids:
            self.addresses.append(value)

        setattr(self, "{}_address_id".format(addr_type), value.id)
        db.session.commit()
        if addr_type == 'billing':
            billing_data_changed.send(self, user_id=self.user_id)
        return self

    @hybrid_property
    def billing_address(self):
        """ Hybrid property allowing only one billing-address per-customer
        """
        return self._billing_address

    @billing_address.setter
    def billing_address(self, value):
        """ setter for billing_address property
        """
        self.set_address('billing', value)

    @hybrid_property
    def delivery_address(self):
        """ Hybrid property allowing only one delivery_address per-customer
        """
        return self._delivery_address

    @delivery_address.setter
    def delivery_address(self, value):
        """ setter for delivery_address property
        """
        self.set_address('delivery', value)

    @property
    def organizer_ready(self):
        if self.user:
            return self.billing_address and self.user.accounts.count()
        else:
            return False
Beispiel #8
0
        elif self.id == self.customer.delivery_address_id:
            return 'delivery'
        else:
            return None

    type = property(_type_get, lambda x, y: None)

    def as_dict(self, include=None, exclude=None):
        include = include or []
        include.extend(['type'])

        return super(Address, self).as_dict(include, exclude)


user_roles = db.Table('user_roles', db.metadata,
    db.Column('user_id', db.Integer, db.ForeignKey('users.id'),
              primary_key=True),
    db.Column('role_id', db.Integer, db.ForeignKey('roles.id'),
              primary_key=True),
)


class CustomerIsTooOldError(Exception):
    pass


class Customer(db.Model, CRUDMixin):
    MIN_BIRTHDATE_YEAR = 1900

    sex = db.Column(db.Unicode(1), index=True)
    birthdate = db.Column(db.DateTime, index=True)
Beispiel #9
0
    type = property(_type_get, lambda x, y: None)

    def as_dict(self, include=None, exclude=None):
        include = include or []
        include.extend(['type'])

        return super(Address, self).as_dict(include, exclude)


user_roles = db.Table(
    'user_roles',
    db.metadata,
    db.Column('user_id',
              db.Integer,
              db.ForeignKey('users.id'),
              primary_key=True),
    db.Column('role_id',
              db.Integer,
              db.ForeignKey('roles.id'),
              primary_key=True),
)


class Customer(db.Model, CRUDMixin):
    sex = db.Column(db.Unicode(1), index=True)
    birthdate = db.Column(db.DateTime, index=True)
    first_name = db.Column(db.Unicode(255), default=u'')
    last_name = db.Column(db.Unicode(255), default=u'')
    email = db.Column(db.String(80), index=True)
    phone = db.Column(db.String(80), default='')
Beispiel #10
0
 def customer_id(cls):
     return db.Column(db.Integer, db.ForeignKey('customers.id'),
                      nullable=False, index=True)
Beispiel #11
0
 def delivery_country_id(cls):
     return db.Column(db.Integer, db.ForeignKey('countries.id',
                      use_alter=True, name='fk_delivery_country'))
Beispiel #12
0
 def order_id(cls):
     return db.Column(db.Integer, db.ForeignKey('orders.id'))
Beispiel #13
0
 def author_id(cls):
     return db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False)