def _dns_fill_record(form, prefix, record, request): for label in ('name', 'type', 'content', 'type'): setattr(record, label, form.cleaned_data[prefix + label] or None) record.domain = get_domain(record.name) if (record.type in ('A', 'AAAA') and form.cleaned_data[prefix + 'ptr']): try: created = set_revdns_record(record.content, record.name) except DNSError as e: messages.error(request, unicode(e)) else: if created: messages.warning( request, "Created a PTR DNS record for %s." % record.content)
def _dns_fill_record(form, prefix, record, request): for label in ('name', 'type', 'content', 'type'): setattr(record, label, form.cleaned_data[prefix + label] or None) record.domain = get_domain(record.name) if (record.type in ('A', 'AAAA') and form.cleaned_data[prefix + 'ptr']): try: created = set_revdns_record(record.content, record.name) except DNSError as e: messages.error(request, unicode(e)) else: if created: messages.warning( request, "Created a PTR DNS record for %s." % record.content )
def post(self, *args, **kwargs): self.object = self.get_object() profile = self.request.user.get_profile() if not profile.has_perm(self.edit_perm, self.object.venture): return HttpResponseForbidden( "You don't have permission to edit this." ) if 'dns' in self.request.POST: dns_records = self.get_dns(self.limit_types) ips = {ip.address for ip in self.object.ipaddress_set.all()} self.dns_formset = DNSFormSet( self.request.POST, queryset=dns_records, prefix='dns', hostnames=self.get_hostnames(), limit_types=self.limit_types, ips=ips, ) if self.dns_formset.is_valid(): for form in self.dns_formset.extra_forms: # Bind the newly created records to domains. if form.has_changed(): form.instance.domain = get_domain(form.instance.name) # Save the user for newly added records form.instance.saving_user = self.request.user for form in self.dns_formset.initial_forms: if form.has_changed(): # Save the user for modified records form.instance.saving_user = self.request.user # Make sure the PTR record is updated on field change if form.instance.ptr and ( 'name' in form.changed_data or 'content' in form.changed_data ): r = Record.objects.get(id=form.instance.id) for ptr in get_revdns_records( r.content).filter(content=r.name): ptr.saving_user = self.request.user ptr.delete() messages.warning( self.request, "PTR record for %s and %s deleted." % ( r.name, r.content, ), ) form.changed_data.append('ptr') self.dns_formset.save() for r, data in self.dns_formset.changed_objects: # Handle PTR creation/deletion if 'ptr' in data: if r.ptr: try: set_revdns_record(r.content, r.name) except DNSError as e: messages.error(self.request, unicode(e)) else: messages.warning( self.request, "PTR record for %s and %s created." % ( r.name, r.content, ), ) else: for ptr in get_revdns_records( r.content).filter(content=r.name): ptr.saving_user = self.request.user ptr.delete() messages.warning( self.request, "PTR record for %s and %s deleted." % ( r.name, r.content, ), ) for r in self.dns_formset.new_objects: # Handle PTR creation if r.ptr: try: set_revdns_record(r.content, r.name) except DNSError as e: messages.error(self.request, unicode(e)) else: messages.warning( self.request, "PTR record for %s created." % r.content ) for r in self.dns_formset.deleted_objects: # Handle PTR deletion for ptr in get_revdns_records( r.content).filter(content=r.name): messages.warning( self.request, "PTR record for %s deleted." % r.name ) ptr.saving_user = self.request.user ptr.delete() messages.success(self.request, "DNS records updated.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Errors in the DNS form.") elif 'dhcp' in self.request.POST: dhcp_records = self.get_dhcp() macs = {e.mac for e in self.object.ethernet_set.all()} ips = {ip.address for ip in self.object.ipaddress_set.all()} self.dhcp_formset = DHCPFormSet( dhcp_records, macs, ips, self.request.POST, prefix='dhcp', ) if self.dhcp_formset.is_valid(): self.dhcp_formset.save() messages.success(self.request, "DHCP records updated.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Errors in the DHCP form.") elif 'ip' in self.request.POST: self.ip_formset = IPAddressFormSet( self.request.POST, queryset=self.object.ipaddress_set.order_by('address'), prefix='ip', ) if self.ip_formset.is_valid(): for form in self.ip_formset.extra_forms: # Bind the newly created addresses to this device. if form.has_changed(): form.instance.device = self.object self.ip_formset.save() messages.success(self.request, "IP addresses updated.") return HttpResponseRedirect(self.request.path) else: messages.error(self.request, "Errors in the addresses form.") return self.get(*args, **kwargs)