##// END OF EJS Templates
chore(formatting): fixed babelrc format
chore(formatting): fixed babelrc format

File last commit:

r5152:f352498b default
r5238:57ce088c default
Show More
helpers.py
2199 lines | 71.1 KiB | text/x-python | PythonLexer
copyrights: updated for 2023
r5088 # Copyright (C) 2010-2023 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
"""
Helper functions
Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to both as 'h'.
"""
comment-history: fixes/ui changes...
r4408 import base64
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 import collections
project: added all source files and assets
r1
timezone: fix issues with timezone detection of current logic....
r2676 import os
project: added all source files and assets
r1 import random
import hashlib
helpers: python3 port
r5084 import io
audit-logs: improve help text, and expose all audit commands for easier filtering +ui fixes
r2807 import textwrap
helpers: python3 port
r5084 import urllib.request
import urllib.parse
import urllib.error
project: added all source files and assets
r1 import math
import logging
import re
import time
import string
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 import regex
helpers: added a helper to show example api call for automation of certain actions.
r1740 from collections import OrderedDict
dan
search: add syntax highlighting, line numbers and line context...
r48 import pygments
pr-versioning: implemented versioning for pull requests....
r1368 import itertools
jupyter-rendering: added rendering of notebook into MarkupRenderer class.
r1491 import fnmatch
project: added all source files and assets
r1
from datetime import datetime
from functools import partial
from pygments.formatters.html import HtmlFormatter
dan
search: add syntax highlighting, line numbers and line context...
r48 from pygments.lexers import (
get_lexer_by_name, get_lexer_for_filename, get_lexer_for_mimetype)
pyramid: stop relying on pylons translation object in helpers....
r1914
project: added all source files and assets
r1 from pyramid.threadlocal import get_current_request
diffs: added diff navigation to improve UX when browisng the full context diffs.
r4441 from tempita import looper
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 from webhelpers2.html import literal, HTML, escape
from webhelpers2.html._autolink import _auto_link_urls
from webhelpers2.html.tools import (
button_to, highlight, js_obfuscate, strip_links, strip_tags)
from webhelpers2.text import (
chop_at, collapse, convert_accented_entities,
convert_misc_entities, lchop, plural, rchop, remove_formatting,
replace_whitespace, urlify, truncate, wrap_paragraphs)
from webhelpers2.date import time_ago_in_words
from webhelpers2.html.tags import (
_input, NotGiven, _make_safe_id_component as safeid,
form as insecure_form,
auto_discovery_link, checkbox, end_form, file,
hidden, image, javascript_link, link_to, link_to_if, link_to_unless, ol,
helpers: python3 port
r5084 stylesheet_link, submit, text, password, textarea,
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 ul, radio, Options)
project: added all source files and assets
r1 from webhelpers2.number import format_byte_size
helpers: python3 port
r5084 # python3.11 backport fixes for webhelpers2
from rhodecode.lib._vendor.webhelpers_backports import raw_select
project: added all source files and assets
r1
from rhodecode.lib.action_parser import action_parser
html_sanitizer: abstracted bleach into own function/code for later replacement...
r5098 from rhodecode.lib.html_filters import sanitize_html
dan
webhelpers: replaced paginate library with custom lib
r4091 from rhodecode.lib.pagination import Page, RepoPage, SqlPage
json: fixed calls to json after orjson implementation
r4974 from rhodecode.lib import ext_json
templateContext: allow for flexible way to communicate additional info for client code from controllers
r394 from rhodecode.lib.ext_json import json
ui: fixed default gravatar b64 code
r5108 from rhodecode.lib.str_utils import safe_bytes, convert_special_chars, base64_to_str
project: added all source files and assets
r1 from rhodecode.lib.utils import repo_name_slug, get_custom_lexer
helpers: python3 port
r5084 from rhodecode.lib.str_utils import safe_str
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 from rhodecode.lib.utils2 import (
helpers: python3 port
r5084 str2bool,
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 get_commit_safe, datetime_to_time, time_to_datetime, time_to_utcdatetime,
AttributeDict, safe_int, md5, md5_safe, get_host_info)
markup-rendering: added relative image support....
r1527 from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links
project: added all source files and assets
r1 from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError
from rhodecode.lib.vcs.backends.base import BaseChangeset, EmptyCommit
dan
downloads: added more archive options that we support. Exposing this to users so they actually...
r4450 from rhodecode.lib.vcs.conf.settings import ARCHIVE_SPECS
dan
search: add support for elastic search 6...
r3319 from rhodecode.lib.index.search_utils import get_matching_line_offsets
project: added all source files and assets
r1 from rhodecode.config.conf import DATE_FORMAT, DATETIME_FORMAT
from rhodecode.model.changeset_status import ChangesetStatusModel
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
r4477 from rhodecode.model.db import Permission, User, Repository, UserApiKeys, FileStore
project: added all source files and assets
r1 from rhodecode.model.repo_group import RepoGroupModel
from rhodecode.model.settings import IssueTrackerSettingsModel
dan
search: add support for elastic search 6...
r3319
project: added all source files and assets
r1 log = logging.getLogger(__name__)
dan
assets: convert pylons static files to pyramid static_url, fixes #3504
r454
project: added all source files and assets
r1 DEFAULT_USER = User.DEFAULT_USER
DEFAULT_USER_EMAIL = User.DEFAULT_USER_EMAIL
dan
assets: convert pylons static files to pyramid static_url, fixes #3504
r454
helpers: add possibility to add GET flags into assets.
r1472 def asset(path, ver=None, **kwargs):
dan
assets: convert pylons static files to pyramid static_url, fixes #3504
r454 """
Helper to generate a static asset file path for rhodecode assets
eg. h.asset('images/image.png', ver='3923')
:param path: path of asset
:param ver: optional version query param to append as ?ver=
"""
request = get_current_request()
statics: don't generate empty ver= if not ver was given.
r542 query = {}
helpers: add possibility to add GET flags into assets.
r1472 query.update(kwargs)
statics: don't generate empty ver= if not ver was given.
r542 if ver:
query = {'ver': ver}
dan
static: use static_path instead of static_url to account for http vs https
r577 return request.static_path(
modernize: updates for python3
r5095 f'rhodecode:public/{path}', _query=query)
dan
assets: convert pylons static files to pyramid static_url, fixes #3504
r454
dan
annotations: replace annotated source code viewer with renderer...
r986 default_html_escape_table = {
helpers: python3 port
r5084 ord('&'): '&amp;',
ord('<'): '&lt;',
ord('>'): '&gt;',
ord('"'): '&quot;',
ord("'"): '&#39;',
dan
annotations: replace annotated source code viewer with renderer...
r986 }
def html_escape(text, html_escape_table=default_html_escape_table):
project: added all source files and assets
r1 """Produce entities within text."""
dan
annotations: replace annotated source code viewer with renderer...
r986 return text.translate(html_escape_table)
project: added all source files and assets
r1
json: fixed calls to json after orjson implementation
r4974 def str_json(*args, **kwargs):
return ext_json.str_json(*args, **kwargs)
def formatted_str_json(*args, **kwargs):
return ext_json.formatted_str_json(*args, **kwargs)
project: added all source files and assets
r1 def chop_at_smart(s, sub, inclusive=False, suffix_if_chopped=None):
"""
Truncate string ``s`` at the first occurrence of ``sub``.
If ``inclusive`` is true, truncate just after ``sub`` rather than at it.
"""
suffix_if_chopped = suffix_if_chopped or ''
pos = s.find(sub)
if pos == -1:
return s
if inclusive:
pos += len(sub)
chopped = s[:pos]
left = s[pos:].strip()
if left and suffix_if_chopped:
chopped += suffix_if_chopped
return chopped
artifacts: added changes for new grid to display artifacts.
r3987 def shorter(text, size=20, prefix=False):
project: added all source files and assets
r1 postfix = '...'
if len(text) > size:
artifacts: added changes for new grid to display artifacts.
r3987 if prefix:
# shorten in front
return postfix + text[-(size - len(postfix)):]
else:
return text[:size - len(postfix)] + postfix
project: added all source files and assets
r1 return text
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 def reset(name, value=None, id=NotGiven, type="reset", **attrs):
project: added all source files and assets
r1 """
Reset button
"""
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 return _input(type, name, value, id, attrs)
def select(name, selected_values, options, id=NotGiven, **attrs):
project: added all source files and assets
r1
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 if isinstance(options, (list, tuple)):
options_iter = options
# Handle old value,label lists ... where value also can be value,label lists
options = Options()
for opt in options_iter:
if isinstance(opt, tuple) and len(opt) == 2:
value, label = opt
python3: fix use of basetring
r4917 elif isinstance(opt, str):
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 value = label = opt
else:
raise ValueError('invalid select option type %r' % type(opt))
if isinstance(value, (list, tuple)):
option_group = options.add_optgroup(label)
for opt2 in value:
if isinstance(opt2, tuple) and len(opt2) == 2:
helpers: fixed options generation typo.
r4124 group_value, group_label = opt2
python3: fix use of basetring
r4917 elif isinstance(opt2, str):
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 group_value = group_label = opt2
else:
raise ValueError('invalid select option type %r' % type(opt2))
option_group.add_option(group_label, group_value)
else:
options.add_option(label, value)
return raw_select(name, selected_values, options, id=id, **attrs)
project: added all source files and assets
r1
def branding(name, length=40):
return truncate(name, length, indicator="")
def FID(raw_id, path):
"""
Creates a unique ID for filenode based on it's hash of path and commit
it's safe to use in urls
:param raw_id:
:param path:
"""
return 'c-%s-%s' % (short_id(raw_id), md5_safe(path)[:12])
class _GetError(object):
"""Get error from form_errors, and represent it as span wrapped error
message
:param field_name: field to fetch errors for
:param form_errors: form errors dict
"""
def __call__(self, field_name, form_errors):
tmpl = """<span class="error_msg">%s</span>"""
if form_errors and field_name in form_errors:
return literal(tmpl % form_errors.get(field_name))
files: updated based on new design
r3706
project: added all source files and assets
r1 get_error = _GetError()
class _ToolTip(object):
def __call__(self, tooltip_title, trim_at=50):
"""
Special function just to wrap our text into nice formatted
autowrapped text
:param tooltip_title:
"""
tooltip_title = escape(tooltip_title)
tooltip_title = tooltip_title.replace('<', '&lt;').replace('>', '&gt;')
return tooltip_title
commit-cache: update repo group/repo every 5 min
r3705
files: updated based on new design
r3706
project: added all source files and assets
r1 tooltip = _ToolTip()
helpers: python3 port
r5084 files_icon = '<i class="file-breadcrumb-copy tooltip icon-clipboard clipboard-action" data-clipboard-text="{}" title="Copy file path"></i>'
project: added all source files and assets
r1
search: fixed UI of search items after redesign.
r3745
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 def files_breadcrumbs(repo_name, repo_type, commit_id, file_path, landing_ref_name=None, at_ref=None,
limit_items=False, linkify_last_item=False, hide_last_item=False,
copy_path_icon=True):
files: updated based on new design
r3706
files: use ref names in the url, and make usage of default landing refs....
r4372 if at_ref:
route_qry = {'at': at_ref}
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 default_landing_ref = at_ref or landing_ref_name or commit_id
files: use ref names in the url, and make usage of default landing refs....
r4372 else:
route_qry = None
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 default_landing_ref = commit_id
project: added all source files and assets
r1
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 # first segment is a `HOME` link to repo files root location
helpers: python3 port
r5084 root_name = literal('<i class="icon-home"></i>')
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373
files: updated based on new design
r3706 url_segments = [
link_to(
root_name,
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 repo_files_by_ref_url(
repo_name,
repo_type,
f_path=None, # None here is a special case for SVN repos,
# that won't prefix with a ref
ref_name=default_landing_ref,
commit_id=commit_id,
query=route_qry
)
files: updated based on new design
r3706 )]
project: added all source files and assets
r1
files: updated based on new design
r3706 path_segments = file_path.split('/')
project: added all source files and assets
r1 last_cnt = len(path_segments) - 1
for cnt, segment in enumerate(path_segments):
if not segment:
continue
segment_html = escape(segment)
search: fixed UI of search items after redesign.
r3745 last_item = cnt == last_cnt
files: use ref names in the url, and make usage of default landing refs....
r4372 if last_item and hide_last_item:
# iterate over and hide last element
continue
search: fixed UI of search items after redesign.
r3745 if last_item and linkify_last_item is False:
# plain version
url_segments.append(segment_html)
else:
project: added all source files and assets
r1 url_segments.append(
link_to(
segment_html,
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 repo_files_by_ref_url(
repo_name,
repo_type,
ui: new file tree switcher...
r3655 f_path='/'.join(path_segments[:cnt + 1]),
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 ref_name=default_landing_ref,
commit_id=commit_id,
query=route_qry
),
search: fixed UI of search items after redesign.
r3745 ))
project: added all source files and assets
r1
files: updated based on new design
r3706 limited_url_segments = url_segments[:1] + ['...'] + url_segments[-5:]
if limit_items and len(limited_url_segments) < len(url_segments):
url_segments = limited_url_segments
full_path = file_path
files: use ref names in the url, and make usage of default landing refs....
r4372 if copy_path_icon:
icon = files_icon.format(escape(full_path))
else:
icon = ''
files: updated based on new design
r3706 if file_path == '':
return root_name
else:
return literal(' / '.join(url_segments) + icon)
project: added all source files and assets
r1
files: fixed case of partial url in generating files metadata
r3716 def files_url_data(request):
matchdict = request.matchdict
if 'f_path' not in matchdict:
matchdict['f_path'] = ''
files: escape special chars for metadata loading to prevent breaking url with chars like #, <....
r4749 else:
python3: fix urllib usage
r4914 matchdict['f_path'] = urllib.parse.quote(safe_str(matchdict['f_path']))
files: fixed case of partial url in generating files metadata
r3716 if 'commit_id' not in matchdict:
matchdict['commit_id'] = 'tip'
json: fixed calls to json after orjson implementation
r4974 return ext_json.str_json(matchdict)
files: fixed case of partial url in generating files metadata
r3716
files: use a common function to handle url-by-refs, and fix landing refs for SVN....
r4373 def repo_files_by_ref_url(db_repo_name, db_repo_type, f_path, ref_name, commit_id, query=None, ):
_is_svn = is_svn(db_repo_type)
final_f_path = f_path
if _is_svn:
"""
For SVN the ref_name cannot be used as a commit_id, it needs to be prefixed with
actually commit_id followed by the ref_name. This should be done only in case
This is a initial landing url, without additional paths.
like: /1000/tags/1.0.0/?at=tags/1.0.0
"""
if ref_name and ref_name != 'tip':
# NOTE(marcink): for svn the ref_name is actually the stored path, so we prefix it
# for SVN we only do this magic prefix if it's root, .eg landing revision
# of files link. If we are in the tree we don't need this since we traverse the url
# that has everything stored
if f_path in ['', '/']:
final_f_path = '/'.join([ref_name, f_path])
# SVN always needs a commit_id explicitly, without a named REF
default_commit_id = commit_id
else:
"""
For git and mercurial we construct a new URL using the names instead of commit_id
like: /master/some_path?at=master
"""
# We currently do not support branches with slashes
if '/' in ref_name:
default_commit_id = commit_id
else:
default_commit_id = ref_name
# sometimes we pass f_path as None, to indicate explicit no prefix,
# we translate it to string to not have None
final_f_path = final_f_path or ''
files_url = route_path(
'repo_files',
repo_name=db_repo_name,
commit_id=default_commit_id,
f_path=final_f_path,
_query=query
)
return files_url
dan
search: add support for elastic search 6...
r3319 def code_highlight(code, lexer, formatter, use_hl_filter=False):
"""
Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
If ``outfile`` is given and a valid file object (an object
with a ``write`` method), the result will be written to it, otherwise
it is returned as a string.
"""
if use_hl_filter:
# add HL filter
from rhodecode.lib.index import search_utils
lexer.add_filter(search_utils.ElasticSearchHLFilter())
return pygments.format(pygments.lex(code, lexer), formatter)
project: added all source files and assets
r1 class CodeHtmlFormatter(HtmlFormatter):
"""
My code Html Formatter for source codes
"""
helpers: python3 port
r5084 def wrap(self, source):
project: added all source files and assets
r1 return self._wrap_div(self._wrap_pre(self._wrap_code(source)))
def _wrap_code(self, source):
for cnt, it in enumerate(source):
i, t = it
helpers: python3 port
r5084 t = f'<div id="L{cnt+1}">{t}</div>'
project: added all source files and assets
r1 yield i, t
def _wrap_tablelinenos(self, inner):
helpers: python3 port
r5084 dummyoutfile = io.StringIO()
project: added all source files and assets
r1 lncount = 0
for t, line in inner:
if t:
lncount += 1
dummyoutfile.write(line)
fl = self.linenostart
mw = len(str(lncount + fl - 1))
sp = self.linenospecial
st = self.linenostep
la = self.lineanchors
aln = self.anchorlinenos
nocls = self.noclasses
if sp:
lines = []
for i in range(fl, fl + lncount):
if i % st == 0:
if i % sp == 0:
if aln:
lines.append('<a href="#%s%d" class="special">%*d</a>' %
(la, i, mw, i))
else:
lines.append('<span class="special">%*d</span>' % (mw, i))
else:
if aln:
lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
else:
lines.append('%*d' % (mw, i))
else:
lines.append('')
ls = '\n'.join(lines)
else:
lines = []
for i in range(fl, fl + lncount):
if i % st == 0:
if aln:
lines.append('<a href="#%s%d">%*d</a>' % (la, i, mw, i))
else:
lines.append('%*d' % (mw, i))
else:
lines.append('')
ls = '\n'.join(lines)
# in case you wonder about the seemingly redundant <div> here: since the
# content in the other cell also is wrapped in a div, some browsers in
# some configurations seem to mess up the formatting...
if nocls:
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td><div class="linenodiv" '
'style="background-color: #f0f0f0; padding-right: 10px">'
'<pre style="line-height: 125%">' +
ls + '</pre></div></td><td id="hlcode" class="code">')
else:
yield 0, ('<table class="%stable">' % self.cssclass +
'<tr><td class="linenos"><div class="linenodiv"><pre>' +
ls + '</pre></div></td><td id="hlcode" class="code">')
yield 0, dummyoutfile.getvalue()
yield 0, '</td></tr></table>'
dan
search: add syntax highlighting, line numbers and line context...
r48 class SearchContentCodeHtmlFormatter(CodeHtmlFormatter):
def __init__(self, **kw):
# only show these line numbers if set
self.only_lines = kw.pop('only_line_numbers', [])
self.query_terms = kw.pop('query_terms', [])
self.max_lines = kw.pop('max_lines', 5)
self.line_context = kw.pop('line_context', 3)
self.url = kw.pop('url', None)
super(CodeHtmlFormatter, self).__init__(**kw)
def _wrap_code(self, source):
for cnt, it in enumerate(source):
i, t = it
t = '<pre>%s</pre>' % t
yield i, t
def _wrap_tablelinenos(self, inner):
yield 0, '<table class="code-highlight %stable">' % self.cssclass
last_shown_line_number = 0
current_line_number = 1
for t, line in inner:
if not t:
yield t, line
continue
if current_line_number in self.only_lines:
if last_shown_line_number + 1 != current_line_number:
yield 0, '<tr>'
yield 0, '<td class="line">...</td>'
yield 0, '<td id="hlcode" class="code"></td>'
yield 0, '</tr>'
yield 0, '<tr>'
if self.url:
yield 0, '<td class="line"><a href="%s#L%i">%i</a></td>' % (
self.url, current_line_number, current_line_number)
else:
yield 0, '<td class="line"><a href="">%i</a></td>' % (
current_line_number)
yield 0, '<td id="hlcode" class="code">' + line + '</td>'
yield 0, '</tr>'
last_shown_line_number = current_line_number
current_line_number += 1
yield 0, '</table>'
dan
annotations: replace annotated source code viewer with renderer...
r986 def hsv_to_rgb(h, s, v):
""" Convert hsv color values to rgb """
if s == 0.0:
return v, v, v
i = int(h * 6.0) # XXX assume int() truncates!
f = (h * 6.0) - i
p = v * (1.0 - s)
q = v * (1.0 - s * f)
t = v * (1.0 - s * (1.0 - f))
i = i % 6
if i == 0:
return v, t, p
if i == 1:
return q, v, p
if i == 2:
return p, v, t
if i == 3:
return p, q, v
if i == 4:
return t, p, v
if i == 5:
return v, p, q
def unique_color_generator(n=10000, saturation=0.10, lightness=0.95):
"""
Generator for getting n of evenly distributed colors using
hsv color and golden ratio. It always return same order of colors
:param n: number of colors to generate
:param saturation: saturation of returned colors
:param lightness: lightness of returned colors
:returns: RGB tuple
"""
golden_ratio = 0.618033988749895
h = 0.22717784590367374
python3: removed use of xrang
r4906 for _ in range(n):
dan
annotations: replace annotated source code viewer with renderer...
r986 h += golden_ratio
h %= 1
HSV_tuple = [h, saturation, lightness]
RGB_tuple = hsv_to_rgb(*HSV_tuple)
helpers: python3 port
r5084 yield [str(int(x * 256)) for x in RGB_tuple]
dan
annotations: replace annotated source code viewer with renderer...
r986
def color_hasher(n=10000, saturation=0.10, lightness=0.95):
"""
Returns a function which when called with an argument returns a unique
color for that argument, eg.
:param n: number of colors to generate
:param saturation: saturation of returned colors
:param lightness: lightness of returned colors
:returns: css RGB string
>>> color_hash = color_hasher()
>>> color_hash('hello')
'rgb(34, 12, 59)'
>>> color_hash('hello')
'rgb(34, 12, 59)'
>>> color_hash('other')
'rgb(90, 224, 159)'
"""
color_dict = {}
cgenerator = unique_color_generator(
saturation=saturation, lightness=lightness)
def get_color_string(thing):
if thing in color_dict:
col = color_dict[thing]
else:
python3: fixed usage of .next() and .func_name
r4936 col = color_dict[thing] = next(cgenerator)
dan
annotations: replace annotated source code viewer with renderer...
r986 return "rgb(%s)" % (', '.join(col))
return get_color_string
dan
search: add syntax highlighting, line numbers and line context...
r48 def get_lexer_safe(mimetype=None, filepath=None):
"""
Tries to return a relevant pygments lexer using mimetype/filepath name,
defaulting to plain text if none could be found
"""
lexer = None
try:
if mimetype:
lexer = get_lexer_for_mimetype(mimetype)
if not lexer:
helpers: fixed some errors on helpers, and removed dead code.
r254 lexer = get_lexer_for_filename(filepath)
dan
search: add syntax highlighting, line numbers and line context...
r48 except pygments.util.ClassNotFound:
pass
if not lexer:
lexer = get_lexer_by_name('text')
return lexer
lexers: added small extensions table to extend syntaxt hl for file sources....
r796 def get_lexer_for_filenode(filenode):
lexer = get_custom_lexer(filenode.extension) or filenode.lexer
return lexer
project: added all source files and assets
r1 def pygmentize(filenode, **kwargs):
"""
pygmentize function using pygments
:param filenode:
"""
lexers: added small extensions table to extend syntaxt hl for file sources....
r796 lexer = get_lexer_for_filenode(filenode)
project: added all source files and assets
r1 return literal(code_highlight(filenode.content, lexer,
CodeHtmlFormatter(**kwargs)))
def is_following_repo(repo_name, user_id):
from rhodecode.model.scm import ScmModel
return ScmModel().is_following_repo(repo_name, user_id)
class _Message(object):
"""A message returned by ``Flash.pop_messages()``.
Converting the message to a string returns the message text. Instances
also have the following attributes:
* ``message``: the message text.
* ``category``: the category specified when the message was created.
"""
flash: fixed display problem with flash messages on error pages.
r4219 def __init__(self, category, message, sub_data=None):
project: added all source files and assets
r1 self.category = category
self.message = message
flash: fixed display problem with flash messages on error pages.
r4219 self.sub_data = sub_data or {}
project: added all source files and assets
r1
def __str__(self):
return self.message
__unicode__ = __str__
def __html__(self):
helpers: python3 port
r5084 return escape(safe_str(self.message))
project: added all source files and assets
r1
helpers: remove usage of pylons session.
r2095 class Flash(object):
# List of allowed categories. If None, allow any category.
categories = ["warning", "notice", "error", "success"]
# Default category if none is specified.
default_category = "notice"
def __init__(self, session_key="flash", categories=None,
default_category=None):
"""
Instantiate a ``Flash`` object.
``session_key`` is the key to save the messages under in the user's
session.
project: added all source files and assets
r1
helpers: remove usage of pylons session.
r2095 ``categories`` is an optional list which overrides the default list
of categories.
``default_category`` overrides the default category used for messages
when none is specified.
"""
self.session_key = session_key
if categories is not None:
self.categories = categories
if default_category is not None:
self.default_category = default_category
if self.categories and self.default_category not in self.categories:
raise ValueError(
"unrecognized default category %r" % (self.default_category,))
def pop_messages(self, session=None, request=None):
"""
Return all accumulated messages and delete them from the session.
project: added all source files and assets
r1
The return value is a list of ``Message`` objects.
"""
pyramid: make flash messages not rely strictly on pylons session....
r1905 messages = []
project: added all source files and assets
r1
helpers: remove usage of pylons session.
r2095 if not session:
if not request:
request = get_current_request()
pyramid: make flash messages not rely strictly on pylons session....
r1905 session = request.session
project: added all source files and assets
r1
# Pop the 'old' pylons flash messages. They are tuples of the form
# (category, message)
for cat, msg in session.pop(self.session_key, []):
messages.append(_Message(cat, msg))
# Pop the 'new' pyramid flash messages for each category as list
# of strings.
for cat in self.categories:
for msg in session.pop_flash(queue=cat):
flash: fixed display problem with flash messages on error pages.
r4219 sub_data = {}
if hasattr(msg, 'rsplit'):
flash_data = msg.rsplit('|DELIM|', 1)
org_message = flash_data[0]
if len(flash_data) > 1:
sub_data = json.loads(flash_data[1])
else:
org_message = msg
messages.append(_Message(cat, org_message, sub_data=sub_data))
project: added all source files and assets
r1 # Map messages from the default queue to the 'notice' category.
for msg in session.pop_flash():
messages.append(_Message('notice', msg))
session.save()
return messages
helpers: remove usage of pylons session.
r2095 def json_alerts(self, session=None, request=None):
notifications: move all notifications into polymer for consistency fix #4201
r822 payloads = []
flash: fixed display problem with flash messages on error pages.
r4219 messages = flash.pop_messages(session=session, request=request) or []
for message in messages:
payloads.append({
'message': {
helpers: python3 port
r5084 'message': '{}'.format(message.message),
flash: fixed display problem with flash messages on error pages.
r4219 'level': message.category,
'force': True,
'subdata': message.sub_data
}
})
json: fixed calls to json after orjson implementation
r4974 return safe_str(json.dumps(payloads))
notifications: move all notifications into polymer for consistency fix #4201
r822
session: ignore duplicates for flash messages by default, this gives no benefits, explicitly request duplicates present instead
r3940 def __call__(self, message, category=None, ignore_duplicate=True,
helpers: remove usage of pylons session.
r2095 session=None, request=None):
if not session:
if not request:
request = get_current_request()
session = request.session
session.flash(
message, queue=category, allow_duplicate=not ignore_duplicate)
project: added all source files and assets
r1 flash = Flash()
#==============================================================================
# SCM FILTERS available via h.
#==============================================================================
from rhodecode.lib.vcs.utils import author_name, author_email
py3: don't use lambda assignments.
r4397 from rhodecode.lib.utils2 import age, age_from_seconds
project: added all source files and assets
r1 from rhodecode.model.db import User, ChangesetStatus
py3: don't use lambda assignments.
r4397
project: added all source files and assets
r1 email = author_email
py3: don't use lambda assignments.
r4397
def capitalize(raw_text):
return raw_text.capitalize()
def short_id(long_id):
return long_id[:12]
def hide_credentials(url):
from rhodecode.lib.utils2 import credentials_filter
return credentials_filter(url)
project: added all source files and assets
r1
helpers: python3 port
r5084 import zoneinfo
timezone: fix issues with timezone detection of current logic....
r2676 import tzlocal
local_timezone = tzlocal.get_localzone()
pull-requests: overhaul of the UX by adding new sidebar...
r4482 def get_timezone(datetime_iso, time_is_local=False):
helpers: fix tzinfo can be undefined if wrong object is passed.
r1352 tzinfo = '+00:00'
project: added all source files and assets
r1
dan
datetimes: fix datetimes to work across app, converting to utc
r155 # detect if we have a timezone info, otherwise, add it
timezone: fix issues with timezone detection of current logic....
r2676 if time_is_local and isinstance(datetime_iso, datetime) and not datetime_iso.tzinfo:
force_timezone = os.environ.get('RC_TIMEZONE', '')
if force_timezone:
helpers: python3 port
r5084 force_timezone = zoneinfo.ZoneInfo(force_timezone)
timezone: fix issues with timezone detection of current logic....
r2676 timezone = force_timezone or local_timezone
helpers: python3 port
r5084
offset = datetime_iso.replace(tzinfo=timezone).strftime('%z')
timezone: fix issues with timezone detection of current logic....
r2676 tzinfo = '{}:{}'.format(offset[:-2], offset[-2:])
pull-requests: overhaul of the UX by adding new sidebar...
r4482 return tzinfo
def age_component(datetime_iso, value=None, time_is_local=False, tooltip=True):
title = value or format_date(datetime_iso)
tzinfo = get_timezone(datetime_iso, time_is_local=time_is_local)
dan
datetimes: fix datetimes to work across app, converting to utc
r155
project: added all source files and assets
r1 return literal(
pull-requests: simplified the UI for pr view....
r4136 '<time class="timeago {cls}" title="{tt_title}" datetime="{dt}{tzinfo}">{title}</time>'.format(
cls='tooltip' if tooltip else '',
tt_title=('{title}{tzinfo}'.format(title=title, tzinfo=tzinfo)) if tooltip else '',
title=title, dt=datetime_iso, tzinfo=tzinfo
))
project: added all source files and assets
r1
dan
visual: fixed show revision/commit lenght settings
r3404 def _shorten_commit_id(commit_id, commit_len=None):
if commit_len is None:
request = get_current_request()
commit_len = request.call_context.visual.show_sha_length
return commit_id[:commit_len]
project: added all source files and assets
r1
dan
visual: fixed show revision/commit lenght settings
r3404 def show_id(commit, show_idx=None, commit_len=None):
project: added all source files and assets
r1 """
Configurable function that shows ID
by default it's r123:fffeeefffeee
:param commit: commit instance
"""
dan
visual: fixed show revision/commit lenght settings
r3404 if show_idx is None:
request = get_current_request()
show_idx = request.call_context.visual.show_revision_number
project: added all source files and assets
r1
dan
visual: fixed show revision/commit lenght settings
r3404 raw_id = _shorten_commit_id(commit.raw_id, commit_len=commit_len)
project: added all source files and assets
r1 if show_idx:
return 'r%s:%s' % (commit.idx, raw_id)
else:
return '%s' % (raw_id, )
def format_date(date):
"""
use a standardized formatting for dates used in RhodeCode
:param date: date/datetime object
:return: formatted date
"""
if date:
_fmt = "%a, %d %b %Y %H:%M:%S"
helpers: python3 port
r5084 return safe_str(date.strftime(_fmt))
return ""
project: added all source files and assets
r1
class _RepoChecker(object):
def __init__(self, backend_alias):
self._backend_alias = backend_alias
def __call__(self, repository):
if hasattr(repository, 'alias'):
_type = repository.alias
elif hasattr(repository, 'repo_type'):
_type = repository.repo_type
else:
_type = repository
return _type == self._backend_alias
search: show all results for super-admin, and get repo type from index it if exists.
r3376
project: added all source files and assets
r1 is_git = _RepoChecker('git')
is_hg = _RepoChecker('hg')
is_svn = _RepoChecker('svn')
def get_repo_type_by_name(repo_name):
repo = Repository.get_by_repo_name(repo_name)
search: show all results for super-admin, and get repo type from index it if exists.
r3376 if repo:
return repo.repo_type
project: added all source files and assets
r1
def is_svn_without_proxy(repository):
if is_svn(repository):
vcs: moved svn proxy settings into vcs related settings...
r754 from rhodecode.model.settings import VcsSettingsModel
conf = VcsSettingsModel().get_ui_settings_as_config_obj()
return not str2bool(conf.get('vcs_svn_proxy', 'http_requests_enabled'))
project: added all source files and assets
r1 return False
helpers: optimize slight calls for link_to_user to save some...
r253 def discover_user(author):
"""
pull-requests: added update pull-requests email+notifications...
r4120 Tries to discover RhodeCode User based on the author string. Author string
helpers: optimize slight calls for link_to_user to save some...
r253 is typically `FirstName LastName <email@address.com>`
"""
# if author is already an instance use it for extraction
if isinstance(author, User):
return author
# Valid email in the attribute passed, see if they're in the system
_email = author_email(author)
if _email != '':
user = User.get_by_email(_email, case_insensitive=True, cache=True)
if user is not None:
return user
# Maybe it's a username, we try to extract it and fetch by username ?
_author = author_name(author)
user = User.get_by_username(_author, case_insensitive=True, cache=True)
if user is not None:
return user
return None
project: added all source files and assets
r1 def email_or_none(author):
# extract email from the commit string
_email = author_email(author)
ux: adjust email_or_none so that it does not swap emails
r409
# If we have an email, use it, otherwise
# see if it contains a username we can get an email from
project: added all source files and assets
r1 if _email != '':
return _email
ux: adjust email_or_none so that it does not swap emails
r409 else:
helpers: remove bad warning calls introduced in commit c8bec9b70fb9
r416 user = User.get_by_username(
author_name(author), case_insensitive=True, cache=True)
dan
assets: convert pylons static files to pyramid static_url, fixes #3504
r454
project: added all source files and assets
r1 if user is not None:
ux: adjust email_or_none so that it does not swap emails
r409 return user.email
project: added all source files and assets
r1
# No valid email, not a valid user in the system, none!
return None
def link_to_user(author, length=0, **kwargs):
user = discover_user(author)
helpers: optimize slight calls for link_to_user to save some...
r253 # user can be None, but if we have it already it means we can re-use it
# in the person() function, so we save 1 intensive-query
if user:
author = user
project: added all source files and assets
r1 display_person = person(author, 'username_or_name_or_email')
if length:
display_person = shorter(display_person, length)
dan
permissions: fixed missing user info on global and repository permissions pages.
r4418 if user and user.username != user.DEFAULT_USER:
project: added all source files and assets
r1 return link_to(
escape(display_person),
user-profile: migrated to pyramid views.
r1502 route_path('user_profile', username=user.username),
project: added all source files and assets
r1 **kwargs)
else:
return escape(display_person)
Bartłomiej Wołyńczyk
Public user group profile Task #5326
r2638 def link_to_group(users_group_name, **kwargs):
return link_to(
escape(users_group_name),
route_path('user_group_profile', user_group_name=users_group_name),
**kwargs)
project: added all source files and assets
r1 def person(author, show_attr="username_and_name"):
user = discover_user(author)
if user:
helpers: optimize slight calls for link_to_user to save some...
r253 return getattr(user, show_attr)
project: added all source files and assets
r1 else:
_author = author_name(author)
_email = email(author)
return _author or _email
ux: #4037 use gravatar for commit email and add tooltips for name + email
r410 def author_string(email):
if email:
user = User.get_by_email(email, case_insensitive=True, cache=True)
if user:
security: use new safe escaped user attributes across the application....
r1815 if user.first_name or user.last_name:
security: fixed XSS inside the tooltip for author string.
r1779 return '%s %s &lt;%s&gt;' % (
security: use new safe escaped user attributes across the application....
r1815 user.first_name, user.last_name, email)
ux: #4037 use gravatar for commit email and add tooltips for name + email
r410 else:
return email
else:
return email
else:
return None
project: added all source files and assets
r1 def person_by_id(id_, show_attr="username_and_name"):
# attr to return from fetched user
helpers: python3 port
r5084 def person_getter(usr):
return getattr(usr, show_attr)
project: added all source files and assets
r1
#maybe it's an ID ?
if str(id_).isdigit() or isinstance(id_, int):
id_ = int(id_)
user = User.get(id_)
if user is not None:
return person_getter(user)
return id_
hovercacrds: added new tooltips and hovercards to expose certain information for objects shown in UI
r4026 def gravatar_with_user(request, author, show_disabled=False, tooltip=False):
_render = request.get_partial_renderer('rhodecode:templates/base/base.mako')
return _render('gravatar_with_user', author, show_disabled=show_disabled, tooltip=tooltip)
project: added all source files and assets
r1
code: fixes to escape characters improperly used
r5149 tags_patterns = OrderedDict(
(
(
"lang",
(
re.compile(r"\[(lang|language)\ \=\&gt;\ *([a-zA-Z\-\/\#\+\.]*)\]"),
'<div class="metatag" tag="lang">\\2</div>',
),
),
(
"see",
(
re.compile(r"\[see\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]"),
'<div class="metatag" tag="see">see: \\1 </div>',
),
),
(
"url",
(
re.compile(
r"\[url\ \=\&gt;\ \[([a-zA-Z0-9\ \.\-\_]+)\]\((http://|https://|/)(.*?)\)\]"
),
'<div class="metatag" tag="url"> <a href="\\2\\3">\\1</a> </div>',
),
),
(
"license",
(
re.compile(
r"\[license\ \=\&gt;\ *([a-zA-Z0-9\/\=\?\&amp;\ \:\/\.\-]*)\]"
),
helpers: fix tests and tag replacers
r5152 # don't make it a raw string here...
'<div class="metatag" tag="license"><a href="http:\/\/www.opensource.org/licenses/\\1">\\1</a></div>',
code: fixes to escape characters improperly used
r5149 ),
),
(
"ref",
(
re.compile(
r"\[(requires|recommends|conflicts|base)\ \=\&gt;\ *([a-zA-Z0-9\-\/]*)\]"
),
'<div class="metatag" tag="ref \\1">\\1: <a href="/\\2">\\2</a></div>',
),
),
(
"state",
(
re.compile(r"\[(stable|featured|stale|dead|dev|deprecated)\]"),
'<div class="metatag" tag="state \\1">\\1</div>',
),
),
# label in grey
(
"label",
(re.compile(r"\[([a-z]+)\]"), '<div class="metatag" tag="label">\\1</div>'),
),
# generic catch all in grey
(
"generic",
(
re.compile(r"\[([a-zA-Z0-9\.\-\_]+)\]"),
'<div class="metatag" tag="generic">\\1</div>',
),
),
)
)
meta-tags: cleanup support for metatags....
r2091
def extract_metatags(value):
project: added all source files and assets
r1 """
meta-tags: cleanup support for metatags....
r2091 Extract supported meta-tags from given text value
project: added all source files and assets
r1 """
helpers: fix metatags extraction in case description is empty
r2226 tags = []
project: added all source files and assets
r1 if not value:
helpers: fix metatags extraction in case description is empty
r2226 return tags, ''
project: added all source files and assets
r1
code: fixes to escape characters improperly used
r5149 for key, val in list(tags_patterns.items()):
meta-tags: cleanup support for metatags....
r2091 pat, replace_html = val
tags.extend([(key, x.group()) for x in pat.finditer(value)])
value = pat.sub('', value)
project: added all source files and assets
r1
meta-tags: cleanup support for metatags....
r2091 return tags, value
project: added all source files and assets
r1
meta-tags: cleanup support for metatags....
r2091 def style_metatag(tag_type, value):
project: added all source files and assets
r1 """
meta-tags: cleanup support for metatags....
r2091 converts tags from value into html equivalent
project: added all source files and assets
r1 """
if not value:
return ''
meta-tags: cleanup support for metatags....
r2091 html_value = value
code: fixes to escape characters improperly used
r5149 tag_data = tags_patterns.get(tag_type)
meta-tags: cleanup support for metatags....
r2091 if tag_data:
pat, replace_html = tag_data
helpers: python3 port
r5084 # convert to plain `str` instead of a markup tag to be used in
# regex expressions. safe_str doesn't work here
FOLD: into unicode changes
r4959 html_value = pat.sub(replace_html, value)
project: added all source files and assets
r1
meta-tags: cleanup support for metatags....
r2091 return html_value
project: added all source files and assets
r1
authentication: show icons only for activated plugins.
r3291 def bool2icon(value, show_at_false=True):
project: added all source files and assets
r1 """
Returns boolean value of a given value, represented as html element with
classes that will represent icons
:param value: given value to convert to html node
"""
if value: # does bool conversion
helpers: bool2icon will expose title of values as tooltip.
r4001 return HTML.tag('i', class_="icon-true", title='True')
project: added all source files and assets
r1 else: # not true as bool
authentication: show icons only for activated plugins.
r3291 if show_at_false:
helpers: bool2icon will expose title of values as tooltip.
r4001 return HTML.tag('i', class_="icon-false", title='False')
authentication: show icons only for activated plugins.
r3291 return HTML.tag('i')
project: added all source files and assets
r1
reviewers: added observers as another way to define reviewers....
r4500
def b64(inp):
helpers: python3 port
r5084 return base64.b64encode(safe_bytes(inp))
reviewers: added observers as another way to define reviewers....
r4500
project: added all source files and assets
r1 #==============================================================================
# PERMS
#==============================================================================
pull-requests: added update pull-requests email+notifications...
r4120 from rhodecode.lib.auth import (
HasPermissionAny, HasPermissionAll,
HasRepoPermissionAny, HasRepoPermissionAll, HasRepoGroupPermissionAll,
HasRepoGroupPermissionAny, HasRepoPermissionAnyApi, get_csrf_token,
csrf_token_key, AuthUser)
project: added all source files and assets
r1
#==============================================================================
# GRAVATAR URL
#==============================================================================
class InitialsGravatar(object):
def __init__(self, email_address, first_name, last_name, size=30,
background=None, text_color='#fff'):
self.size = size
self.first_name = first_name
self.last_name = last_name
self.email_address = email_address
self.background = background or self.str2color(email_address)
self.text_color = text_color
def get_color_bank(self):
"""
returns a predefined list of colors that gravatars can use.
Those are randomized distinct colors that guarantee readability and
uniqueness.
generated with: http://phrogz.net/css/distinct-colors.html
"""
return [
'#bf3030', '#a67f53', '#00ff00', '#5989b3', '#392040', '#d90000',
'#402910', '#204020', '#79baf2', '#a700b3', '#bf6060', '#7f5320',
'#008000', '#003059', '#ee00ff', '#ff0000', '#8c4b00', '#007300',
'#005fb3', '#de73e6', '#ff4040', '#ffaa00', '#3df255', '#203140',
'#47004d', '#591616', '#664400', '#59b365', '#0d2133', '#83008c',
'#592d2d', '#bf9f60', '#73e682', '#1d3f73', '#73006b', '#402020',
'#b2862d', '#397341', '#597db3', '#e600d6', '#a60000', '#736039',
'#00b318', '#79aaf2', '#330d30', '#ff8080', '#403010', '#16591f',
'#002459', '#8c4688', '#e50000', '#ffbf40', '#00732e', '#102340',
'#bf60ac', '#8c4646', '#cc8800', '#00a642', '#1d3473', '#b32d98',
'#660e00', '#ffd580', '#80ffb2', '#7391e6', '#733967', '#d97b6c',
'#8c5e00', '#59b389', '#3967e6', '#590047', '#73281d', '#665200',
'#00e67a', '#2d50b3', '#8c2377', '#734139', '#b2982d', '#16593a',
'#001859', '#ff00aa', '#a65e53', '#ffcc00', '#0d3321', '#2d3959',
'#731d56', '#401610', '#4c3d00', '#468c6c', '#002ca6', '#d936a3',
'#d94c36', '#403920', '#36d9a3', '#0d1733', '#592d4a', '#993626',
'#cca300', '#00734d', '#46598c', '#8c005e', '#7f1100', '#8c7000',
'#00a66f', '#7382e6', '#b32d74', '#d9896c', '#ffe680', '#1d7362',
'#364cd9', '#73003d', '#d93a00', '#998a4d', '#59b3a1', '#5965b3',
'#e5007a', '#73341d', '#665f00', '#00b38f', '#0018b3', '#59163a',
'#b2502d', '#bfb960', '#00ffcc', '#23318c', '#a6537f', '#734939',
'#b2a700', '#104036', '#3d3df2', '#402031', '#e56739', '#736f39',
'#79f2ea', '#000059', '#401029', '#4c1400', '#ffee00', '#005953',
'#101040', '#990052', '#402820', '#403d10', '#00ffee', '#0000d9',
'#ff80c4', '#a66953', '#eeff00', '#00ccbe', '#8080ff', '#e673a1',
'#a62c00', '#474d00', '#1a3331', '#46468c', '#733950', '#662900',
'#858c23', '#238c85', '#0f0073', '#b20047', '#d9986c', '#becc00',
'#396f73', '#281d73', '#ff0066', '#ff6600', '#dee673', '#59adb3',
'#6559b3', '#590024', '#b2622d', '#98b32d', '#36ced9', '#332d59',
'#40001a', '#733f1d', '#526600', '#005359', '#242040', '#bf6079',
'#735039', '#cef23d', '#007780', '#5630bf', '#66001b', '#b24700',
'#acbf60', '#1d6273', '#25008c', '#731d34', '#a67453', '#50592d',
'#00ccff', '#6600ff', '#ff0044', '#4c1f00', '#8a994d', '#79daf2',
'#a173e6', '#d93662', '#402310', '#aaff00', '#2d98b3', '#8c40ff',
'#592d39', '#ff8c40', '#354020', '#103640', '#1a0040', '#331a20',
'#331400', '#334d00', '#1d5673', '#583973', '#7f0022', '#4c3626',
'#88cc00', '#36a3d9', '#3d0073', '#d9364c', '#33241a', '#698c23',
'#5995b3', '#300059', '#e57382', '#7f3300', '#366600', '#00aaff',
'#3a1659', '#733941', '#663600', '#74b32d', '#003c59', '#7f53a6',
'#73000f', '#ff8800', '#baf279', '#79caf2', '#291040', '#a6293a',
'#b2742d', '#587339', '#0077b3', '#632699', '#400009', '#d9a66c',
'#294010', '#2d4a59', '#aa00ff', '#4c131b', '#b25f00', '#5ce600',
'#267399', '#a336d9', '#990014', '#664e33', '#86bf60', '#0088ff',
'#7700b3', '#593a16', '#073300', '#1d4b73', '#ac60bf', '#e59539',
'#4f8c46', '#368dd9', '#5c0073'
]
def rgb_to_hex_color(self, rgb_tuple):
"""
Converts an rgb_tuple passed to an hex color.
:param rgb_tuple: tuple with 3 ints represents rgb color space
"""
return '#' + ("".join(map(chr, rgb_tuple)).encode('hex'))
def email_to_int_list(self, email_str):
"""
Get every byte of the hex digest value of email and turn it to integer.
It's going to be always between 0-255
"""
digest = md5_safe(email_str.lower())
return [int(digest[i * 2:i * 2 + 2], 16) for i in range(16)]
def pick_color_bank_index(self, email_str, color_bank):
return self.email_to_int_list(email_str)[0] % len(color_bank)
def str2color(self, email_str):
"""
Tries to map in a stable algorithm an email to color
:param email_str:
"""
color_bank = self.get_color_bank()
# pick position (module it's length so we always find it in the
# bank even if it's smaller than 256 values
pos = self.pick_color_bank_index(email_str, color_bank)
return color_bank[pos]
def normalize_email(self, email_address):
# default host used to fill in the fake/missing email
FOLD: into unicode changes
r4959 default_host = 'localhost'
project: added all source files and assets
r1
if not email_address:
helpers: python3 port
r5084 email_address = f'{User.DEFAULT_USER}@{default_host}'
email_address = safe_str(email_address)
if '@' not in email_address:
email_address = f'{email_address}@{default_host}'
if email_address.endswith('@'):
email_address = f'{email_address}{default_host}'
email_address = convert_special_chars(email_address)
project: added all source files and assets
r1 return email_address
def get_initials(self):
"""
Returns 2 letter initials calculated based on the input.
The algorithm picks first given email address, and takes first letter
of part before @, and then the first letter of server name. In case
the part before @ is in a format of `somestring.somestring2` it replaces
the server letter with first letter of somestring2
In case function was initialized with both first and lastname, this
overrides the extraction from email by first letter of the first and
last name. We add special logic to that functionality, In case Full name
is compound, like Guido Von Rossum, we use last part of the last name
(Von Rossum) picking `R`.
Function also normalizes the non-ascii characters to they ascii
representation, eg Ä„ => A
"""
# replace non-ascii to ascii
helpers: python3 port
r5084 first_name = convert_special_chars(self.first_name)
last_name = convert_special_chars(self.last_name)
# multi word last names, Guido Von Rossum, we take the last part only
last_name = last_name.split(' ', 1)[-1]
project: added all source files and assets
r1
# do NFKD encoding, and also make sure email has proper format
email_address = self.normalize_email(self.email_address)
# first push the email initials
prefix, server = email_address.split('@', 1)
security: use new safe escaped user attributes across the application....
r1815 # check if prefix is maybe a 'first_name.last_name' syntax
project: added all source files and assets
r1 _dot_split = prefix.rsplit('.', 1)
dan
initial-gravatars: fix case of dot beeing present before @domain.
r2062 if len(_dot_split) == 2 and _dot_split[1]:
project: added all source files and assets
r1 initials = [_dot_split[0][0], _dot_split[1][0]]
else:
initials = [prefix[0], server[0]]
helpers: python3 port
r5084 # get first letter of first and last names to create initials
project: added all source files and assets
r1 fn_letter = (first_name or " ")[0].strip()
helpers: python3 port
r5084 ln_letter = (last_name or " ")[0].strip()
project: added all source files and assets
r1
if fn_letter:
initials[0] = fn_letter
if ln_letter:
initials[1] = ln_letter
return ''.join(initials).upper()
def get_img_data_by_type(self, font_family, img_type):
default_user = """
<svg xmlns="http://www.w3.org/2000/svg"
version="1.1" x="0px" y="0px" width="{size}" height="{size}"
viewBox="-15 -10 439.165 429.164"
xml:space="preserve"
ui: fixed default gravatar b64 code
r5108 font-family="{font_family}"
project: added all source files and assets
r1 style="background:{background};" >
<path d="M204.583,216.671c50.664,0,91.74-48.075,
91.74-107.378c0-82.237-41.074-107.377-91.74-107.377
c-50.668,0-91.74,25.14-91.74,107.377C112.844,
168.596,153.916,216.671,
204.583,216.671z" fill="{text_color}"/>
<path d="M407.164,374.717L360.88,
270.454c-2.117-4.771-5.836-8.728-10.465-11.138l-71.83-37.392
c-1.584-0.823-3.502-0.663-4.926,0.415c-20.316,
15.366-44.203,23.488-69.076,23.488c-24.877,
0-48.762-8.122-69.078-23.488
c-1.428-1.078-3.346-1.238-4.93-0.415L58.75,
259.316c-4.631,2.41-8.346,6.365-10.465,11.138L2.001,374.717
c-3.191,7.188-2.537,15.412,1.75,22.005c4.285,
6.592,11.537,10.526,19.4,10.526h362.861c7.863,0,15.117-3.936,
19.402-10.527 C409.699,390.129,
410.355,381.902,407.164,374.717z" fill="{text_color}"/>
</svg>""".format(
size=self.size,
background='#979797', # @grey4
text_color=self.text_color,
font_family=font_family)
return {
"default_user": default_user
}[img_type]
def get_img_data(self, svg_type=None):
"""
generates the svg metadata for image
"""
ui: dropped the custom font in favor of builtin ones....
r3164 fonts = [
'-apple-system',
'BlinkMacSystemFont',
'Segoe UI',
'Roboto',
'Oxygen-Sans',
'Ubuntu',
'Cantarell',
'Helvetica Neue',
'sans-serif'
]
font_family = ','.join(fonts)
project: added all source files and assets
r1 if svg_type:
return self.get_img_data_by_type(font_family, svg_type)
initials = self.get_initials()
img_data = """
<svg xmlns="http://www.w3.org/2000/svg" pointer-events="none"
width="{size}" height="{size}"
style="width: 100%; height: 100%; background-color: {background}"
viewBox="0 0 {size} {size}">
<text text-anchor="middle" y="50%" x="50%" dy="0.35em"
pointer-events="auto" fill="{text_color}"
font-family="{font_family}"
style="font-weight: 400; font-size: {f_size}px;">{text}
</text>
</svg>""".format(
size=self.size,
gravatars: reduce the size of fonts inside the initials gravatar
r3654 f_size=self.size/2.05, # scale the text inside the box nicely
project: added all source files and assets
r1 background=self.background,
text_color=self.text_color,
text=initials.upper(),
font_family=font_family)
return img_data
def generate_svg(self, svg_type=None):
ui: fixed default gravatar b64 code
r5108 img_data = base64_to_str(self.get_img_data(svg_type))
return "data:image/svg+xml;base64,{}".format(img_data)
project: added all source files and assets
r1
helpers: cleanup gravatars code.
r4478 def initials_gravatar(request, email_address, first_name, last_name, size=30, store_on_disk=False):
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
r4477
project: added all source files and assets
r1 svg_type = None
if email_address == User.DEFAULT_USER_EMAIL:
svg_type = 'default_user'
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
r4477
project: added all source files and assets
r1 klass = InitialsGravatar(email_address, first_name, last_name, size)
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
r4477
if store_on_disk:
from rhodecode.apps.file_store import utils as store_utils
from rhodecode.apps.file_store.exceptions import FileNotAllowedException, \
FileOverSizeException
from rhodecode.model.db import Session
image_key = md5_safe(email_address.lower()
+ first_name.lower() + last_name.lower())
storage = store_utils.get_file_storage(request.registry.settings)
filename = '{}.svg'.format(image_key)
subdir = 'gravatars'
# since final name has a counter, we apply the 0
uid = storage.apply_counter(0, store_utils.uid_filename(filename, randomized=False))
store_uid = os.path.join(subdir, uid)
db_entry = FileStore.get_by_store_uid(store_uid)
if db_entry:
return request.route_path('download_file', fid=store_uid)
img_data = klass.get_img_data(svg_type=svg_type)
img_file = store_utils.bytes_to_file_obj(img_data)
try:
store_uid, metadata = storage.save_file(
img_file, filename, directory=subdir,
extensions=['.svg'], randomized_name=False)
except (FileNotAllowedException, FileOverSizeException):
raise
try:
entry = FileStore.create(
file_uid=store_uid, filename=metadata["filename"],
file_hash=metadata["sha256"], file_size=metadata["size"],
file_display_name=filename,
helpers: python3 port
r5084 file_description=f'user gravatar `{safe_str(filename)}`',
helpers: allow save-on-disk svg gravatars instead of the on-the-fly generated ones
r4477 hidden=True, check_acl=False, user_id=1
)
Session().add(entry)
Session().commit()
log.debug('Stored upload in DB as %s', entry)
except Exception:
raise
return request.route_path('download_file', fid=store_uid)
else:
return klass.generate_svg(svg_type=svg_type)
project: added all source files and assets
r1
helpers: cleanup gravatars code.
r4478 def gravatar_external(request, gravatar_url_tmpl, email_address, size=30):
return safe_str(gravatar_url_tmpl)\
.replace('{email}', email_address) \
.replace('{md5email}', md5_safe(email_address.lower())) \
.replace('{netloc}', request.host) \
.replace('{scheme}', request.scheme) \
.replace('{size}', safe_str(size))
dan
reviewers: added validation and default review rules via API calls....
r1795 def gravatar_url(email_address, size=30, request=None):
helpers: cleanup gravatars code.
r4478 request = request or get_current_request()
pylons: remove pylons as dependency...
r2351 _use_gravatar = request.call_context.visual.use_gravatar
project: added all source files and assets
r1
email_address = email_address or User.DEFAULT_USER_EMAIL
python3: removed more unicode usage
r4952 if isinstance(email_address, str):
project: added all source files and assets
r1 # hashlib crashes on unicode items
email_address = safe_str(email_address)
# empty email or default user
if not email_address or email_address == User.DEFAULT_USER_EMAIL:
helpers: cleanup gravatars code.
r4478 return initials_gravatar(request, User.DEFAULT_USER_EMAIL, '', '', size=size)
project: added all source files and assets
r1
if _use_gravatar:
helpers: cleanup gravatars code.
r4478 gravatar_url_tmpl = request.call_context.visual.gravatar_url \
or User.DEFAULT_GRAVATAR_URL
return gravatar_external(request, gravatar_url_tmpl, email_address, size=size)
project: added all source files and assets
r1 else:
helpers: cleanup gravatars code.
r4478 return initials_gravatar(request, email_address, '', '', size=size)
project: added all source files and assets
r1
def breadcrumb_repo_link(repo):
"""
Makes a breadcrumbs path link to repo
ex::
group >> subgroup >> repo
:param repo: a Repository instance
"""
path = [
feat: changed last change of repo group to be commit of a latest repository.
r3689 link_to(group.name, route_path('repo_group_home', repo_group_name=group.group_name),
title='last change:{}'.format(format_date(group.last_commit_change)))
project: added all source files and assets
r1 for group in repo.groups_with_parents
] + [
feat: changed last change of repo group to be commit of a latest repository.
r3689 link_to(repo.just_name, route_path('repo_summary', repo_name=repo.repo_name),
title='last change:{}'.format(format_date(repo.last_commit_change)))
project: added all source files and assets
r1 ]
return literal(' &raquo; '.join(path))
dan
search: new UI for search, and repo group context search...
r3442 def breadcrumb_repo_group_link(repo_group):
"""
Makes a breadcrumbs path link to repo
ex::
group >> subgroup
:param repo_group: a Repository Group instance
"""
path = [
link_to(group.name,
feat: changed last change of repo group to be commit of a latest repository.
r3689 route_path('repo_group_home', repo_group_name=group.group_name),
title='last change:{}'.format(format_date(group.last_commit_change)))
dan
search: new UI for search, and repo group context search...
r3442 for group in repo_group.parents
] + [
link_to(repo_group.name,
feat: changed last change of repo group to be commit of a latest repository.
r3689 route_path('repo_group_home', repo_group_name=repo_group.group_name),
title='last change:{}'.format(format_date(repo_group.last_commit_change)))
dan
search: new UI for search, and repo group context search...
r3442 ]
return literal(' &raquo; '.join(path))
project: added all source files and assets
r1 def format_byte_size_binary(file_size):
"""
Formats file/folder sizes to standard.
"""
files: ported repository files controllers to pyramid views.
r1927 if file_size is None:
file_size = 0
project: added all source files and assets
r1 formatted_size = format_byte_size(file_size, binary=True)
return formatted_size
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 def urlify_text(text_, safe=True, **href_attrs):
project: added all source files and assets
r1 """
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 Extract urls from text and make html links out of them
project: added all source files and assets
r1 """
url_pat = re.compile(r'''(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@#.&+]'''
code: fixes to escape characters improperly used
r5149 r'''|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)''')
project: added all source files and assets
r1
def url_func(match_obj):
url_full = match_obj.groups()[0]
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 a_options = dict(href_attrs)
a_options['href'] = url_full
a_text = url_full
return HTML.tag("a", a_text, **a_options)
dan
hovercards: enable hovercards on parsed commits references.
r4047 _new_text = url_pat.sub(url_func, text_)
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090
project: added all source files and assets
r1 if safe:
dan
hovercards: enable hovercards on parsed commits references.
r4047 return literal(_new_text)
return _new_text
project: added all source files and assets
r1
dan
hovercards: enable hovercards on parsed commits references.
r4047 def urlify_commits(text_, repo_name):
project: added all source files and assets
r1 """
Extract commit ids from text and make link from them
:param text_:
dan
hovercards: enable hovercards on parsed commits references.
r4047 :param repo_name: repo name to build the URL with
project: added all source files and assets
r1 """
repo-commits: ported changeset code into pyramid views....
r1951
dan
hovercards: enable hovercards on parsed commits references.
r4047 url_pat = re.compile(r'(^|\s)([0-9a-fA-F]{12,40})($|\s)')
project: added all source files and assets
r1
def url_func(match_obj):
commit_id = match_obj.groups()[1]
pref = match_obj.groups()[0]
suf = match_obj.groups()[2]
tmpl = (
dan
hovercards: enable hovercards on parsed commits references.
r4047 '%(pref)s<a class="tooltip-hovercard %(cls)s" href="%(url)s" data-hovercard-alt="%(hovercard_alt)s" data-hovercard-url="%(hovercard_url)s">'
project: added all source files and assets
r1 '%(commit_id)s</a>%(suf)s'
)
return tmpl % {
'pref': pref,
'cls': 'revision-link',
dan
hovercards: enable hovercards on parsed commits references.
r4047 'url': route_url(
'repo_commit', repo_name=repo_name, commit_id=commit_id),
project: added all source files and assets
r1 'commit_id': commit_id,
dan
hovercards: enable hovercards on parsed commits references.
r4047 'suf': suf,
'hovercard_alt': 'Commit: {}'.format(commit_id),
'hovercard_url': route_url(
'hovercard_repo_commit', repo_name=repo_name, commit_id=commit_id)
project: added all source files and assets
r1 }
dan
hovercards: enable hovercards on parsed commits references.
r4047 new_text = url_pat.sub(url_func, text_)
project: added all source files and assets
r1
dan
hovercards: enable hovercards on parsed commits references.
r4047 return new_text
project: added all source files and assets
r1
dan
integrations: add integration support...
r411 def _process_url_func(match_obj, repo_name, uid, entry,
comments: allow parsing the issue tracker patterns inside the markup...
r1672 return_raw_data=False, link_format='html'):
project: added all source files and assets
r1 pref = ''
if match_obj.group().startswith(' '):
pref = ' '
issue_id = ''.join(match_obj.groups())
comments: allow parsing the issue tracker patterns inside the markup...
r1672
if link_format == 'html':
tmpl = (
hovercacrds: added new tooltips and hovercards to expose certain information for objects shown in UI
r4026 '%(pref)s<a class="tooltip %(cls)s" href="%(url)s" title="%(title)s">'
comments: allow parsing the issue tracker patterns inside the markup...
r1672 '%(issue-prefix)s%(id-repr)s'
'</a>')
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 elif link_format == 'html+hovercard':
tmpl = (
'%(pref)s<a class="tooltip-hovercard %(cls)s" href="%(url)s" data-hovercard-url="%(hovercard_url)s">'
'%(issue-prefix)s%(id-repr)s'
'</a>')
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 elif link_format in ['rst', 'rst+hovercard']:
comments: allow parsing the issue tracker patterns inside the markup...
r1672 tmpl = '`%(issue-prefix)s%(id-repr)s <%(url)s>`_'
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 elif link_format in ['markdown', 'markdown+hovercard']:
tmpl = '[%(pref)s%(issue-prefix)s%(id-repr)s](%(url)s)'
comments: allow parsing the issue tracker patterns inside the markup...
r1672 else:
raise ValueError('Bad link_format:{}'.format(link_format))
project: added all source files and assets
r1
(repo_name_cleaned,
security: fix possible XSS in the issue tracker URL.
r3439 parent_group_name) = RepoGroupModel()._get_group_name_and_parent(repo_name)
project: added all source files and assets
r1
# variables replacement
named_vars = {
'id': issue_id,
'repo': repo_name,
'repo_name': repo_name_cleaned,
hovercacrds: added new tooltips and hovercards to expose certain information for objects shown in UI
r4026 'group_name': parent_group_name,
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 # set dummy keys so we always have them
'hostname': '',
'netloc': '',
'scheme': ''
project: added all source files and assets
r1 }
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046
request = get_current_request()
if request:
# exposes, hostname, netloc, scheme
host_data = get_host_info(request)
named_vars.update(host_data)
project: added all source files and assets
r1 # named regex variables
named_vars.update(match_obj.groupdict())
_url = string.Template(entry['url']).safe_substitute(**named_vars)
issue-trackers: fix XSS with description field.
r4487 desc = string.Template(escape(entry['desc'])).safe_substitute(**named_vars)
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 hovercard_url = string.Template(entry.get('hovercard_url', '')).safe_substitute(**named_vars)
project: added all source files and assets
r1
security: fix possible XSS in the issue tracker URL.
r3439 def quote_cleaner(input_str):
"""Remove quotes as it's HTML"""
return input_str.replace('"', '')
dan
integrations: add integration support...
r411 data = {
project: added all source files and assets
r1 'pref': pref,
security: fix possible XSS in the issue tracker URL.
r3439 'cls': quote_cleaner('issue-tracker-link'),
'url': quote_cleaner(_url),
project: added all source files and assets
r1 'id-repr': issue_id,
'issue-prefix': entry['pref'],
'serv': entry['url'],
html_sanitizer: abstracted bleach into own function/code for later replacement...
r5098 'title': sanitize_html(desc, strip=True),
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 'hovercard_url': hovercard_url
project: added all source files and assets
r1 }
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046
dan
integrations: add integration support...
r411 if return_raw_data:
return {
'id': issue_id,
'url': _url
}
return tmpl % data
project: added all source files and assets
r1
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445 def get_active_pattern_entries(repo_name):
project: added all source files and assets
r1 repo = None
if repo_name:
# Retrieving repo_name to avoid invalid repo_name to explode on
# IssueTrackerSettingsModel but still passing invalid name further down
helpers: commit parsers, use caching queries to save tons of time when parsing over the same repo...
r258 repo = Repository.get_by_repo_name(repo_name, cache=True)
project: added all source files and assets
r1
settings_model = IssueTrackerSettingsModel(repo=repo)
helpers: commit parsers, use caching queries to save tons of time when parsing over the same repo...
r258 active_entries = settings_model.get_settings(cache=True)
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445 return active_entries
project: added all source files and assets
r1
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 pr_pattern_re = regex.compile(r'(?:(?:^!)|(?: !))(\d+)')
helpers: use pre-comiled patterns for PRs for small speedup.
r4203
pull-requests: overhaul of the UX by adding new sidebar...
r4482 allowed_link_formats = [
'html', 'rst', 'markdown', 'html+hovercard', 'rst+hovercard', 'markdown+hovercard']
helpers: use a small compilation cache to not re-compile same patterns over and over.
r4791 compile_cache = {
}
helpers: use pre-comiled patterns for PRs for small speedup.
r4203
security: fix possible XSS in the issue tracker URL.
r3439 def process_patterns(text_string, repo_name, link_format='html', active_entries=None):
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445
pull-requests: overhaul of the UX by adding new sidebar...
r4482 if link_format not in allowed_link_formats:
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445 raise ValueError('Link format can be only one of:{} got {}'.format(
pull-requests: overhaul of the UX by adding new sidebar...
r4482 allowed_link_formats, link_format))
helpers: use a small compilation cache to not re-compile same patterns over and over.
r4791 issues_data = []
errors = []
new_text = text_string
issue-trackers: cache the fetched issue tracker patterns in changelog page before loop iteration
r2445
issue-tracker: cache active patterns in the template so we can re-use them when rendering comments....
r4202 if active_entries is None:
pull-requests: overhaul of the UX by adding new sidebar...
r4482 log.debug('Fetch active issue tracker patterns for repo: %s', repo_name)
issue-tracker: cache active patterns in the template so we can re-use them when rendering comments....
r4202 active_entries = get_active_pattern_entries(repo_name)
users: enable full edit mode for super admins....
r4740 log.debug('Got %s pattern entries to process', len(active_entries))
helpers: use a small compilation cache to not re-compile same patterns over and over.
r4791
helpers: python3 port
r5084 for uid, entry in list(active_entries.items()):
project: added all source files and assets
r1
if not (entry['pat'] and entry['url']):
log.debug('skipping due to missing data')
continue
logging: use lazy parameter evaluation in log calls.
r3061 log.debug('issue tracker entry: uid: `%s` PAT:%s URL:%s PREFIX:%s',
uid, entry['pat'], entry['url'], entry['pref'])
project: added all source files and assets
r1
helpers: pre-compiled issue tracker patterns early for speedup.
r4204 if entry.get('pat_compiled'):
pattern = entry['pat_compiled']
helpers: use a small compilation cache to not re-compile same patterns over and over.
r4791 elif entry['pat'] in compile_cache:
pattern = compile_cache[entry['pat']]
helpers: pre-compiled issue tracker patterns early for speedup.
r4204 else:
try:
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 pattern = regex.compile(r'%s' % entry['pat'])
except regex.error as e:
regex_err = ValueError('{}:{}'.format(entry['pat'], e))
log.exception('issue tracker pattern: `%s` failed to compile', regex_err)
errors.append(regex_err)
helpers: pre-compiled issue tracker patterns early for speedup.
r4204 continue
helpers: use a small compilation cache to not re-compile same patterns over and over.
r4791 compile_cache[entry['pat']] = pattern
project: added all source files and assets
r1
dan
integrations: add integration support...
r411 data_func = partial(
_process_url_func, repo_name=repo_name, entry=entry, uid=uid,
return_raw_data=True)
for match_obj in pattern.finditer(text_string):
issues_data.append(data_func(match_obj))
url_func = partial(
comments: allow parsing the issue tracker patterns inside the markup...
r1672 _process_url_func, repo_name=repo_name, entry=entry, uid=uid,
link_format=link_format)
dan
integrations: add integration support...
r411
hovercards: added commit hovercard for files, and dashboard views.
r4032 new_text = pattern.sub(url_func, new_text)
logging: use lazy parameter evaluation in log calls.
r3061 log.debug('processed prefix:uid `%s`', uid)
project: added all source files and assets
r1
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 # finally use global replace, eg !123 -> pr-link, those will not catch
# if already similar pattern exists
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 server_url = '${scheme}://${netloc}'
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 pr_entry = {
'pref': '!',
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 'url': server_url + '/_admin/pull-requests/${id}',
'desc': 'Pull Request !${id}',
'hovercard_url': server_url + '/_hovercard/pull_request/${id}'
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 }
pr_url_func = partial(
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 _process_url_func, repo_name=repo_name, entry=pr_entry, uid=None,
link_format=link_format+'+hovercard')
helpers: use pre-comiled patterns for PRs for small speedup.
r4203 new_text = pr_pattern_re.sub(pr_url_func, new_text)
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 log.debug('processed !pr pattern')
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 return new_text, issues_data, errors
project: added all source files and assets
r1
pull-requests: overhaul of the UX by adding new sidebar...
r4482 def urlify_commit_message(commit_text, repository=None, active_pattern_entries=None,
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 issues_container_callback=None, error_container=None):
project: added all source files and assets
r1 """
Parses given text message and makes proper links.
issues are linked to given issue-server, and rest is a commit link
"""
issue-tracker: cache active patterns in the template so we can re-use them when rendering comments....
r4202
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 def escaper(_text):
return _text.replace('<', '&lt;').replace('>', '&gt;')
project: added all source files and assets
r1
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 new_text = escaper(commit_text)
dan
integrations: add integration support...
r411
# extract http/https links and make them real urls
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 new_text = urlify_text(new_text, safe=False)
dan
integrations: add integration support...
r411
project: added all source files and assets
r1 # urlify commits - extract commit ids and make link out of them, if we have
# the scope of repository present.
if repository:
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 new_text = urlify_commits(new_text, repository)
project: added all source files and assets
r1
# process issue tracker patterns
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 new_text, issues, errors = process_patterns(
new_text, repository or '', active_entries=active_pattern_entries)
project: added all source files and assets
r1
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 if issues_container_callback is not None:
for issue in issues:
issues_container_callback(issue)
pull-requests: overhaul of the UX by adding new sidebar...
r4482
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 if error_container is not None:
error_container.extend(errors)
dan
patterns: enabled global !NUM match for new style pull-request references.
r4041 return literal(new_text)
project: added all source files and assets
r1
files: render images inside the file view....
r1580 def render_binary(repo_name, file_obj):
"""
Choose how to render a binary file
"""
file-renderer: escape alt text to prevent XSS on binary files with bad filenames.
r2993
files: fixed unicode problems on image preview, and make images center, no-stretch.
r4344 # unicode
files: render images inside the file view....
r1580 filename = file_obj.name
# images
files: fixed unicode problems on image preview, and make images center, no-stretch.
r4344 for ext in ['*.png', '*.jpeg', '*.jpg', '*.ico', '*.gif']:
files: render images inside the file view....
r1580 if fnmatch.fnmatch(filename, pat=ext):
files: ported repository files controllers to pyramid views.
r1927 src = route_path(
'repo_file_raw', repo_name=repo_name,
file-renderer: escape alt text to prevent XSS on binary files with bad filenames.
r2993 commit_id=file_obj.commit.raw_id,
f_path=file_obj.path)
files: fixed unicode problems on image preview, and make images center, no-stretch.
r4344
file-renderer: escape alt text to prevent XSS on binary files with bad filenames.
r2993 return literal(
files: fixed unicode problems on image preview, and make images center, no-stretch.
r4344 '<img class="rendered-binary" alt="rendered-image" src="{}">'.format(src))
files: render images inside the file view....
r1580
project: added all source files and assets
r1 def renderer_from_filename(filename, exclude=None):
jupyter-rendering: add required packaging to handle rendering of jupyter notebooks....
r1488 """
files: render images inside the file view....
r1580 choose a renderer based on filename, this works only for text based files
jupyter-rendering: add required packaging to handle rendering of jupyter notebooks....
r1488 """
# ipython
jupyter-rendering: added rendering of notebook into MarkupRenderer class.
r1491 for ext in ['*.ipynb']:
if fnmatch.fnmatch(filename, pat=ext):
return 'jupyter'
jupyter-rendering: add required packaging to handle rendering of jupyter notebooks....
r1488
is_markup = MarkupRenderer.renderer_from_filename(filename, exclude=exclude)
if is_markup:
return is_markup
return None
project: added all source files and assets
r1
markup: make relative links pint to raw files for images and to standard files as links....
r2003 def render(source, renderer='rst', mentions=False, relative_urls=None,
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 repo_name=None, active_pattern_entries=None, issues_container_callback=None):
markup-rendering: added relative image support....
r1527
def maybe_convert_relative_links(html_source):
markup: make relative links pint to raw files for images and to standard files as links....
r2003 if relative_urls:
return relative_links(html_source, relative_urls)
markup-rendering: added relative image support....
r1527 return html_source
pull-requests: make the renderer stored and saved for each pull requests....
r2903 if renderer == 'plain':
return literal(
MarkupRenderer.plain(source, leading_newline=False))
elif renderer == 'rst':
comments: allow parsing the issue tracker patterns inside the markup...
r1672 if repo_name:
# process patterns on comments if we pass in repo name
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 source, issues, errors = process_patterns(
issue-tracker: cache active patterns in the template so we can re-use them when rendering comments....
r4202 source, repo_name, link_format='rst',
active_entries=active_pattern_entries)
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 if issues_container_callback is not None:
for issue in issues:
issues_container_callback(issue)
comments: allow parsing the issue tracker patterns inside the markup...
r1672
helpers: python3 port
r5084 rendered_block = maybe_convert_relative_links(
MarkupRenderer.rst(source, mentions=mentions))
return literal(f'<div class="rst-block">{rendered_block}</div>')
pull-requests: make the renderer stored and saved for each pull requests....
r2903
jupyter-rendering: add required packaging to handle rendering of jupyter notebooks....
r1488 elif renderer == 'markdown':
comments: allow parsing the issue tracker patterns inside the markup...
r1672 if repo_name:
# process patterns on comments if we pass in repo name
issue-trackers: implemented more sophisticated ticket data extraction based on advanced regex module, and special...
r4498 source, issues, errors = process_patterns(
issue-tracker: cache active patterns in the template so we can re-use them when rendering comments....
r4202 source, repo_name, link_format='markdown',
active_entries=active_pattern_entries)
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650 if issues_container_callback is not None:
for issue in issues:
issues_container_callback(issue)
helpers: python3 port
r5084 rendered_block = maybe_convert_relative_links(
MarkupRenderer.markdown(source, flavored=True, mentions=mentions))
return literal(f'<div class="markdown-block">{rendered_block}</div>')
pull-requests: make the renderer stored and saved for each pull requests....
r2903
jupyter-rendering: added rendering of notebook into MarkupRenderer class.
r1491 elif renderer == 'jupyter':
helpers: python3 port
r5084 rendered_block = maybe_convert_relative_links(
MarkupRenderer.jupyter(source))
return literal(f'<div class="ipynb">{rendered_block}</div>')
jupyter-rendering: add required packaging to handle rendering of jupyter notebooks....
r1488
# None means just show the file-source
return None
project: added all source files and assets
r1
def commit_status(repo, commit_id):
return ChangesetStatusModel().get_status(repo, commit_id)
def commit_status_lbl(commit_status):
return dict(ChangesetStatus.STATUSES).get(commit_status)
def commit_time(repo_name, commit_id):
repo = Repository.get_by_repo_name(repo_name)
commit = repo.get_commit(commit_id=commit_id)
return commit.date
def get_permission_name(key):
return dict(Permission.PERMS).get(key)
pyramid: stop relying on pylons translation object in helpers....
r1914 def journal_filter_help(request):
_ = request.translate
audit-logs: improve help text, and expose all audit commands for easier filtering +ui fixes
r2807 from rhodecode.lib.audit_logger import ACTIONS
actions = '\n'.join(textwrap.wrap(', '.join(sorted(ACTIONS.keys())), 80))
pyramid: stop relying on pylons translation object in helpers....
r1914
project: added all source files and assets
r1 return _(
'Example filter terms:\n' +
' repository:vcs\n' +
' username:marcin\n' +
audit-logs: added *basic* support for NOT query term in audit logs.
r1824 ' username:(NOT marcin)\n' +
project: added all source files and assets
r1 ' action:*push*\n' +
' ip:127.0.0.1\n' +
' date:20120101\n' +
' date:[20120101100000 TO 20120102]\n' +
'\n' +
audit-logs: improve help text, and expose all audit commands for easier filtering +ui fixes
r2807 'Actions: {actions}\n' +
'\n' +
project: added all source files and assets
r1 'Generate wildcards using \'*\' character:\n' +
' "repository:vcs*" - search everything starting with \'vcs\'\n' +
' "repository:*vcs*" - search for repository containing \'vcs\'\n' +
'\n' +
'Optional AND / OR operators in queries\n' +
' "repository:vcs OR repository:test"\n' +
' "username:test AND repository:test*"\n'
audit-logs: improve help text, and expose all audit commands for easier filtering +ui fixes
r2807 ).format(actions=actions)
project: added all source files and assets
r1
def not_mapped_error(repo_name):
pyramid: stop relying on pylons translation object in helpers....
r1914 from rhodecode.translation import _
project: added all source files and assets
r1 flash(_('%s repository is not mapped to db perhaps'
' it was created or renamed from the filesystem'
' please run the application again'
' in order to rescan repositories') % repo_name, category='error')
def ip_range(ip_addr):
from rhodecode.model.db import UserIpMap
s, e = UserIpMap._get_ip_range(ip_addr)
return '%s - %s' % (s, e)
def form(url, method='post', needs_csrf_token=True, **attrs):
"""Wrapper around webhelpers.tags.form to prevent CSRF attacks."""
if method.lower() != 'get' and needs_csrf_token:
raise Exception(
'Forms to POST/PUT/DELETE endpoints should have (in general) a ' +
'CSRF token. If the endpoint does not require such token you can ' +
'explicitly set the parameter needs_csrf_token to false.')
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 return insecure_form(url, method=method, **attrs)
project: added all source files and assets
r1
helpers: make sure request parameter is not rendered inside secure form.
r2088 def secure_form(form_url, method="POST", multipart=False, **attrs):
project: added all source files and assets
r1 """Start a form tag that points the action to an url. This
form tag will also include the hidden field containing
the auth token.
The url options should be given either as a string, or as a
``url()`` function. The method for the form defaults to POST.
Options:
``multipart``
If set to True, the enctype is set to "multipart/form-data".
``method``
The method to use when submitting the form, usually either
"GET" or "POST". If "PUT", "DELETE", or another verb is used, a
hidden input with name _method is added to simulate the verb
over POST.
"""
pyramid: if possible fetch csrf tokens from pyramid session....
r1918
if 'request' in attrs:
session = attrs['request'].session
helpers: make sure request parameter is not rendered inside secure form.
r2088 del attrs['request']
pylons: fixed code and test suite after removal of pylons.
r2358 else:
raise ValueError(
'Calling this form requires request= to be passed as argument')
pyramid: if possible fetch csrf tokens from pyramid session....
r1918
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 _form = insecure_form(form_url, method, multipart, **attrs)
pyramid: if possible fetch csrf tokens from pyramid session....
r1918 token = literal(
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 '<input type="hidden" name="{}" value="{}">'.format(
csrf_token_key, get_csrf_token(session)))
pyramid: if possible fetch csrf tokens from pyramid session....
r1918
dan
webhelpers: port most of the items from webhelpers to webhelpers2...
r4090 return literal("%s\n%s" % (_form, token))
project: added all source files and assets
r1
dan
forms: add deform for integration settings forms
r518
project: added all source files and assets
r1 def dropdownmenu(name, selected, options, enable_filter=False, **attrs):
select_html = select(name, selected, options, **attrs)
dan
file: new file editors...
r3754
project: added all source files and assets
r1 select2 = """
<script>
$(document).ready(function() {
$('#%s').select2({
dan
file: new file editors...
r3754 containerCssClass: 'drop-menu %s',
project: added all source files and assets
r1 dropdownCssClass: 'drop-menu-dropdown',
dropdownAutoWidth: true%s
});
});
</script>
"""
dan
file: new file editors...
r3754
project: added all source files and assets
r1 filter_option = """,
minimumResultsForSearch: -1
"""
input_id = attrs.get('id') or name
dan
file: new file editors...
r3754 extra_classes = ' '.join(attrs.pop('extra_classes', []))
project: added all source files and assets
r1 filter_enabled = "" if enable_filter else filter_option
dan
file: new file editors...
r3754 select_script = literal(select2 % (input_id, extra_classes, filter_enabled))
project: added all source files and assets
r1
return literal(select_html+select_script)
def get_visual_attr(tmpl_context_var, attr_name):
"""
A safe way to get a variable from visual variable of template context
:param tmpl_context_var: instance of tmpl_context, usually present as `c`
:param attr_name: name of the attribute we fetch from the c.visual
"""
visual = getattr(tmpl_context_var, 'visual', None)
if not visual:
return
else:
return getattr(visual, attr_name, None)
def get_last_path_part(file_node):
if not file_node.path:
helpers: python3 port
r5084 return '/'
path = safe_str(file_node.path.split('/')[-1])
return '../' + path
project: added all source files and assets
r1
home: moved home and repo group views into pyramid....
r1774 def route_url(*args, **kwargs):
repo-settings: expose pull api endpoint into remote pull view for easier automation.
r1551 """
dan
hovercards: allow hovercards on parsed !PR patterns....
r4046 Wrapper around pyramids `route_url` (fully qualified url) function.
repo-settings: expose pull api endpoint into remote pull view for easier automation.
r1551 """
req = get_current_request()
home: moved home and repo group views into pyramid....
r1774 return req.route_url(*args, **kwargs)
repo-settings: expose pull api endpoint into remote pull view for easier automation.
r1551
home: moved home and repo group views into pyramid....
r1774 def route_path(*args, **kwargs):
project: added all source files and assets
r1 """
pylons: remove pylons as dependency...
r2351 Wrapper around pyramids `route_path` function.
project: added all source files and assets
r1 """
req = get_current_request()
home: moved home and repo group views into pyramid....
r1774 return req.route_path(*args, **kwargs)
project: added all source files and assets
r1
helpers: added route_path_or_none helper for routes that might be added by plugins
r780 def route_path_or_none(*args, **kwargs):
try:
return route_path(*args, **kwargs)
except KeyError:
return None
routing: remove usage of url.current from pylons.
r2104 def current_route_path(request, **kw):
new_args = request.GET.mixed()
new_args.update(kw)
return request.current_route_path(_query=new_args)
helpers: added separate curl example, we'll need to it generate artifacts curl examples.
r3990 def curl_api_example(method, args):
helpers: added a helper to show example api call for automation of certain actions.
r1740 args_json = json.dumps(OrderedDict([
('id', 1),
('auth_token', 'SECRET'),
('method', method),
('args', args)
]))
helpers: added separate curl example, we'll need to it generate artifacts curl examples.
r3990
return "curl {api_url} -X POST -H 'content-type:text/plain' --data-binary '{args_json}'".format(
api_url=route_url('apiv2'),
args_json=args_json
)
def api_call_example(method, args):
"""
Generates an API call example via CURL
"""
curl_call = curl_api_example(method, args)
helpers: added a helper to show example api call for automation of certain actions.
r1740 return literal(
helpers: added separate curl example, we'll need to it generate artifacts curl examples.
r3990 curl_call +
helpers: added a helper to show example api call for automation of certain actions.
r1740 "<br/><br/>SECRET can be found in <a href=\"{token_url}\">auth-tokens</a> page, "
"and needs to be of `api calls` role."
helpers: added separate curl example, we'll need to it generate artifacts curl examples.
r3990 .format(token_url=route_url('my_account_auth_tokens')))
notifications: removed usage of pylons translation from models.
r1921
def notification_description(notification, request):
"""
Generate notification human readable description based on notification type
"""
from rhodecode.model.notification import NotificationModel
return NotificationModel().make_description(
notification, translate=request.translate)
core: added support for golang go import functionality
r2204
def go_import_header(request, db_repo=None):
"""
Creates a header for go-import functionality in Go Lang
"""
if not db_repo:
return
if 'go-get' not in request.GET:
return
clone_url = db_repo.clone_url()
prefix = re.split(r'^https?:\/\/', clone_url)[-1]
# we have a repo and go-get flag,
return literal('<meta name="go-import" content="{} {} {}">'.format(
prefix, db_repo.repo_type, clone_url))
default-reviewers: introduce new voting rule logic that allows...
r2484
def reviewer_as_json(*args, **kwargs):
from rhodecode.apps.repository.utils import reviewer_as_json as _reviewer_as_json
return _reviewer_as_json(*args, **kwargs)
dan
search: add support for elastic search 6...
r3319
def get_repo_view_type(request):
route_name = request.matched_route.name
route_to_view_type = {
changelog: rename changelog to commits pages
r3742 'repo_changelog': 'commits',
'repo_commits': 'commits',
dan
search: add support for elastic search 6...
r3319 'repo_files': 'files',
'repo_summary': 'summary',
'repo_commit': 'commit'
go-to search: updated logic of goto switcher...
r3556 }
dan
search: add support for elastic search 6...
r3319
return route_to_view_type.get(route_name)
templates: use a single helper to select activated menu entries in templates.
r4062
def is_active(menu_entry, selected):
"""
Returns active class for selecting menus in templates
<li class=${h.is_active('settings', current_active)}></li>
"""
if not isinstance(menu_entry, list):
menu_entry = [menu_entry]
if selected in menu_entry:
return "active"
pull-requests: fixed multiple repetitions of referenced tickets in pull requests summary sidebar.
r4650
class IssuesRegistry(object):
"""
issue_registry = IssuesRegistry()
some_func(issues_callback=issues_registry(...))
"""
def __init__(self):
self.issues = []
self.unique_issues = collections.defaultdict(lambda: [])
def __call__(self, commit_dict=None):
def callback(issue):
if commit_dict and issue:
issue['commit'] = commit_dict
self.issues.append(issue)
self.unique_issues[issue['id']].append(issue)
return callback
def get_issues(self):
return self.issues
@property
def issues_unique_count(self):
return len(set(i['id'] for i in self.issues))