Commit 00ca44b2 authored by Sergios Aftsidis's avatar Sergios Aftsidis
Browse files

Merge pull request #14 from REANNZ/instserver-m2m-feb24

InstServer M2M: fix instid handling in server edit
parents fec0b3d5 fa23dd2c
......@@ -62,12 +62,13 @@ class InstServerForm(forms.ModelForm):
class Meta:
model = InstServer
exclude = ['instid']
def clean_ertype(self):
ertype = self.cleaned_data['ertype']
if not ertype:
raise forms.ValidationError('This field is required.')
for institution in self.cleaned_data['instid']:
for institution in self.inst_list:
inst_type = institution.ertype
type_list = [inst_type]
if inst_type == 3:
......
......@@ -562,10 +562,6 @@ def add_server(request, server_pk):
'You have no rights to edit this server'
)
return HttpResponseRedirect(reverse("servers"))
form.fields['instid'] = forms.ModelChoiceField(
queryset=Institution.objects.filter(pk=inst.pk),
empty_label=None
)
if server:
edit = True
......@@ -582,8 +578,10 @@ def add_server(request, server_pk):
try:
server = InstServer.objects.get(instid=inst, pk=server_pk)
form = InstServerForm(request_data, instance=server)
form.inst_list = server.instid.all()
except InstServer.DoesNotExist:
form = InstServerForm(request_data)
form.inst_list = [inst]
if server_pk:
messages.add_message(
request,
......@@ -594,12 +592,9 @@ def add_server(request, server_pk):
if form.is_valid():
form.save()
if not inst in form.instance.instid.all():
form.instance.instid.add(inst)
return HttpResponseRedirect(reverse("servers"))
else:
form.fields['instid'] = forms.ModelChoiceField(
queryset=Institution.objects.filter(pk=inst.pk),
empty_label=None
)
if server:
edit = True
return render_to_response(
......
Markdown is supported
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