Added a one off command to add many invited user to a service.

It would be nice to add something that can do this from the Admin app.
This commit is contained in:
Rebecca Law
2018-06-28 17:09:45 +01:00
parent 17a971cb77
commit 213e32cf6d
2 changed files with 49 additions and 0 deletions

View File

@@ -33,6 +33,7 @@ from app.dao.services_dao import (
dao_fetch_service_by_id
)
from app.dao.users_dao import (delete_model_user, delete_user_verify_codes)
from app.invite.rest import create_invited_user
from app.models import PROVIDERS, User, SMS_TYPE, EMAIL_TYPE, Notification
from app.performance_platform.processing_time import (send_processing_time_for_start_and_end)
from app.utils import (
@@ -622,3 +623,47 @@ def migrate_data_to_ft_notification_status(start_date, end_date):
total_updated += result.rowcount
print('Total inserted/updated records = {}'.format(total_updated))
@notify_command(name='bulk-invite-user-to-service')
@click.option('-f', '--file_name', required=True,
help="Full path of the file containing a list of email address for people to invite to a service")
@click.option('-s', '--service_id', required=True, help='The id of the service that the invite is for')
@click.option('-u', '--user_id', required=True, help='The id of the user that the invite is from')
@click.option('-a', '--auth_type', required=False,
help='The authentication type for the user, sms_auth or email_auth. Defaults to sms_auth if not provided')
@click.option('-p', '--permissions', required=True, help='Comma separated list of permissions.')
def bulk_invite_user_to_service(file_name, service_id, user_id, auth_type, permissions):
# permissions
# manage_users | manage_templates | manage_settings
# send messages ==> send_texts | send_emails | send_letters
# Access API keys manage_api_keys
# platform_admin
# view_activity
# "send_texts,send_emails,send_letters,view_activity"
file = open(file_name)
for email_address in file:
data = {
'service': service_id,
'email_address': email_address.strip(),
'from_user': user_id,
'permissions': permissions,
'auth_type': auth_type,
'invite_link_host': current_app.config['ADMIN_BASE_URL']
}
with current_app.test_request_context(
path='/service/{}/invite/'.format(service_id),
method='POST',
data=json.dumps(data),
headers={"Content-Type": "application/json"}
):
try:
response = create_invited_user(service_id)
if response[1] != 201:
print("*** ERROR occurred for email address: {}".format(email_address.strip()))
print(response[0].get_data(as_text=True))
except Exception as e:
print("*** ERROR occurred for email address: {}. \n{}".format(email_address.strip(), e))
file.close()