Skip to content

Commit

Permalink
WIP - Warnings and criticals added to preboot selection
Browse files Browse the repository at this point in the history
  • Loading branch information
hipek8 committed Aug 8, 2024
1 parent ab748bf commit 8afdd0c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/ralph/deployment/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import ProgrammingError
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -770,35 +769,31 @@ def assign_configuration_path(cls, instances, configuration_path, **kwargs):
instance.save()


def get_preboots():
try:
criticals = [
(p.id, "[CRITICAL!]" + p.name) for p in Preboot.active_objects
.filter(critical_after__lt=timezone.now())
]
warnings = [
(p.id, "[WARNING!]" + p.name) for p in Preboot.active_objects
.exclude(id__in=[p[0] for p in criticals])
.filter(warning_after__lt=timezone.now())
]
good = [
(p.id, p.name) for p in Preboot.active_objects
.exclude(id__in=[p[0] for p in warnings + criticals])
]
return good + warnings + criticals
except ProgrammingError:
# This is weird, but because of the time get_preboots() is called it's needed
# to make everything work
return [(p.id, p.name) for p in Preboot.objects.all()]
class PrebootChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
if obj.critical_after and obj.critical_after < timezone.now().date():
return f"[CRITICAL!]{obj.name}"
elif obj.warning_after and obj.warning_after < timezone.now().date():
return f"[WARNING!]{obj.name}"
else:
return obj.name


@deployment_action(
verbose_name=_('Apply preboot'),
form_fields={
'preboot': {
'field': forms.ChoiceField(
'field': PrebootChoiceField(
label=_('Preboot'),
choices=get_preboots()
queryset=Preboot.active_objects.order_by(
"-disappears_after",
"-critical_after",
"-warning_after",
"name",
),
widget=forms.Select(
attrs={"id": "preboot-select"}
)
),
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,23 @@ <h1>{% trans 'Tranisition' %}&nbsp;{{ transition.name|lower }} for {{ verbose_na
}
})
})(ralph.jQuery);

document.addEventListener('DOMContentLoaded', function() {
const prebootSelect = document.getElementById('preboot-select');
prebootSelect.addEventListener('change', function() {
const selectedOption = this.options[this.selectedIndex].text;
if (selectedOption.startsWith('[CRITICAL!]')) {
// TODO(HAL-2734) A proper info
alert("You selected: CRITICAL");
}
else if (selectedOption.startsWith('[WARNING!]')) {
{
// TODO(HAL-2734) A proper info
alert("You selected: WARNING");
}
}
});

});
</script>
{% endblock %}

0 comments on commit 8afdd0c

Please sign in to comment.