@user_passes_test((lambda u: u.is_staff))
def add_endpoint(request, pid):
    product = get_object_or_404(Product, id=pid)
    template = 'dojo/add_endpoint.html'
    if ('_popup' in request.GET):
        template = 'dojo/add_related.html'
    form = AddEndpointForm(product=product)
    if (request.method == 'POST'):
        form = AddEndpointForm(request.POST, product=product)
        if form.is_valid():
            endpoints = form.save()
            tags = request.POST.getlist('tags')
            t = ', '.join(tags)
            for e in endpoints:
                e.tags = t
            messages.add_message(request, messages.SUCCESS, 'Endpoint added successfully.', extra_tags='alert-success')
            if ('_popup' in request.GET):
                resp = '<script type="text/javascript">opener.emptyEndpoints(window);</script>'
                for endpoint in endpoints:
                    resp += ('<script type="text/javascript">opener.dismissAddAnotherPopupDojo(window, "%s", "%s");</script>' % (escape(endpoint._get_pk_val()), escape(endpoint)))
                resp += '<script type="text/javascript">window.close();</script>'
                return HttpResponse(resp)
            else:
                return HttpResponseRedirect(((reverse('endpoints') + '?product=') + pid))
    product_tab = None
    if ('_popup' not in request.GET):
        product_tab = Product_Tab(product.id, 'Add Endpoint', tab='endpoints')
    return render(request, template, {'product_tab': product_tab, 'name': 'Add Endpoint', 'form': form})
@user_passes_test((lambda u: u.is_staff))
def add_endpoint(request, pid):
    product = get_object_or_404(Product, id=pid)
    template = 'dojo/add_endpoint.html'
    if ('_popup' in request.GET):
        template = 'dojo/add_related.html'
    form = AddEndpointForm(product=product)
    if (request.method == 'POST'):
        form = AddEndpointForm(request.POST, product=product)
        if form.is_valid():
            endpoints = form.save()
            tags = request.POST.getlist('tags')
            t = ', '.join(tags)
            for e in endpoints:
                e.tags = t
            messages.add_message(request, messages.SUCCESS, 'Endpoint added successfully.', extra_tags='alert-success')
            if ('_popup' in request.GET):
                resp = '<script type="text/javascript">opener.emptyEndpoints(window);</script>'
                for endpoint in endpoints:
                    resp += ('<script type="text/javascript">opener.dismissAddAnotherPopupDojo(window, "%s", "%s");</script>' % (escape(endpoint._get_pk_val()), escape(endpoint)))
                resp += '<script type="text/javascript">window.close();</script>'
                return HttpResponse(resp)
            else:
                return HttpResponseRedirect(((reverse('endpoints') + '?product=') + pid))
    product_tab = None
    if ('_popup' not in request.GET):
        product_tab = Product_Tab(product.id, 'Add Endpoint', tab='endpoints')
    return render(request, template, {'product_tab': product_tab, 'name': 'Add Endpoint', 'form': form})