2017-02-23 22:28:06 +01:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
#from crispy_forms.helper import FormHelper
|
|
|
|
#from crispy_forms.layout import Submit, Layout
|
|
|
|
#from django.urls import reverse
|
|
|
|
|
2017-02-28 19:44:15 +01:00
|
|
|
from .models import Maintainer, Contact, InetNum
|
|
|
|
|
|
|
|
class WhoisObjectMixin(object):
|
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
|
|
super(WhoisObjectMixin, self).__init__(*args, **kwargs)
|
|
|
|
self._user = user
|
|
|
|
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
if instance and instance.pk:
|
|
|
|
self.fields['handle'].widget.attrs['readonly'] = True
|
|
|
|
|
|
|
|
def clean_handle(self):
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
if instance and instance.pk:
|
|
|
|
return instance.handle
|
|
|
|
else:
|
|
|
|
return self.cleaned_data['handle']
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(WhoisObjectMixin, self).clean()
|
|
|
|
if cleaned_data.get("handle") == "AUTO":
|
|
|
|
cleaned_data['handle'] = self._meta.model.genGenericHandle(cleaned_data.get("name"))
|
2017-02-23 22:28:06 +01:00
|
|
|
|
|
|
|
class MntForm(forms.ModelForm):
|
2017-02-28 19:44:15 +01:00
|
|
|
class Meta:
|
|
|
|
model = Maintainer
|
|
|
|
fields = ['handle', 'description', 'admin_c']
|
|
|
|
|
|
|
|
def __init__(self, user, *args, **kwargs):
|
|
|
|
super(MntForm, self).__init__(*args, **kwargs)
|
|
|
|
self._user = user
|
|
|
|
if 'admin_c' in self.fields:
|
|
|
|
self.fields['admin_c'].queryset = Contact.objects.filter(mnt_by=user.maintainer_set.all())
|
|
|
|
|
|
|
|
class MntInitialForm(MntForm):
|
2017-02-23 22:28:06 +01:00
|
|
|
class Meta:
|
|
|
|
model = Maintainer
|
|
|
|
fields = ['handle', 'description']
|
|
|
|
|
2017-02-28 19:44:15 +01:00
|
|
|
class ContactForm(WhoisObjectMixin, forms.ModelForm):
|
2017-02-23 22:28:06 +01:00
|
|
|
class Meta:
|
|
|
|
model = Contact
|
2017-02-28 19:44:15 +01:00
|
|
|
fields = ['type', 'handle', 'name', 'mnt_by']
|
2017-02-23 22:28:06 +01:00
|
|
|
|
2017-02-28 19:44:15 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-02-23 22:28:06 +01:00
|
|
|
super(ContactForm, self).__init__(*args, **kwargs)
|
2017-02-28 19:44:15 +01:00
|
|
|
|
|
|
|
class ContactInitialForm(ContactForm):
|
|
|
|
class Meta:
|
|
|
|
model = Contact
|
|
|
|
fields = ['handle', 'name']
|
|
|
|
|
|
|
|
class InetNumForm(WhoisObjectMixin, forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = InetNum
|
|
|
|
fields = ['protocol', 'parent_range', 'address', 'description', 'mnt_lower']
|
|
|
|
|
|
|
|
def __init__(self, lower=False, *args, **kwargs):
|
|
|
|
super(MntForm, self).__init__(*args, **kwargs)
|
|
|
|
self._editLower = lower
|
|
|
|
if 'admin_c' in self.fields:
|
|
|
|
self.fields['admin_c'].queryset = Contact.objects.filter(mnt_by=self.user.maintainer_set.all())
|
|
|
|
|
|
|
|
if self._editLower:
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# FIXME: Reset certain field sto instance:
|
|
|
|
pass
|