config.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. # -*- coding: utf-8 -*-
  2. from urllib.parse import urlparse
  3. from paste.deploy.converters import asbool
  4. from tracim_backend.lib.utils.logger import logger
  5. from depot.manager import DepotManager
  6. from tracim_backend.models.contents import CONTENT_TYPES
  7. from tracim_backend.models.data import ActionDescription
  8. class CFG(object):
  9. """Object used for easy access to config file parameters."""
  10. def __setattr__(self, key, value):
  11. """
  12. Log-ready setter.
  13. Logs all configuration parameters except password.
  14. :param key:
  15. :param value:
  16. :return:
  17. """
  18. if 'PASSWORD' not in key and \
  19. ('URL' not in key or type(value) == str) and \
  20. 'CONTENT' not in key:
  21. # We do not show PASSWORD for security reason
  22. # we do not show URL because At the time of configuration setup,
  23. # it can't be evaluated
  24. # We do not show CONTENT in order not to pollute log files
  25. logger.info(self, 'CONFIG: [ {} | {} ]'.format(key, value))
  26. else:
  27. logger.info(self, 'CONFIG: [ {} | <value not shown> ]'.format(key))
  28. self.__dict__[key] = value
  29. def __init__(self, settings):
  30. """Parse configuration file."""
  31. ###
  32. # General
  33. ###
  34. mandatory_msg = \
  35. 'ERROR: {} configuration is mandatory. Set it before continuing.'
  36. self.DEPOT_STORAGE_DIR = settings.get(
  37. 'depot_storage_dir',
  38. )
  39. if not self.DEPOT_STORAGE_DIR:
  40. raise Exception(
  41. mandatory_msg.format('depot_storage_dir')
  42. )
  43. self.DEPOT_STORAGE_NAME = settings.get(
  44. 'depot_storage_name',
  45. )
  46. if not self.DEPOT_STORAGE_NAME:
  47. raise Exception(
  48. mandatory_msg.format('depot_storage_name')
  49. )
  50. self.PREVIEW_CACHE_DIR = settings.get(
  51. 'preview_cache_dir',
  52. )
  53. if not self.PREVIEW_CACHE_DIR:
  54. raise Exception(
  55. 'ERROR: preview_cache_dir configuration is mandatory. '
  56. 'Set it before continuing.'
  57. )
  58. self.DATA_UPDATE_ALLOWED_DURATION = int(settings.get(
  59. 'content.update.allowed.duration',
  60. 0,
  61. ))
  62. self.WEBSITE_TITLE = settings.get(
  63. 'website.title',
  64. 'TRACIM',
  65. )
  66. self.WEBSITE_BASE_URL = settings.get(
  67. 'website.base_url',
  68. '',
  69. )
  70. # TODO - G.M - 26-03-2018 - [Cleanup] These params seems deprecated for tracimv2, # nopep8
  71. # Verify this
  72. #
  73. # self.WEBSITE_HOME_TITLE_COLOR = settings.get(
  74. # 'website.title.color',
  75. # '#555',
  76. # )
  77. # self.WEBSITE_HOME_IMAGE_PATH = settings.get(
  78. # '/assets/img/home_illustration.jpg',
  79. # )
  80. # self.WEBSITE_HOME_BACKGROUND_IMAGE_PATH = settings.get(
  81. # '/assets/img/bg.jpg',
  82. # )
  83. #
  84. self.WEBSITE_SERVER_NAME = settings.get(
  85. 'website.server_name',
  86. None,
  87. )
  88. if not self.WEBSITE_SERVER_NAME:
  89. self.WEBSITE_SERVER_NAME = urlparse(self.WEBSITE_BASE_URL).hostname
  90. logger.warning(
  91. self,
  92. 'NOTE: Generated website.server_name parameter from '
  93. 'website.base_url parameter -> {0}'
  94. .format(self.WEBSITE_SERVER_NAME)
  95. )
  96. self.WEBSITE_HOME_TAG_LINE = settings.get(
  97. 'website.home.tag_line',
  98. '',
  99. )
  100. self.WEBSITE_SUBTITLE = settings.get(
  101. 'website.home.subtitle',
  102. '',
  103. )
  104. self.WEBSITE_HOME_BELOW_LOGIN_FORM = settings.get(
  105. 'website.home.below_login_form',
  106. '',
  107. )
  108. self.WEBSITE_TREEVIEW_CONTENT = settings.get(
  109. 'website.treeview.content',
  110. )
  111. self.USER_AUTH_TOKEN_VALIDITY = int(settings.get(
  112. 'user.auth_token.validity',
  113. '604800',
  114. ))
  115. self.DEBUG = asbool(settings.get('debug', False))
  116. # TODO - G.M - 27-03-2018 - [Email] Restore email config
  117. ###
  118. # EMAIL related stuff (notification, reply)
  119. ##
  120. self.EMAIL_NOTIFICATION_NOTIFIED_EVENTS = [
  121. ActionDescription.COMMENT,
  122. ActionDescription.CREATION,
  123. ActionDescription.EDITION,
  124. ActionDescription.REVISION,
  125. ActionDescription.STATUS_UPDATE
  126. ]
  127. self.EMAIL_NOTIFICATION_NOTIFIED_CONTENTS = [
  128. CONTENT_TYPES.Page.slug,
  129. CONTENT_TYPES.Thread.slug,
  130. CONTENT_TYPES.File.slug,
  131. CONTENT_TYPES.Comment.slug,
  132. # CONTENT_TYPES.Folder.slug -- Folder is skipped
  133. ]
  134. if settings.get('email.notification.from'):
  135. raise Exception(
  136. 'email.notification.from configuration is deprecated. '
  137. 'Use instead email.notification.from.email and '
  138. 'email.notification.from.default_label.'
  139. )
  140. self.EMAIL_NOTIFICATION_FROM_EMAIL = settings.get(
  141. 'email.notification.from.email',
  142. )
  143. self.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL = settings.get(
  144. 'email.notification.from.default_label'
  145. )
  146. self.EMAIL_NOTIFICATION_REPLY_TO_EMAIL = settings.get(
  147. 'email.notification.reply_to.email',
  148. )
  149. self.EMAIL_NOTIFICATION_REFERENCES_EMAIL = settings.get(
  150. 'email.notification.references.email'
  151. )
  152. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_HTML = settings.get(
  153. 'email.notification.content_update.template.html',
  154. )
  155. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_TEXT = settings.get(
  156. 'email.notification.content_update.template.text',
  157. )
  158. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML = settings.get(
  159. 'email.notification.created_account.template.html',
  160. './tracim_backend/templates/mail/created_account_body_html.mak',
  161. )
  162. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT = settings.get(
  163. 'email.notification.created_account.template.text',
  164. './tracim_backend/templates/mail/created_account_body_text.mak',
  165. )
  166. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_SUBJECT = settings.get(
  167. 'email.notification.content_update.subject',
  168. )
  169. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_SUBJECT = settings.get(
  170. 'email.notification.created_account.subject',
  171. '[{website_title}] Created account',
  172. )
  173. self.EMAIL_NOTIFICATION_PROCESSING_MODE = settings.get(
  174. 'email.notification.processing_mode',
  175. )
  176. self.EMAIL_NOTIFICATION_ACTIVATED = asbool(settings.get(
  177. 'email.notification.activated',
  178. ))
  179. self.EMAIL_NOTIFICATION_SMTP_SERVER = settings.get(
  180. 'email.notification.smtp.server',
  181. )
  182. self.EMAIL_NOTIFICATION_SMTP_PORT = settings.get(
  183. 'email.notification.smtp.port',
  184. )
  185. self.EMAIL_NOTIFICATION_SMTP_USER = settings.get(
  186. 'email.notification.smtp.user',
  187. )
  188. self.EMAIL_NOTIFICATION_SMTP_PASSWORD = settings.get(
  189. 'email.notification.smtp.password',
  190. )
  191. self.EMAIL_NOTIFICATION_LOG_FILE_PATH = settings.get(
  192. 'email.notification.log_file_path',
  193. None,
  194. )
  195. # self.EMAIL_REPLY_ACTIVATED = asbool(settings.get(
  196. # 'email.reply.activated',
  197. # False,
  198. # ))
  199. #
  200. # self.EMAIL_REPLY_IMAP_SERVER = settings.get(
  201. # 'email.reply.imap.server',
  202. # )
  203. # self.EMAIL_REPLY_IMAP_PORT = settings.get(
  204. # 'email.reply.imap.port',
  205. # )
  206. # self.EMAIL_REPLY_IMAP_USER = settings.get(
  207. # 'email.reply.imap.user',
  208. # )
  209. # self.EMAIL_REPLY_IMAP_PASSWORD = settings.get(
  210. # 'email.reply.imap.password',
  211. # )
  212. # self.EMAIL_REPLY_IMAP_FOLDER = settings.get(
  213. # 'email.reply.imap.folder',
  214. # )
  215. # self.EMAIL_REPLY_CHECK_HEARTBEAT = int(settings.get(
  216. # 'email.reply.check.heartbeat',
  217. # 60,
  218. # ))
  219. # self.EMAIL_REPLY_TOKEN = settings.get(
  220. # 'email.reply.token',
  221. # )
  222. # self.EMAIL_REPLY_IMAP_USE_SSL = asbool(settings.get(
  223. # 'email.reply.imap.use_ssl',
  224. # ))
  225. # self.EMAIL_REPLY_IMAP_USE_IDLE = asbool(settings.get(
  226. # 'email.reply.imap.use_idle',
  227. # True,
  228. # ))
  229. # self.EMAIL_REPLY_CONNECTION_MAX_LIFETIME = int(settings.get(
  230. # 'email.reply.connection.max_lifetime',
  231. # 600, # 10 minutes
  232. # ))
  233. # self.EMAIL_REPLY_USE_HTML_PARSING = asbool(settings.get(
  234. # 'email.reply.use_html_parsing',
  235. # True,
  236. # ))
  237. # self.EMAIL_REPLY_USE_TXT_PARSING = asbool(settings.get(
  238. # 'email.reply.use_txt_parsing',
  239. # True,
  240. # ))
  241. # self.EMAIL_REPLY_LOCKFILE_PATH = settings.get(
  242. # 'email.reply.lockfile_path',
  243. # ''
  244. # )
  245. # if not self.EMAIL_REPLY_LOCKFILE_PATH and self.EMAIL_REPLY_ACTIVATED:
  246. # raise Exception(
  247. # mandatory_msg.format('email.reply.lockfile_path')
  248. # )
  249. #
  250. self.EMAIL_PROCESSING_MODE = settings.get(
  251. 'email.processing_mode',
  252. 'sync',
  253. ).upper()
  254. if self.EMAIL_PROCESSING_MODE not in (
  255. self.CST.ASYNC,
  256. self.CST.SYNC,
  257. ):
  258. raise Exception(
  259. 'email.processing_mode '
  260. 'can ''be "{}" or "{}", not "{}"'.format(
  261. self.CST.ASYNC,
  262. self.CST.SYNC,
  263. self.EMAIL_PROCESSING_MODE,
  264. )
  265. )
  266. self.EMAIL_SENDER_REDIS_HOST = settings.get(
  267. 'email.async.redis.host',
  268. 'localhost',
  269. )
  270. self.EMAIL_SENDER_REDIS_PORT = int(settings.get(
  271. 'email.async.redis.port',
  272. 6379,
  273. ))
  274. self.EMAIL_SENDER_REDIS_DB = int(settings.get(
  275. 'email.async.redis.db',
  276. 0,
  277. ))
  278. ###
  279. # WSGIDAV (Webdav server)
  280. ###
  281. # TODO - G.M - 27-03-2018 - [WebDav] Restore wsgidav config
  282. #self.WSGIDAV_CONFIG_PATH = settings.get(
  283. # 'wsgidav.config_path',
  284. # 'wsgidav.conf',
  285. #)
  286. # TODO: Convert to importlib
  287. # http://stackoverflow.com/questions/41063938/use-importlib-instead-imp-for-non-py-file
  288. #self.wsgidav_config = imp.load_source(
  289. # 'wsgidav_config',
  290. # self.WSGIDAV_CONFIG_PATH,
  291. #)
  292. # self.WSGIDAV_PORT = self.wsgidav_config.port
  293. # self.WSGIDAV_CLIENT_BASE_URL = settings.get(
  294. # 'wsgidav.client.base_url',
  295. # None,
  296. # )
  297. #
  298. # if not self.WSGIDAV_CLIENT_BASE_URL:
  299. # self.WSGIDAV_CLIENT_BASE_URL = \
  300. # '{0}:{1}'.format(
  301. # self.WEBSITE_SERVER_NAME,
  302. # self.WSGIDAV_PORT,
  303. # )
  304. # logger.warning(self,
  305. # 'NOTE: Generated wsgidav.client.base_url parameter with '
  306. # 'followings parameters: website.server_name and '
  307. # 'wsgidav.conf port'.format(
  308. # self.WSGIDAV_CLIENT_BASE_URL,
  309. # )
  310. # )
  311. #
  312. # if not self.WSGIDAV_CLIENT_BASE_URL.endswith('/'):
  313. # self.WSGIDAV_CLIENT_BASE_URL += '/'
  314. # TODO - G.M - 27-03-2018 - [Caldav] Restore radicale config
  315. ###
  316. # RADICALE (Caldav server)
  317. ###
  318. # self.RADICALE_SERVER_HOST = settings.get(
  319. # 'radicale.server.host',
  320. # '127.0.0.1',
  321. # )
  322. # self.RADICALE_SERVER_PORT = int(settings.get(
  323. # 'radicale.server.port',
  324. # 5232,
  325. # ))
  326. # # Note: Other parameters needed to work in SSL (cert file, etc)
  327. # self.RADICALE_SERVER_SSL = asbool(settings.get(
  328. # 'radicale.server.ssl',
  329. # False,
  330. # ))
  331. # self.RADICALE_SERVER_FILE_SYSTEM_FOLDER = settings.get(
  332. # 'radicale.server.filesystem.folder',
  333. # )
  334. # if not self.RADICALE_SERVER_FILE_SYSTEM_FOLDER:
  335. # raise Exception(
  336. # mandatory_msg.format('radicale.server.filesystem.folder')
  337. # )
  338. # self.RADICALE_SERVER_ALLOW_ORIGIN = settings.get(
  339. # 'radicale.server.allow_origin',
  340. # None,
  341. # )
  342. # if not self.RADICALE_SERVER_ALLOW_ORIGIN:
  343. # self.RADICALE_SERVER_ALLOW_ORIGIN = self.WEBSITE_BASE_URL
  344. # logger.warning(self,
  345. # 'NOTE: Generated radicale.server.allow_origin parameter with '
  346. # 'followings parameters: website.base_url ({0})'
  347. # .format(self.WEBSITE_BASE_URL)
  348. # )
  349. #
  350. # self.RADICALE_SERVER_REALM_MESSAGE = settings.get(
  351. # 'radicale.server.realm_message',
  352. # 'Tracim Calendar - Password Required',
  353. # )
  354. #
  355. # self.RADICALE_CLIENT_BASE_URL_HOST = settings.get(
  356. # 'radicale.client.base_url.host',
  357. # 'http://{}:{}'.format(
  358. # self.RADICALE_SERVER_HOST,
  359. # self.RADICALE_SERVER_PORT,
  360. # ),
  361. # )
  362. #
  363. # self.RADICALE_CLIENT_BASE_URL_PREFIX = settings.get(
  364. # 'radicale.client.base_url.prefix',
  365. # '/',
  366. # )
  367. # # Ensure finished by '/'
  368. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[-1]:
  369. # self.RADICALE_CLIENT_BASE_URL_PREFIX += '/'
  370. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[0]:
  371. # self.RADICALE_CLIENT_BASE_URL_PREFIX \
  372. # = '/' + self.RADICALE_CLIENT_BASE_URL_PREFIX
  373. #
  374. # if not self.RADICALE_CLIENT_BASE_URL_HOST:
  375. # logger.warning(self,
  376. # 'Generated radicale.client.base_url.host parameter with '
  377. # 'followings parameters: website.server_name -> {}'
  378. # .format(self.WEBSITE_SERVER_NAME)
  379. # )
  380. # self.RADICALE_CLIENT_BASE_URL_HOST = self.WEBSITE_SERVER_NAME
  381. #
  382. # self.RADICALE_CLIENT_BASE_URL_TEMPLATE = '{}{}'.format(
  383. # self.RADICALE_CLIENT_BASE_URL_HOST,
  384. # self.RADICALE_CLIENT_BASE_URL_PREFIX,
  385. # )
  386. self.PREVIEW_JPG_RESTRICTED_DIMS = asbool(settings.get(
  387. 'preview.jpg.restricted_dims', False
  388. ))
  389. preview_jpg_allowed_dims_str = settings.get('preview.jpg.allowed_dims', '') # nopep8
  390. allowed_dims = []
  391. if preview_jpg_allowed_dims_str:
  392. for sizes in preview_jpg_allowed_dims_str.split(','):
  393. parts = sizes.split('x')
  394. assert len(parts) == 2
  395. width, height = parts
  396. assert width.isdecimal()
  397. assert height.isdecimal()
  398. size = PreviewDim(int(width), int(height))
  399. allowed_dims.append(size)
  400. if not allowed_dims:
  401. size = PreviewDim(256, 256)
  402. allowed_dims.append(size)
  403. self.PREVIEW_JPG_ALLOWED_DIMS = allowed_dims
  404. def configure_filedepot(self):
  405. depot_storage_name = self.DEPOT_STORAGE_NAME
  406. depot_storage_path = self.DEPOT_STORAGE_DIR
  407. depot_storage_settings = {'depot.storage_path': depot_storage_path}
  408. DepotManager.configure(
  409. depot_storage_name,
  410. depot_storage_settings,
  411. )
  412. class CST(object):
  413. ASYNC = 'ASYNC'
  414. SYNC = 'SYNC'
  415. TREEVIEW_FOLDERS = 'folders'
  416. TREEVIEW_ALL = 'all'
  417. class PreviewDim(object):
  418. def __init__(self, width: int, height: int) -> None:
  419. self.width = width
  420. self.height = height
  421. def __repr__(self):
  422. return "<PreviewDim width:{width} height:{height}>".format(
  423. width=self.width,
  424. height=self.height,
  425. )