Django Library PaTT
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.py 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. from .access import create_project_possible, create_task_possible, acc_task
  2. from django.conf import settings
  3. from django.db.models.functions import Lower
  4. from django.utils.translation import gettext as _
  5. from .help import actionbar as actionbar_add_help
  6. import inspect
  7. import logging
  8. import mycreole
  9. import patt
  10. from .search import common_searches
  11. from themes import empty_entry_parameters, color_icon_url, gray_icon_url
  12. from users.context import menubar as menubar_users
  13. from users.context import PROFILE_ENTRY_UID
  14. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  15. ATTACHMENT_UID = 'attachment'
  16. BACK_UID = 'back'
  17. COMMENTNEW_UID = 'commentnew'
  18. CREATE_PROJECT_UID = 'create-project'
  19. CREATE_TASK_UID = 'create-task'
  20. HELP_UID = 'help'
  21. PRINTVIEW_UID = 'printview'
  22. TASKEDIT_UID = 'taskedit'
  23. VIEW_PROJECTLIST_UID = 'view-projectlist'
  24. VIEW_TASKLIST_UID = 'view-tasklist'
  25. def context_adaption(context, request, **kwargs):
  26. caller_name = inspect.currentframe().f_back.f_code.co_name
  27. try:
  28. context.set_additional_title(kwargs.pop('title'))
  29. except KeyError:
  30. pass # no title in kwargs
  31. menubar(context, request, caller_name, **kwargs)
  32. actionbar(context, request, caller_name, **kwargs)
  33. navigationbar(context, request, caller_name, **kwargs)
  34. for key in kwargs:
  35. context[key] = kwargs[key]
  36. logger.debug("context adapted: %s", repr(context))
  37. def navigationbar(context, request, caller_name, **kwargs):
  38. bar = context[context.NAVIGATIONBAR]
  39. add_back_menu(request, bar)
  40. finalise_bar(request, bar)
  41. def menubar(context, request, caller_name, **kwargs):
  42. bar = context[context.MENUBAR]
  43. menubar_users(bar, request)
  44. add_help_menu(request, bar)
  45. add_tasklist_menu(request, bar)
  46. add_filter_submenu(request, bar, VIEW_TASKLIST_UID)
  47. add_projectlist_menu(request, bar)
  48. add_printview_menu(request, bar)
  49. finalise_bar(request, bar)
  50. def actionbar(context, request, caller_name, **kwargs):
  51. bar = context[context.ACTIONBAR]
  52. if caller_name == 'patt_tasklist':
  53. if create_task_possible(request.user):
  54. add_newtask_menu(request, bar, kwargs.get('project_id'))
  55. elif caller_name == 'patt_projectlist':
  56. if create_project_possible(request.user):
  57. add_newproject_menu(request, bar)
  58. elif caller_name == 'patt_taskview':
  59. acc = acc_task(kwargs['task'], request.user)
  60. if acc.modify or acc.modify_limited:
  61. add_edittask_menu(request, bar, kwargs['task'].id)
  62. if acc.add_comments:
  63. add_newcomment_menu(request, bar, kwargs['task'].id)
  64. add_manageupload_menu(request, bar, kwargs['task'])
  65. elif caller_name == 'patt_helpview':
  66. actionbar_add_help(context, request, **kwargs)
  67. finalise_bar(request, bar)
  68. def finalise_bar(request, bar):
  69. if len(bar) == 0:
  70. bar.append_entry(*empty_entry_parameters(request))
  71. def add_help_menu(request, bar):
  72. bar.append_entry(
  73. HELP_UID, # uid
  74. _('Help'), # name
  75. color_icon_url(request, 'help.png'), # icon
  76. patt.url_helpview(request, 'main'), # url
  77. True, # left
  78. False # active
  79. )
  80. def add_tasklist_menu(request, bar):
  81. bar.append_entry(
  82. VIEW_TASKLIST_UID, # uid
  83. _('Tasklist'), # name
  84. color_icon_url(request, 'task.png'), # icon
  85. patt.url_tasklist(request), # url
  86. True, # left
  87. patt.is_tasklistview(request) # active
  88. )
  89. def add_projectlist_menu(request, bar):
  90. bar.append_entry(
  91. VIEW_PROJECTLIST_UID, # uid
  92. _('Projectlist'), # name
  93. color_icon_url(request, 'folder.png'), # icon
  94. patt.url_projectlist(request), # url
  95. True, # left
  96. patt.is_projectlistview(request) # active
  97. )
  98. def add_printview_menu(request, bar):
  99. bar.append_entry(
  100. PRINTVIEW_UID, # uid
  101. _('Printview'), # name
  102. color_icon_url(request, 'print.png'), # icon
  103. patt.url_printview(request), # url
  104. True, # left
  105. patt.is_printview(request) # active
  106. )
  107. def add_newtask_menu(request, bar, project_id):
  108. bar.append_entry(
  109. CREATE_TASK_UID, # uid
  110. _('New Task'), # name
  111. color_icon_url(request, 'plus.png'), # icon
  112. patt.url_tasknew(request, project_id), # url
  113. True, # left
  114. False # active
  115. )
  116. def add_edittask_menu(request, bar, task_id):
  117. bar.append_entry(
  118. TASKEDIT_UID, # uid
  119. _('Edit'), # name
  120. color_icon_url(request, 'edit.png'), # icon
  121. patt.url_taskedit(request, task_id), # url
  122. True, # left
  123. False # active
  124. )
  125. def add_newcomment_menu(request, bar, task_id):
  126. bar.append_entry(
  127. COMMENTNEW_UID, # uid
  128. _('Add Comment'), # name
  129. color_icon_url(request, 'edit2.png'), # icon
  130. patt.url_commentnew(request, task_id), # url
  131. True, # left
  132. False # active
  133. )
  134. def add_newproject_menu(request, bar):
  135. bar.append_entry(
  136. CREATE_PROJECT_UID, # uid
  137. _('New Project'), # name
  138. color_icon_url(request, 'plus.png'), # icon
  139. patt.url_projectnew(request), # url
  140. True, # left
  141. False # active
  142. )
  143. def add_manageupload_menu(request, bar, task):
  144. bar.append_entry(
  145. ATTACHMENT_UID, # uid
  146. _("Attachments"), # name
  147. color_icon_url(request, 'upload.png'), # icon
  148. mycreole.url_manage_uploads(request, task.attachment_target_path), # url
  149. True, # left
  150. False, # active
  151. )
  152. def add_back_menu(request, bar):
  153. bar.append_entry(
  154. BACK_UID, # uid
  155. _('Back'), # name
  156. gray_icon_url(request, 'back.png'), # icon
  157. 'javascript:history.back()', # url
  158. True, # left
  159. False # active
  160. )
  161. def add_filter_submenu(request, bar, menu_uid):
  162. bar.append_entry_to_entry(
  163. menu_uid,
  164. menu_uid + '-easysearch', # uid
  165. _('Easysearch'), # name
  166. gray_icon_url(request, 'search.png'), # icon
  167. patt.url_easysearch(request), # url
  168. True, # left
  169. False # active
  170. )
  171. if patt.get_search_query(request) is not None:
  172. bar.append_entry_to_entry(
  173. menu_uid,
  174. menu_uid + '-save', # uid
  175. _('Save Search as Filter'), # name
  176. gray_icon_url(request, 'save.png'), # icon
  177. patt.url_filteredit(request), # url
  178. True, # left
  179. False # active
  180. )
  181. bar.append_entry_to_entry(
  182. menu_uid,
  183. menu_uid + '-all', # uid
  184. _('All Tasks'), # name
  185. gray_icon_url(request, 'task.png'), # icon
  186. patt.url_tasklist(request), # url
  187. True, # left
  188. False # active
  189. )
  190. cs = common_searches(request)
  191. for common_filter_id in cs:
  192. bar.append_entry_to_entry(
  193. menu_uid,
  194. menu_uid + '-common', # uid
  195. _(cs[common_filter_id][0]), # name
  196. gray_icon_url(request, 'filter.png'), # icon
  197. patt.url_tasklist(request, common_filter_id=common_filter_id), # url
  198. True, # left
  199. False # active
  200. )
  201. for s in request.user.search_set.order_by(Lower('name')):
  202. active = patt.is_tasklistview(request, s.id)
  203. if active is True:
  204. url = patt.url_filteredit(request, s.id)
  205. else:
  206. url = patt.url_tasklist(request, user_filter_id=s.id)
  207. if active:
  208. icon = 'settings.png'
  209. else:
  210. icon = 'favourite.png'
  211. bar.append_entry_to_entry(
  212. menu_uid,
  213. menu_uid + '-sub', # uid
  214. s.name, # name
  215. gray_icon_url(request, icon), # icon
  216. url, # url
  217. True, # left
  218. active # active
  219. )