app_cfg.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. # -*- coding: utf-8 -*-
  2. """
  3. Global configuration file for TG2-specific settings in tracim.
  4. This file complements development/deployment.ini.
  5. Please note that **all the argument values are strings**. If you want to
  6. convert them into boolean, for example, you should use the
  7. :func:`paste.deploy.converters.asbool` function, as in::
  8. from paste.deploy.converters import asbool
  9. setting = asbool(global_conf.get('the_setting'))
  10. """
  11. import imp
  12. import importlib
  13. from urllib.parse import urlparse
  14. import tg
  15. from paste.deploy.converters import asbool
  16. from tg.configuration.milestones import environment_loaded
  17. from tgext.pluggable import plug
  18. from tgext.pluggable import replace_template
  19. from tracim.lib.utils import lazy_ugettext as l_
  20. import tracim
  21. from tracim import model
  22. from tracim.config import TracimAppConfig
  23. from tracim.lib.base import logger
  24. from tracim.lib.daemons import DaemonsManager
  25. from tracim.lib.daemons import MailSenderDaemon
  26. from tracim.lib.daemons import RadicaleDaemon
  27. from tracim.lib.daemons import WsgiDavDaemon
  28. from tracim.model.data import ActionDescription
  29. from tracim.model.data import ContentType
  30. base_config = TracimAppConfig()
  31. base_config.renderers = []
  32. base_config.use_toscawidgets = False
  33. base_config.use_toscawidgets2 = True
  34. base_config.package = tracim
  35. #Enable json in expose
  36. base_config.renderers.append('json')
  37. #Enable genshi in expose to have a lingua franca for extensions and pluggable apps
  38. #you can remove this if you don't plan to use it.
  39. base_config.renderers.append('genshi')
  40. #Set the default renderer
  41. base_config.default_renderer = 'mako'
  42. base_config.renderers.append('mako')
  43. #Configure the base SQLALchemy Setup
  44. base_config.use_sqlalchemy = True
  45. base_config.model = tracim.model
  46. base_config.DBSession = tracim.model.DBSession
  47. # This value can be modified by tracim.lib.auth.wrapper.AuthConfigWrapper but have to be specified before
  48. base_config.auth_backend = 'sqlalchemy'
  49. # base_config.flash.cookie_name
  50. # base_config.flash.default_status -> Default message status if not specified (ok by default)
  51. base_config['flash.template'] = '''
  52. <div class="alert alert-${status}" style="margin-top: 1em;">
  53. <button type="button" class="close" data-dismiss="alert">&times;</button>
  54. <div id="${container_id}">
  55. <img src="/assets/icons/32x32/status/flash-${status}.png"/>
  56. ${message}
  57. </div>
  58. </div>
  59. '''
  60. # -> string.Template instance used as the flash template when rendered from server side, will receive $container_id, $message and $status variables.
  61. # flash.js_call -> javascript code which will be run when displaying the flash from javascript. Default is webflash.render(), you can use webflash.payload() to retrieve the message and show it with your favourite library.
  62. # flash.js_template -> string.Template instance used to replace full javascript support for flash messages. When rendering flash message for javascript usage the following code will be used instead of providing the standard webflash object. If you replace js_template you must also ensure cookie parsing and delete it for already displayed messages. The template will receive: $container_id, $cookie_name, $js_call variables.
  63. base_config['templating.genshi.name_constant_patch'] = True
  64. # Configure the authentication backend
  65. # YOU MUST CHANGE THIS VALUE IN PRODUCTION TO SECURE YOUR APP
  66. base_config.sa_auth.cookie_secret = "3283411b-1904-4554-b0e1-883863b53080"
  67. # INFO - This is the way to specialize the resetpassword email properties
  68. # plug(base_config, 'resetpassword', None, mail_subject=reset_password_email_subject)
  69. plug(base_config, 'resetpassword', 'reset_password')
  70. replace_template(base_config, 'resetpassword.templates.index', 'tracim.templates.reset_password_index')
  71. replace_template(base_config, 'resetpassword.templates.change_password', 'mako:tracim.templates.reset_password_change_password')
  72. daemons = DaemonsManager()
  73. def start_daemons(manager: DaemonsManager):
  74. """
  75. Sart Tracim daemons
  76. """
  77. from tg import config
  78. # Don't start daemons if they are disabled
  79. if 'disable_daemons' in config and config['disable_daemons']:
  80. return
  81. manager.run('radicale', RadicaleDaemon)
  82. manager.run('webdav', WsgiDavDaemon)
  83. manager.run('mail_sender', MailSenderDaemon)
  84. environment_loaded.register(lambda: start_daemons(daemons))
  85. # Note: here are fake translatable strings that allow to translate messages for reset password email content
  86. duplicated_email_subject = l_('Password reset request')
  87. duplicated_email_body = l_('''
  88. We've received a request to reset the password for this account.
  89. Please click this link to reset your password:
  90. %(password_reset_link)s
  91. If you no longer wish to make the above change, or if you did not initiate this request, please disregard and/or delete this e-mail.
  92. ''')
  93. #######
  94. #
  95. # INFO - D.A. - 2014-10-31
  96. # The following code is a dirty way to integrate translation for resetpassword tgapp in tracim
  97. # TODO - Integrate these translations into tgapp-resetpassword
  98. #
  99. l_('New password')
  100. l_('Confirm new password')
  101. l_('Save new password')
  102. l_('Email address')
  103. l_('Send Request')
  104. l_('Password reset request sent')
  105. l_('Invalid password reset request')
  106. l_('Password reset request timed out')
  107. l_('Invalid password reset request')
  108. l_('Password changed successfully')
  109. l_('''
  110. We've received a request to reset the password for this account.
  111. Please click this link to reset your password:
  112. %(password_reset_link)s
  113. If you no longer wish to make the above change, or if you did not initiate this request, please disregard and/or delete this e-mail.
  114. ''')
  115. class CFG(object):
  116. """
  117. Singleton used for easy access to config file parameters
  118. """
  119. _instance = None
  120. @classmethod
  121. def get_instance(cls) -> 'CFG':
  122. if not CFG._instance:
  123. CFG._instance = CFG()
  124. return CFG._instance
  125. def __setattr__(self, key, value):
  126. """
  127. Log-ready setter. this is used for logging configuration (every parameter except password)
  128. :param key:
  129. :param value:
  130. :return:
  131. """
  132. if 'PASSWORD' not in key and 'URL' not in key and 'CONTENT' not in key:
  133. # We do not show PASSWORD for security reason
  134. # we do not show URL because the associated config uses tg.lurl() which is evaluated when at display time.
  135. # At the time of configuration setup, it can't be evaluated
  136. # We do not show CONTENT in order not to pollute log files
  137. logger.info(self, 'CONFIG: [ {} | {} ]'.format(key, value))
  138. else:
  139. logger.info(self, 'CONFIG: [ {} | <value not shown> ]'.format(key))
  140. self.__dict__[key] = value
  141. def __init__(self):
  142. self.DATA_UPDATE_ALLOWED_DURATION = int(tg.config.get('content.update.allowed.duration', 0))
  143. self.WEBSITE_TITLE = tg.config.get('website.title', 'TRACIM')
  144. self.WEBSITE_HOME_TITLE_COLOR = tg.config.get('website.title.color', '#555')
  145. self.WEBSITE_HOME_IMAGE_URL = tg.lurl('/assets/img/home_illustration.jpg')
  146. self.WEBSITE_HOME_BACKGROUND_IMAGE_URL = tg.lurl('/assets/img/bg.jpg')
  147. self.WEBSITE_BASE_URL = tg.config.get('website.base_url', '')
  148. self.WEBSITE_SERVER_NAME = tg.config.get('website.server_name', None)
  149. if not self.WEBSITE_SERVER_NAME:
  150. self.WEBSITE_SERVER_NAME = urlparse(self.WEBSITE_BASE_URL).hostname
  151. logger.warning(
  152. self,
  153. 'NOTE: Generated website.server_name parameter from '
  154. 'website.base_url parameter -> {0}'
  155. .format(self.WEBSITE_SERVER_NAME)
  156. )
  157. self.WEBSITE_HOME_TAG_LINE = tg.config.get('website.home.tag_line', '')
  158. self.WEBSITE_SUBTITLE = tg.config.get('website.home.subtitle', '')
  159. self.WEBSITE_HOME_BELOW_LOGIN_FORM = tg.config.get('website.home.below_login_form', '')
  160. if tg.config.get('email.notification.from'):
  161. raise Exception(
  162. 'email.notification.from configuration is deprecated. '
  163. 'Use instead email.notification.from.email and '
  164. 'email.notification.from.default_label.'
  165. )
  166. self.EMAIL_NOTIFICATION_FROM_EMAIL = \
  167. tg.config.get('email.notification.from.email')
  168. self.EMAIL_NOTIFICATION_FROM_DEFAULT_LABEL = \
  169. tg.config.get('email.notification.from.default_label')
  170. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_HTML = tg.config.get('email.notification.content_update.template.html')
  171. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_TEMPLATE_TEXT = tg.config.get('email.notification.content_update.template.text')
  172. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_HTML = tg.config.get(
  173. 'email.notification.created_account.template.html',
  174. './tracim/templates/mail/created_account_body_html.mak',
  175. )
  176. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_TEMPLATE_TEXT = tg.config.get(
  177. 'email.notification.created_account.template.text',
  178. './tracim/templates/mail/created_account_body_text.mak',
  179. )
  180. self.EMAIL_NOTIFICATION_CONTENT_UPDATE_SUBJECT = tg.config.get('email.notification.content_update.subject')
  181. self.EMAIL_NOTIFICATION_CREATED_ACCOUNT_SUBJECT = tg.config.get(
  182. 'email.notification.created_account.subject',
  183. '[{website_title}] Created account',
  184. )
  185. self.EMAIL_NOTIFICATION_PROCESSING_MODE = tg.config.get('email.notification.processing_mode')
  186. self.EMAIL_NOTIFICATION_ACTIVATED = asbool(tg.config.get('email.notification.activated'))
  187. self.EMAIL_NOTIFICATION_SMTP_SERVER = tg.config.get('email.notification.smtp.server')
  188. self.EMAIL_NOTIFICATION_SMTP_PORT = tg.config.get('email.notification.smtp.port')
  189. self.EMAIL_NOTIFICATION_SMTP_USER = tg.config.get('email.notification.smtp.user')
  190. self.EMAIL_NOTIFICATION_SMTP_PASSWORD = tg.config.get('email.notification.smtp.password')
  191. self.TRACKER_JS_PATH = tg.config.get('js_tracker_path')
  192. self.TRACKER_JS_CONTENT = self.get_tracker_js_content(self.TRACKER_JS_PATH)
  193. self.WEBSITE_TREEVIEW_CONTENT = tg.config.get('website.treeview.content')
  194. self.EMAIL_NOTIFICATION_NOTIFIED_EVENTS = [
  195. ActionDescription.COMMENT,
  196. ActionDescription.CREATION,
  197. ActionDescription.EDITION,
  198. ActionDescription.REVISION,
  199. ActionDescription.STATUS_UPDATE
  200. ]
  201. self.EMAIL_NOTIFICATION_NOTIFIED_CONTENTS = [
  202. ContentType.Page,
  203. ContentType.Thread,
  204. ContentType.File,
  205. ContentType.Comment,
  206. # ContentType.Folder -- Folder is skipped
  207. ]
  208. self.RADICALE_SERVER_HOST = tg.config.get('radicale.server.host', '0.0.0.0')
  209. self.RADICALE_SERVER_PORT = int(
  210. tg.config.get('radicale.server.port', 5232)
  211. )
  212. # Note: Other parameters needed to work in SSL (cert file, etc)
  213. self.RADICALE_SERVER_SSL = asbool(tg.config.get('radicale.server.ssl', False))
  214. self.RADICALE_SERVER_FILE_SYSTEM_FOLDER = tg.config.get(
  215. 'radicale.server.filesystem.folder',
  216. '~/.config/radicale/collections'
  217. )
  218. self.RADICALE_SERVER_ALLOW_ORIGIN = tg.config.get(
  219. 'radicale.server.allow_origin',
  220. None,
  221. )
  222. if not self.RADICALE_SERVER_ALLOW_ORIGIN:
  223. self.RADICALE_SERVER_ALLOW_ORIGIN = self.WEBSITE_BASE_URL
  224. logger.warning(
  225. self,
  226. 'NOTE: Generated radicale.server.allow_origin parameter with '
  227. 'followings parameters: website.base_url ({0})'
  228. .format(self.WEBSITE_BASE_URL)
  229. )
  230. self.RADICALE_SERVER_REALM_MESSAGE = tg.config.get(
  231. 'radicale.server.realm_message',
  232. 'Tracim Calendar - Password Required',
  233. )
  234. self.RADICALE_CLIENT_BASE_URL_TEMPLATE = \
  235. tg.config.get('radicale.client.base_url', None)
  236. if not self.RADICALE_CLIENT_BASE_URL_TEMPLATE:
  237. self.RADICALE_CLIENT_BASE_URL_TEMPLATE = \
  238. 'http://{0}:{1}'.format(
  239. self.WEBSITE_SERVER_NAME,
  240. self.RADICALE_SERVER_PORT,
  241. )
  242. logger.warning(
  243. self,
  244. 'NOTE: Generated radicale.client.base_url parameter with '
  245. 'followings parameters: website.server_name, '
  246. 'radicale.server.port -> {0}'
  247. .format(self.RADICALE_CLIENT_BASE_URL_TEMPLATE)
  248. )
  249. self.USER_AUTH_TOKEN_VALIDITY = int(tg.config.get(
  250. 'user.auth_token.validity',
  251. '604800',
  252. ))
  253. self.WSGIDAV_CONFIG_PATH = tg.config.get(
  254. 'wsgidav.config_path',
  255. 'wsgidav.conf',
  256. )
  257. # TODO: Convert to importlib (cf http://stackoverflow.com/questions/41063938/use-importlib-instead-imp-for-non-py-file)
  258. self.wsgidav_config = imp.load_source(
  259. 'wsgidav_config',
  260. self.WSGIDAV_CONFIG_PATH,
  261. )
  262. self.WSGIDAV_PORT = self.wsgidav_config.port
  263. self.WSGIDAV_CLIENT_BASE_URL = \
  264. tg.config.get('wsgidav.client.base_url', None)
  265. if not self.WSGIDAV_CLIENT_BASE_URL:
  266. self.WSGIDAV_CLIENT_BASE_URL = \
  267. '{0}:{1}'.format(
  268. self.WEBSITE_SERVER_NAME,
  269. self.WSGIDAV_PORT,
  270. )
  271. logger.warning(
  272. self,
  273. 'NOTE: Generated wsgidav.client.base_url parameter with '
  274. 'followings parameters: website.server_name and '
  275. 'wsgidav.conf port'.format(
  276. self.WSGIDAV_CLIENT_BASE_URL,
  277. )
  278. )
  279. if not self.WSGIDAV_CLIENT_BASE_URL.endswith('/'):
  280. self.WSGIDAV_CLIENT_BASE_URL += '/'
  281. self.EMAIL_PROCESSING_MODE = tg.config.get(
  282. 'email.processing_mode',
  283. 'sync',
  284. ).upper()
  285. if self.EMAIL_PROCESSING_MODE not in (
  286. self.CST.ASYNC,
  287. self.CST.SYNC,
  288. ):
  289. raise Exception(
  290. 'email.processing_mode '
  291. 'can ''be "{}" or "{}", not "{}"'.format(
  292. self.CST.ASYNC,
  293. self.CST.SYNC,
  294. self.EMAIL_PROCESSING_MODE,
  295. )
  296. )
  297. self.EMAIL_SENDER_REDIS_HOST = tg.config.get(
  298. 'email.async.redis.host',
  299. 'localhost',
  300. )
  301. self.EMAIL_SENDER_REDIS_PORT = int(tg.config.get(
  302. 'email.async.redis.port',
  303. 6379,
  304. ))
  305. self.EMAIL_SENDER_REDIS_DB = int(tg.config.get(
  306. 'email.async.redis.db',
  307. 0,
  308. ))
  309. def get_tracker_js_content(self, js_tracker_file_path = None):
  310. js_tracker_file_path = tg.config.get('js_tracker_path', None)
  311. if js_tracker_file_path:
  312. logger.info(self, 'Reading JS tracking code from file {}'.format(js_tracker_file_path))
  313. with open (js_tracker_file_path, 'r') as js_file:
  314. data = js_file.read()
  315. return data
  316. else:
  317. return ''
  318. class CST(object):
  319. ASYNC = 'ASYNC'
  320. SYNC = 'SYNC'
  321. TREEVIEW_FOLDERS = 'folders'
  322. TREEVIEW_ALL = 'all'
  323. #######
  324. #
  325. # INFO - D.A. - 2014-11-05
  326. # Allow to process asynchronous tasks
  327. # This is used for email notifications
  328. #
  329. # import tgext.asyncjob
  330. # tgext.asyncjob.plugme(base_config)
  331. #
  332. # OR
  333. #
  334. # plug(base_config, 'tgext.asyncjob', app_globals=base_config)
  335. #
  336. # OR
  337. #
  338. # Add some variable to each templates
  339. base_config.variable_provider = lambda: {
  340. 'CFG': CFG.get_instance()
  341. }