def create(cls, params): """ Return whether or not the coupon was created successfully. :return: bool """ payment_params = params payment_params['code'] = payment_params['code'].upper() if payment_params.get('amount_off'): payment_params['amount_off'] = \ dollars_to_cents(payment_params['amount_off']) PaymentCoupon.create(**payment_params) if 'id' in payment_params: payment_params['code'] = payment_params['id'] del payment_params['id'] if 'redeem_by' in payment_params: if payment_params.get('redeem_by') is not None: params['redeem_by'] = payment_params.get('redeem_by').replace( tzinfo=pytz.UTC) coupon = Coupon(**payment_params) db.session.add(coupon) db.session.commit() return True
def create(cls, params): """ Create a coupon code and return true is successful """ payment_params = params payment_params['code'] = payment_params['code'].upper() if payment_params.get('amount_off'): payment_params['amount_off'] = dollars_to_cents( payment_params['amount_off']) PaymentCoupon.create(**payment_params) # Stripe will save the coupon to id field on stripe while on our database, we want it to save on code field if 'id' in payment_params: payment_params['code'] = payment_params['id'] del payment_params['id'] # Converting th eunix time to day stim stamp that is acceptable by the databse if 'redeem_by' in payment_params: if payment_params.get('redeem_by') is not None: params['redeem_by'] = payment_params.get('redeem_by').replace( datetime.datetime.utcnow) coupon = Coupon(**payment_params) db.session.add(coupon) db.session.commit() return True
def bulk_delete(cls, ids): """ Override the general bulk_delete method because we need to delete them one at a time while also deleting them on Stripe. :param ids: List of ids to be deleted :type ids: list :return: int """ delete_count = 0 for id in ids: coupon = Coupon.query.get(id) if coupon is None: continue # Delete on Stripe. stripe_response = PaymentCoupon.delete(coupon.code) # If successful, delete it locally. if stripe_response.get('deleted'): coupon.delete() delete_count += 1 return delete_count
def bulk_delete(cls, ids): """ Override the general bulk delete method to delete coupon from application and stripe """ delete_count = 0 for id in ids: coupon = Coupon.query.get(id) print(coupon) if coupon is None: continue # Delete on stripe stripe_delete = PaymentCoupon.delete(coupon) # If successful, delete it locally if stripe_delete.get('deleted'): db.session.delete(coupon) delete_count += 1 return delete_count