applications.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # coding=utf-8
  2. import typing
  3. from tracim_backend.app_models.contents import ContentType
  4. if typing.TYPE_CHECKING:
  5. from tracim_backend.config import CFG
  6. from tracim_backend.app_models.contents import ContentStatus
  7. class Application(object):
  8. """
  9. Application class with data needed for frontend
  10. """
  11. def __init__(
  12. self,
  13. label: str,
  14. slug: str,
  15. fa_icon: str,
  16. is_active: bool,
  17. config: typing.Dict[str, str],
  18. main_route: str,
  19. app_config: 'CFG',
  20. ) -> None:
  21. """
  22. @param label: public label of application
  23. @param slug: identifier of application
  24. @param fa_icon: font awesome icon class
  25. @param is_active: True if application enable, False if inactive
  26. @param config: a dict with eventual application config
  27. @param main_route: the route of the frontend "home" screen of
  28. the application. For exemple, if you have an application
  29. called "calendar", the main route will be something
  30. like /#/workspace/{wid}/calendar.
  31. """
  32. self.label = label
  33. self.slug = slug
  34. self.fa_icon = fa_icon
  35. self.hexcolor = self._get_hexcolor_or_default(slug, app_config)
  36. self.is_active = is_active
  37. self.config = config
  38. self.main_route = main_route
  39. self.content_types = []
  40. # TODO - G.M - 2018-08-07 - Refactor slug coherence issue like this one.
  41. # we probably should not have 2 kind of slug
  42. @property
  43. def minislug(self):
  44. return self.slug.replace('contents/', '')
  45. def add_content_type(
  46. self,
  47. label: str,
  48. slug: str,
  49. creation_label: str,
  50. available_statuses: typing.List['ContentStatus'],
  51. slug_alias: typing.List[str] = None,
  52. allow_sub_content: bool = False,
  53. ):
  54. content_type = ContentType(
  55. slug=slug,
  56. fa_icon=self.fa_icon,
  57. label=label,
  58. hexcolor=self.hexcolor,
  59. creation_label=creation_label,
  60. available_statuses=available_statuses,
  61. slug_alias=slug_alias,
  62. allow_sub_content=allow_sub_content,
  63. )
  64. self.content_types.append(content_type)
  65. def _get_hexcolor_or_default(self, slug: str, app_config: 'CFG') -> str:
  66. assert app_config.APPS_COLORS
  67. assert 'primary' in app_config.APPS_COLORS
  68. if slug in app_config.APPS_COLORS:
  69. return app_config.APPS_COLORS[slug]
  70. return app_config.APPS_COLORS['primary']