Commits

George Notaras committed 74dcfd3

Added custom model form for the Domain model. Basic input validation has been implemented.

  • Participants
  • Parent commits 72972e5

Comments (0)

Files changed (2)

src/powerdns_manager/admin.py

 from django.utils.crypto import get_random_string
 
 from powerdns_manager import settings
+from powerdns_manager.forms import DomainModelForm
 from powerdns_manager.forms import SoaRecordModelForm
 from powerdns_manager.forms import NsRecordModelForm
 from powerdns_manager.forms import MxRecordModelForm
 
 
 class DomainAdmin(admin.ModelAdmin):
-    #form = DomainModelForm
-    #actions = [test_action, ]
-    
+    form = DomainModelForm
     fields = ('date_modified', 'name', 'type', 'master')
     readonly_fields = ('date_modified', )
     list_display = ('name', 'export_zone_html_link', 'type', 'master', 'date_modified')

src/powerdns_manager/forms.py

 
 
 
+class DomainModelForm(forms.ModelForm):
+    """Base ModelForm for zone instances.
+    
+    """
+    class Meta:
+        model = cache.get_model('powerdns_manager', 'Domain')
+        
+    def clean_name(self):
+        name = self.cleaned_data.get('name')
+        validate_hostname(name, supports_cidr_notation=True)
+        return name
+    
+    def clean_master(self):
+        """Cleans the 'master' field.
+        
+        Expected value is a comma-delimited list of nameservers.
+        The list may contain hostnames and IP addresses.
+        
+        TODO: This field is valid only for slave zones
+        
+        """
+        master = self.cleaned_data.get('master')
+        master_servers = [m.strip() for m in master.split(',') if m.strip()]
+        for master_server in master_servers:
+            validate_hostname(master_server, reject_ip=False)
+        return master
+
+
 class BaseRecordModelForm(forms.ModelForm):
     """Base ModelForm for Record instances.