def static_dynamic_view(request): template = 'core/core_interfaces.html' if request.session['ctnr'].name == 'global': return cy_render(request, template, {}) StaticInterface = get_model('cyder', 'staticinterface') DynamicInterface = get_model('cyder', 'dynamicinterface') statics = _filter(request, StaticInterface).select_related('system') dynamics = (_filter(request, DynamicInterface).select_related('system', 'range')) page_obj = list(statics) + list(dynamics) def details(obj): data = {} data['url'] = obj.get_table_update_url() data['data'] = [] if isinstance(obj, StaticInterface): data['data'].append(('System', '1', obj.system)) data['data'].append(('Type', '2', 'static')) data['data'].append(('MAC', '3', obj.mac)) data['data'].append(('IP', '4', obj.ip_str)) elif isinstance(obj, DynamicInterface): data['data'].append(('System', '1', obj.system)) data['data'].append(('Type', '2', 'dynamic')) data['data'].append(('MAC', '3', obj)) data['data'].append(('IP', '4', obj.range)) data['data'].append(('Last seen', '5', obj.last_seen)) return data from cyder.base.tablefier import Tablefier table = Tablefier(page_obj, request, custom=details).get_table() if table: if 'sort' not in request.GET: sort, order = 1, 'asc' else: sort = int(request.GET['sort']) order = request.GET['order'] if 'order' in request.GET else 'asc' sort_fn = lambda x: x[sort]['value'][0].lower() table['data'] = sorted(table['data'], key=sort_fn, reverse=(order == 'desc')) return cy_render(request, template, { 'page_obj': page_obj, 'obj_table': table, }) else: return cy_render(request, template, {'no_interfaces': True})
def static_dynamic_view(request): template = 'core/core_interfaces.html' if request.session['ctnr'].name == 'global': return render(request, template, {}) StaticInterface = get_model('cyder', 'staticinterface') DynamicInterface = get_model('cyder', 'dynamicinterface') statics = _filter(request, StaticInterface).select_related('system') dynamics = (_filter(request, DynamicInterface) .select_related('system', 'range')) page_obj = list(statics) + list(dynamics) def details(obj): data = {} data['url'] = obj.get_table_update_url() data['data'] = [] if isinstance(obj, StaticInterface): data['data'].append(('System', '1', obj.system)) data['data'].append(('Type', '2', 'static')) data['data'].append(('MAC', '3', obj.mac)) data['data'].append(('IP', '4', obj.ip_str)) elif isinstance(obj, DynamicInterface): data['data'].append(('System', '1', obj.system)) data['data'].append(('Type', '2', 'dynamic')) data['data'].append(('MAC', '3', obj)) data['data'].append(('IP', '4', obj.range)) data['data'].append(('Last seen', '5', obj.last_seen)) return data from cyder.base.tablefier import Tablefier table = Tablefier(page_obj, request, custom=details).get_table() if table: if 'sort' not in request.GET: sort, order = 1, 'asc' else: sort = int(request.GET['sort']) order = request.GET['order'] if 'order' in request.GET else 'asc' sort_fn = lambda x: str(x[sort]['value'][0]).lower() table['data'] = sorted(table['data'], key=sort_fn, reverse=(order == 'desc')) return render(request, template, { 'page_obj': page_obj, 'obj_table': table, }) else: return render(request, template, {'no_interfaces': True})
def cy_view(request, 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 # kwargs everywhere in the dispatchers. obj_type = obj_type or request.path.split('/')[2] Klass, FormKlass = get_klasses(obj_type) obj = get_object_or_404(Klass, pk=pk) if pk else None form = None if request.method == 'POST': object_table = None page_obj = None form = FormKlass(request.POST, instance=obj) if form.is_valid(): try: if perm(request, ACTION_CREATE, obj=obj, obj_class=Klass): obj = form.save() if Klass.__name__ == 'Ctnr': request = ctnr_update_session(request, obj) if (hasattr(obj, 'ctnr_set') and not obj.ctnr_set.exists()): obj.ctnr_set.add(request.session['ctnr']) object_table = tablefy([obj], request=request) return HttpResponse( json.dumps({'row': object_table})) except (ValidationError, ValueError) as e: if form.errors is None: form.errors = ErrorDict() form.errors.update(e.message_dict) return HttpResponse(json.dumps({'errors': form.errors})) except DatabaseError as e: # DatabaseError(number, description) if form.errors is None: form.errors = ErrorDict() form.errors.setdefault('__all__', []).append(e.args[1]) return HttpResponse(json.dumps({'errors': form.errors})) else: return HttpResponse(json.dumps({'errors': form.errors})) elif request.method == 'GET': object_list = _filter(request, Klass) form = FormKlass(instance=obj) page_obj = make_paginator(request, do_sort(request, object_list), 50) object_table = tablefy(page_obj, request=request) if isinstance(form, UsabilityFormMixin): form.make_usable(request) return cy_render(request, template, { 'form': form, 'obj': obj, 'page_obj': page_obj, 'object_table': object_table, 'obj_type': obj_type, 'pretty_obj_type': Klass.pretty_type, 'pk': pk, })
def cy_view(request, 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 # kwargs everywhere in the dispatchers. obj_type = obj_type or request.path.split('/')[2] Klass, FormKlass = get_klasses(obj_type) obj = get_object_or_404(Klass, pk=pk) if pk else None form = None if request.method == 'POST': object_table = None page_obj = None form = FormKlass(request.POST, instance=obj) if form.is_valid(): try: if perm(request, ACTION_CREATE, obj=obj, obj_class=Klass): obj = form.save() if Klass.__name__ == 'Ctnr': request = ctnr_update_session(request, obj) if (hasattr(obj, 'ctnr_set') and not obj.ctnr_set.exists()): obj.ctnr_set.add(request.session['ctnr']) object_table = tablefy([obj], request=request) return HttpResponse(json.dumps({'row': object_table})) except (ValidationError, ValueError) as e: if form.errors is None: form.errors = ErrorDict() form.errors.update(e.message_dict) return HttpResponse(json.dumps({'errors': form.errors})) except DatabaseError as e: # DatabaseError(number, description) if form.errors is None: form.errors = ErrorDict() form.errors.setdefault('__all__', []).append(e.args[1]) return HttpResponse(json.dumps({'errors': form.errors})) else: return HttpResponse(json.dumps({'errors': form.errors})) elif request.method == 'GET': object_list = _filter(request, Klass) form = FormKlass(instance=obj) page_obj = make_paginator(request, do_sort(request, object_list), 50) object_table = tablefy(page_obj, request=request) if isinstance(form, UsabilityFormMixin): form.make_usable(request) return cy_render( request, template, { 'form': form, 'obj': obj, 'page_obj': page_obj, 'object_table': object_table, 'obj_type': obj_type, 'pretty_obj_type': Klass.pretty_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 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, }, )
return redirect(obj.get_list_url()) except (ValidationError, ValueError), e: if hasattr(e, 'messages'): e = e.messages if not form._errors: form._errors = ErrorDict() form._errors['__all__'] = ErrorList(e) if is_ajax_form(request): return HttpResponse(json.dumps({'errors': form.errors})) elif request.method == 'GET': form = FormKlass(instance=obj) object_list = _filter(request, Klass) page_obj = make_paginator(request, do_sort(request, object_list), 50) if issubclass(type(form), UsabilityFormMixin): form.make_usable(request) return render(request, 'cydns/cydns_view.html', { 'form': form, 'obj': obj, 'obj_type': obj_type, 'object_table': tablefy(page_obj, request=request), 'page_obj': page_obj, 'pretty_obj_type': Klass.pretty_type, 'pk': pk, })
return redirect(obj.get_list_url()) except (ValidationError, ValueError), e: if hasattr(e, 'messages'): e = e.messages if not form._errors: form._errors = ErrorDict() form._errors['__all__'] = ErrorList(e) if is_ajax_form(request): return HttpResponse(json.dumps({'errors': form.errors})) elif request.method == 'GET': form = FormKlass(instance=obj) object_list = _filter(request, Klass) page_obj = make_paginator(request, do_sort(request, object_list), 50) if issubclass(type(form), UsabilityFormMixin): form.make_usable(request) return cy_render( request, 'cydns/cydns_view.html', { 'form': form, 'obj': obj, 'obj_type': obj_type, 'object_table': tablefy(page_obj, request=request), 'page_obj': page_obj, 'pretty_obj_type': Klass.pretty_type, 'pk': pk, })