def create_next_payment(db, last_payment): last_date = last_payment.created_at; next_action_date = last_date + relativedelta( months = +1 ) next_payment = NextPayments(db, { 'account_id': last_payment.account_id, 'parrot_id': last_payment.parrot_id, 'action_date': next_action_date }); next_payment.insert()
def main(): connection = None log('cron4', 'Starting') try: connection, db = connect() next_payments = NextPayments.find( db, {'action_date': {'$lte': datetime.now()}}, {'parrot_id':1,'account_id':1,'_id':1} ).sort([('_id', -1)]) for next_payment in next_payments: subscription = Subscriptions.findOne(db, {'account_id': next_payment.get('account_id'), 'parrot_id': next_payment.get('parrot_id')}) if subscription and subscription.active: created_message = Queue.insert( 'payments', { 'subscription_id': str(subscription.id), 'account_id': str(subscription.account_id), 'parrot_id': str(subscription.parrot_id) } ) if created_message: db.next_payments.remove({'_id': next_payment.get('_id')}) else: db.next_payments.remove({'_id': next_payment.get('_id')}) finally: if connection: connection.close() log('cron4', 'Finishing')
def test_cron4_no_subscription(self): account = Accounts(self.db, { 'email': '*****@*****.**', 'password': '******', 'name': 'Daniel', 'startup': 'Payparrot', 'url': 'http://payparrot.com/', 'callback_url': 'http://www.epistemonikos.org', 'notification_url': 'http://www.epistemonikos.org', }) account.insert() parrot = Parrots(self.db, { 'twitter_id': '123123123', 'oauth_token': 'asd', 'oauth_token_secret': 'asdf', 'twitter_info': {}, 'payments': [], 'twitter_info': { 'screen_name': 'danielgua' } }) parrot.insert() subscription = Subscriptions(self.db, {'account_id': account.id, 'active': False, 'parrot_id': parrot.id, 'twitter_screen_name': parrot.twitter_info.get("screen_name")}) subscription.insert() last_date = datetime.now(); next_action_date = last_date; next_payment = NextPayments(self.db, { 'account_id': account.id, 'parrot_id': parrot.id, 'action_date': next_action_date }); next_payment.insert() from payparrot_scripts.crons.cron4 import main as cron4 cron4() message = Queue.get_message('payments') self.assertFalse(message) self.assertEqual(0, self.db.next_payments.find({'_id': next_payment.id}).count())