def send_welcome_email(user_record, user): """Send this email when user is created from backend. :param user_record: User record. :param user: User account. """ user_record = user_record.replace_refs() code = user_record['organisation'].get('code', '') plain_platform_name = 'SONAR' pname = platform_name(user_record['organisation']) if pname: plain_platform_name = pname token = generate_reset_password_token(user) reset_link = url_for_security( 'reset_password', token=token, next=f'/{code}', _external=True ) send_email( [user_record['email']], f'{_("Welcome to")} {plain_platform_name}', 'users/email/welcome', { 'user': user_record, 'reset_link': reset_link, 'platform_name': plain_platform_name } )
def _send_email(self, recipients, email_type, **kwargs): """Send an email to concerned people. :param recipients: Send email to this list. :param email_type: Type of email. """ if not isinstance(recipients, list) or not recipients: raise Exception('No recipients found in list') send_email(recipients, _('Record validation'), f'validation/email/{email_type}', kwargs, False, current_i18n.language)
def publish(pid=None): """Publish a deposit or send a message for review.""" deposit = DepositRecord.get_record_by_pid(pid) if not deposit or deposit[ 'step'] != DepositRecord.STEP_DIFFUSION or deposit[ 'status'] not in [ DepositRecord.STATUS_IN_PROGRESS, DepositRecord.STATUS_ASK_FOR_CHANGES ]: abort(400) user = UserRecord.get_record_by_ref_link(deposit['user']['$ref']) # Deposit can be validated directly if user.is_granted(UserRecord.ROLE_MODERATOR): deposit['status'] = DepositRecord.STATUS_VALIDATED # Create document based on deposit deposit.create_document() else: deposit['status'] = DepositRecord.STATUS_TO_VALIDATE subdivision = SubdivisionRecord.get_record_by_ref_link( user['subdivision']['$ref']) if user.get('subdivision') else None moderators_emails = user.get_moderators_emails( subdivision['pid'] if subdivision else None) email_subject = _('Deposit to validate') if subdivision: email_subject += f' ({get_language_value(subdivision["name"])})' if moderators_emails: # Send an email to validators send_email( moderators_emails, email_subject, 'deposits/email/validation', { 'deposit': deposit, 'user': user, 'link': current_app.config.get('SONAR_APP_ANGULAR_URL') }, False) deposit.log_action(user, 'submit') deposit.commit() deposit.reindex() db.session.commit() return make_response()
def send_welcome_email(user_record, user): """Send this email when user is created from backend. :param user_record: User record. :param user: User account. """ user_record = user_record.replace_refs() token = generate_reset_password_token(user) reset_link = url_for_security('reset_password', token=token, _external=True) send_email([user_record['email']], _('Welcome on SONAR'), 'users/email/welcome', { 'user': user_record, 'reset_link': reset_link })
def review(pid=None): """Review a deposit and change the deposit status depending on action.""" deposit = DepositRecord.get_record_by_pid(pid) if not deposit or deposit['status'] != DepositRecord.STATUS_TO_VALIDATE: abort(400) payload = request.get_json() if not payload: abort(400) if 'action' not in payload or 'user' not in payload or payload[ 'action'] not in [ DepositRecord.REVIEW_ACTION_APPROVE, DepositRecord.REVIEW_ACTION_REJECT, DepositRecord.REVIEW_ACTION_ASK_FOR_CHANGES ]: abort(400) user = UserRecord.get_record_by_ref_link(payload['user']['$ref']) if not user or not user.is_moderator: abort(403) subject = None status = None if payload['action'] == DepositRecord.REVIEW_ACTION_APPROVE: subject = _('Deposit approval') status = DepositRecord.STATUS_VALIDATED # Create document based on deposit deposit.create_document() elif payload['action'] == DepositRecord.REVIEW_ACTION_REJECT: subject = _('Deposit rejection') status = DepositRecord.STATUS_REJECTED else: subject = _('Ask for changes on deposit') status = DepositRecord.STATUS_ASK_FOR_CHANGES deposit['status'] = status # Log action deposit.log_action(payload['user'], payload['action'], payload['comment']) # Load user who creates the deposit deposit_user = UserRecord.get_record_by_ref_link(deposit['user']['$ref']) send_email( [deposit_user['email']], subject, 'deposits/email/{action}'.format(action=payload['action']), { 'deposit': deposit, 'deposit_user': deposit_user, 'user': user, 'date': datetime.now().strftime('%d.%m.%Y %H:%M:%S'), 'comment': payload['comment'], 'link': current_app.config.get('SONAR_APP_ANGULAR_URL') }, False) deposit.commit() deposit.reindex() db.session.commit() return make_response(jsonify(deposit))