Guest User

Untitled

a guest
Jun 2nd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. import decimal
  2.  
  3. from billing.lib.exceptions import NoResultFound
  4. from billing.model.defaults import *
  5. from billing.model.address import Address
  6. from billing.model.company import Company
  7. from billing.model.creditcard import CreditCard,PaymentAccount
  8. from billing.model.membership import SiteMembership
  9.  
  10. from billing.model.meta import Session
  11.  
  12.  
  13. class SecretQuestion(Base):
  14. """A class that represents a secret question for account lookup."""
  15. __tablename__ = "secret_questions"
  16. __table_args__ = (UniqueConstraint("company_id", "question"), {"mysql_engine": "InnoDB"})
  17. query = Session.query_property()
  18.  
  19. id = Column(Integer, primary_key=True)
  20. question = Column(Unicode(255), unique=True, index=True, nullable=False)
  21. company_id = Column(Integer, ForeignKey(Company.id), index=True, nullable=False)
  22. active = Column(Boolean, default=True, server_default="1", nullable=False, index=True)
  23. created = Column(DateTime, nullable=False, default=datetime.datetime.now)
  24. modified = Column(DateTime, nullable=True, onupdate=datetime.datetime.now)
  25.  
  26. def __repr__(self):
  27. return self.question
  28.  
  29.  
  30. DDL("ALTER TABLE %(fullname)s COMMENT = 'A secret question to use for confirmation. Ie., What city did you grow up in?'").execute_at("after-create", SecretQuestion.__table__)
  31.  
  32. class Account(Base):
  33. """A class representing a global account."""
  34. __tablename__ = "accounts"
  35. __table_args__ = (UniqueConstraint("company_id", "username"), {"mysql_engine": "InnoDB"})
  36. query = Session.query_property()
  37.  
  38.  
  39. id = Column(mysql.BIGINT(unsigned=True), primary_key=True)
  40. company_id = Column(Integer, ForeignKey(Company.id), nullable=False, index=True)
  41. first_name = Column(Unicode(50), nullable=False, index=True)
  42. last_name = Column(Unicode(50), nullable=False, index=True)
  43. email = Column(Unicode(100), unique=True, nullable=False, index=True)
  44. active = Column(Boolean, nullable=False, server_default="1", default=True, index=True)
  45. username = Column(Unicode(100), unique=True, nullable=False, index=True)
  46. password = Column(Unicode(30), nullable=False, index=True)
  47. secret_question_id = Column(Integer(unsigned=True), ForeignKey(SecretQuestion.id), nullable=True, index=True)
  48. secret_answer = Column(Unicode(255), nullable=True, index=True)
  49. primary_payment_account_id = Column(mysql.BIGINT(unsigned=True), ForeignKey(CreditCard.id, name="primary_payment_account_id", use_alter=True), nullable=True, index=True)
  50. secondary_payment_account_id = Column(mysql.BIGINT(unsigned=True), ForeignKey(CreditCard.id, name="secondary_payment_account_id", use_alter=True), nullable=True, index=True)
  51. created = Column(DateTime, nullable=False, default=datetime.datetime.now)
  52. modified = Column(DateTime, nullable=True, onupdate=datetime.datetime.now)
  53.  
  54. # relations
  55. question = relation("SecretQuestion", backref=backref("accounts", order_by=id), lazy=True)
  56. memberships = relation("SiteMembership", primaryjoin=id==SiteMembership.account_id, backref=backref("account"), lazy=True)
  57. primary_payment = relation("PaymentAccount", primaryjoin=primary_payment_account_id==PaymentAccount.id, backref=backref("account", foreign_keys=[primary_payment_account_id,]), lazy=True)
  58. secondary_payment = relation("PaymentAccount", primaryjoin=secondary_payment_account_id==PaymentAccount.id, backref=backref("account2", foreign_keys=[secondary_payment_account_id,]), lazy=True)
  59. credit_cards = relation("CreditCard", primaryjoin=id==CreditCard.account_id, lazy=True)
  60. addresses = relation("Address", backref=backref("accounts", order_by=id), lazy=True)
  61. company = relation("Company", backref=backref("company", order_by=id), lazy=True)
  62.  
  63. @classmethod
  64. def by_username(self, username=None):
  65. """Lookup a user by their username."""
  66. if username:
  67. try:
  68. return self.query.filter_by(username=username).one()
  69. except NoResultFound, e:
  70. return False
  71.  
  72.  
  73. @classmethod
  74. def by_id(self, id=None):
  75. """Lookup a user by their user id."""
  76. if id:
  77. try:
  78. return self.query.filter_by(id=id).one()
  79. except NoResultFound, e:
  80. return False
  81.  
  82.  
  83. @classmethod
  84. def by_email(self, email=None):
  85. """Lookup a user by their user email."""
  86. if email:
  87. try:
  88. return self.query.filter_by(email=email).one()
  89. except NoResultFound, e:
  90. return False
  91.  
  92.  
  93. @property
  94. def full_name(self):
  95. """Returns their full name for easier display."""
  96. return "%s %s" % (self.first_name, self.last_name)
  97.  
  98. @property
  99. def transactions(self):
  100. from billing.model.transaction import Transaction
  101. return Transaction.query.filter_by(account_id=self.id).all()
  102.  
  103. @property
  104. def total_billing(self):
  105. from billing.model.transaction import Transaction
  106. total = decimal.Decimal(0)
  107. t = Transaction.query.filter_by(account_id=self.id).all()
  108. for transaction in t:
  109. total += decimal.Decimal(transaction.amount) if transaction.amount else decimal.Decimal(0)
  110. return total
  111. #return Transaction.query.filter_by(account_id=self.id).values(Transaction.amount).one()
  112.  
  113. @property
  114. def total_transactions(self):
  115. from billing.model.transaction import Transaction
  116. #return 5
  117. return Transaction.query.filter_by(account_id=self.id).value(func.count(Transaction.id).label("total"))
  118.  
  119.  
  120. @property
  121. def last_transaction_id(self):
  122. from billing.model.transaction import Transaction
  123. latest_trans = Transaction.query.filter_by(account_id=self.id).\
  124. filter_by(payment_account_id=self.primary_payment_account_id).\
  125. order_by(Transaction.created.desc()).limit(1).first()
  126. return latest_trans.processor_tid
  127.  
  128. def __repr__(self):
  129. return self.full_name
  130.  
  131.  
  132. Index("ix_account_username_password", Account.username, Account.password)
  133. Index("ix_account_username_password_active", Account.username, Account.password, Account.active)
  134. Index("ix_account_username_active", Account.username, Account.active)
  135. Index("ix_account_username_sqid", Account.username, Account.secret_question_id)
  136. Index("ix_account_username_ccid", Account.username, Account.primary_payment_account_id)
  137. Index("ix_account_id_ccid", Account.id, Account.primary_payment_account_id)
  138. Index("ix_account_id_sqid", Account.id, Account.secret_question_id)
  139. DDL("ALTER TABLE %(fullname)s COMMENT = 'A global account to which we link all products.'").execute_at("after-create", Account.__table__)
Add Comment
Please, Sign In to add comment