def main( data_api_url, data_api_access_token, notify_api_key, notify_template_id, offset_days, dry_run=None, date_closed=None, user_id_list=None ): """ Send emails to buyers via Notify, reminding them to award their closed briefs offset_days: send emails for briefs that closed X days ago dry_run: log instead of sending emails date_closed: if supplied, send emails for briefs that closed on this date user_id_list: if supplied, only send emails to buyers with these user IDs """ logger.info("Data API URL: {data_api_url}", extra={'data_api_url': data_api_url}) date_closed = _get_brief_closing_date(offset_days, date_closed) if not date_closed: logger.error('Not allowed to notify about briefs that closed less than {} days ago', offset_days) return False data_api_client = dmapiclient.DataAPIClient(data_api_url, data_api_access_token) notify_client = scripts_notify_client(notify_api_key, logger=logger) closed_briefs = brief_data_helpers.get_briefs_closed_on_date(data_api_client, date_closed) if not closed_briefs: logger.info("No briefs closed on {date_closed}", extra={"date_closed": date_closed}) return True # If user list supplied, only continue for briefs that have at least one user in that list if user_id_list: closed_briefs = _filter_briefs_by_user_id_list(closed_briefs, user_id_list) logger.info("{briefs_count} closed brief(s) found with closing date {date_closed}", extra={ 'briefs_count': len(closed_briefs), "date_closed": date_closed }) failed_users_by_brief_id = {} for brief in closed_briefs: failed_users_for_this_brief = [] for user in brief['users']: failed_user_id = send_email_to_brief_user_via_notify( notify_client, notify_template_id, user, brief, user_id_list, dry_run ) if failed_user_id: failed_users_for_this_brief.append(failed_user_id) if failed_users_for_this_brief: failed_users_by_brief_id[brief['id']] = failed_users_for_this_brief if failed_users_by_brief_id: _log_failures(failed_users_by_brief_id, date_closed) return False return True
def notify_suppliers_with_incomplete_applications(framework_slug, data_api_client, notify_api_key, dry_run, logger, supplier_ids=None): framework = data_api_client.get_framework(framework_slug)['frameworks'] if framework['status'] != 'open': raise ValueError( "Suppliers cannot amend applications unless the framework is open." ) mail_client = scripts_notify_client(notify_api_key, logger=logger) error_count = 0 for sf in data_api_client.find_framework_suppliers_iter(framework_slug): # Restrict suppliers to those specified in the argument, if given. # While this is inefficient for a small number of supplier IDs, looking up # each supplier individually for a large number of supplier IDs would be worse. if supplier_ids: if sf['supplierId'] not in supplier_ids: continue message = build_message(sf, framework_slug, data_api_client) if message: primary_email = sf.get('declaration', { 'primaryContactEmail': None }).get('primaryContactEmail', None) if primary_email: error_count += send_notification(mail_client, message, framework, primary_email, sf['supplierId'], dry_run) for user in data_api_client.find_users( supplier_id=sf['supplierId']).get('users', []): if user['active']: error_count += send_notification(mail_client, message, framework, user['emailAddress'], sf['supplierId'], dry_run) return error_count
notify_suppliers_of_framework_application_event if __name__ == "__main__": arguments = docopt(__doc__) logger = logging_helpers.configure_logger({"dmapiclient": logging.INFO}) run_id = None if not arguments.get("--resume-run-id") else UUID( arguments["--resume-run-id"]) failure_count = notify_suppliers_of_framework_application_event( data_api_client=DataAPIClient( base_url=get_api_endpoint_from_stage(arguments["<stage>"], "api"), auth_token=get_auth_token("api", arguments["<stage>"]), ), notify_client=scripts_notify_client( arguments['<govuk_notify_api_key>'], logger=logger), notify_template_id=arguments['<govuk_notify_template_id>'], framework_slug=arguments["<framework_slug>"], stage=arguments["<stage>"], dry_run=arguments["--dry-run"], logger=logger, run_id=run_id, ) if failure_count: logger.error("Failed sending {failure_count} messages", extra={"failure_count": failure_count}) sys.exit(failure_count)
logger = logging_helpers.configure_logger() if __name__ == '__main__': arguments = docopt(__doc__) supplier_ids = get_supplier_ids_from_args(arguments) STAGE = arguments['<stage>'] FRAMEWORK_SLUG = arguments['<framework>'] GOVUK_NOTIFY_API_KEY = arguments['<notify_api_key>'] GOVUK_NOTIFY_TEMPLATE_ID = arguments['<notify_template_id>'] CONTENT_PATH = arguments['<content_path>'] DRY_RUN = arguments['--dry-run'] content_loader = ContentLoader(CONTENT_PATH) content_loader.load_messages(FRAMEWORK_SLUG, ['e-signature']) mail_client = scripts_notify_client(GOVUK_NOTIFY_API_KEY, logger=logger) api_client = DataAPIClient(base_url=get_api_endpoint_from_stage(STAGE), auth_token=get_auth_token('api', STAGE)) context_helper = SuccessfulSupplierContextForNotify( api_client, FRAMEWORK_SLUG, supplier_ids=supplier_ids, logger=logger) context_helper.populate_data() context_data = context_helper.get_users_personalisations() framework = api_client.get_framework(FRAMEWORK_SLUG).get('frameworks') prefix = "[Dry Run] " if DRY_RUN else "" # Add in any framework-specific dates etc here extra_template_context = { "contract_title": content_loader.get_message(FRAMEWORK_SLUG, 'e-signature',
logger = configure_logger({ "dmapiclient.base": logging.WARNING, "framework_helpers": logging.DEBUG if verbose >= 2 else logging.WARNING, "script": logging.DEBUG if verbose else logging.INFO, }) client = DataAPIClient( get_api_endpoint_from_stage(args["<stage>"]), get_auth_token("api", args["<stage>"]), ) notify_client = scripts_notify_client(NOTIFY_API_KEY, logger=logger) framework = client.get_framework(framework_slug)["frameworks"] # Check that the framework is in live or standstill if framework['status'] not in ['live', 'standstill']: logger.error( f"Cannot suspend services for '{framework_slug}' with status {framework['status']}" ) exit(1) supplier_ids = get_supplier_ids_from_args(args) suppliers = find_suppliers_without_agreements(client, framework_slug, supplier_ids) framework_name = framework["name"] contract_title = get_framework_contract_title(FRAMEWORKS_PATH,
arguments = docopt(__doc__) if bool(arguments.get("--notify-key")) != bool( arguments.get("--notify-template-id")): raise ValueError( "Either specify both --notify-key and --notify-template-id or neither" ) stage = arguments['<stage>'] data_api_client = DataAPIClient(get_api_endpoint_from_stage(stage), get_auth_token('api', stage)) framework = data_api_client.get_framework( arguments['<framework_slug>'])["frameworks"] document_directory = arguments['<documents_directory>'] dry_run = arguments['--dry-run'] dm_notify_client = arguments.get("--notify-key") and scripts_notify_client( arguments["--notify-key"], logger=logger) if dry_run: bucket = None else: bucket = S3(get_bucket_name(stage, "agreements")) failure_count = 0 for file_path in get_all_files_of_type(document_directory, "pdf"): try: upload_counterpart_file( bucket, framework, file_path, dry_run,
arguments = docopt(__doc__) # Get arguments stage = arguments['<stage>'] govuk_notify_api_key = arguments['<govuk_notify_api_key>'] govuk_notify_template_id = arguments['<govuk_notify_template_id>'] awarded_at = arguments.get('--awarded_at', None) brief_response_ids = arguments.get('--brief_response_ids', None) dry_run = arguments['--dry-run'] verbose = arguments['--verbose'] # Set defaults, instantiate clients logger = logging_helpers.configure_logger( {"dmapiclient": logging. INFO} if verbose else {"dmapiclient": logging.WARN}) notify_client = scripts_notify_client(govuk_notify_api_key, logger=logger) data_api_client = DataAPIClient( base_url=get_api_endpoint_from_stage(stage), auth_token=get_auth_token('api', stage)) list_of_brief_response_ids = list(map( int, brief_response_ids.split(','))) if brief_response_ids else None # Do send ok = main( data_api_client=data_api_client, mail_client=notify_client, template_id=govuk_notify_template_id, stage=stage, logger=logger, awarded_at=awarded_at,
from dmscripts.helpers.auth_helpers import get_auth_token from dmscripts.helpers import logging_helpers from dmscripts.helpers.logging_helpers import logging from dmscripts.helpers.supplier_data_helpers import get_supplier_ids_from_args from dmutils.env_helpers import get_api_endpoint_from_stage from dmscripts.notify_suppliers_whether_application_made_for_framework import notify_suppliers_whether_application_made logger = logging_helpers.configure_logger({"dmapiclient": logging.INFO}) if __name__ == '__main__': arguments = docopt(__doc__) supplier_ids = get_supplier_ids_from_args(arguments) stage = arguments['<stage>'] mail_client = scripts_notify_client(arguments['<notify_api_key>'], logger=logger) api_client = DataAPIClient(base_url=get_api_endpoint_from_stage(stage), auth_token=get_auth_token('api', stage)) error_count = notify_suppliers_whether_application_made( api_client, mail_client, arguments['<framework>'], logger=logger, dry_run=arguments['--dry-run'], supplier_ids=supplier_ids, ) sys.exit(error_count)
from dmscripts.helpers import logging_helpers from dmscripts.helpers.auth_helpers import get_g12_suppliers, get_auth_token from dmscripts.helpers.email_helpers import scripts_notify_client from dmscripts.helpers.supplier_data_helpers import get_email_addresses_for_supplier NOTIFY_TEMPLATE_ID = "347e1ed7-ec83-45a0-bb16-832f244f8919" if __name__ == "__main__": arguments = docopt(__doc__) stage = arguments["<stage>"] dry_run = arguments["--dry-run"] notify_api_key = arguments["<notify_api_key>"] logger = logging_helpers.configure_logger() mail_client = scripts_notify_client(notify_api_key, logger=logger) api_client = DataAPIClient( base_url=get_api_endpoint_from_stage(stage), auth_token=get_auth_token("api", stage), ) user_emails = [ supplier_user for supplier_id in get_g12_suppliers(stage) for supplier_user in get_email_addresses_for_supplier(api_client, supplier_id) ] user_count = len(user_emails) prefix = "[Dry Run] " if dry_run else "" for count, email in enumerate(user_emails, start=1): logger.info(