# Path: cyder/cydhcp/forms.py
# class RangeWizard(CyderModelForm):
#     vrf = forms.ModelChoiceField(
#         queryset=Vrf.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}),
#         label='VRF')
#     site = forms.ModelChoiceField(
#         queryset=Site.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}))
#     range = forms.ModelChoiceField(
#         queryset=Range.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}))
#     next_ip = forms.ChoiceField(
#         label='Select Free IPv4 IP?',
#         choices=(('on', 'on'), ('False', 'False')),
#         required=False,
#         widget=forms.CheckboxInput(attrs={'class': 'wizard'}))
# 
# Path: cyder/base/mixins.py
# class UsabilityFormMixin(object):
#     def append_required_all(self):
#         for fieldname, field in self.fields.items():
#             if self.fields[fieldname].required is True:
#                 if self.fields[fieldname].label is None:
#                     fname = fieldname.replace('_', ' ')
#                     self.fields[fieldname].label = fname.capitalize() + '*'
#                 else:
#                     self.fields[fieldname].label += '*'
# 
#     def alphabetize_all(self):
#         for fieldname, field in self.fields.items():
#             if hasattr(field, 'queryset'):
#                 self.fields[fieldname].queryset = field.queryset.order_by(
#                     *field.queryset.model.sort_fields)
# 
#     def filter_by_ctnr_all(self, request, skip=None):
#         from cyder.core.ctnr.models import Ctnr
#         ctnr = request.session['ctnr']
#         for fieldname, field in self.fields.items():
#             if skip and fieldname in skip:
#                 continue
# 
#             if not hasattr(field, 'queryset'):
#                 continue
# 
#             queryset = self.fields[fieldname].queryset
#             if queryset.model is Ctnr:
#                 ctnrs = set(c.pk for c in request.session['ctnrs'])
#                 for pk in [1, 2]:
#                     if pk in ctnrs:
#                         ctnrs.remove(pk)
# 
#                 if self.fields[fieldname].initial:
#                     ctnrs.add(self.fields[fieldname].initial.pk)
# 
#                 queryset = queryset.filter(pk__in=ctnrs)
#             else:
#                 queryset = filter_by_ctnr(ctnr=ctnr,
#                                           objects=field.queryset).distinct()
# 
#             if queryset.count() == 1:
#                 self.fields[fieldname].initial = queryset[0]
# 
#             self.fields[fieldname].queryset = queryset
# 
#     def autoselect_system(self):
#         System = get_model('cyder', 'system')
#         if 'system' in self.initial:
#             self.fields['system'] = ModelChoiceField(
#                 widget=HiddenInput(),
#                 empty_label='',
#                 queryset=System.objects.filter(pk=int(self.initial['system'])))
#         elif 'system' in self.fields:
#             del(self.fields['system'])
# 
#     def autoselect_ctnr(self, request):
#         if 'ctnr' not in self.fields:
#             return
# 
#         session_ctnr = request.session['ctnr']
#         if 'ctnr' not in self.initial and session_ctnr.name != "global":
#             self.fields['ctnr'].initial = session_ctnr
#             self.fields['ctnr'].widget = HiddenInput()
# 
#     def make_usable(self, request):
#         self.autoselect_system()
#         self.autoselect_ctnr(request)
#         if 'ctnr' in request.session:
#             self.filter_by_ctnr_all(request)
#         self.alphabetize_all()
#         self.append_required_all()


from django import forms
from cyder.cydns.forms import DNSForm
from cyder.cydns.ptr.models import PTR
from cyder.cydhcp.forms import RangeWizard
from cyder.base.mixins import UsabilityFormMixin



class PTRForm(DNSForm, RangeWizard, UsabilityFormMixin):

    def __init__(self, *args, **kwargs):
        super(PTRForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['fqdn', 'vrf', 'site', 'range',
                                'ip_type', 'next_ip', 'ip_str', 'views', 'ttl',
                                'description', 'ctnr']

    def delete_instance(self, instance):
        instance.delete()

    class Meta:
        model = PTR
        exclude = ('ip', 'reverse_domain', 'ip_upper',
                   'ip_lower')
        widgets = {'views': forms.CheckboxSelectMultiple,

# Path: cyder/cydhcp/forms.py
# class RangeWizard(CyderModelForm):
#     vrf = forms.ModelChoiceField(
#         queryset=Vrf.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}),
#         label='VRF')
#     site = forms.ModelChoiceField(
#         queryset=Site.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}))
#     range = forms.ModelChoiceField(
#         queryset=Range.objects.all(),
#         required=False,
#         widget=forms.Select(attrs={'class': 'wizard'}))
#     next_ip = forms.ChoiceField(
#         label='Select Free IPv4 IP?',
#         choices=(('on', 'on'), ('False', 'False')),
#         required=False,
#         widget=forms.CheckboxInput(attrs={'class': 'wizard'}))
# 
# Path: cyder/base/mixins.py
# class UsabilityFormMixin(object):
#     def append_required_all(self):
#         for fieldname, field in self.fields.items():
#             if self.fields[fieldname].required is True:
#                 if self.fields[fieldname].label is None:
#                     fname = fieldname.replace('_', ' ')
#                     self.fields[fieldname].label = fname.capitalize() + '*'
#                 else:
#                     self.fields[fieldname].label += '*'
# 
#     def alphabetize_all(self):
#         for fieldname, field in self.fields.items():
#             if hasattr(field, 'queryset'):
#                 self.fields[fieldname].queryset = field.queryset.order_by(
#                     *field.queryset.model.sort_fields)
# 
#     def filter_by_ctnr_all(self, request, skip=None):
#         from cyder.core.ctnr.models import Ctnr
#         ctnr = request.session['ctnr']
#         for fieldname, field in self.fields.items():
#             if skip and fieldname in skip:
#                 continue
# 
#             if not hasattr(field, 'queryset'):
#                 continue
# 
#             queryset = self.fields[fieldname].queryset
#             if queryset.model is Ctnr:
#                 ctnrs = set(c.pk for c in request.session['ctnrs'])
#                 for pk in [1, 2]:
#                     if pk in ctnrs:
#                         ctnrs.remove(pk)
# 
#                 if self.fields[fieldname].initial:
#                     ctnrs.add(self.fields[fieldname].initial.pk)
# 
#                 queryset = queryset.filter(pk__in=ctnrs)
#             else:
#                 queryset = filter_by_ctnr(ctnr=ctnr,
#                                           objects=field.queryset).distinct()
# 
#             if queryset.count() == 1:
#                 self.fields[fieldname].initial = queryset[0]
# 
#             self.fields[fieldname].queryset = queryset
# 
#     def autoselect_system(self):
#         System = get_model('cyder', 'system')
#         if 'system' in self.initial:
#             self.fields['system'] = ModelChoiceField(
#                 widget=HiddenInput(),
#                 empty_label='',
#                 queryset=System.objects.filter(pk=int(self.initial['system'])))
#         elif 'system' in self.fields:
#             del(self.fields['system'])
# 
#     def autoselect_ctnr(self, request):
#         if 'ctnr' not in self.fields:
#             return
# 
#         session_ctnr = request.session['ctnr']
#         if 'ctnr' not in self.initial and session_ctnr.name != "global":
#             self.fields['ctnr'].initial = session_ctnr
#             self.fields['ctnr'].widget = HiddenInput()
# 
#     def make_usable(self, request):
#         self.autoselect_system()
#         self.autoselect_ctnr(request)
#         if 'ctnr' in request.session:
#             self.filter_by_ctnr_all(request)
#         self.alphabetize_all()
#         self.append_required_all()


# Path: cyder/cydns/ptr/forms.py
from django import forms
from cyder.cydns.forms import DNSForm
from cyder.cydns.ptr.models import PTR
from cyder.cydhcp.forms import RangeWizard
from cyder.base.mixins import UsabilityFormMixin




class PTRForm(DNSForm, RangeWizard, UsabilityFormMixin):

    def __init__(self, *args, **kwargs):
        super(PTRForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['fqdn', 'vrf', 'site', 'range',
                                'ip_type', 'next_ip', 'ip_str', 'views', 'ttl',
                                'description', 'ctnr']

    def delete_instance(self, instance):
        instance.delete()

    class Meta:
        model = PTR
        exclude = ('ip', 'reverse_domain', 'ip_upper',
                   'ip_lower')
        widgets = {'views': forms.CheckboxSelectMultiple,

                   'ip_type': forms.RadioSelect}