def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select view by id or ids ids = [i.strip() for i in opts['ids'].split(',')] views = View.objects.filter(id__in=ids) # Stop if no given id if not views.exists(): self.stdout.write("There's no view with given ID: '%s'" % (opts['ids'] or opts['id'])) sys.exit(1) # Set source to add or del to_add = set([i.strip() for i in opts['add_sources'].split(',')]) to_del = set([i.strip() for i in opts['rm_sources'].split(',')]) # Walk on view for valid or fail modified_views = [] form_error = None for v in views: # Use Form to valid data = v.__dict__ data.update(dict([(k, V) for k, V in opts.items() if V])) data['sources'] = [str(V.id) for V in v.sources.all()] data['sources'] = set(data['sources']) - to_del data['sources'] = list(data['sources'] | to_add) data['sources'] = [i for i in data['sources'] if i] F = View_Form(data=data, instance=v) if F.is_valid(): v = F.save() modified_views.append(v) else: form_error = F.errors # Walk on all list to print it if modified_views: self.stdout.write( ROW_FORMAT.format(**{ u'id': 'ID', 'sources': 'Source IDs', 'name': u'Name' })) for v in modified_views: v_dict = v.__dict__ v_dict['sources'] = ','.join( [str(s.id) for s in v.sources.all()]) self.stdout.write(ROW_FORMAT.format(**v_dict)) 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 index(request): views = View.objects.user_filter(request.user) views = make_page(views, 1, 30) return render(request, 'customize/index.html', { 'Views': views, 'View_Form': View_Form(user=request.user), })
def edit(request, view_id): """Only in GET, POST is conf/add.""" V = get_object_or_404(View.objects.filter(id=view_id)) F = View_Form(instance=V, user=request.user) return render(request, 'forms/view.html', { 'View_Form': F, })
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select source by id or ids opts['sources'] = [ i.strip() for i in opts['sources'].split(',') ] # Use Form to valid F = View_Form(data=opts) if F.is_valid(): v = F.save() v_dict = v.__dict__ v_dict['sources'] = ','.join([ str(s.id) for s in v.sources.all() ]) self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'sources': 'Source IDs', 'name': u'Name'})) self.stdout.write(ROW_FORMAT.format(**v_dict)) else: for field,errors in F.errors.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 view by id or ids ids = [ i.strip() for i in opts['ids'].split(',') ] views = View.objects.filter(id__in=ids) # Stop if no given id if not views.exists(): self.stdout.write("There's no view with given ID: '%s'" % (opts['ids'] or opts['id']) ) sys.exit(1) # Set source to add or del to_add = set([ i.strip() for i in opts['add_sources'].split(',') ]) to_del = set([ i.strip() for i in opts['rm_sources'].split(',') ]) # Walk on view for valid or fail modified_views = [] form_error = None for v in views: # Use Form to valid data = v.__dict__ data.update(dict([ (k,V) for k,V in opts.items() if V ])) data['sources'] = [ str(V.id) for V in v.sources.all() ] data['sources'] = set(data['sources']) - to_del data['sources'] = list(data['sources'] | to_add) data['sources'] = [ i for i in data['sources'] if i ] F = View_Form(data=data, instance=v) if F.is_valid(): v = F.save() modified_views.append(v) else: form_error = F.errors # Walk on all list to print it if modified_views: self.stdout.write(ROW_FORMAT.format(**{u'id': 'ID', 'sources': 'Source IDs', 'name': u'Name'})) for v in modified_views: v_dict = v.__dict__ v_dict['sources'] = ','.join([ str(s.id) for s in v.sources.all() ]) self.stdout.write(ROW_FORMAT.format(**v_dict)) 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': data = {} F = View_Form(data=request.POST, user=request.user) if F.is_valid(): V = F.save() messages.success(request, _("View added with success.")) data.update({ 'response': 'ok', 'callback-url': V.get_absolute_url(), 'id': V.pk, }) 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, 'forms/view.html', { 'View_Form': View_Form(user=request.user), })
def handle(self, *args, **opts): if opts['quiet']: self.stdout = open(devnull, 'w') # Select source by id or ids opts['sources'] = [i.strip() for i in opts['sources'].split(',')] # Use Form to valid F = View_Form(data=opts) if F.is_valid(): v = F.save() v_dict = v.__dict__ v_dict['sources'] = ','.join([str(s.id) for s in v.sources.all()]) self.stdout.write( ROW_FORMAT.format(**{ u'id': 'ID', 'sources': 'Source IDs', 'name': u'Name' })) self.stdout.write(ROW_FORMAT.format(**v_dict)) else: for field, errors in F.errors.items(): self.stdout.write(field) for err in errors: self.stdout.write('\t' + err)
def update(request, view_id): V = get_object_or_404(View.objects.filter(pk=view_id)) F = View_Form(data=request.POST, instance=V, user=request.user) data = {} if F.is_valid(): F.save() messages.success(request, _("View updated with success.")) data.update({ 'response': 'ok', 'callback-url': V.get_absolute_url(), 'id': V.pk, }) 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 get(request, view_id): V = get_object_or_404(View.objects.filter(pk=view_id)) F = View_Form(instance=V, user=request.user) return render(request, 'forms/view.html', { 'View_Form': F, })
def add(request): """Only for get empty View_Form.""" return render(request, 'forms/view.html', { 'View_Form': View_Form(user=request.user), })