123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # -*- coding: utf-8 -*-
- import datetime
- import random
- import string
- from redis import Redis
- from rq import Queue
-
- 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"
-
-
- 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))
|