diff --git a/apps/profile/management/commands/reimport_stripe_history.py b/apps/profile/management/commands/reimport_stripe_history.py index bb8ba076a..cc5524296 100644 --- a/apps/profile/management/commands/reimport_stripe_history.py +++ b/apps/profile/management/commands/reimport_stripe_history.py @@ -17,49 +17,8 @@ class Command(BaseCommand): ) def handle(self, *args, **options): - stripe.api_key = settings.STRIPE_SECRET - week = (datetime.datetime.now() - datetime.timedelta(days=int(options.get('days', 365)))).strftime('%s') - failed = [] limit = options.get('limit') + days = int(options.get('days')) starting_after = options.get('start') - i = 0 - while True: - logging.debug(" ---> At %s / %s" % (i, starting_after)) - i += 1 - try: - data = stripe.Charge.all(created={'gt': week}, count=limit, starting_after=starting_after) - except stripe.APIConnectionError: - time.sleep(10) - continue - charges = data['data'] - if not len(charges): - logging.debug("At %s (%s), finished" % (i, starting_after)) - break - starting_after = charges[-1]["id"] - customers = [c['customer'] for c in charges if 'customer' in c] - for customer in customers: - if not customer: - print " ***> No customer!" - continue - try: - profile = Profile.objects.get(stripe_id=customer) - user = profile.user - except Profile.DoesNotExist: - logging.debug(" ***> Couldn't find stripe_id=%s" % customer) - failed.append(customer) - continue - except Profile.MultipleObjectsReturned: - logging.debug(" ***> Multiple stripe_id=%s" % customer) - failed.append(customer) - continue - try: - user.profile.setup_premium_history() - except stripe.APIConnectionError: - logging.debug(" ***> Failed: %s" % user.username) - failed.append(user.username) - time.sleep(2) - continue - - return ','.join(failed) - + Profile.reimport_stripe_history(limit, days, starting_after) \ No newline at end of file diff --git a/apps/profile/models.py b/apps/profile/models.py index f63828e43..eb80a43d5 100644 --- a/apps/profile/models.py +++ b/apps/profile/models.py @@ -316,6 +316,52 @@ class Profile(models.Model): (not self.premium_expire or self.premium_expire > datetime.datetime.now())): self.activate_premium() + @classmethod + def reimport_stripe_history(limit=10, days=7, starting_after=0): + stripe.api_key = settings.STRIPE_SECRET + week = (datetime.datetime.now() - datetime.timedelta(days=days)).strftime('%s') + failed = [] + i = 0 + + while True: + logging.debug(" ---> At %s / %s" % (i, starting_after)) + i += 1 + try: + data = stripe.Charge.all(created={'gt': week}, count=limit, starting_after=starting_after) + except stripe.APIConnectionError: + time.sleep(10) + continue + charges = data['data'] + if not len(charges): + logging.debug("At %s (%s), finished" % (i, starting_after)) + break + starting_after = charges[-1]["id"] + customers = [c['customer'] for c in charges if 'customer' in c] + for customer in customers: + if not customer: + print " ***> No customer!" + continue + try: + profile = Profile.objects.get(stripe_id=customer) + user = profile.user + except Profile.DoesNotExist: + logging.debug(" ***> Couldn't find stripe_id=%s" % customer) + failed.append(customer) + continue + except Profile.MultipleObjectsReturned: + logging.debug(" ***> Multiple stripe_id=%s" % customer) + failed.append(customer) + continue + try: + user.profile.setup_premium_history() + except stripe.APIConnectionError: + logging.debug(" ***> Failed: %s" % user.username) + failed.append(user.username) + time.sleep(2) + continue + + return ','.join(failed) + def refund_premium(self, partial=False): refunded = False diff --git a/apps/profile/tasks.py b/apps/profile/tasks.py index 7266e0256..491fb7302 100644 --- a/apps/profile/tasks.py +++ b/apps/profile/tasks.py @@ -79,5 +79,12 @@ class CleanSpam(Task): def run(self, **kwargs): logging.debug(" ---> Finding spammers...") Profile.clear_dead_spammers(confirm=True) + +class ReimportStripeHistory(Task): + name = 'reimport-stripe-history' + + def run(self, **kwargs): + logging.debug(" ---> Reimporting Stripe history...") + Profile.reimport_stripe_history(limit=10, days=1) diff --git a/settings.py b/settings.py index bf5f799f8..4b5ae459c 100644 --- a/settings.py +++ b/settings.py @@ -479,6 +479,11 @@ CELERYBEAT_SCHEDULE = { 'schedule': datetime.timedelta(hours=12), 'options': {'queue': 'beat_tasks', 'timeout': 720*10}, }, + 'reimport-stripe-history': { + 'task': 'reimport-stripe-history', + 'schedule': datetime.timedelta(hours=6), + 'options': {'queue': 'beat_tasks'}, + }, 'clean-spam': { 'task': 'clean-spam', 'schedule': datetime.timedelta(hours=6),