From d7ad60fb9d53ec3b2cc227d8b195d73d415bb14c Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Mon, 28 Nov 2016 13:01:06 +0100 Subject: [PATCH] Add reconcile_subscribers function For every user, check their subscription status with the store. --- manage.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/manage.py b/manage.py index ece6bab..b3bf271 100755 --- a/manage.py +++ b/manage.py @@ -1,7 +1,41 @@ #!/usr/bin/env python +from __future__ import print_function + +import logging +from flask import current_app from pillar import cli +from pillar.cli import manager from cloud import app +log = logging.getLogger(__name__) + + +@manager.command +def reconcile_subscribers(): + """For every user, check their subscription status with the store.""" + from pillar.auth.subscriptions import fetch_user + + users_coll = current_app.data.driver.db['users'] + unsubscribed_users = [] + for user in users_coll.find(): + print('Processing %s' % user['email']) + if 'roles' in user and 'subscriber' in user['roles']: + print('Checking subscription') + user_store = fetch_user(user['email']) + if user_store['cloud_access'] == 0: + print('Removing subscriber role') + users_coll.update( + {'_id': user['_id']}, + {'$pull': {'roles': 'subscriber'}}) + unsubscribed_users.append(user['email']) + + if not unsubscribed_users: + return + + print('The following users have been unsubscribed') + for user in unsubscribed_users: + print(user) + cli.manager.app = app cli.manager.run()