mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-16 21:12:13 +00:00
FEAT(T): Add ability to log items at various depths during a session
Also fixes inability to search by reference number
This commit is contained in:
@@ -71,8 +71,9 @@ class RequirementForm(forms.ModelForm):
|
||||
|
||||
class SessionLogForm(forms.Form):
|
||||
trainees = forms.ModelMultipleChoiceField(models.Trainee.objects.all())
|
||||
items = forms.ModelMultipleChoiceField(models.TrainingItem.objects.all())
|
||||
depth = forms.ChoiceField(choices=models.TrainingItemQualification.CHOICES)
|
||||
items_0 = forms.ModelMultipleChoiceField(models.TrainingItem.objects.all(), required=False)
|
||||
items_1 = forms.ModelMultipleChoiceField(models.TrainingItem.objects.all(), required=False)
|
||||
items_2 = forms.ModelMultipleChoiceField(models.TrainingItem.objects.all(), required=False)
|
||||
supervisor = forms.ModelChoiceField(models.Trainee.objects.all())
|
||||
date = forms.DateField(initial=datetime.date.today)
|
||||
notes = forms.CharField(required=False, widget=forms.Textarea)
|
||||
@@ -88,6 +89,7 @@ class SessionLogForm(forms.Form):
|
||||
supervisor = self.cleaned_data['supervisor']
|
||||
if supervisor in self.cleaned_data.get('trainees', []):
|
||||
raise forms.ValidationError('One may not supervise oneself...')
|
||||
for item in self.cleaned_data.get('items', []):
|
||||
validate_user_can_train_in(supervisor, item)
|
||||
for depth in models.TrainingItemQualification.CHOICES:
|
||||
for item in self.cleaned_data.get('items_{depth.0}', []):
|
||||
validate_user_can_train_in(supervisor, item)
|
||||
return supervisor
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
{% load static %}
|
||||
{% load button from filters %}
|
||||
{% load colour_from_depth from tags %}
|
||||
|
||||
{% block css %}
|
||||
{{ block.super }}
|
||||
@@ -36,18 +37,16 @@
|
||||
</select>
|
||||
</div>
|
||||
<h3>Training Items</h3>
|
||||
<div class="row px-3">
|
||||
<div class="form-group">
|
||||
<label for="selectpicker">Training Items</label>
|
||||
<select multiple name="items" id="items_id" class="selectpicker col-12 px-0" data-live-search="true" data-sourceurl="{% url 'api_secure' model='training_item' %}?fields=description,reference_number&filters=active">
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group pl-3">
|
||||
{% include 'partials/form_field.html' with field=form.depth %}
|
||||
</div>
|
||||
<div class="form-group pl-3">
|
||||
{% include 'partials/form_field.html' with field=form.date %}
|
||||
</div>
|
||||
{% for depth in depths %}
|
||||
<div class="form-group row">
|
||||
<label for="selectpicker" class="col-sm-2 rounded bg-{% colour_from_depth depth.0 %} text-center py-1">{{ depth.1 }} Items</label>
|
||||
<select multiple name="items_{{depth.0}}" id="items_{{depth.0}}_id" class="selectpicker col-sm-10 px-0" data-live-search="true" data-sourceurl="{% url 'api_secure' model='training_item' %}?fields=display_id,description&filters=active">
|
||||
</select>
|
||||
</div>
|
||||
{% endfor %}
|
||||
<h3>Session Information</h3>
|
||||
<div class="form-group">
|
||||
{% include 'partials/form_field.html' with field=form.date %}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
{% include 'partials/form_field.html' with field=form.notes %}
|
||||
|
||||
@@ -219,18 +219,20 @@ class SessionLog(generic.FormView):
|
||||
success_url = reverse_lazy('trainee_list')
|
||||
|
||||
def form_valid(self, form, *args, **kwargs):
|
||||
for trainee in form.cleaned_data.get('trainees'):
|
||||
for item in form.cleaned_data.get('items'):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
models.TrainingItemQualification.objects.create(trainee=trainee, item=item, supervisor=form.cleaned_data.get('supervisor'), depth=form.cleaned_data.get('depth'), notes=form.cleaned_data.get('notes'), date=form.cleaned_data.get('date'))
|
||||
reversion.add_to_revision(trainee)
|
||||
except IntegrityError:
|
||||
pass # There was an attempt to create a duplicate qualification, ignore it
|
||||
for trainee in form.cleaned_data.get('trainees', []):
|
||||
for depth in models.TrainingItemQualification.CHOICES:
|
||||
for item in form.cleaned_data.get(f'items_{depth[0]}', []):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
models.TrainingItemQualification.objects.create(trainee=trainee, item=item, supervisor=form.cleaned_data.get('supervisor'), depth=depth[0], notes=form.cleaned_data.get('notes'), date=form.cleaned_data.get('date'))
|
||||
reversion.add_to_revision(trainee)
|
||||
except IntegrityError:
|
||||
pass # There was an attempt to create a duplicate qualification, ignore it
|
||||
return super().form_valid(form, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["depths"] = models.TrainingItemQualification.CHOICES
|
||||
context["page_title"] = "Log Training Session"
|
||||
get_related(context['form'], context)
|
||||
return context
|
||||
|
||||
Reference in New Issue
Block a user