Beispiel #1
0
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,
    })
Beispiel #2
0
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,
        })
Beispiel #3
0
def range_detail(request, pk):
    mrange = get_object_or_404(Range, pk=pk)

    if mrange.allow == ALLOW_ANY:
        allow = ['Any client']
    elif mrange.allow == ALLOW_KNOWN:
        allow = ['Known clients']
    else:
        allow = []
        if mrange.allow == ALLOW_VRF:
            allow += map(str, Vrf.objects.filter(network=mrange.network))
        if mrange.allow == ALLOW_LEGACY:
            allow += map(str, Ctnr.objects.filter(ranges=mrange))

    allow.sort(key=lambda x: x.lower())

    range_type = mrange.range_type
    range_data = []
    ip_usage_percent = None
    dynamic_interfaces = []
    dynamic_interfaces_page_obj = None
    if range_type == 'st':
        start_upper = mrange.start_upper
        start_lower = mrange.start_lower
        end_upper = mrange.end_upper
        end_lower = mrange.end_lower
        range_data, ip_usage_percent = range_usage(
            two_to_one(start_upper, start_lower),
            two_to_one(end_upper, end_lower),
            mrange.ip_type)
    else:
        ip_usage_percent = mrange.range_usage
        DynamicInterface = get_model('cyder', 'dynamicinterface')
        dynamic_interfaces = DynamicInterface.objects.filter(range=mrange)
        dynamic_interfaces_page_obj = make_paginator(
            request, do_sort(request, dynamic_interfaces), 10)

    if ip_usage_percent:
        ip_usage_percent = "{0}%".format(ip_usage_percent)
    return render(request, 'range/range_detail.html', {
        'obj': mrange,
        'obj_type': 'range',
        'pretty_obj_type': mrange.pretty_type,
        'ranges_table': tablefy((mrange,), info=False, request=request),
        'range_data': make_paginator(request, range_data, 50),
        'range_type': range_type,
        'attrs_table': tablefy(mrange.rangeav_set.all(),
                               request=request),
        'allow_list': allow,
        'range_used': ip_usage_percent,
        'dynamic_intr_table': tablefy(dynamic_interfaces_page_obj, info=True,
                                      request=request),
        'page_obj': dynamic_interfaces_page_obj
    })
Beispiel #4
0
        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,
    })

Beispiel #5
0
        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,
        })