def send_email_alert(to_address: str, username: str, message_id: int = None): """ Send an email to user to alert them they have a new message""" current_app.logger.debug(f"Test if email required {to_address}") if not to_address: return False # Many users will not have supplied email address so return message_path = "" if message_id is not None: message_path = f"/message/{message_id}" # TODO these could be localised if needed, in the future html_template = get_template("message_alert_en.html") text_template = get_template("message_alert_en.txt") inbox_url = f"{current_app.config['APP_BASE_URL']}/inbox{message_path}" html_template = html_template.replace("[USERNAME]", username) html_template = html_template.replace("[PROFILE_LINK]", inbox_url) text_template = text_template.replace("[USERNAME]", username) text_template = text_template.replace("[PROFILE_LINK]", inbox_url) subject = "You have a new message on the HOT Tasking Manager" SMTPService._send_message(to_address, subject, html_template, text_template) return True
def send_email_alert(to_address: str, username: str, message_id: int, subject: str, content: str): """Send an email to user to alert that they have a new message""" current_app.logger.debug(f"Test if email required {to_address}") org_code = current_app.config["ORG_CODE"] settings_url = "{}/settings#notifications".format( current_app.config["APP_BASE_URL"]) if not to_address: return False # Many users will not have supplied email address so return message_path = "" if message_id is not None: message_path = f"/message/{message_id}" # TODO these could be localised if needed, in the future html_template = get_template("message_alert_en.html") text_template = get_template("message_alert_en.txt") inbox_url = f"{current_app.config['APP_BASE_URL']}/inbox{message_path}" replace_list = [ ["[ORG_CODE]", org_code], ["[PROFILE_LINK]", inbox_url], ["[SETTINGS_LINK]", settings_url], ] html_replace_list = replace_list + [[ "[CONTENT]", format_username_link(content) ]] html_template = template_var_replacing(html_template, html_replace_list) replace_list += [["[CONTENT]", content]] text_template = template_var_replacing(text_template, replace_list) SMTPService._send_message(to_address, subject, html_template, text_template) return True
def notify_level_upgrade(user_id: int, username: str, level: str): text_template = get_template("level_upgrade_message_en.txt") if username is not None: text_template = text_template.replace("[USERNAME]", username) text_template = text_template.replace("[LEVEL]", level) level_upgrade_message = Message() level_upgrade_message.to_user_id = user_id level_upgrade_message.subject = "Mapper Level Upgrade " level_upgrade_message.message = text_template level_upgrade_message.save()
def send_verification_email(to_address: str, username: str): """ Sends a verification email with a unique token so we can verify user owns this email address """ # TODO these could be localised if needed, in the future verification_url = SMTPService._generate_email_verification_url( to_address, username) values = { "USERNAME": username, "VERIFICATION_LINK": verification_url, } html_template = get_template("email_verification_en.html", values) subject = "Confirm your email address" SMTPService._send_message(to_address, subject, html_template) return True
def send_verification_email(to_address: str, username: str): """ Sends a verification email with a unique token so we can verify user owns this email address """ org_code = current_app.config["ORG_CODE"] # TODO these could be localised if needed, in the future html_template = get_template("email_verification_en.html") text_template = get_template("email_verification_en.txt") verification_url = SMTPService._generate_email_verification_url( to_address, username) replace_list = [ ["[USERNAME]", username], ["[VERIFICATION_LINK]", verification_url], ["[ORG_CODE]", org_code], ["[ORG_NAME]", current_app.config["ORG_NAME"]], ] html_template = template_var_replacing(html_template, replace_list) text_template = template_var_replacing(text_template, replace_list) subject = "{} Tasking Manager - Email Verification".format(org_code) SMTPService._send_message(to_address, subject, html_template, text_template) return True
def notify_level_upgrade(user_id: int, username: str, level: str): text_template = get_template("level_upgrade_message_en.txt") replace_list = [ ["[USERNAME]", username], ["[LEVEL]", level], ["[ORG_CODE]", current_app.config["ORG_CODE"]], ] text_template = template_var_replacing(text_template, replace_list) level_upgrade_message = Message() level_upgrade_message.to_user_id = user_id level_upgrade_message.subject = "Mapper level upgrade" level_upgrade_message.message = text_template level_upgrade_message.save()
def send_verification_email(to_address: str, username: str): """ Sends a verification email with a unique token so we can verify user owns this email address """ # TODO these could be localised if needed, in the future html_template = get_template("email_verification_en.html") text_template = get_template("email_verification_en.txt") verification_url = SMTPService._generate_email_verification_url( to_address, username) html_template = html_template.replace("[USERNAME]", username) html_template = html_template.replace("[VEFIFICATION_LINK]", verification_url) text_template = text_template.replace("[USERNAME]", username) text_template = text_template.replace("[VEFIFICATION_LINK]", verification_url) subject = "HOT Tasking Manager - Email Verification" SMTPService._send_message(to_address, subject, html_template, text_template) return True
def send_welcome_message(user: User): """ Sends welcome message to all new users at Sign up""" text_template = get_template("welcome_message_en.txt") text_template = text_template.replace("[USERNAME]", user.username) text_template = text_template.replace("[PROFILE_LINK]", get_profile_url(user.username)) welcome_message = Message() welcome_message.message_type = MessageType.SYSTEM.value welcome_message.to_user_id = user.id welcome_message.subject = "Welcome to the HOT Tasking Manager" welcome_message.message = text_template welcome_message.save() return welcome_message.id
def send_email_alert( to_address: str, username: str, user_email_verified: bool, message_id: int, from_username: str, project_id: int, task_id: int, subject: str, content: str, message_type: int, ): """Send an email to user to alert that they have a new message.""" if not user_email_verified: return False current_app.logger.debug(f"Test if email required {to_address}") from_user_link = f"{current_app.config['APP_BASE_URL']}/users/{from_username}" project_link = f"{current_app.config['APP_BASE_URL']}/projects/{project_id}" task_link = f"{current_app.config['APP_BASE_URL']}/projects/{project_id}/tasks/?search={task_id}" settings_url = "{}/settings#notifications".format( current_app.config["APP_BASE_URL"]) if not to_address: return False # Many users will not have supplied email address so return message_path = "" if message_id is not None: message_path = f"/message/{message_id}" inbox_url = f"{current_app.config['APP_BASE_URL']}/inbox{message_path}" values = { "FROM_USER_LINK": from_user_link, "FROM_USERNAME": from_username, "PROJECT_LINK": project_link, "PROJECT_ID": str(project_id) if project_id is not None else None, "TASK_LINK": task_link, "TASK_ID": str(task_id) if task_id is not None else None, "PROFILE_LINK": inbox_url, "SETTINGS_LINK": settings_url, "CONTENT": format_username_link(content), "MESSAGE_TYPE": message_type, } html_template = get_template("message_alert_en.html", values) SMTPService._send_message(to_address, subject, html_template) return True
def send_message_after_validation( status: int, validated_by: int, mapped_by: int, task_id: int, project_id: int ): """ Sends mapper a notification after their task has been marked valid or invalid """ if validated_by == mapped_by: return # No need to send a message to yourself user = UserService.get_user_by_id(mapped_by) text_template = get_template( "invalidation_message_en.txt" if status == TaskStatus.INVALIDATED else "validation_message_en.txt" ) status_text = ( "marked invalid" if status == TaskStatus.INVALIDATED else "validated" ) task_link = MessageService.get_task_link(project_id, task_id) replace_list = [ ["[USERNAME]", user.username], ["[TASK_LINK]", task_link], ["[ORG_NAME]", current_app.config["ORG_NAME"]], ] text_template = template_var_replacing(text_template, replace_list) messages = [] validation_message = Message() validation_message.message_type = ( MessageType.INVALIDATION_NOTIFICATION.value if status == TaskStatus.INVALIDATED else MessageType.VALIDATION_NOTIFICATION.value ) validation_message.project_id = project_id validation_message.task_id = task_id validation_message.from_user_id = validated_by validation_message.to_user_id = mapped_by validation_message.subject = ( f"{task_link} mapped by you in Project {project_id} has been {status_text}" ) validation_message.message = text_template messages.append(dict(message=validation_message, user=user)) # For email alerts MessageService._push_messages(messages)
def send_message_after_validation( status: int, validated_by: int, mapped_by: int, task_id: int, project_id: int ): """ Sends mapper a notification after their task has been marked valid or invalid """ if validated_by == mapped_by: return # No need to send a message to yourself user = UserService.get_user_by_id(mapped_by) if user.validation_message is False: return # No need to send validation message if user.projects_notifications is False: return text_template = get_template( "invalidation_message_en.txt" if status == TaskStatus.INVALIDATED else "validation_message_en.txt" ) status_text = ( "marked invalid" if status == TaskStatus.INVALIDATED else "validated" ) task_link = MessageService.get_task_link(project_id, task_id) text_template = text_template.replace("[USERNAME]", user.username) text_template = text_template.replace("[TASK_LINK]", task_link) validation_message = Message() validation_message.message_type = ( MessageType.INVALIDATION_NOTIFICATION.value if status == TaskStatus.INVALIDATED else MessageType.VALIDATION_NOTIFICATION.value ) validation_message.project_id = project_id validation_message.task_id = task_id validation_message.from_user_id = validated_by validation_message.to_user_id = mapped_by validation_message.subject = f"Your mapping in Project {project_id} on {task_link} has just been {status_text}" validation_message.message = text_template validation_message.add_message() SMTPService.send_email_alert( user.email_address, user.username, validation_message.id )
def send_welcome_message(user: User): """ Sends welcome message to all new users at Sign up""" org_code = current_app.config["ORG_CODE"] text_template = get_template("welcome_message_en.txt") replace_list = [ ["[USERNAME]", user.username], ["[ORG_CODE]", org_code], ["[ORG_NAME]", current_app.config["ORG_NAME"]], ["[SETTINGS_LINK]", MessageService.get_user_settings_link()], ] text_template = template_var_replacing(text_template, replace_list) welcome_message = Message() welcome_message.message_type = MessageType.SYSTEM.value welcome_message.to_user_id = user.id welcome_message.subject = "Welcome to the {} Tasking Manager".format(org_code) welcome_message.message = text_template welcome_message.save() return welcome_message.id
def test_variable_replacing(self): # Act content = get_template("email_verification_en.html") replace_list = [ ["[USERNAME]", "test_user"], ["[VERIFICATION_LINK]", "http://localhost:30/verify.html#1234"], ["[ORG_CODE]", "HOT"], ["[ORG_NAME]", "Organization Test"], ] processed_content = template_var_replacing(content, replace_list) # Assert self.assertIn("test_user", processed_content) self.assertIn("http://localhost:30/verify.html#1234", processed_content) self.assertIn("HOT", processed_content) self.assertIn("Organization Test", processed_content) self.assertNotIn("[USERNAME]", processed_content) self.assertNotIn("[VERIFICATION_LINK]", processed_content) self.assertNotIn("[ORG_CODE]", processed_content) self.assertNotIn("[ORG_NAME]", processed_content)