from django import forms
from exodus.models import Node
from exodus.wllogic import free_masterip
from exodus.contrib import ReadOnlyWidget

class NodeForm(forms.ModelForm):
    #masterip = forms.IPAddressField(widget=forms.HiddenInput)
    class Meta:
       model = Node

    def clean_masterip(self):
        import pdb; pdb.set_trace() ;
        new_network = self.cleaned_data.get('network')
        #XXX: racing conditions with free_masterip and save()?
        # self.instnace.pk is only available when Node has been save before.
        if self.instance.pk is None:
            masterip = free_masterip(new_network)
        else:
            # check if network has changed
            old_network = Node.objects.get(pk=self.instance.pk).network
            if old_network == self.cleaned_data.get('network'):
                masterip = self.cleaned_data.get('masterip')
            else:
                #XXX: Need to update ipconfigurations for depending interfaces
                masterip = free_masterip(new_network) 
        raise forms.ValidationError("Foutmelding van Rick en Roland")
        return masterip        

    #def save(self, commit):
    #    # want to override this, but can't figure out how.
    #    super(NodeForm, self).save()
