config.py 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. 'noreply+{user_id}@trac.im'
  143. )
  144. self.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL = settings.get(
  145. 'email.notification.from.default_label',
  146. 'Tracim Notifications'
  147. )
  148. self.EMAIL_NOTIFICATION_REPLY_TO_EMAIL = settings.get(
  149. 'email.notification.reply_to.email',
  150. )
  151. self.EMAIL_NOTIFICATION_REFERENCES_EMAIL = settings.get(
  152. 'email.notification.references.email'
  153. )
  154. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_HTML = settings.get(
  155. 'email.notification.content_update.template.html',
  156. )
  157. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_TEXT = settings.get(
  158. 'email.notification.content_update.template.text',
  159. )
  160. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML = settings.get(
  161. 'email.notification.created_account.template.html',
  162. './tracim_backend/templates/mail/created_account_body_html.mak',
  163. )
  164. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT = settings.get(
  165. 'email.notification.created_account.template.text',
  166. './tracim_backend/templates/mail/created_account_body_text.mak',
  167. )
  168. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_SUBJECT = settings.get(
  169. 'email.notification.content_update.subject',
  170. )
  171. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_SUBJECT = settings.get(
  172. 'email.notification.created_account.subject',
  173. '[{website_title}] Created account',
  174. )
  175. self.EMAIL_NOTIFICATION_PROCESSING_MODE = settings.get(
  176. 'email.notification.processing_mode',
  177. )
  178. self.EMAIL_NOTIFICATION_ACTIVATED = asbool(settings.get(
  179. 'email.notification.activated',
  180. ))
  181. self.EMAIL_NOTIFICATION_SMTP_SERVER = settings.get(
  182. 'email.notification.smtp.server',
  183. )
  184. self.EMAIL_NOTIFICATION_SMTP_PORT = settings.get(
  185. 'email.notification.smtp.port',
  186. )
  187. self.EMAIL_NOTIFICATION_SMTP_USER = settings.get(
  188. 'email.notification.smtp.user',
  189. )
  190. self.EMAIL_NOTIFICATION_SMTP_PASSWORD = settings.get(
  191. 'email.notification.smtp.password',
  192. )
  193. self.EMAIL_NOTIFICATION_LOG_FILE_PATH = settings.get(
  194. 'email.notification.log_file_path',
  195. None,
  196. )
  197. # self.EMAIL_REPLY_ACTIVATED = asbool(settings.get(
  198. # 'email.reply.activated',
  199. # False,
  200. # ))
  201. #
  202. # self.EMAIL_REPLY_IMAP_SERVER = settings.get(
  203. # 'email.reply.imap.server',
  204. # )
  205. # self.EMAIL_REPLY_IMAP_PORT = settings.get(
  206. # 'email.reply.imap.port',
  207. # )
  208. # self.EMAIL_REPLY_IMAP_USER = settings.get(
  209. # 'email.reply.imap.user',
  210. # )
  211. # self.EMAIL_REPLY_IMAP_PASSWORD = settings.get(
  212. # 'email.reply.imap.password',
  213. # )
  214. # self.EMAIL_REPLY_IMAP_FOLDER = settings.get(
  215. # 'email.reply.imap.folder',
  216. # )
  217. # self.EMAIL_REPLY_CHECK_HEARTBEAT = int(settings.get(
  218. # 'email.reply.check.heartbeat',
  219. # 60,
  220. # ))
  221. # self.EMAIL_REPLY_TOKEN = settings.get(
  222. # 'email.reply.token',
  223. # )
  224. # self.EMAIL_REPLY_IMAP_USE_SSL = asbool(settings.get(
  225. # 'email.reply.imap.use_ssl',
  226. # ))
  227. # self.EMAIL_REPLY_IMAP_USE_IDLE = asbool(settings.get(
  228. # 'email.reply.imap.use_idle',
  229. # True,
  230. # ))
  231. # self.EMAIL_REPLY_CONNECTION_MAX_LIFETIME = int(settings.get(
  232. # 'email.reply.connection.max_lifetime',
  233. # 600, # 10 minutes
  234. # ))
  235. # self.EMAIL_REPLY_USE_HTML_PARSING = asbool(settings.get(
  236. # 'email.reply.use_html_parsing',
  237. # True,
  238. # ))
  239. # self.EMAIL_REPLY_USE_TXT_PARSING = asbool(settings.get(
  240. # 'email.reply.use_txt_parsing',
  241. # True,
  242. # ))
  243. # self.EMAIL_REPLY_LOCKFILE_PATH = settings.get(
  244. # 'email.reply.lockfile_path',
  245. # ''
  246. # )
  247. # if not self.EMAIL_REPLY_LOCKFILE_PATH and self.EMAIL_REPLY_ACTIVATED:
  248. # raise Exception(
  249. # mandatory_msg.format('email.reply.lockfile_path')
  250. # )
  251. #
  252. self.EMAIL_PROCESSING_MODE = settings.get(
  253. 'email.processing_mode',
  254. 'sync',
  255. ).upper()
  256. if self.EMAIL_PROCESSING_MODE not in (
  257. self.CST.ASYNC,
  258. self.CST.SYNC,
  259. ):
  260. raise Exception(
  261. 'email.processing_mode '
  262. 'can ''be "{}" or "{}", not "{}"'.format(
  263. self.CST.ASYNC,
  264. self.CST.SYNC,
  265. self.EMAIL_PROCESSING_MODE,
  266. )
  267. )
  268. self.EMAIL_SENDER_REDIS_HOST = settings.get(
  269. 'email.async.redis.host',
  270. 'localhost',
  271. )
  272. self.EMAIL_SENDER_REDIS_PORT = int(settings.get(
  273. 'email.async.redis.port',
  274. 6379,
  275. ))
  276. self.EMAIL_SENDER_REDIS_DB = int(settings.get(
  277. 'email.async.redis.db',
  278. 0,
  279. ))
  280. ###
  281. # WSGIDAV (Webdav server)
  282. ###
  283. # TODO - G.M - 27-03-2018 - [WebDav] Restore wsgidav config
  284. #self.WSGIDAV_CONFIG_PATH = settings.get(
  285. # 'wsgidav.config_path',
  286. # 'wsgidav.conf',
  287. #)
  288. # TODO: Convert to importlib
  289. # http://stackoverflow.com/questions/41063938/use-importlib-instead-imp-for-non-py-file
  290. #self.wsgidav_config = imp.load_source(
  291. # 'wsgidav_config',
  292. # self.WSGIDAV_CONFIG_PATH,
  293. #)
  294. # self.WSGIDAV_PORT = self.wsgidav_config.port
  295. # self.WSGIDAV_CLIENT_BASE_URL = settings.get(
  296. # 'wsgidav.client.base_url',
  297. # None,
  298. # )
  299. #
  300. # if not self.WSGIDAV_CLIENT_BASE_URL:
  301. # self.WSGIDAV_CLIENT_BASE_URL = \
  302. # '{0}:{1}'.format(
  303. # self.WEBSITE_SERVER_NAME,
  304. # self.WSGIDAV_PORT,
  305. # )
  306. # logger.warning(self,
  307. # 'NOTE: Generated wsgidav.client.base_url parameter with '
  308. # 'followings parameters: website.server_name and '
  309. # 'wsgidav.conf port'.format(
  310. # self.WSGIDAV_CLIENT_BASE_URL,
  311. # )
  312. # )
  313. #
  314. # if not self.WSGIDAV_CLIENT_BASE_URL.endswith('/'):
  315. # self.WSGIDAV_CLIENT_BASE_URL += '/'
  316. # TODO - G.M - 27-03-2018 - [Caldav] Restore radicale config
  317. ###
  318. # RADICALE (Caldav server)
  319. ###
  320. # self.RADICALE_SERVER_HOST = settings.get(
  321. # 'radicale.server.host',
  322. # '127.0.0.1',
  323. # )
  324. # self.RADICALE_SERVER_PORT = int(settings.get(
  325. # 'radicale.server.port',
  326. # 5232,
  327. # ))
  328. # # Note: Other parameters needed to work in SSL (cert file, etc)
  329. # self.RADICALE_SERVER_SSL = asbool(settings.get(
  330. # 'radicale.server.ssl',
  331. # False,
  332. # ))
  333. # self.RADICALE_SERVER_FILE_SYSTEM_FOLDER = settings.get(
  334. # 'radicale.server.filesystem.folder',
  335. # )
  336. # if not self.RADICALE_SERVER_FILE_SYSTEM_FOLDER:
  337. # raise Exception(
  338. # mandatory_msg.format('radicale.server.filesystem.folder')
  339. # )
  340. # self.RADICALE_SERVER_ALLOW_ORIGIN = settings.get(
  341. # 'radicale.server.allow_origin',
  342. # None,
  343. # )
  344. # if not self.RADICALE_SERVER_ALLOW_ORIGIN:
  345. # self.RADICALE_SERVER_ALLOW_ORIGIN = self.WEBSITE_BASE_URL
  346. # logger.warning(self,
  347. # 'NOTE: Generated radicale.server.allow_origin parameter with '
  348. # 'followings parameters: website.base_url ({0})'
  349. # .format(self.WEBSITE_BASE_URL)
  350. # )
  351. #
  352. # self.RADICALE_SERVER_REALM_MESSAGE = settings.get(
  353. # 'radicale.server.realm_message',
  354. # 'Tracim Calendar - Password Required',
  355. # )
  356. #
  357. # self.RADICALE_CLIENT_BASE_URL_HOST = settings.get(
  358. # 'radicale.client.base_url.host',
  359. # 'http://{}:{}'.format(
  360. # self.RADICALE_SERVER_HOST,
  361. # self.RADICALE_SERVER_PORT,
  362. # ),
  363. # )
  364. #
  365. # self.RADICALE_CLIENT_BASE_URL_PREFIX = settings.get(
  366. # 'radicale.client.base_url.prefix',
  367. # '/',
  368. # )
  369. # # Ensure finished by '/'
  370. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[-1]:
  371. # self.RADICALE_CLIENT_BASE_URL_PREFIX += '/'
  372. # if '/' != self.RADICALE_CLIENT_BASE_URL_PREFIX[0]:
  373. # self.RADICALE_CLIENT_BASE_URL_PREFIX \
  374. # = '/' + self.RADICALE_CLIENT_BASE_URL_PREFIX
  375. #
  376. # if not self.RADICALE_CLIENT_BASE_URL_HOST:
  377. # logger.warning(self,
  378. # 'Generated radicale.client.base_url.host parameter with '
  379. # 'followings parameters: website.server_name -> {}'
  380. # .format(self.WEBSITE_SERVER_NAME)
  381. # )
  382. # self.RADICALE_CLIENT_BASE_URL_HOST = self.WEBSITE_SERVER_NAME
  383. #
  384. # self.RADICALE_CLIENT_BASE_URL_TEMPLATE = '{}{}'.format(
  385. # self.RADICALE_CLIENT_BASE_URL_HOST,
  386. # self.RADICALE_CLIENT_BASE_URL_PREFIX,
  387. # )
  388. self.PREVIEW_JPG_RESTRICTED_DIMS = asbool(settings.get(
  389. 'preview.jpg.restricted_dims', False
  390. ))
  391. preview_jpg_allowed_dims_str = settings.get('preview.jpg.allowed_dims', '') # nopep8
  392. allowed_dims = []
  393. if preview_jpg_allowed_dims_str:
  394. for sizes in preview_jpg_allowed_dims_str.split(','):
  395. parts = sizes.split('x')
  396. assert len(parts) == 2
  397. width, height = parts
  398. assert width.isdecimal()
  399. assert height.isdecimal()
  400. size = PreviewDim(int(width), int(height))
  401. allowed_dims.append(size)
  402. if not allowed_dims:
  403. size = PreviewDim(256, 256)
  404. allowed_dims.append(size)
  405. self.PREVIEW_JPG_ALLOWED_DIMS = allowed_dims
  406. def configure_filedepot(self):
  407. depot_storage_name = self.DEPOT_STORAGE_NAME
  408. depot_storage_path = self.DEPOT_STORAGE_DIR
  409. depot_storage_settings = {'depot.storage_path': depot_storage_path}
  410. DepotManager.configure(
  411. depot_storage_name,
  412. depot_storage_settings,
  413. )
  414. class CST(object):
  415. ASYNC = 'ASYNC'
  416. SYNC = 'SYNC'
  417. TREEVIEW_FOLDERS = 'folders'
  418. TREEVIEW_ALL = 'all'
  419. class PreviewDim(object):
  420. def __init__(self, width: int, height: int) -> None:
  421. self.width = width
  422. self.height = height
  423. def __repr__(self):
  424. return "<PreviewDim width:{width} height:{height}>".format(
  425. width=self.width,
  426. height=self.height,
  427. )