Debian Codegraph @

adjutant_ui/content/tasks/views.py

raw
1 # Copyright (c) 2016 Catalyst IT Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from django.urls import reverse
16 from django.utils.translation import gettext_lazy as _
17
18 from horizon import exceptions
19 from horizon import forms
20 from horizon import tabs
21
22 from horizon.utils import memoized
23
24 from adjutant_ui.api import adjutant
25 from adjutant_ui.content.tasks import forms as task_forms
26 from adjutant_ui.content.tasks import tables as task_tables
27 from adjutant_ui.content.tasks import tabs as task_tabs
28
29 import json
30
31
32 class IndexView(tabs.TabbedTableView):
33 tab_group_class = task_tabs.TaskTabs
34 template_name = 'management/tasks/index.html'
35 redirect_url = 'horizon:management:tasks:index'
36 page_title = _("Admin Tasks")
37
38
39 class TaskDetailView(tabs.TabView):
40 tab_group_class = task_tabs.TaskDetailTabs
41 template_name = 'horizon/common/_detail.html'
42 redirect_url = 'horizon:management:tasks:index'
43 page_title = "{{ task.task_type }}"
44
45 def get_context_data(self, **kwargs):
46 context = super(TaskDetailView, self).get_context_data(**kwargs)
47 task = self.get_data()
48 context["task"] = task
49 context["url"] = reverse(self.redirect_url)
50 context["actions"] = self._get_actions(
51 adjutant.task_obj_get(self.request, task=task))
52 return context
53
54 def _get_actions(self, task):
55 if task.status == 'Approved; Incomplete':
56 table = task_tables.ApprovedTaskTable(self.request)
57 else:
58 table = task_tables.TaskTable(self.request)
59 return table.render_row_actions(task)
60
61 @memoized.memoized_method
62 def get_data(self):
63 return adjutant.task_get(self.request, self.kwargs['task_id']).json()
64
65 def get_tabs(self, request, *args, **kwargs):
66 task = self.get_data()
67 return self.tab_group_class(request, task=task, **kwargs)
68
69
70 class UpdateTaskView(forms.ModalFormView):
71 form_class = task_forms.UpdateTaskForm
72 form_id = "update_user_form"
73 modal_header = _("Update Task")
74 submit_label = _("Update")
75 submit_url = 'horizon:management:tasks:update'
76 template_name = 'management/tasks/update.html'
77 context_object_name = 'project_users'
78 success_url = "horizon:management:tasks:detail"
79 page_title = _("Update Task")
80
81 def get_success_url(self):
82 return reverse(self.success_url,
83 args=(self.kwargs['task_id'],))
84
85 @memoized.memoized_method
86 def get_object(self):
87 try:
88 return adjutant.task_get(self.request,
89 self.kwargs['task_id']).json()
90 except Exception:
91 msg = _('Unable to retrieve user.')
92 url = reverse('horizon:management:tasks:index')
93 exceptions.handle(self.request, msg, redirect=url)
94
95 def get_context_data(self, **kwargs):
96 context = super(UpdateTaskView, self).get_context_data(**kwargs)
97 context['task'] = self.get_object()
98 args = (self.kwargs['task_id'],)
99 context['submit_url'] = reverse(self.submit_url, args=args)
100 return context
101
102 def get_initial(self):
103 task = self.get_object()
104 task_data = {}
105 for data in [action['data'] for action in task['actions']]:
106 task_data.update(data)
107 data = {'task_id': self.kwargs['task_id'],
108 'task_type': task['task_type'],
109 'task_data': json.dumps(task_data, indent=4),
110 }
111 return data