def update(request, host_id): H = get_object_or_404(Host.objects.filter(pk=host_id)) F = Host_Form(data=request.POST, instance=H) data = {} if F.is_valid(): F.save() messages.success(request, _("Host updated with success.")) data['response'] = 'ok' data['callback-url'] = H.get_absolute_url() else: for field, error in F.errors.items(): messages.error(request, '<b>%s</b>: %s' % (field, error)) data['response'] = 'error' return render_HTML_JSON(request, data, 'base/messages.html', {})
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select host by id or ids ids = [i.strip() for i in opts['ids'].split(',')] hosts = Host.objects.filter(hostid__in=ids) # Stop if no given id if not hosts.exists(): self.stdout.write("There's no host with given ID: '%s'" % (opts['ids'] or opts['id'])) sys.exit(1) existing_ids = [h.hostid for h in hosts] non_existing_ids = [id for id in ids if id not in existing_ids] modified_hosts = [] form_error = None # Walk on host for valid or fail for h in hosts: # Make validation if not opts['storage']: opts['storage'] = h.storage.id if h.group and not opts['group']: opts['group'] = h.group.id # Create new data computing instance and options data = dict([(key.replace('_id', ''), val) for key, val in h.__dict__.items() if val is not None]) data.update(opts) # Use Form to valid F = Host_Form(data=data, instance=h) if F.is_valid(): h = F.save() modified_hosts.append(h) else: form_error = F.errors # Walk on all list to print it if modified_hosts: self.stdout.write('* Host updated:') self.stdout.write( ROW_FORMAT.format( **{ u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID' })) for h in modified_hosts: self.stdout.write(ROW_FORMAT.format(**h.__dict__)) if non_existing_ids: self.stdout.write('* No host with following IDs:') for id in non_existing_ids: self.stdout.write(id) if form_error: self.stdout.write('* Error:') for field, errors in form_error.items(): self.stdout.write(field) for err in errors: self.stdout.write('\t' + err)
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select host by id or ids ids = [ i.strip() for i in opts['ids'].split(',') ] hosts = Host.objects.filter(hostid__in=ids) # Stop if no given id if not hosts.exists(): self.stdout.write("There's no host with given ID: '%s'" % (opts['ids'] or opts['id']) ) sys.exit(1) existing_ids = [ h.hostid for h in hosts ] non_existing_ids = [ id for id in ids if id not in existing_ids ] modified_hosts = [] form_error = None # Walk on host for valid or fail for h in hosts: # Make validation if not opts['storage']: opts['storage'] = h.storage.id if h.group and not opts['group']: opts['group'] = h.group.id # Create new data computing instance and options data = dict( [ (key.replace('_id', ''),val) for key,val in h.__dict__.items() if val is not None ] ) data.update(opts) # Use Form to valid F = Host_Form(data=data, instance=h) if F.is_valid(): h = F.save() modified_hosts.append(h) else: form_error = F.errors # Walk on all list to print it if modified_hosts: self.stdout.write('* Host updated:') self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID'})) for h in modified_hosts: self.stdout.write(ROW_FORMAT.format(**h.__dict__)) if non_existing_ids: self.stdout.write('* No host with following IDs:') for id in non_existing_ids: self.stdout.write(id) if form_error: self.stdout.write('* Error:') for field,errors in form_error.items(): self.stdout.write(field) for err in errors: self.stdout.write('\t'+err)
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Create all if enabled if opts['all']: for s in Storage.objects.all(): s.create_hosts() self.stdout.write('All host from %s created.' % s) return # Select host by id or ids if opts['ids']: ids = [i.strip() for i in opts['ids'].split(',')] else: self.stdout.write("You must give one or more ID.") self.print_help('host', 'help') sys.exit(1) # Walk on ids to dispatch it on category non_saved_ids = [] existing_ids = [] non_existing_ids = [] repaired_ids = [] for id in ids: storage = Storage.objects.which_storage(id) if not storage: self.stdout.write('Host with ID %s not found in storage(s).' % id) non_existing_ids.append(id) # Repair and stop if already exists elif Host.objects.filter(hostid=id).exists(): self.stdout.write('Host with ID %s already exists in db.' % id) h = Host.objects.get(hostid=id) if h.storage != storage: self.stdout.write( "%s wasn't linked to good storage, now linked to %s" % (id, storage)) h.storage = storage h.save() repaired_ids.append(id) else: existing_ids.append(id) else: non_saved_ids.append(id) # Create hosts self.stdout.write('* Host creation:') self.stdout.write( ROW_FORMAT.format( **{ u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID' })) for id in non_saved_ids: host_info = storage.get_info(id) data = { 'name': host_info['Name'], 'hostid': id, 'storage': storage.id, 'group': opts['group'] } # Use Form to valid F = Host_Form(data=data) if F.is_valid(): h = F.save() self.stdout.write(ROW_FORMAT.format(**h.__dict__)) else: self.stdout.write(h) for field, errors in F.errors.items(): self.stdout.write(field) for err in errors: self.stdout.write('\t' + err) # Show repaired hosts if repaired_ids: self.stdout.write('* Hosts repaired:') for h in Host.objects.filter(hostid__in=repaired_ids): self.stdout.write( ROW_FORMAT.format( **{ u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID' })) self.stdout.write(ROW_FORMAT.format(**h.__dict__))
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Create all if enabled if opts['all']: for s in Storage.objects.all(): s.create_hosts() self.stdout.write('All host from %s created.' % s) return # Select host by id or ids if opts['ids']: ids = [ i.strip() for i in opts['ids'].split(',') ] else: self.stdout.write("You must give one or more ID.") self.print_help('host', 'help') sys.exit(1) # Walk on ids to dispatch it on category non_saved_ids = [] existing_ids = [] non_existing_ids = [] repaired_ids = [] for id in ids: storage = Storage.objects.which_storage(id) if not storage: self.stdout.write('Host with ID %s not found in storage(s).' % id) non_existing_ids.append(id) # Repair and stop if already exists elif Host.objects.filter(hostid=id).exists(): self.stdout.write('Host with ID %s already exists in db.' % id) h = Host.objects.get(hostid=id) if h.storage != storage: self.stdout.write("%s wasn't linked to good storage, now linked to %s" % (id,storage)) h.storage = storage h.save() repaired_ids.append(id) else: existing_ids.append(id) else: non_saved_ids.append(id) # Create hosts self.stdout.write('* Host creation:') self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID'})) for id in non_saved_ids: host_info = storage.get_info(id) data = { 'name': host_info['Name'], 'hostid': id, 'storage': storage.id, 'group': opts['group'] } # Use Form to valid F = Host_Form(data=data) if F.is_valid(): h = F.save() self.stdout.write(ROW_FORMAT.format(**h.__dict__)) else: self.stdout.write(h) for field,errors in F.errors.items(): self.stdout.write(field) for err in errors: self.stdout.write('\t'+err) # Show repaired hosts if repaired_ids: self.stdout.write('* Hosts repaired:') for h in Host.objects.filter(hostid__in=repaired_ids): self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'group_id': 'Group ID', 'hostid': 'Host ID', 'name': u'Name', 'storage_id': 'Storage ID'})) self.stdout.write(ROW_FORMAT.format(**h.__dict__))