# Path: querybuilder/decorators.py
# def superuser_only(function):
#     def _inner(request, *args, **kwargs):
#         if not request.user.is_superuser:
#             raise PermissionDenied
# 
#         return function(request, *args, **kwargs)
#     return _inner


from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.core import serializers
from django.utils import simplejson
from django.template import RequestContext
from models import *
from forms import *
from querybuilder.models import *
from querybuilder.decorators import superuser_only
from encoders import json_table_encoder, json_graph_encoder
    from querybuilder.models import get_apps, get_models_in_app
import re
    return redirect_to_design()

@superuser_only
def delete_element(request, element_id):
    elm = get_object_or_404(Element, id = element_id)
    elm.delete()
    return redirect_to_design()

 ## Edit Dashboard elements ##

@superuser_only
def edit_element(request, element_id):
    element = get_object_or_404(Element, id = element_id)
    if element.get_type() == 'graph':
        if request.method == 'POST':
            form = GraphForm(request.POST, instance = element.get_child())
            if form.is_valid():
                ret = form.save()
        else:
            form = GraphForm(instance = element.get_child())
    elif element.get_type() == 'table':
        if request.method == 'POST':
            form = TableForm(request.POST, instance = element.get_child())
            if form.is_valid():
                form.save()
        else:
            form = TableForm(instance = element.get_child())
    request.session['return_url'] = '/dashboard/design/edit_element/%s/' % (element.id)
    return render_to_response('dashboard/design/edit_element.html', {'form': form, 'element': element, 'mode': 'design'}, context_instance = get_context(request))


# Path: querybuilder/decorators.py
# def superuser_only(function):
#     def _inner(request, *args, **kwargs):
#         if not request.user.is_superuser:
#             raise PermissionDenied
# 
#         return function(request, *args, **kwargs)
#     return _inner


# Path: dashboard/views.py
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.core import serializers
from django.utils import simplejson
from django.template import RequestContext
from models import *
from forms import *
from querybuilder.models import *
from querybuilder.decorators import superuser_only
from encoders import json_table_encoder, json_graph_encoder
    from querybuilder.models import get_apps, get_models_in_app
import re

    return redirect_to_design()

@superuser_only
def delete_element(request, element_id):
    elm = get_object_or_404(Element, id = element_id)
    elm.delete()
    return redirect_to_design()

 ## Edit Dashboard elements ##

@superuser_only
def edit_element(request, element_id):
    element = get_object_or_404(Element, id = element_id)
    if element.get_type() == 'graph':
        if request.method == 'POST':
            form = GraphForm(request.POST, instance = element.get_child())
            if form.is_valid():
                ret = form.save()
        else:
            form = GraphForm(instance = element.get_child())
    elif element.get_type() == 'table':
        if request.method == 'POST':
            form = TableForm(request.POST, instance = element.get_child())
            if form.is_valid():
                form.save()
        else:
            form = TableForm(instance = element.get_child())
    request.session['return_url'] = '/dashboard/design/edit_element/%s/' % (element.id)
    return render_to_response('dashboard/design/edit_element.html', {'form': form, 'element': element, 'mode': 'design'}, context_instance = get_context(request))


@superuser_only