def device_post_save(sender, instance, raw, using, **kwargs): """A hook for creating ``HistoryChange`` entries when a device changes.""" dirty = set() for field, orig, new in _field_changes(instance, ignore={ 'last_seen', 'cached_cost', 'cached_price', 'raw', 'uptime_seconds', 'uptime_timestamp' }): dirty.add(field) HistoryChange( device=instance, field_name=field, old_value=unicode(orig), new_value=unicode(new), user=instance.saving_user, comment=instance.save_comment, plugin=instance.saving_plugin, ).save() if { 'venture', 'venture_role', 'position', 'chassis_position', 'parent', 'model' } & dirty: update_txt_records(instance)
def device_post_save(sender, instance, raw, using, **kwargs): """A hook for creating ``HistoryChange`` entries when a device changes.""" dirty = set() for field, orig, new in _field_changes(instance, ignore={ 'last_seen', 'cached_cost', 'cached_price', 'raw', 'uptime_seconds', 'uptime_timestamp'}): dirty.add(field) HistoryChange( device=instance, field_name=field, old_value=unicode(orig), new_value=unicode(new), user=instance.saving_user, comment=instance.save_comment, plugin=instance.saving_plugin, ).save() if {'venture', 'venture_role', 'position', 'chassis_position', 'parent', 'model'} & dirty: update_txt_records(instance)
def device_related_post_delete(sender, instance, using, **kwargs): """A hook for updating DNS TXT records when ipaddress is deleted.""" if instance.device: update_txt_records(instance.device)
def device_ipaddress_post_save(sender, instance, raw, using, **kwargs): """A hook for updating DNS TXT records when ipaddress is changed.""" if instance.device: update_txt_records(instance.device) if instance.dirty_fields.get('device'): update_txt_records(instance.dirty_fields.get('device'))
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, ), ) try: ipaddress = IPAddress.objects.get( address=r.content, ) except IPAddress.DoesNotExist: pass else: if ipaddress.device: update_txt_records(ipaddress.device) 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)