def test_append_action_to_issue_rss(self): db_issue = get_enabled_issues_as_query().first() l1 = DBDiscussionSession.query(RSS).count() self.assertTrue( rss.append_action_to_issue_rss(db_issue, self.user, 'test_title', 'test_description', get_global_url())) l2 = DBDiscussionSession.query(RSS).count() self.assertEqual(l1 + 1, l2) DBDiscussionSession.query(RSS).filter( RSS.issue_uid == db_issue.uid, RSS.author_uid == self.user.uid, RSS.title == 'test_title', RSS.description == 'test_description').delete() DBDiscussionSession.flush() transaction.commit() l3 = DBDiscussionSession.query(RSS).count() self.assertEqual(l2 - 1, l3, "RSS appended more than once.") self.assertTrue(rss.rewrite_issue_rss(1, get_global_url()))
def send_mail(mailer, subject, body, recipient, lang): """ Try except block for sending an email :param mailer: current mailer :param subject: subject text of the mail :param body: body text of the mail :param recipient: recipient of the mail :param lang: current language :return: duple with boolean for sent message, message-string """ logger('email_helper', 'sending mail with subject \'' + subject + '\' to ' + recipient) _t = Translator(lang) if not mailer: logger('email_helper', 'mailer is none', error=True) return False, _t.get(_.internalKeyError) send_message = False body = body + '\n\n---\n' + _t.get(_.emailBodyText).format( get_global_url()) sender = os.environ.get("MAIL_DEFAULT__SENDER", None) message = Message(subject=subject, sender=sender, recipients=[recipient], body=body) # try sending an catching errors try: from threading import Thread t = Thread(target=__thread_to_send_mail, args=( mailer, message, recipient, body, )) t.start() send_message = True message = _t.get(_.emailWasSent) except smtplib.SMTPConnectError as exception: code = str(exception.smtp_code) error = str(exception.smtp_error) logger('email_helper', 'exception smtplib.SMTPConnectError smtp code / error ' + code + '/' + error, error=True) message = _t.get(_.emailWasNotSent) except socket_error as serr: logger('email_helper', 'socket error while sending ' + str(serr), error=True) message = _t.get(_.emailWasNotSent) return send_message, message
def __get_text_for_add_something(nickname, lang, url, keyword, for_html=True): nl = '<br>' if for_html else '\n' _t = Translator(lang) intro = _t.get(keyword).format(nickname) clickForMore = start_with_capital(_t.get(_.clickForMore)) if for_html: url = f'{get_global_url()}/discuss{url}' url = f'<a href="{url}">{clickForMore}</a>' else: url = get_global_url() + '/discuss' + url return f'{intro}{nl}{clickForMore}: {url}'
def test_get_text_for_add_text_message(self): for language, is_html, message in list( itertools.product(['en', 'de'], [True, False], [ _.statementAddedMessageContent, _.argumentAddedMessageContent ])): text = tg.get_text_for_message(self.name, language, self.url, message, is_html) _t = Translator(language) intro = _t.get(message).format(self.name) if is_html: intro = intro + '<br>' + _t.get( _.clickForMore) + ': <a href="{}/discuss{}">' + _t.get( _.clickForMore) + '</a>' intro = intro.format(get_global_url(), self.url) else: intro = intro + '\n' + _t.get(_.clickForMore) intro = intro + ': {}/discuss{}'.format( get_global_url(), self.url) self.assertEqual(text, intro) print(text)
def send_mail(mailer, subject, body, recipient, lang): """ Try except block for sending an email :param mailer: current mailer :param subject: subject text of the mail :param body: body text of the mail :param recipient: recipient of the mail :param lang: current language :return: duple with boolean for sent message, message-string """ LOG.debug("Sending mail with subject '%s' to %s", subject, recipient) _t = Translator(lang) if not mailer: LOG.debug("Mailer is none") return False, _t.get(_.internalKeyError) send_message = False body = body + '\n\n---\n' + _t.get(_.emailBodyText).format( get_global_url()) sender = os.environ.get("MAIL_DEFAULT__SENDER", None) message = Message(subject=subject, sender=sender, recipients=[recipient], body=body) # try sending an catching errors try: t = Thread(target=__thread_to_send_mail, args=( mailer, message, recipient, body, )) t.start() send_message = True status_message = _t.get(_.emailWasSent) except smtplib.SMTPConnectError as exception: code = str(exception.smtp_code) error = str(exception.smtp_error) LOG.debug( "Exception smtplib.SMTPConnectionError smtp code / error %s / %s", code, error) status_message = _t.get(_.emailWasNotSent) except socket_error as serr: LOG.debug("Socker error while sending %s", serr) status_message = _t.get(_.emailWasNotSent) return send_message, status_message, message
def __send_request_for_popup_to_socketio(nickname, popup_type, message='', url=None, increase_counter=False, use_https=False): """ Sends an request to the socket io server :param popup_type: String (success, warning, info) :param nickname: nickname of the user :param message: Some message :param url: URL for the event, what happened :param increase_counter: True, when the notification counter in D-BAS should be increased :param use_https: Boolean :return: Status code of the request """ LOG.debug("Send request to socketio server") if popup_type not in ['success', 'warning', 'info']: popup_type = 'info' params = '?type={}&nickname={}&'.format(popup_type, nickname) if message: params += 'msg={}&'.format(message) if url: params += 'url={}&'.format(url) if increase_counter: params += 'increase_counter=True&' params = params[:-1] port = __get_port() if use_https: link = '{}:{}/'.format(get_global_url(), port) else: link = 'http://localhost:{}/'.format(port) rurl = '{}publish{}'.format(link, params) return __open_url(rurl)
def get_text_for_message(nickname, lang, path, message_content, for_html=True) -> str: """ This method creates a message used for example in mails or messages. :param nickname: The nickname of the addressed user :param lang: The language to be used in the email :param path: The url for the user where he can find the changes :param message_content: The key variable which will be translated into a message :param for_html: A boolean to determine if the Message should contain a clickable link :return: A Message addressed to a user which can contain a clickable or non-clickable link """ _t = Translator(lang) intro = _t.get(message_content).format(nickname) clickForMore = start_with_capital(_t.get(_.clickForMore)) dbas_url = get_global_url() message_appendix_auto_generated = _t.get(_.emailBodyText).format(dbas_url) abs_path = f'{dbas_url}/discuss{path}' link = f'<a href="{abs_path}">{clickForMore}</a>' if for_html else abs_path msg = f'{intro}\n\n{link}\n\n---\n\n{message_appendix_auto_generated}' return msg.replace("\n", "<br>") if for_html else msg
def __send_request_for_recent_review_to_socketio(reviewer_name, reviewer_image_url, queue, use_https): """ Sends request to the socketio server for updating the last reviewer view :param reviewer_name: User.nickname :param reviewer_image_url: String :param queue: String :param use_https: Boolean :return: Status code of the request """ LOG.debug("Private method for updating last reviewer view") params = '?reviewer_name=' + reviewer_name + '&img_url=' + reviewer_image_url + '&queue=' + queue port = __get_port() if use_https: link = '{}:{}/'.format(get_global_url(), port) else: link = 'http://localhost:{}/'.format(port) rurl = '{}recent_review{}'.format(link, params) return __open_url(rurl)
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])): _t = Translator(language) subject = "Test Mail" body = get_text_for_message( nickname=DummyUser().firstname, lang=language, url=url, message_content=_.argumentAddedMessageContent, for_html=for_html) was_send, was_send_msg, msg = send_mail(mailer=DummyMailer, subject=subject, body=body, recipient=DummyUser(), lang=language) body = body + '\n\n---\n' + _t.get(_.emailBodyText).format( get_global_url()) self.assertTrue(was_send) self.assertEqual(msg.body, body)
def prepare_extras_dict(self, current_slug: str, is_reportable: bool, show_bar_icon: bool, show_graph_icon: bool, registry: Registry, application_url: str, path: str, db_user: User, broke_limit=False, add_premise_container_style='display: none', add_statement_container_style='display: none', ongoing_discussion=True): """ Creates the extras.dict() with many options! :param current_slug: :param is_reportable: Same as discussion.bubbles.last.is_markable, but TAL has no last indicator :param show_bar_icon: True, if the discussion space should show the graph icon :param show_graph_icon: True, if the discussion space should show the barometer icon :param registry: Pyramids registry :param application_url: current app url :param path: current path :param db_user: User :param broke_limit: Boolean :param add_premise_container_style: style string, default 'display:none;' :param add_statement_container_style: style string, default 'display:none;' :param ongoing_discussion: Boolean :return: dict() """ LOG.debug("Entering prepare_extras_dict") is_user_from_ldap = False is_logged_in = False nickname = None public_nickname = None is_user_male = False is_user_female = False is_admin = False is_special = False if db_user: is_user_from_ldap = db_user.validate_password(PW_FOR_LDAP_USER) is_logged_in = True nickname = db_user.nickname public_nickname = db_user.public_nickname is_user_male = db_user.gender == 'm' is_user_female = db_user.gender == 'f' is_admin = db_user.is_admin() is_special = db_user.is_special() if db_user.nickname == nick_of_anonymous_user: db_user = None is_logged_in = False return_dict = dict() return_dict['url'] = get_global_url() return_dict['year'] = datetime.datetime.now().year return_dict['restart_url'] = current_slug return_dict['is_in_discussion'] = 'discuss' in path return_dict['logged_in'] = is_logged_in return_dict['nickname'] = nickname return_dict['public_nickname'] = public_nickname return_dict[ 'add_premise_container_style'] = add_premise_container_style return_dict[ 'add_statement_container_style'] = add_statement_container_style return_dict['users_avatar'] = get_profile_picture(db_user, 25) return_dict['ongoing_discussion'] = ongoing_discussion return_dict['slug'] = current_slug return_dict['is_user_male'] = is_user_male return_dict['is_user_female'] = is_user_female return_dict['is_user_neutral'] = not return_dict[ 'is_user_male'] and not return_dict['is_user_female'] return_dict['broke_limit'] = 'true' if broke_limit else 'false' return_dict['use_with_ldap'] = is_user_from_ldap return_dict['development_mode'] = is_development_mode(registry) return_dict['is_development'] = registry.settings.get( 'mode', '') == 'development' return_dict['is_production'] = registry.settings.get( 'mode', '') == 'production' return_dict['review_count'] = get_complete_review_count(db_user) return_dict['modern_bubbles'] = usage_of_modern_bubbles(registry) return_dict['usage_of_matomo'] = usage_of_matomo(registry) self.add_language_options_for_extra_dict(return_dict) is_author, points = get_reputation_of(db_user) is_author_bool = is_author or points > limit_to_open_issues return_dict['is_reportable'] = is_reportable return_dict['is_admin'] = is_admin return_dict['is_special'] = is_special return_dict['is_author'] = is_author_bool return_dict['is_user'] = not (is_admin or is_author_bool or is_special) return_dict['show_bar_icon'] = show_bar_icon return_dict['show_graph_icon'] = show_graph_icon return_dict['close_premise_container'] = True return_dict['close_statement_container'] = True return_dict['date'] = arrow.utcnow().format('DD-MM-YYYY') return_dict['count_of'] = { 'arguments': DBDiscussionSession.query(Argument).count(), 'users': DBDiscussionSession.query(User).count(), 'discussions': DBDiscussionSession.query(Issue).count(), 'reviews': get_count_of_all(), } self.__add_title_text(return_dict, is_logged_in) self.__add_button_text(return_dict) self.__add_tag_text(return_dict) self.__add_login_button_properties(return_dict) message_dict = dict() message_dict['new_count'] = count_of_new_notifications( db_user) if db_user else 0 message_dict['has_unread'] = message_dict['new_count'] > 0 inbox = get_box_for(db_user, self.system_lang, application_url, True) if db_user else [] outbox = get_box_for(db_user, self.system_lang, application_url, False) if db_user else [] message_dict['inbox'] = inbox message_dict['outbox'] = outbox message_dict['total_in'] = len(inbox) message_dict['total_out'] = len(outbox) return_dict['notifications'] = message_dict return return_dict
def test_create_initial_issue_rss(self): self.assertTrue(rss.create_initial_issue_rss(get_global_url(), 'en')) return True
def test_create_news_rss(self): self.assertTrue(rss.create_news_rss(get_global_url(), 'en'))
def __write_rss_feeds(): issues = get_enabled_issues_as_query().all() for issue in issues: rewrite_issue_rss(issue.uid, get_global_url()) create_news_rss(get_global_url(), 'en')
def test_get_global_url(self): self.assertIn('https://dbas.cs.uni-duesseldorf.de', lib.get_global_url())