def cydns_view(request, pk=None): """List, create, update view in one for a flatter heirarchy. """ # Infer obj_type from URL, saves trouble of having to specify # kwargs everywhere in the dispatchers. obj_type = request.path.split('/')[2] # Get the record form. Klass, FormKlass, FQDNFormKlass = get_klasses(obj_type) # Get the object if updating. record = get_object_or_404(Klass, pk=pk) if pk else None form = FQDNFormKlass(instance=record) if request.method == 'POST': qd, domain, errors = _fqdn_to_domain(request.POST.copy()) # Validate form. if errors: print errors fqdn_form = FQDNFormKlass(request.POST) fqdn_form._errors = ErrorDict() fqdn_form._errors['__all__'] = ErrorList(errors) return render(request, 'cydns/cydns_view.html', { 'form': fqdn_form, 'obj_type': obj_type, 'pk': pk, 'obj': record }) else: form = FormKlass(qd, instance=record) try: if perm(request, cy.ACTION_CREATE, obj=record, obj_class=Klass): record = form.save() # If domain, add to current ctnr. if obj_type == 'domain': request.session['ctnr'].domains.add(record) return redirect(record.get_list_url()) except (ValidationError, ValueError): form = _revert(domain, request.POST, form, FQDNFormKlass) object_list = _filter(request, Klass) page_obj = make_paginator(request, do_sort(request, object_list), 50) return render( request, 'cydns/cydns_view.html', { 'form': form, 'obj': record, 'page_obj': page_obj, 'object_table': tablefy(page_obj, views=True), 'obj_type': obj_type, 'pk': pk, })
def cydns_view(request, pk=None): """List, create, update view in one for a flatter heirarchy. """ # Infer obj_type from URL, saves trouble of having to specify # kwargs everywhere in the dispatchers. obj_type = request.path.split('/')[2] # Get the record form. Klass, FormKlass, FQDNFormKlass = get_klasses(obj_type) # Get the object if updating. record = get_object_or_404(Klass, pk=pk) if pk else None form = FQDNFormKlass(instance=record) if request.method == 'POST': qd, domain, errors = _fqdn_to_domain(request.POST.copy()) # Validate form. if errors: print errors fqdn_form = FQDNFormKlass(request.POST) fqdn_form._errors = ErrorDict() fqdn_form._errors['__all__'] = ErrorList(errors) return render(request, 'cydns/cydns_view.html', { 'form': fqdn_form, 'obj_type': obj_type, 'pk': pk, 'obj': record }) else: form = FormKlass(qd, instance=record) try: if perm(request, cy.ACTION_CREATE, obj=record, obj_class=Klass): record = form.save() # If domain, add to current ctnr. if obj_type == 'domain': request.session['ctnr'].domains.add(record) return redirect(record.get_list_url()) except (ValidationError, ValueError): form = _revert(domain, request.POST, form, FQDNFormKlass) object_list = _filter(request, Klass) page_obj = make_paginator( request, do_sort(request, object_list), 50) return render(request, 'cydns/cydns_view.html', { 'form': form, 'obj': record, 'page_obj': page_obj, 'object_table': tablefy(page_obj, views=True), 'obj_type': obj_type, 'pk': pk, })
def cy_view(request, get_klasses_fn, template, pk=None, obj_type=None): """List, create, update view in one for a flatter heirarchy. """ # Infer obj_type from URL, saves trouble of having to specify obj_type = obj_type or request.path.split('/')[2] Klass, FormKlass, FQDNFormKlass = get_klasses_fn(obj_type) obj = get_object_or_404(Klass, pk=pk) if pk else None form = FormKlass(instance=obj) if request.method == 'POST': form = FormKlass(request.POST, instance=obj) if form.is_valid(): try: if perm(request, cy.ACTION_CREATE, obj=obj, obj_class=Klass): obj = form.save() return redirect( request.META.get('HTTP_REFERER', obj.get_list_url())) except (ValidationError, ValueError) as e: if form._errors is None: form._errors = ErrorDict() form._errors["__all__"] = ErrorList(e.messages) return render(request, template, { 'form': form, 'obj': obj, 'obj_type': obj_type, 'pk': pk, }) elif request.method == 'GET': form = FormKlass(initial=qd_to_py_dict(request.GET)) object_list = _filter(request, Klass) page_obj = make_paginator(request, do_sort(request, object_list), 50) return render( request, template, { 'form': form, 'obj': obj, 'page_obj': page_obj, 'object_table': tablefy(page_obj), 'obj_type': obj_type, 'pk': pk, })
def cy_view(request, get_klasses_fn, template, pk=None, obj_type=None): """List, create, update view in one for a flatter heirarchy. """ # Infer obj_type from URL, saves trouble of having to specify obj_type = obj_type or request.path.split("/")[2] Klass, FormKlass, FQDNFormKlass = get_klasses_fn(obj_type) obj = get_object_or_404(Klass, pk=pk) if pk else None form = FormKlass(instance=obj) if request.method == "POST": form = FormKlass(request.POST, instance=obj) if form.is_valid(): try: if perm(request, cy.ACTION_CREATE, obj=obj, obj_class=Klass): obj = form.save() return redirect(request.META.get("HTTP_REFERER", obj.get_list_url())) except (ValidationError, ValueError) as e: if form._errors is None: form._errors = ErrorDict() form._errors["__all__"] = ErrorList(e.messages) return render(request, template, {"form": form, "obj": obj, "obj_type": obj_type, "pk": pk}) elif request.method == "GET": form = FormKlass(initial=qd_to_py_dict(request.GET)) object_list = _filter(request, Klass) page_obj = make_paginator(request, do_sort(request, object_list), 50) return render( request, template, { "form": form, "obj": obj, "page_obj": page_obj, "object_table": tablefy(page_obj), "obj_type": obj_type, "pk": pk, }, )