105
112
return constraints
115
def message_from_form_stats(action, done, not_actionable, not_permitted):
116
"""Return a message suitable for user display from the given stats."""
117
action_name = 'The action "%s"' % action.display_bulk
118
# singular/plural messages.
120
'%s was successfully performed on %d node.',
121
'%s was successfully performed on %d nodes.'
123
not_actionable_templates = [
124
('%s could not be performed on %d node because its '
125
'state does not allow that action.'),
126
('%s could not be performed on %d nodes because their '
127
'state does not allow that action.'),
129
not_permitted_templates = [
130
('%s could not be performed on %d node because you '
131
"don't have the required permissions."),
132
('%s could not be performed on %d nodes because you '
133
"don't have the required permissions."),
136
(done, done_templates),
137
(not_actionable, not_actionable_templates),
138
(not_permitted, not_permitted_templates)]
140
for number, message_templates in number_message:
141
singular, plural = message_templates
143
message_template = singular if number == 1 else plural
144
message.append(message_template % (action_name, number))
145
# Override the action name so that only the first sentence will
146
# contain the full name of the action.
148
return ' '.join(message)
108
151
def prefetch_nodes_listing(nodes_query):
109
152
"""Prefetch any data needed to display a given query of nodes.
119
162
.prefetch_related('nodegroup__nodegroupinterface_set'))
122
class NodeListView(PaginatedListView):
165
class NodeListView(PaginatedListView, FormMixin, ProcessFormView):
124
167
context_object_name = "node_list"
168
form_class = BulkNodeActionForm
126
def get(self, request, *args, **kwargs):
170
def populate_modifiers(self, request):
127
171
self.query = request.GET.get("query")
128
172
self.query_error = None
129
173
self.sort_by = request.GET.get("sort")
130
174
self.sort_dir = request.GET.get("dir")
176
def get(self, request, *args, **kwargs):
177
"""Handle a GET request."""
178
self.populate_modifiers(request)
132
179
return super(NodeListView, self).get(request, *args, **kwargs)
181
def get_preserved_params(self):
182
"""List of GET parameters that need to be preserved by POST
185
These are sorting and search option we want a POST request to
186
preserve so that the display after a POST request is similar
187
to the display before the request."""
188
return ["dir", "query", "page", "sort"]
190
def get_preserved_query(self):
192
param: self.request.GET.get(param)
193
for param in self.get_preserved_params()
194
if self.request.GET.get(param) is not None}
195
return urlencode(params)
197
def get_next_url(self):
198
return reverse('node-list') + "?" + self.get_preserved_query()
200
def get_success_url(self):
201
return self.get_next_url()
203
def get_form_kwargs(self):
204
kwargs = super(NodeListView, self).get_form_kwargs()
205
kwargs['user'] = self.request.user
208
def post(self, request, *args, **kwargs):
209
"""Handle a POST request."""
210
self.populate_modifiers(request)
211
return super(NodeListView, self).post(request, *args, **kwargs)
213
def form_invalid(self, form):
214
"""Handle the view response when the form is invalid."""
215
self.object_list = self.get_queryset()
216
context = self.get_context_data(
217
object_list=self.object_list,
219
return self.render_to_response(context)
221
def form_valid(self, form):
222
"""Handle the view response when the form is valid."""
224
action_class = ACTIONS_DICT[form.cleaned_data['action']]
225
message = message_from_form_stats(action_class, *stats)
226
messages.info(self.request, message)
227
return super(NodeListView, self).form_valid(form)
134
229
def _compose_sort_order(self):
135
230
"""Put together a tuple describing the sort order.
199
294
def get_context_data(self, **kwargs):
200
295
context = super(NodeListView, self).get_context_data(**kwargs)
201
296
context.update(get_longpoll_context())
297
form_class = self.get_form_class()
298
form = self.get_form(form_class)
299
context["preserved_query"] = self.get_preserved_query()
300
context["form"] = form
202
301
context["input_query"] = self.query
203
302
context["input_query_error"] = self.query_error
204
303
links, classes = self._prepare_sort_links()