def credit_cards(db): """ Create credit card fixtures. They reset per test. :param db: Pytest fixture :return: SQLAlchemy database session """ db.session.query(CreditCard).delete() may_29_2015 = datetime.date(2015, 05, 29) june_29_2015 = datetime.datetime(2015, 06, 29, 0, 0, 0) june_29_2015 = pytz.utc.localize(june_29_2015) credit_cards = [ { 'user_id': 1, 'brand': 'Visa', 'last4': 4242, 'exp_date': june_29_2015 }, { 'user_id': 1, 'brand': 'Visa', 'last4': 4242, 'exp_date': timedelta_months(12, may_29_2015) } ] for card in credit_cards: db.session.add(CreditCard(**card)) db.session.commit() return db
def is_expiring_soon(cls, compare_date=None, exp_date=None): """ Determine whether or not this credit card is expiring soon. :param compare_date: Date to compare at :type compare_date: date :param exp_date: Expiration date :type exp_date: date :return: bool """ return exp_date <= timedelta_months( CreditCard.IS_EXPIRING_THRESHOLD_MONTHS, compare_date=compare_date)
def mark_old_credit_cards(cls, compare_date=None): """ Mark credit cards that are going to expire soon or have expired. :param compare_date: Date to compare at :type compare_date: date :return: Result of updating the records """ today_with_delta = timedelta_months( CreditCard.IS_EXPIRING_THRESHOLD_MONTHS, compare_date) CreditCard.query.filter(CreditCard.exp_date <= today_with_delta) \ .update({CreditCard.is_expiring: True}) return db.session.commit()