utils.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from redis import Redis
  4. from rq import Queue
  5. from tracim_backend.config import CFG
  6. DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
  7. DEFAULT_WEBDAV_CONFIG_FILE = "wsgidav.conf"
  8. DEFAULT_TRACIM_CONFIG_FILE = "development.ini"
  9. def get_redis_connection(config: CFG) -> Redis:
  10. """
  11. :param config: current app_config
  12. :return: redis connection
  13. """
  14. return Redis(
  15. host=config.EMAIL_SENDER_REDIS_HOST,
  16. port=config.EMAIL_SENDER_REDIS_PORT,
  17. db=config.EMAIL_SENDER_REDIS_DB,
  18. )
  19. def get_rq_queue(redis_connection: Redis, queue_name: str ='default') -> Queue:
  20. """
  21. :param queue_name: name of queue
  22. :return: wanted queue
  23. """
  24. return Queue(name=queue_name, connection=redis_connection)
  25. def cmp_to_key(mycmp):
  26. """
  27. List sort related function
  28. Convert a cmp= function into a key= function
  29. """
  30. class K(object):
  31. def __init__(self, obj, *args):
  32. self.obj = obj
  33. def __lt__(self, other):
  34. return mycmp(self.obj, other.obj) < 0
  35. def __gt__(self, other):
  36. return mycmp(self.obj, other.obj) > 0
  37. def __eq__(self, other):
  38. return mycmp(self.obj, other.obj) == 0
  39. def __le__(self, other):
  40. return mycmp(self.obj, other.obj) <= 0
  41. def __ge__(self, other):
  42. return mycmp(self.obj, other.obj) >= 0
  43. def __ne__(self, other):
  44. return mycmp(self.obj, other.obj) != 0
  45. return K
  46. def current_date_for_filename() -> str:
  47. """
  48. ISO8601 current date, adapted to be used in filename (for
  49. webdav feature for example), with trouble-free characters.
  50. :return: current date as string like "2018-03-19T15.49.27.246592"
  51. """
  52. # INFO - G.M - 19-03-2018 - As ':' is in transform_to_bdd method in
  53. # webdav utils, it may cause trouble. So, it should be replaced to
  54. # a character which will not change in bdd.
  55. return datetime.datetime.now().isoformat().replace(':', '.')