123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- # -*- coding: utf-8 -*-
- import datetime
- import random
- import string
- from enum import Enum
- import colorsys
-
- from redis import Redis
- from rq import Queue
- import typing
- if typing.TYPE_CHECKING:
- from tracim_backend.config import CFG
-
- DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
- DEFAULT_WEBDAV_CONFIG_FILE = "wsgidav.conf"
- DEFAULT_TRACIM_CONFIG_FILE = "development.ini"
- CONTENT_FRONTEND_URL_SCHEMA = 'workspaces/{workspace_id}/contents/{content_type}/{content_id}' # nopep8
- WORKSPACE_FRONTEND_URL_SCHEMA = 'workspaces/{workspace_id}' # nopep8
-
-
- def get_root_frontend_url(config: 'CFG') -> str:
- """
- Return website base url with always '/' at the end
- """
- base_url = ''
- if config.WEBSITE_BASE_URL[-1] == '/':
- base_url = config.WEBSITE_BASE_URL
- else:
- base_url = config.WEBSITE_BASE_URL + '/'
- return base_url
-
-
- def get_login_frontend_url(config: 'CFG'):
- """
- Return login page url
- """
- return get_root_frontend_url(config) + 'login'
-
-
- def get_email_logo_frontend_url(config: 'CFG'):
- # TODO - G.M - 11-06-2018 - [emailTemplateURL] correct value for email_logo_frontend_url # nopep8
- return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH4QUTDjMSlsws9AAAB89JREFUaN7tmWtwlFcZx3/PeXeXXLmKQJEGsFZApIUBnaqjUm4VgdYyCUib0cJoKLmHW5EWmlGoQkMICURgEPuhtiYDU7XcyVgKVugULZVCQAwUYyENYAmEXHb3PH7IFjYQNgnZHfnA+bKz+7573v//PPf/C/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h5CDfmUeufTcaE9QBkFY7AyIuImXxT3bfI7doUvQayFbUDMSZYRHiLq64n2JByOXzvBzLGbkJ1GarnP4OPmBt60Or0v+E381Fdj+ILOhYT4p1ADOhTzcA3EdjeXvCtEwDIGNc0J6vNB/0FDXXbmk9uc05wMSYLq+nAgYArtWJ3keYW0eVUVheE9/3ArbYXkND+mbO2H8gk0AnAlGZAb3WlWpQ/I/o6q9JevWMxIyJdWWZ+V4xnOUaebQH4MVS2I/59eO3bFGbWdEiNiVhrmVncH+NfhMg0IA4RH1bfQnQlq9LKwiYnRbTB/9HmKLrWfg2j94HUoI1/Z3XOOe6te+vuWf8DkM0cb7DOQZgAAAAASUVORK5CYII=' # nopep8'
-
-
- def get_redis_connection(config: 'CFG') -> Redis:
- """
- :param config: current app_config
- :return: redis connection
- """
- return Redis(
- host=config.EMAIL_SENDER_REDIS_HOST,
- port=config.EMAIL_SENDER_REDIS_PORT,
- db=config.EMAIL_SENDER_REDIS_DB,
- )
-
-
- def get_rq_queue(redis_connection: Redis, queue_name: str ='default') -> Queue:
- """
- :param queue_name: name of queue
- :return: wanted queue
- """
-
- return Queue(name=queue_name, connection=redis_connection)
-
-
- def cmp_to_key(mycmp):
- """
- List sort related function
-
- Convert a cmp= function into a key= function
- """
- class K(object):
- def __init__(self, obj, *args):
- self.obj = obj
-
- def __lt__(self, other):
- return mycmp(self.obj, other.obj) < 0
-
- def __gt__(self, other):
- return mycmp(self.obj, other.obj) > 0
-
- def __eq__(self, other):
- return mycmp(self.obj, other.obj) == 0
-
- def __le__(self, other):
- return mycmp(self.obj, other.obj) <= 0
-
- def __ge__(self, other):
- return mycmp(self.obj, other.obj) >= 0
-
- def __ne__(self, other):
- return mycmp(self.obj, other.obj) != 0
-
- return K
-
-
- def current_date_for_filename() -> str:
- """
- ISO8601 current date, adapted to be used in filename (for
- webdav feature for example), with trouble-free characters.
- :return: current date as string like "2018-03-19T15.49.27.246592"
- """
- # INFO - G.M - 19-03-2018 - As ':' is in transform_to_bdd method in
- # webdav utils, it may cause trouble. So, it should be replaced to
- # a character which will not change in bdd.
- return datetime.datetime.now().isoformat().replace(':', '.')
-
- # INFO - G.M - 2018-08-02 - Simple password generator, inspired by
- # https://gist.github.com/23maverick23/4131896
-
-
- ALLOWED_AUTOGEN_PASSWORD_CHAR = string.ascii_letters + \
- string.digits + \
- string.punctuation
-
- DEFAULT_PASSWORD_GEN_CHAR_LENGTH = 12
-
-
- def password_generator(
- length: int=DEFAULT_PASSWORD_GEN_CHAR_LENGTH,
- chars: str=ALLOWED_AUTOGEN_PASSWORD_CHAR
- ) -> str:
- """
- :param length: length of the new password
- :param chars: characters allowed
- :return: password as string
- """
- return ''.join(random.choice(chars) for char_number in range(length))
-
-
- def clamp(val: float, minimum: float = 0.0, maximum: float= 255.0) -> int:
- """ Fix value between min an max"""
- if val < minimum:
- return minimum
- if val > maximum:
- return maximum
- return int(val)
-
-
- COLOR_DARKEN_SCALE_FACTOR = 0.85
- COLOR_LIGHTEN_SCALE_FACTOR = 1.15
-
-
- class Color(object):
- def __init__(self, base_hex_code: str):
- """
- :param base_hex_code: hex color like '#FFFFFF'
- """
-
- assert len(base_hex_code) == 7
- self._base_hex_code = base_hex_code
-
- # INFO - G.M - 2018-08-10 - get_hexcolor, inspired by
- # https://thadeusb.com/weblog/2010/10/10/python_scale_hex_color/
-
- def get_hexcolor(self, scalefactor: float) -> str:
- """
-
- :param scalefactor: factor of scaling,
- value between 0 and 1 darken the color,
- value >1 lighten the color.
- :return: new hex_color
- """
-
- hex_color = self._base_hex_code.strip('#')
- assert scalefactor > 0
-
- r = int(hex_color[:2], 16)
- g = int(hex_color[2:4], 16)
- b = int(hex_color[4:], 16)
-
- h, l, s = colorsys.rgb_to_hls(r, g, b)
- l = scalefactor * l
- r, g, b = colorsys.hls_to_rgb(h, l, s)
-
- return "#%02x%02x%02x" % (clamp(r), clamp(g), clamp(b))
-
- @property
- def normal(self):
- return self._base_hex_code
-
- @property
- def darken(self):
- return self.get_hexcolor(COLOR_DARKEN_SCALE_FACTOR)
-
- @property
- def lighten(self):
- return self.get_hexcolor(COLOR_LIGHTEN_SCALE_FACTOR)
|