config.py 15KB

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