def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select storage by id ids = [i.strip() for i in opts['ids'].split(',')] storages = Storage.objects.filter(id__in=ids) if not storages.exists(): self.stdout.write("No storage with id '%s' exists." % opts['id']) sys.exit(1) existing_ids = [s.id for s in storages] non_existing_ids = [id for id in ids if int(id) not in existing_ids] modified_storages = [] form_error = None # Format options to left 'None' opts = dict([(k, v) for k, v in opts.items() if v is not None]) # Walk on host for valid or fail for s in storages: # Make validation # Create new data computing instance and options data = dict([(key.replace('_id', ''), val) for key, val in s.__dict__.items()]) data.update(opts) # Use Form to valid F = Storage_Form(data=data, instance=s) if F.is_valid(): h = F.save() modified_storages.append(s) else: form_error = F.errors # Walk on all list to print it if modified_storages: self.stdout.write('* Storage(s) updated:') self.stdout.write( ROW_FORMAT.format( **{ u'id': 'ID', 'name': 'Name', 'address': 'Address', 'port': 'Port', 'url_prefix': 'URL prefix', 'protocol': 'Protocol', 'login': '******' })) for s in modified_storages: self.stdout.write(ROW_FORMAT.format(**s.__dict__)) if non_existing_ids: self.stdout.write('* No storage 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 update(request, storage_id): S = get_object_or_404(Storage.objects.filter(pk=storage_id)) F = Storage_Form(data=request.POST, instance=S) data = {} if F.is_valid(): F.save() messages.success(request, _("Storage updated with success.")) data['response'] = 'ok' data['callback-url'] = S.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') # Walk on names and valid with form. F = Storage_Form(data=opts) if F.is_valid(): s = F.save() self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'name': 'Name', 'address': 'Address', 'port': 'Port', 'url_prefix': 'URL prefix', 'protocol': 'Protocol', 'login': '******'})) self.stdout.write(ROW_FORMAT.format(**s.__dict__)) elif not opts['quiet']: self.stdout.write('* Error(s)') for f,errs in F.errors.items(): self.stdout.write(f) for err in errs: self.stdout.write('\t'+err)
def update(request, storage_id): S = get_object_or_404(Storage.objects.filter(pk=storage_id)) F = Storage_Form(data=request.POST, instance=S) data = {} if F.is_valid(): F.save() messages.success(request, _("Storage updated with success.")) data['response'] = 'ok' data['callback-url'] = S.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 storage by id ids = [ i.strip() for i in opts['ids'].split(',') ] storages = Storage.objects.filter(id__in=ids) if not storages.exists(): self.stdout.write("No storage with id '%s' exists." % opts['id']) sys.exit(1) existing_ids = [ s.id for s in storages ] non_existing_ids = [ id for id in ids if int(id) not in existing_ids ] modified_storages = [] form_error = None # Format options to left 'None' opts = dict([ (k,v) for k,v in opts.items() if v is not None ]) # Walk on host for valid or fail for s in storages: # Make validation # Create new data computing instance and options data = dict( [ (key.replace('_id', ''),val) for key,val in s.__dict__.items() ] ) data.update(opts) # Use Form to valid F = Storage_Form(data=data, instance=s) if F.is_valid(): h = F.save() modified_storages.append(s) else: form_error = F.errors # Walk on all list to print it if modified_storages: self.stdout.write('* Storage(s) updated:') self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'name': 'Name', 'address': 'Address', 'port': 'Port', 'url_prefix': 'URL prefix', 'protocol': 'Protocol', 'login': '******'})) for s in modified_storages: self.stdout.write(ROW_FORMAT.format(**s.__dict__)) if non_existing_ids: self.stdout.write('* No storage 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 add(request): if request.method == 'POST': F = Storage_Form(request.POST) data = {} if F.is_valid(): S = F.save() messages.success(request, _("Storage added with success.")) data['response'] = 'ok' data['callback-url'] = S.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', {}) else: return render(request, 'storages/storage.html', { 'Storage_Form': Storage_Form(), })
def add(request): if request.method == 'POST': F = Storage_Form(request.POST) data = {} if F.is_valid(): S = F.save() messages.success(request, _("Storage added with success.")) data['response'] = 'ok' data['callback-url'] = S.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', {}) else: return render(request, 'storages/storage.html', { 'Storage_Form': Storage_Form(), })
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Walk on names and valid with form. F = Storage_Form(data=opts) if F.is_valid(): s = F.save() self.stdout.write( ROW_FORMAT.format( **{ u'id': 'ID', 'name': 'Name', 'address': 'Address', 'port': 'Port', 'url_prefix': 'URL prefix', 'protocol': 'Protocol', 'login': '******' })) self.stdout.write(ROW_FORMAT.format(**s.__dict__)) elif not opts['quiet']: self.stdout.write('* Error(s)') for f, errs in F.errors.items(): self.stdout.write(f) for err in errs: self.stdout.write('\t' + err)
def add(request): return render(request, 'storages/storage.html', { 'Storage_Form': Storage_Form(), })
def get(request, storage_id): S = get_object_or_404(Storage.objects.filter(pk=storage_id)) F = Storage_Form(instance=S) return render(request, 'storages/storage.html', { 'Storage_Form': F, })