def set_new_user(mailer: Mailer, user_data: Dict[str, Any], password: str, _tn: Translator) -> Dict[str, Any]: """ Public interface for creating a new user. :param mailer: The mailer used to send Emails to the new user. :param user_data: Dictionary containing user information. :param password: The desired password to be set. (un-hashed) :param _tn: The translator object to be used for messaging. :return: A dictionary containing whether the operation was a success, an optional error message and the newly created user if the transaction was successful. """ # copy the dict to not change the mutable structure temporary_user = dict(user_data) db_group = DBDiscussionSession.query(Group).filter_by(name='users').first() # does the group exists? if not db_group: LOG.debug("Internal error occurred") return { 'success': False, 'error': _tn.get(Keywords.errorTryLateOrContant), 'user': None } if DBDiscussionSession.query(User).filter( User.nickname == temporary_user['nickname']).first(): LOG.debug("User already exists") return { 'success': False, 'error': _tn.get(Keywords.nickIsTaken), 'user': None } temporary_user['password'] = password temporary_user['db_group_uid'] = db_group.uid success, info, db_new_user = __create_new_user(temporary_user, _tn.get_lang()) if db_new_user: # sending an email and message subject = _tn.get(Keywords.accountRegistration) body = _tn.get(Keywords.accountWasRegistered).format( temporary_user['firstname'], temporary_user['lastname'], temporary_user['email']) send_mail(mailer, subject, body, temporary_user['email'], _tn.get_lang()) send_welcome_notification(db_new_user.uid, _tn) LOG.debug("Set new user in db") return {'success': success, 'error': '', 'user': db_new_user} LOG.debug("New user not found in db") return { 'success': False, 'error': _tn.get(Keywords.errorTryLateOrContant), 'user': None }
def set_new_user(mailer, firstname, lastname, nickname, gender, email, password, _tn): """ Let's create a new user :param mailer: instance of pyramid mailer :param firstname: String :param lastname: String :param nickname: String :param gender: String :param email: String :param password: String :param _tn: Translaator :return: Boolean, msg """ # getting the authors group db_group = DBDiscussionSession.query(Group).filter_by(name='users').first() # does the group exists? if not db_group: logger('User', 'Internal error occured') return {'success': False, 'error': _tn.get(_.errorTryLateOrContant), 'user': None} # sanity check db_user = DBDiscussionSession.query(User).filter_by(nickname=nickname).first() if db_user: logger('User', 'User already exists') return {'success': False, 'error': _tn.get(_.nickIsTaken), 'user': None} user = { 'firstname': firstname, 'lastname': lastname, 'email': email, 'nickname': nickname, 'password': password, 'gender': gender, 'db_group_uid': db_group.uid } success, info, db_new_user = __create_new_user(user, _tn.get_lang()) if db_new_user: # sending an email and message subject = _tn.get(_.accountRegistration) body = _tn.get(_.accountWasRegistered).format(firstname, lastname, email) send_mail(mailer, subject, body, email, _tn.get_lang()) send_welcome_notification(db_new_user.uid, _tn) logger('User', 'set new user in db') return {'success': success, 'error': '', 'user': db_new_user} logger('User', 'new user not found in db') return { 'success': False, 'error': _tn.get(_.errorTryLateOrContant), 'user': None }
def debug_that_mail(request): """ :param request: :return: """ LOG.debug("Send a debug E-Mail") text = request.get('text', 'empty text input') LOG.debug("You got access: %s", text) send_mail(get_mailer(request), '[D-BAS] Debug Mail', 'Debug: {}'.format(text), request.validated['user'].email, 'en') return {'success': True}
def debug_that_mail(request): """ :param request: :return: """ logger('Websocket', 'debug_mail') text = request.get('text', 'empty text input') logger('Websocket', 'you got access: {}'.format(text)) send_mail(get_mailer(request), '[D-BAS] Debug Mail', 'Debug: {}'.format(text), request.validated['user'].email, 'en') return {'success': True}
def request_password(email: str, mailer: Mailer, _tn: Translator): """ Create new hashed password and send mail.. :param email: Mail-address which should be queried :param mailer: pyramid Mailer :param _tn: Translator :return: dict with info about mailing """ db_user = DBDiscussionSession.query(User).filter(func.lower(User.email) == func.lower(email)).first() if not db_user: LOG.debug("User could not be found for mail %s", email) return { 'success': False, 'message': _tn.get(_.emailSentGeneric) } rnd_pwd = get_rnd_passwd() hashed_pwd = get_hashed_password(rnd_pwd) db_user.password = hashed_pwd DBDiscussionSession.add(db_user) db_language = DBDiscussionSession.query(Language).get(db_user.settings.lang_uid) body = _tn.get(_.nicknameIs) + db_user.nickname + '\n' body += _tn.get(_.newPwdIs) + rnd_pwd + '\n\n' body += _tn.get(_.newPwdInfo) subject = _tn.get(_.dbasPwdRequest) success, _success_message, message = send_mail(mailer, subject, body, email, db_language.ui_locales) return { 'success': success, 'message': _tn.get(_.emailSentGeneric) }
def test_mail_for_reaction_on_argument(self): url = UrlManager(slug='cat-or-dog') url = url.get_url_for_reaction_on_argument(argument_uid=123, relation=Relations.REBUT, confrontation_argument=35) for language, for_html in list( itertools.product(['de', 'en'], [True, False])): subject = "Test Mail" body = get_text_for_message( nickname=DummyUser().firstname, lang=language, path=url, message_content=_.argumentAddedMessageContent, for_html=for_html) was_send, was_send_msg, msg = send_mail( mailer=DummyMailer, subject=subject, body=body, recipient=DummyUser().email, lang=language) self.assertTrue(was_send) self.assertEqual(msg.body, body)
def test_mail_for_justifying_statement(self): url = UrlManager(slug='cat-or-dog') url = url.get_url_for_justifying_statement(statement_uid=123, attitude=Attitudes.AGREE) for language, for_html in list( itertools.product(['de', 'en'], [True, False])): subject = "Test Mail" body = get_text_for_message( nickname=DummyUser().firstname, lang=language, path=url, message_content=_.statementAddedMessageContent, for_html=for_html) was_send, was_send_msg, msg = send_mail( mailer=DummyMailer, subject=subject, body=body, recipient=DummyUser().email, lang=language) self.assertTrue(was_send) self.assertEqual(msg.body, body)
def request_password(email: str, mailer: Mailer, _tn: Translator): """ Create new hashed password and send mail.. :param email: Mail-address which should be queried :param mailer: pyramid Mailer :param _tn: Translator :return: dict with info about mailing """ db_user = DBDiscussionSession.query(User).filter(func.lower(User.email) == func.lower(email)).first() if not db_user: logger('user_password_request', 'Mail unknown') return { 'success': False, 'message': _tn.get(_.emailUnknown) } rnd_pwd = get_rnd_passwd() hashed_pwd = get_hashed_password(rnd_pwd) # set the hashed one db_user.password = hashed_pwd DBDiscussionSession.add(db_user) transaction.commit() db_language = DBDiscussionSession.query(Language).get(db_user.settings.lang_uid) body = _tn.get(_.nicknameIs) + db_user.nickname + '\n' body += _tn.get(_.newPwdIs) + rnd_pwd + '\n\n' body += _tn.get(_.newPwdInfo) subject = _tn.get(_.dbasPwdRequest) success, message = send_mail(mailer, subject, body, email, db_language.ui_locales) return { 'success': success, 'message': message }
def set_new_user(mailer, user_data, password, _tn): """ :param mailer: :param user_data: dict with firstname, lastname, nickname, email, gender :param password: :param _tn: :return: """ # getting the authors group firstname = user_data.get('firstname') lastname = user_data.get('lastname') nickname = user_data.get('nickname') email = user_data.get('email') gender = user_data.get('gender') db_group = DBDiscussionSession.query(Group).filter_by(name='users').first() # does the group exists? if not db_group: LOG.debug("Internal error occured") return { 'success': False, 'error': _tn.get(_.errorTryLateOrContant), 'user': None } # sanity check db_user = DBDiscussionSession.query(User).filter_by( nickname=nickname).first() if db_user: LOG.debug("User already exists") return { 'success': False, 'error': _tn.get(_.nickIsTaken), 'user': None } user = { 'firstname': firstname, 'lastname': lastname, 'email': email, 'nickname': nickname, 'password': password, 'gender': gender, 'db_group_uid': db_group.uid } success, info, db_new_user = __create_new_user(user, _tn.get_lang()) if db_new_user: # sending an email and message subject = _tn.get(_.accountRegistration) body = _tn.get(_.accountWasRegistered).format(firstname, lastname, email) send_mail(mailer, subject, body, email, _tn.get_lang()) send_welcome_notification(db_new_user.uid, _tn) LOG.debug("Set new user in db") return {'success': success, 'error': '', 'user': db_new_user} LOG.debug("New user not found in db") return { 'success': False, 'error': _tn.get(_.errorTryLateOrContant), 'user': None }