class Transaction(db.Model): __tablename__ = "transaction" id = db.Column(db.Integer(), primary_key=True) transaction_no = db.Column(db.String(100), unique=True) transaction_type = db.relationship('ConfigTransactionType') transaction_type_id = db.Column(db.Integer, db.ForeignKey('configtransactiontype.id')) account = db.relationship('Account') account_id = db.Column(db.Integer, db.ForeignKey('account.id')) reversing_transaction = db.relationship('Transaction', remote_side=[id], backref='reversal', uselist=False) reversing_transaction_id = db.Column(db.Integer, db.ForeignKey('transaction.id'), nullable=True) currency_id = db.Column(db.Integer, db.ForeignKey('configcurrency.id')) currency = db.relationship('ConfigCurrency') amount = db.Column(db.Numeric(18, 2), default=D('0.0')) details = db.Column(db.String(255), nullable=True) notified = db.Column(db.Boolean, default=False) date_created = db.Column(db.DateTime()) def __unicode__(self): return '%s' % self.transaction_no
class PaypalTransaction(db.Model): __tablename__ = "paypaltransaction" id = db.Column(db.Integer(), primary_key=True) paypal_transaction_type_code = db.Column(db.String(50), nullable=False) paypal_payer_id = db.Column(db.String(50), nullable=True) payer_id = db.Column(db.Integer(), db.ForeignKey('paypalprofile.id'), nullable=True) payer = db.relationship('PaypalProfile', backref='transactions') mpesa_recipient = db.Column(db.String(25)) transaction = db.relationship( 'Transaction', backref='paypal_transactions', uselist=False, primaryjoin= "Transaction.transaction_no==PaypalTransaction.paypal_transaction_id") paypal_transaction_id = db.Column( db.String(50), db.ForeignKey('transaction.transaction_no'), unique=True) sale_id = db.Column(db.String(50), nullable=True) invoice_number = db.Column(db.String(50), nullable=True) parent_transaction_id = db.Column( db.String(50), db.ForeignKey('transaction.transaction_no'), nullable=True) parent_transaction = db.relationship( 'Transaction', backref='child_transactions', uselist=False, primaryjoin= "PaypalTransaction.parent_transaction_id==Transaction.transaction_no") create_time = db.Column(db.DateTime(timezone=True), nullable=False) update_time = db.Column(db.DateTime(timezone=True), nullable=True) state = db.Column(db.String(50), nullable=False) intent = db.Column(db.String(20), nullable=True) payment_method = db.Column(db.String(20), nullable=True) date_created = db.Column(db.DateTime(), nullable=False) def __unicode__(self): return '%s' % self.paypal_transaction_id
class TransactionStatus(db.Model): __tablename__ = "transactionstatus" id = db.Column(db.Integer(), primary_key=True) transaction = db.relationship('Transaction', backref='transaction_statuses') transaction_id = db.Column(db.Integer, db.ForeignKey('transaction.id')) status = db.relationship('ConfigTransactionStatus', backref='transaction_statuses') status_id = db.Column(db.Integer, db.ForeignKey('configtransactionstatus.id')) details = db.Column(db.String(400), nullable=True) date_created = db.Column(db.DateTime())
class PaypalProfile(db.Model): __tablename__ = "paypalprofile" id = db.Column(db.Integer(), primary_key=True) user = db.relationship('User', backref='paypal_profile', uselist=False, primaryjoin='User.email==PaypalProfile.email') email = db.Column(db.String(100), db.ForeignKey('user.email'), unique=True, nullable=False) address = db.relationship('PaypalAddress', backref='paypal_addresses', uselist=False) paypal_user_id = name = db.Column(db.String(100), unique=True, nullable=False) name = db.Column(db.String(100), nullable=False) given_name = db.Column(db.String(100), nullable=False) family_name = db.Column(db.String(100), nullable=False) middle_name = db.Column(db.String(100), nullable=True) gender = db.Column(db.String(10), nullable=True) phone_number = db.Column(db.String(100), nullable=False) birthday = db.Column(db.Date(), nullable=False) email_verified = db.Column(db.Boolean, default=False, nullable=True) verified_account = db.Column(db.Boolean, default=False, nullable=False) account_type = db.Column(db.String(10), nullable=False) account_creation_date = db.Column(db.DateTime(), nullable=False) date_created = db.Column(db.DateTime(), nullable=False) date_updated = db.Column(db.DateTime(), nullable=False) def __unicode__(self): return '[%s] %s' % (self.paypal_user_id, self.email)
class MPesaTransaction(db.Model): __tablename__ = "mpesatransaction" id = db.Column(db.Integer(), primary_key=True) mpesa_transaction_no = db.Column(db.String(50)) mpesa_txn_id = db.Column(db.String(50)) recipient = db.relationship( 'MPesaProfile', backref='transactions', primaryjoin= "MPesaProfile.mobile_phone_no==MPesaTransaction.recipient_phone_no") recipient_phone_no = db.Column( db.String(50), db.ForeignKey('mpesaprofile.mobile_phone_no'), nullable=False) total_amount = db.Column(db.String(50), nullable=False) total_amount_currency = db.Column(db.String(4), nullable=False, default='KES') reference_id = db.Column(db.String(50), nullable=True) transaction = db.relationship( 'Transaction', backref='mpesa_transactions', uselist=False, primaryjoin= "Transaction.transaction_no==MPesaTransaction.merchant_transaction_id") merchant_transaction_id = db.Column( db.String(50), db.ForeignKey('transaction.transaction_no'), nullable=False) date_created = db.Column(db.DateTime()) date_approved = db.Column(db.DateTime()) def __unicode__(self): return '%s' % self.mpesa_transaction_no
class ConfigLedgerAccountingRule(db.Model): __tablename__ = "configledgeraccountingrule" id = db.Column(db.Integer(), primary_key=True) product = db.relationship('Product') product_id = db.Column(db.Integer, db.ForeignKey('product.id')) transaction_type = db.relationship('ConfigTransactionType', backref='accounting_rules') transaction_type_id = db.Column(db.Integer, db.ForeignKey('configtransactiontype.id')) debit_account_id = db.Column(db.Integer, db.ForeignKey('configledgeraccount.id')) debit_account = db.relationship('ConfigLedgerAccount', backref='debits', foreign_keys=[debit_account_id]) credit_account_id = db.Column(db.Integer, db.ForeignKey('configledgeraccount.id')) credit_account = db.relationship('ConfigLedgerAccount', backref='credits', foreign_keys=[credit_account_id]) date_created = db.Column(db.DateTime()) def __unicode__(self): return self.transaction_type.name
class SMSMessage(db.Model): __tablename__ = "smsmessage" INCOMING = 0 OUTGOING = 1 id = db.Column(db.Integer(), primary_key=True) notification_type_id = db.Column(db.Integer, db.ForeignKey('confignotificationtype.id'), nullable=False) notification_type = db.relationship('ConfigNotificationType') message_type = db.Column(db.Integer(), default=INCOMING) message_sender = db.Column(db.String(15), nullable=True) message = db.Column(db.String(320)) message_recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) message_recipient = db.relationship("User") delivered = db.Column(db.Boolean, default=False) date_delivered = db.Column(db.DateTime()) date_created = db.Column(db.DateTime())
class TransactionEntry(db.Model): __tablename__ = "transactionentry" (DEBIT, CREDIT) = (0, 1) (ITEM_TYPES) = ((DEBIT, 'Debit'), (CREDIT, 'Credit')) id = db.Column(db.Integer(), primary_key=True) transaction = db.relationship('Transaction', backref='transaction_entries') transaction_id = db.Column(db.Integer, db.ForeignKey('transaction.id')) account = db.relationship('Account', backref='account_entries') account_id = db.Column(db.Integer, db.ForeignKey('account.id')) ledger_account = db.relationship('ConfigLedgerAccount') ledger_account_id = db.Column(db.Integer, db.ForeignKey('configledgeraccount.id')) item_type = db.Column(db.Integer()) balance_increment = db.Column(db.Numeric(6, 2), default=D('0.0')) date_created = db.Column(db.DateTime())
class AccountVerification(db.Model): __tablename__ = "accountverification" __table_args__ = (db.UniqueConstraint('mobile_code', 'email_code'), ) id = db.Column(db.Integer(), primary_key=True) email_code = db.Column(db.String(15), nullable=False) mobile_code = db.Column(db.String(10), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) user = db.relationship('User') expiry_date = db.Column(db.DateTime, nullable=False) date_created = db.Column(db.DateTime, nullable=False)
class PaypalAddress(db.Model): __tablename__ = "paypaladdress" id = db.Column(db.Integer(), primary_key=True) paypal_profile = db.relationship('PaypalProfile', backref='addresses') paypal_profile_id = db.Column(db.Integer, db.ForeignKey('paypalprofile.id'), nullable=False) street_address = db.Column(db.String(100), nullable=False) locality = db.Column(db.String(50), nullable=False) region = db.Column(db.String(50), nullable=False) postal_code = db.Column(db.String(15), nullable=True) country = db.Column(db.String(3), nullable=False) date_created = db.Column(db.DateTime(), nullable=False)
class ConfigLedgerAccount(db.Model): __tablename__ = "configledgeraccount" NORMAL = 'NORMAL' CONTRA = 'CONTRA' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(100), unique=True) code = db.Column(db.String(100), unique=True) account_category = db.relationship('ConfigLedgerAccountCategory') account_category_id = db.Column(db.Integer, db.ForeignKey('configledgeraccountcategory.id')) #contra|normal balance_direction = db.Column(db.String(10), default=NORMAL) date_created = db.Column(db.DateTime()) def __unicode__(self): return self.name
class MPesaProfile(db.Model): __tablename__ = "mpesaprofile" id = db.Column(db.Integer(), primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('user.id'), nullable=False, unique=True) user = db.relationship( 'User', backref='mpesa_profile', uselist=False ) # primaryjoin='User.phone==MPesaProfile.mobile_phone_no') mobile_phone_no = db.Column(db.String(25), unique=True, nullable=False) registered_name = db.Column(db.String(100), nullable=True) date_created = db.Column(db.DateTime(), nullable=False) date_updated = db.Column(db.DateTime(), nullable=False) def __unicode__(self): return '[%s] %s' % (self.user_id, self.mobile_phone_no)
class User(db.Model): __tablename__ = "user" id = db.Column(db.Integer(), primary_key=True) first_name = db.Column(db.String(255), nullable=False) last_name = db.Column(db.String(255), nullable=False) password = db.Column(db.String(255), nullable=False) email = db.Column(db.String(255), unique=True, nullable=False) email_verified = db.Column(db.Boolean, default=False) phone = db.Column(db.String(25), unique=True, nullable=False) phone_verified = db.Column(db.Boolean, default=False) date_joined = db.Column(db.DateTime(), nullable=False) last_login = db.Column(db.DateTime(), nullable=True) date_updated = db.Column(db.DateTime(), nullable=False) date_created = db.Column(db.DateTime(), nullable=False) accounts = db.relationship('Account', lazy='dynamic') def __init__(self, password=None, *args, **kwargs): if password: self.password = User.hashed_password(password) super(User, self).__init__(*args, **kwargs) def __unicode__(self): return self.get_full_name() def get_full_name(self): return '%s %s' % (self.first_name, self.last_name) @property def is_authenticated(self): return (int(session.get('user_id', 0)) == self.id) @property def is_active(self): return (self.email_verified and self.phone_verified) @property def is_anonymous(self): return not self.is_authenticated def get_id(self): return unicode(self.id) def __repr__(self): return '<User %r>' % (self.email) @staticmethod def hashed_password(password): return bcrypt.generate_password_hash(password) @staticmethod def get_user_with_email_and_password(email, password): user = User.query.filter_by(email=email).first() if user and bcrypt.check_password_hash(user.password, password): return user