mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
FIX(T): 500 error editing training item qualifications
For the second time with this piece of functionality...how did that ever work?
This commit is contained in:
@@ -16,7 +16,8 @@ def validate_user_can_train_in(supervisor, item):
|
||||
|
||||
class QualificationForm(forms.ModelForm):
|
||||
related_models = {
|
||||
'item': models.TrainingItem
|
||||
'item': models.TrainingItem,
|
||||
'supervisor': models.Trainee
|
||||
}
|
||||
|
||||
class Meta:
|
||||
@@ -27,29 +28,34 @@ class QualificationForm(forms.ModelForm):
|
||||
cleaned_data = super().clean()
|
||||
item = cleaned_data.get('item')
|
||||
trainee = cleaned_data.get('trainee')
|
||||
supervisor = self.cleaned_data.get('supervisor')
|
||||
validate_user_can_train_in(supervisor, item)
|
||||
if not item.user_has_requirements(trainee):
|
||||
self.add_error('item', 'Missing prerequisites')
|
||||
|
||||
def clean_date(self):
|
||||
date = self.cleaned_data['date']
|
||||
date = self.cleaned_data.get('date')
|
||||
if date > date.today():
|
||||
raise forms.ValidationError('Qualification date may not be in the future')
|
||||
return date
|
||||
|
||||
def clean_supervisor(self):
|
||||
supervisor = self.cleaned_data['supervisor']
|
||||
item = self.cleaned_data['item']
|
||||
if supervisor.pk == self.cleaned_data['trainee'].pk:
|
||||
supervisor = self.cleaned_data.get('supervisor')
|
||||
if supervisor.pk == self.cleaned_data.get('trainee').pk:
|
||||
raise forms.ValidationError('One may not supervise oneself...')
|
||||
validate_user_can_train_in(supervisor, item)
|
||||
return supervisor
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pk = kwargs.pop('pk', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['date'].widget.format = '%Y-%m-%d'
|
||||
|
||||
|
||||
class AddQualificationForm(QualificationForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.pop('pk', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
if pk:
|
||||
self.fields['trainee'].initial = Profile.objects.get(pk=pk)
|
||||
self.fields['date'].widget.format = '%Y-%m-%d'
|
||||
|
||||
|
||||
class RequirementForm(forms.ModelForm):
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<label for="item_id" class="col col-form-label">Item</label>
|
||||
<select name="item" id="item_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" required>
|
||||
{% if item %}
|
||||
<option value="{{form.item.pk}}" selected>{{item}}</option>
|
||||
<option value="{{form.item.value}}" selected>{{item}}</option>
|
||||
{% endif %}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
@@ -103,7 +103,7 @@ class TraineeList(generic.ListView):
|
||||
class AddQualification(generic.CreateView, ModalURLMixin):
|
||||
template_name = "edit_training_record.html"
|
||||
model = models.TrainingItemQualification
|
||||
form_class = forms.QualificationForm
|
||||
form_class = forms.AddQualificationForm
|
||||
|
||||
def form_valid(self, form, *args, **kwargs):
|
||||
reversion.add_to_revision(form.cleaned_data['trainee'])
|
||||
|
||||
Reference in New Issue
Block a user