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 10KB

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