def netbox_do_save(form): """Save netbox. Netboxgroups needs to be set manually because of database structure, thus we do a commit=False save first. """ netbox = form.save(commit=False) # Prevents saving m2m relationships netbox.save() # Save the function field function = form.cleaned_data['function'] if function: try: func = NetboxInfo.objects.get(netbox=netbox, variable='function') except NetboxInfo.DoesNotExist: func = NetboxInfo(netbox=netbox, variable='function', value=function) else: func.value = function func.save() # Save the groups netboxgroups = form.cleaned_data['groups'] NetboxCategory.objects.filter(netbox=netbox).delete() for netboxgroup in netboxgroups: NetboxCategory.objects.create(netbox=netbox, category=netboxgroup) return netbox
def netbox_do_save(form, serial_form, group_form): """Save netbox""" clean_data = form.cleaned_data primary_key = clean_data.get('id') data = { 'ip': clean_data['ip'], 'sysname': clean_data['sysname'], 'room': clean_data['room'], 'category': clean_data['category'], 'organization': clean_data['organization'], 'read_only': clean_data['read_only'], 'read_write': clean_data['read_write'], 'snmp_version': clean_data['snmp_version'], 'up_to_date': False, } serial = serial_form.cleaned_data['serial'] if serial: device, _ = Device.objects.get_or_create(serial=serial) data['device'] = device elif not primary_key: device = Device.objects.create(serial=None) data['device'] = device if 'type' in clean_data and clean_data['type']: data['type'] = NetboxType.objects.get(pk=clean_data['type']) if primary_key: netbox = Netbox.objects.get(pk=primary_key) for key in data: setattr(netbox, key, data[key]) else: netbox = Netbox(**data) netbox.save() function = serial_form.cleaned_data['function'] if function: try: func = NetboxInfo.objects.get(netbox=netbox, variable='function') except NetboxInfo.DoesNotExist: func = NetboxInfo( netbox=netbox, variable='function', value=function ) else: func.value = function func.save() if group_form: netboxgroups = group_form.cleaned_data['netboxgroups'] NetboxCategory.objects.filter(netbox=netbox).delete() for netboxgroup in netboxgroups: NetboxCategory.objects.create(netbox=netbox, category=netboxgroup) return netbox
def netbox_do_save(form, serial_form, group_form): """Save netbox""" clean_data = form.cleaned_data primary_key = clean_data.get('id') data = { 'ip': clean_data['ip'], 'sysname': clean_data['sysname'], 'room': clean_data['room'], 'category': clean_data['category'], 'organization': clean_data['organization'], 'read_only': clean_data['read_only'], 'read_write': clean_data['read_write'], 'snmp_version': clean_data['snmp_version'], 'up_to_date': False, } serial = serial_form.cleaned_data['serial'] if serial: device, _ = Device.objects.get_or_create(serial=serial) data['device'] = device elif not primary_key: device = Device.objects.create(serial=None) data['device'] = device if 'type' in clean_data and clean_data['type']: data['type'] = NetboxType.objects.get(pk=clean_data['type']) if primary_key: netbox = Netbox.objects.get(pk=primary_key) for key in data: setattr(netbox, key, data[key]) else: netbox = Netbox(**data) netbox.save() function = serial_form.cleaned_data['function'] if function: try: func = NetboxInfo.objects.get(netbox=netbox, variable='function') except NetboxInfo.DoesNotExist: func = NetboxInfo(netbox=netbox, variable='function', value=function) else: func.value = function func.save() if group_form: netboxgroups = group_form.cleaned_data['netboxgroups'] NetboxCategory.objects.filter(netbox=netbox).delete() for netboxgroup in netboxgroups: NetboxCategory.objects.create(netbox=netbox, category=netboxgroup) return netbox
def netbox_do_save(form): """Save netbox. Netboxgroups needs to be set manually because of database structure, thus we do a commit=False save first. """ netbox = form.save(commit=False) # Prevents saving m2m relationships netbox.save() # Save the function field function = form.cleaned_data['function'] if function: try: func = NetboxInfo.objects.get(netbox=netbox, variable='function') except NetboxInfo.DoesNotExist: func = NetboxInfo(netbox=netbox, variable='function', value=function) else: func.value = function func.save() # Save the groups netboxgroups = form.cleaned_data['groups'] NetboxCategory.objects.filter(netbox=netbox).delete() for netboxgroup in netboxgroups: NetboxCategory.objects.create(netbox=netbox, category=netboxgroup) # Update the list of management profiles current_profiles = set(form.cleaned_data['profiles']) old_profiles = set(netbox.profiles.all()) to_add = current_profiles.difference(old_profiles) to_remove = old_profiles.difference(current_profiles) for profile in to_remove: NetboxProfile.objects.get(netbox=netbox, profile=profile).delete() for profile in to_add: NetboxProfile(netbox=netbox, profile=profile).save() return netbox