# HG changeset patch # User Mads Kiilerich # Date 1604936563 -3600 # Node ID f375751fe3fabc0a8607f86ec4d5d8447c11a8c0 # Parent 5d8bfda01cf598757f2b8d62b592061448381b33 lib: move extract_mentioned_usernames and MENTIONS_REGEX to webutils diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/lib/helpers.py --- a/kallithea/lib/helpers.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/lib/helpers.py Mon Nov 09 16:42:43 2020 +0100 @@ -38,17 +38,17 @@ from kallithea.lib.diffs import BIN_FILENODE, CHMOD_FILENODE, DEL_FILENODE, MOD_FILENODE, NEW_FILENODE, RENAMED_FILENODE from kallithea.lib.markup_renderer import url_re from kallithea.lib.pygmentsutils import get_custom_lexer -from kallithea.lib.utils2 import (MENTIONS_REGEX, AttributeDict, age, asbool, credentials_filter, fmt_date, link_to_ref, safe_bytes, safe_int, safe_str, - shorter, time_to_datetime) +from kallithea.lib.utils2 import (AttributeDict, age, asbool, credentials_filter, fmt_date, link_to_ref, safe_bytes, safe_int, safe_str, shorter, + time_to_datetime) from kallithea.lib.vcs.backends.base import BaseChangeset, EmptyChangeset from kallithea.lib.vcs.exceptions import ChangesetDoesNotExistError #============================================================================== # SCM FILTERS available via h. #============================================================================== from kallithea.lib.vcs.utils import author_email, author_name -from kallithea.lib.webutils import (HTML, Option, canonical_url, checkbox, chop_at, end_form, escape, form, format_byte_size, hidden, html_escape, js, jshtml, - link_to, literal, password, pop_flash_messages, radio, reset, safeid, select, session_csrf_secret_name, - session_csrf_secret_token, submit, text, textarea, truncate, url, wrap_paragraphs) +from kallithea.lib.webutils import (HTML, MENTIONS_REGEX, Option, canonical_url, checkbox, chop_at, end_form, escape, form, format_byte_size, hidden, + html_escape, js, jshtml, link_to, literal, password, pop_flash_messages, radio, reset, safeid, select, + session_csrf_secret_name, session_csrf_secret_token, submit, text, textarea, truncate, url, wrap_paragraphs) from kallithea.model import db from kallithea.model.changeset_status import ChangesetStatusModel diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/lib/markup_renderer.py --- a/kallithea/lib/markup_renderer.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/lib/markup_renderer.py Mon Nov 09 16:42:43 2020 +0100 @@ -36,7 +36,7 @@ from docutils.core import publish_parts from docutils.parsers.rst import directives -from kallithea.lib.utils2 import MENTIONS_REGEX +from kallithea.lib import webutils log = logging.getLogger(__name__) @@ -242,5 +242,5 @@ def wrapp(match_obj): uname = match_obj.groups()[0] return r'\ **@%(uname)s**\ ' % {'uname': uname} - mention_hl = MENTIONS_REGEX.sub(wrapp, source).strip() + mention_hl = webutils.MENTIONS_REGEX.sub(wrapp, source).strip() return cls.rst(mention_hl) diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/lib/utils.py --- a/kallithea/lib/utils.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/lib/utils.py Mon Nov 09 16:42:43 2020 +0100 @@ -36,8 +36,9 @@ import mercurial.ui import kallithea.lib.conf +from kallithea.lib import webutils from kallithea.lib.exceptions import InvalidCloneUriException -from kallithea.lib.utils2 import ascii_bytes, aslist, extract_mentioned_usernames, safe_bytes, safe_str +from kallithea.lib.utils2 import ascii_bytes, aslist, safe_bytes, safe_str from kallithea.lib.vcs.backends.git.repository import GitRepository from kallithea.lib.vcs.backends.hg.repository import MercurialRepository from kallithea.lib.vcs.conf import settings @@ -494,7 +495,7 @@ def extract_mentioned_users(text): """ Returns set of actual database Users @mentioned in given text. """ result = set() - for name in extract_mentioned_usernames(text): + for name in webutils.extract_mentioned_usernames(text): user = db.User.get_by_username(name, case_insensitive=True) if user is not None and not user.is_default_user: result.add(user) diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/lib/utils2.py Mon Nov 09 16:42:43 2020 +0100 @@ -422,22 +422,6 @@ return datetime.datetime.fromtimestamp(tm) -# Must match regexp in kallithea/public/js/base.js MentionsAutoComplete() -# Check char before @ - it must not look like we are in an email addresses. -# Matching is greedy so we don't have to look beyond the end. -MENTIONS_REGEX = re.compile(r'(?:^|(?<=[^a-zA-Z0-9]))@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])') - - -def extract_mentioned_usernames(text): - r""" - Returns list of (possible) usernames @mentioned in given text. - - >>> extract_mentioned_usernames('@1-2.a_X,@1234 not@not @ddd@not @n @ee @ff @gg, @gg;@hh @n\n@zz,') - ['1-2.a_X', '1234', 'ddd', 'ee', 'ff', 'gg', 'gg', 'hh', 'zz'] - """ - return MENTIONS_REGEX.findall(text) - - class AttributeDict(dict): def __getattr__(self, attr): return self.get(attr, None) diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/lib/webutils.py --- a/kallithea/lib/webutils.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/lib/webutils.py Mon Nov 09 16:42:43 2020 +0100 @@ -23,6 +23,7 @@ import json import logging import random +import re from tg import request, session from webhelpers2.html import HTML, escape, literal @@ -306,3 +307,19 @@ better to escape too much than too little. """ return js(escape(val)) + + +# Must match regexp in kallithea/public/js/base.js MentionsAutoComplete() +# Check char before @ - it must not look like we are in an email addresses. +# Matching is greedy so we don't have to look beyond the end. +MENTIONS_REGEX = re.compile(r'(?:^|(?<=[^a-zA-Z0-9]))@([a-zA-Z0-9][-_.a-zA-Z0-9]*[a-zA-Z0-9])') + + +def extract_mentioned_usernames(text): + r""" + Returns list of (possible) usernames @mentioned in given text. + + >>> extract_mentioned_usernames('@1-2.a_X,@1234 not@not @ddd@not @n @ee @ff @gg, @gg;@hh @n\n@zz,') + ['1-2.a_X', '1234', 'ddd', 'ee', 'ff', 'gg', 'gg', 'hh', 'zz'] + """ + return MENTIONS_REGEX.findall(text) diff -r 5d8bfda01cf5 -r f375751fe3fa kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py Mon Nov 09 16:48:34 2020 +0100 +++ b/kallithea/tests/other/test_libs.py Mon Nov 09 16:42:43 2020 +0100 @@ -111,7 +111,7 @@ assert asbool(str_bool) == expected def test_mention_extractor(self): - from kallithea.lib.utils2 import extract_mentioned_usernames + from kallithea.lib.webutils import extract_mentioned_usernames sample = ( "@first hi there @world here's my email username@example.com " "@lukaszb check @one_more22 it pls @ ttwelve @D[] @one@two@three "