Commit 137d0724 authored by BERJON Matthieu's avatar BERJON Matthieu
Browse files

Overriding the form_valid method to validate the open-bar runner



I overrode the `form_valid` of the `RunnerCreate` class. I checked if
the `open_bar` form field is true and the user is a super user and set
the model field to true. If the user isn't a super user but the
`open_bar` field is to true then I set the model field to False and send
an error message to the template.

I updated as well the template to show in the list of runner when one is
open bar.

Signed-off-by: BERJON Matthieu's avatarMatthieu Berjon <matthieu.berjon@inria.fr>
parent fc8e3ae5
......@@ -522,8 +522,26 @@ class RunnerCreate(SuccessMessageMixin, LoginRequiredMixin, CreateView):
success_url = reverse_lazy('main:runner_list')
template_name = 'runner_add_update.html'
def form_valid(self, form):
""" Validate some fields before saving them."""
obj = form.save(commit=False)
# If the open-bar argument is true and the user is a superuser.
# We setup the field as True
if form.cleaned_data.get('open_bar') and self.request.user.is_superuser:
obj.open_bar = True
# If the open-bar argument is true but the user isn't
# We send an error message and force the field to False
if form.cleaned_data.get('open_bar') and not self.request.user.is_superuser:
obj.open_bar = False
messages.add_message(self.request, messages.ERROR, 'You don\'t have sufficient privileges to create an open bar runner.')
obj.save()
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(RunnerCreate, self).get_form_kwargs()
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
......
......@@ -35,9 +35,13 @@
<td>{{ runner.cpu_count }}</td>
<td>{{ runner.mem_in_GB }}</td>
<td>
{% for app in runner.webapps.all %}
{{ app.name }}{% if not forloop.last %}, {% endif %}
{% endfor %}
{% if runner.open_bar %}
open-bar
{% else %}
{% for app in runner.webapps.all %}
{{ app.name }}{% if not forloop.last %}, {% endif %}
{% endfor %}
{% endif %}
</td>
<td><a href="{% url 'main:runner_update' runner.id %}"><i class="fas fa-pencil-alt"></i></a></td>
<td><a class="btn btn-danger" href="{% url 'main:runner_delete' runner.id %}"><i class="fas fa-trash-alt"></i></a></td>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment