application.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import typing
  2. from copy import copy
  3. from tracim_backend.exceptions import AppDoesNotExist
  4. from tracim_backend.app_models.workspace_menu_entries import WorkspaceMenuEntry
  5. from tracim_backend.app_models.workspace_menu_entries import dashboard_menu_entry
  6. from tracim_backend.app_models.workspace_menu_entries import all_content_menu_entry
  7. class ApplicationApi(object):
  8. def __init__(
  9. self,
  10. app_list,
  11. show_all: bool = False,
  12. ) -> None:
  13. self.apps = app_list
  14. self.show_all = show_all
  15. def get_one(self, slug):
  16. for app in self.apps:
  17. if app.slug == slug:
  18. return app
  19. raise AppDoesNotExist('Application {app} does not exist'.format(app=slug)) # nopep8
  20. def get_all(self):
  21. active_apps = []
  22. for app in self.apps:
  23. if self.show_all or app.is_active:
  24. active_apps.append(app)
  25. return active_apps
  26. def get_content_types(self):
  27. active_content_types = []
  28. for app in self.get_all():
  29. if app.content_types:
  30. for content_type in app.content_types:
  31. active_content_types.append(content_type)
  32. return active_content_types
  33. def get_default_workspace_menu_entry(
  34. self,
  35. workspace: 'Workspace',
  36. ) -> typing.List[WorkspaceMenuEntry]:
  37. """
  38. Get default menu entry for a workspace
  39. """
  40. menu_entries = [
  41. copy(dashboard_menu_entry),
  42. copy(all_content_menu_entry),
  43. ]
  44. for app in self.get_all():
  45. if app.main_route:
  46. new_entry = WorkspaceMenuEntry(
  47. slug=app.slug,
  48. label=app.label,
  49. hexcolor=app.hexcolor,
  50. fa_icon=app.fa_icon,
  51. route=app.main_route
  52. )
  53. menu_entries.append(new_entry)
  54. for entry in menu_entries:
  55. entry.route = entry.route.replace(
  56. '{workspace_id}',
  57. str(workspace.workspace_id)
  58. )
  59. return menu_entries