config.py 17KB

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