Ejemplo n.º 1
0
class ExternalLedger(common.LedgerModel):
    __tablename__ = 'external_ledger'

    record_id = db.Column(UUID)
    record_table = db.Column(db.Enum('transaction',
                                     'exchange',
                                     'transfer',
                                     name='record_table_enum'),
                             nullable=False,
                             index=True)
    processor = db.Column(db.String(255), nullable=False, index=True)
    reference_number = db.Column(db.String(255), nullable=False, index=True)
    currency_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('currency.id'),
                            nullable=False)
    currency = db.relationship('Currency',
                               backref='external_ledger_entries',
                               lazy='select')
    fee_id = db.Column(db.BigInteger(unsigned=True),
                       db.ForeignKey('fee.id'),
                       nullable=True)
    fee = db.relationship('Fee',
                          backref='external_ledger_entries',
                          lazy='select')
    full_name = db.Column(db.String(255), nullable=True)
Ejemplo n.º 2
0
class CampaignGoal(common.ConfigurationModel, common.ActiveMixin,
                   common.MetadataMixin):
    __tablename__ = 'campaign_goal'

    predecessor_id = db.Column(db.BigInteger(unsigned=True),
                               db.ForeignKey('campaign_goal.id'),
                               nullable=True)
    predecessor = db.relationship('CampaignGoal',
                                  uselist=False,
                                  backref=db.backref(
                                      'descendant',
                                      remote_side="CampaignGoal.id"))

    campaign = db.relationship('Campaign', backref='goals', lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=False)

    type = db.Column(
        db.Enum('petition',
                'project',
                'purchase',
                name='campaign_goal_type_enum'))

    purchase = db.relationship('Purchase',
                               backref='campaign_goal',
                               uselist=False,
                               lazy='select')
    purchase_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('purchase.id'),
                            nullable=True)
Ejemplo n.º 3
0
class CampaignGoalAssociation(db.Model, common.TrackIPMixin,
                              common.TrackTimeMixin, common.EnabledMixin,
                              common.FieldUpdateMixin):
    __tablename__ = 'campaign_goal_association'

    user = db.relationship('User', backref='goals', lazy='select')
    user_id = db.Column(db.BigInteger(unsigned=True),
                        db.ForeignKey('user.id'),
                        nullable=False,
                        primary_key=True)
    campaign = db.relationship('Campaign',
                               backref='goal_associations',
                               lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=False,
                            primary_key=True)
    campaign_goal = db.relationship('CampaignGoal',
                                    backref='participants',
                                    lazy='select')
    campaign_goal_id = db.Column(db.BigInteger(unsigned=True),
                                 db.ForeignKey('campaign_goal.id'),
                                 nullable=False,
                                 primary_key=True)
    participation = db.Column(
        db.Enum('opted-in',
                'opted-out',
                'participating',
                'nonparticipating',
                name='participation_enum'))
    pledge = db.Column(db.DECIMAL(precision=24, scale=4), nullable=True)

    __table_args__ = (db.UniqueConstraint(user_id, campaign_id,
                                          campaign_goal_id), {})
Ejemplo n.º 4
0
class Transaction(common.LedgerModel):
    balance = db.relationship('Balance', backref='transactions', lazy='select')
    balance_id = db.Column(db.BigInteger(unsigned=True),
                           db.ForeignKey('balance.id'),
                           nullable=False)
    campaign_goal = db.relationship('CampaignGoal', backref='purchased_goals', lazy='select')
    campaign_goal_id = db.Column(db.BigInteger(unsigned=True),
                                 db.ForeignKey('campaign_goal.id'),
                                 nullable=True)
Ejemplo n.º 5
0
class Invitee(common.UUIDMixin, common.EnabledMixin, common.Model):
    email = db.Column(db.String(255), nullable=False, index=True)
    accepted = db.Column(DateTimeTZ, nullable=True)
    campaign = db.relationship('Campaign', backref='invitees', lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=False)
    user = db.relationship('User', backref='invitations', lazy='select')
    user_id = db.Column(db.BigInteger(unsigned=True),
                        db.ForeignKey('user.id'),
                        nullable=True)

    __table_args__ = (db.UniqueConstraint(campaign_id, email), {})
Ejemplo n.º 6
0
class CampaignMeta(common.Model, common.EnabledMixin, common.KeyValueMixin):
    __tablename__ = 'campaign_meta'

    campaign = db.relationship('Campaign', backref='metadata', lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=False)
Ejemplo n.º 7
0
class UserMeta(common.Model,
               common.IDMixin,
               common.KeyValueMixin):
    __tablename__ = 'user_meta'

    user = db.relationship('User', backref='metadata', lazy='select')
    user_id = db.Column(db.BigInteger(unsigned=True),
                        db.ForeignKey('user.id'),
                        nullable=False)
Ejemplo n.º 8
0
class CampaignGoalLedger(common.LedgerModel):
    __tablename__ = 'campaign_goal_ledger'

    campaign = db.relationship('Campaign',
                               backref='goals_ledger',
                               lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=False)
    campaign_goal = db.relationship('CampaignGoal',
                                    backref='ledger',
                                    lazy='select')
    campaign_goal_id = db.Column(db.BigInteger(unsigned=True),
                                 db.ForeignKey('campaign_goal.id'),
                                 nullable=False)
    party_id = db.Column(db.BigInteger(unsigned=True))
    party_type = db.Column(db.Enum(
        'user', 'campaign', name='campaign_goal_ledger_target_type_enum'),
                           nullable=False,
                           index=True)
Ejemplo n.º 9
0
class Exchange(common.LedgerModel):
    debit_currency_id = db.Column(db.BigInteger(unsigned=True),
                                  db.ForeignKey('currency.id'),
                                  nullable=False)
    debit_currency = db.relationship('Currency',
                                     backref='debit_exchanges',
                                     lazy='select',
                                     primaryjoin="Exchange.debit_currency_id==Currency.id")
    credit_currency_id = db.Column(db.BigInteger(unsigned=True),
                                   db.ForeignKey('currency.id'),
                                   nullable=False)
    credit_currency = db.relationship('Currency',
                                      backref='credit_exchanges',
                                      lazy='select',
                                      primaryjoin="Exchange.credit_currency_id==Currency.id")
    exchange_rate = db.Column(db.DECIMAL(precision=24, scale=4), nullable=False)
    balance = db.relationship('Balance', backref='exchanges', lazy='select')
    balance_id = db.Column(db.BigInteger(unsigned=True),
                           db.ForeignKey('balance.id'),
                           nullable=False)
Ejemplo n.º 10
0
from sqlalchemy.ext.hybrid import hybrid_property

from werkzeug.security import generate_password_hash, check_password_hash

from pooldlib.postgresql import db, common


UserPurchase = db.Table('user_purchase', db.metadata,
                        db.Column('id', db.BigInteger(unsigned=True), primary_key=True),
                        db.Column('user_id', db.BigInteger(unsigned=True), db.ForeignKey('user.id'), nullable=False),
                        db.Column('purchase_id', db.BigInteger(unsigned=True), db.ForeignKey('purchase.id'), nullable=False))


class User(common.Model,
           common.IDMixin,
           common.NullNameMixin,
           common.EnabledMixin,
           common.VerifiedMixin,
           common.SerializationMixin,
           common.BalanceMixin,
           common.MetadataMixin):

    username = db.Column(db.String(40), unique=True, nullable=False)
    _password = db.Column('password', db.String(64), nullable=False)
    purchases = db.relationship('Purchase', secondary=UserPurchase, backref='purchasing_user')

    @property
    def password(self):
        return self._password

    @password.setter
Ejemplo n.º 11
0
class Balance(common.Model, common.EnabledMixin):

    currency_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('currency.id'),
                            nullable=False)
    currency = db.relationship('Currency', backref='balances')
    amount = db.Column(db.DECIMAL(precision=24, scale=4),
                       nullable=False,
                       default=0)
    user_id = db.Column(db.BigInteger(unsigned=True),
                        db.ForeignKey('user.id'),
                        nullable=True)
    user = db.relationship('User', backref='balances', lazy='select')
    campaign_id = db.Column(db.BigInteger(unsigned=True),
                            db.ForeignKey('campaign.id'),
                            nullable=True)
    campaign = db.relationship('Campaign', backref='balances', lazy='select')
    type = db.Column(db.Enum('user', 'campaign', name='balance_type_enum'))

    @classmethod
    def filter_by(cls, currency=None, query=None):
        from pooldlib.postgresql import Currency
        if hasattr(currency, 'id'):
            currency = currency.id

        if not query:
            query = cls.query

        if currency and isinstance(currency, basestring):
            query = query.join(Currency)
            return query.filter(Currency.code == currency)

        if currency:
            return query.filter(cls.currency_id == currency)

        return query

    @classmethod
    def first(cls, *args, **kw):
        return cls.filter_by(*args, **kw).first()

    @classmethod
    def filter_by_user(cls, user=None, currency=None):
        if hasattr(user, 'id'):
            user = user.id

        if not user:
            return

        query = cls.query.filter(cls.user_id == user)
        return super(Balance, cls).filter_by(currency=currency,
                                             query=query)

    @classmethod
    def filter_by_campaign(cls, campaign=None, currency=None):
        if hasattr(campaign, 'id'):
            campaign = campaign.id

        if not campaign:
            return

        query = cls.query.filter(cls.campaign_id == campaign)
        return super(Balance, cls).filter_by(currency=currency,
                                             query=query)
Ejemplo n.º 12
0
class IDMixin(object):
    id = db.Column(db.BigInteger(unsigned=True), primary_key=True)
Ejemplo n.º 13
0
class Transfer(common.LedgerModel):
    record_id = db.Column(UUID, unique=False, index=True)
    balance = db.relationship('Balance', backref='transfers', lazy='select')
    balance_id = db.Column(db.BigInteger(unsigned=True),
                           db.ForeignKey('balance.id'),
                           nullable=False)