Usage: scripts/get_active_users_csv.py <stage> Example scripts/get_active_users_csv.py preview > output.csv """ import csv import sys sys.path.insert(0, '.') from dmscripts.helpers.auth_helpers import get_auth_token from dmutils.env_helpers import get_api_endpoint_from_stage from docopt import docopt from dmapiclient import DataAPIClient if __name__ == '__main__': arguments = docopt(__doc__) stage = arguments['<stage>'] data_api_client = DataAPIClient(get_api_endpoint_from_stage(stage), get_auth_token('api', stage)) writer = csv.writer(sys.stdout) writer.writerow(['email address']) for user in filter( lambda u: u['active'], data_api_client.find_users_iter(personal_data_removed=False)): writer.writerow([user['emailAddress']])
def get_email_addresses_for_supplier(api_client: DataAPIClient, supplier_id: int) -> List[str]: """Get the email addresses for each user belonging to `supplier_id`""" supplier_users = api_client.find_users_iter(supplier_id=supplier_id, personal_data_removed=False) return [user["emailAddress"] for user in supplier_users if user["active"]]
# Get script arguments stage = arguments['<stage>'] dry_run = arguments['--dry-run'] verbose = arguments['--verbose'] # Set defaults, instantiate clients prefix = '[DRY RUN]: ' if dry_run else '' logger = logging_helpers.configure_logger( {"dmapiclient": logging. INFO} if verbose else {"dmapiclient": logging.WARN}) data_api_client = DataAPIClient( base_url=get_api_endpoint_from_stage(stage), auth_token=get_auth_token('api', stage)) users = sorted(data_api_client.find_users_iter(role='supplier'), key=lambda i: i['supplier']['supplierId']) users_by_supplier_id = groupby(users, lambda i: i['supplier']['supplierId']) for supplier_id, users in users_by_supplier_id: if all(user['personalDataRemoved'] for user in users): supplier = data_api_client.get_supplier(supplier_id)['suppliers'] for contact_information in supplier['contactInformation']: if not contact_information['personalDataRemoved']: logger.warn( """{}Removing contact information #{} data for supplier {}""" .format(prefix, contact_information['id'], supplier_id)) if not dry_run:
def notify_suppliers_of_framework_application_event( data_api_client: DataAPIClient, notify_client: DMNotifyClient, notify_template_id: str, framework_slug: str, stage: str, dry_run: bool, logger: Logger, run_id: Optional[UUID] = None, ) -> int: run_is_new = not run_id run_id = run_id or uuid4() logger.info( f"{'Starting' if run_is_new else 'Resuming'} run id {{run_id}}", extra={"run_id": str(run_id)}) framework = data_api_client.get_framework(framework_slug)["frameworks"] framework_context = { "framework_name": framework["name"], "updates_url": f"{get_web_url_from_stage(stage)}/suppliers/frameworks/{framework['slug']}/updates", "framework_dashboard_url": f"{get_web_url_from_stage(stage)}/suppliers/frameworks/{framework['slug']}/", "clarification_questions_closed": "no" if framework["clarificationQuestionsOpen"] else "yes", **_formatted_dates_from_framework(framework), } failure_count = 0 for supplier_framework in data_api_client.find_framework_suppliers_iter( framework_slug): for user in data_api_client.find_users_iter( supplier_id=supplier_framework["supplierId"]): if user["active"]: # generating ref separately so we can exclude certain parameters from the context dict notify_ref = notify_client.get_reference( user["emailAddress"], notify_template_id, { "framework_slug": framework["slug"], "run_id": str(run_id), }, ) if dry_run: # Use the sent references cache unless we're re-running the script following a failure if notify_client.has_been_sent( notify_ref, use_recent_cache=run_is_new): logger.debug( "[DRY RUN] Would NOT send notification to {email_hash} (already sent)", extra={ "email_hash": hash_string(user["emailAddress"]) }, ) else: logger.info( "[DRY RUN] Would send notification to {email_hash}", extra={ "email_hash": hash_string(user["emailAddress"]) }, ) else: try: # Use the sent references cache unless we're re-running the script following a failure notify_client.send_email( user["emailAddress"], notify_template_id, framework_context, allow_resend=False, reference=notify_ref, use_recent_cache=run_is_new, ) except EmailError as e: failure_count += 1 logger.error( "Failed sending to {email_hash}: {e}", extra={ "email_hash": hash_string(user["emailAddress"]), "e": str(e), }, ) if isinstance(e, EmailTemplateError): raise # do not try to continue return failure_count
stage = arguments['<stage>'] dry_run = arguments['--dry-run'] verbose = arguments['--verbose'] # Set defaults, instantiate clients prefix = '[DRY RUN]: ' if dry_run else '' logger = logging_helpers.configure_logger( {"dmapiclient": logging.INFO} if verbose else {"dmapiclient": logging.WARN} ) data_api_client = DataAPIClient( base_url=get_api_endpoint_from_stage(stage), auth_token=get_auth_token('api', stage) ) users = sorted(data_api_client.find_users_iter(role='supplier'), key=lambda i: i['supplier']['supplierId']) users_by_supplier_id = groupby(users, lambda i: i['supplier']['supplierId']) for supplier_id, users in users_by_supplier_id: if all(user['personalDataRemoved'] for user in users): supplier = data_api_client.get_supplier(supplier_id)['suppliers'] for contact_information in supplier['contactInformation']: if not contact_information['personalDataRemoved']: logger.warn("""{}Removing contact information #{} data for supplier {}""".format( prefix, contact_information['id'], supplier_id) ) if not dry_run: data_api_client.remove_contact_information_personal_data(