# 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
def view_element(request, dashboard_id, element_id, column_id = None):
    dashboard = get_object_or_404(Dashboard, id = dashboard_id)
    element = get_object_or_404(Element, id = element_id)
    raw_data_list = []
    for data_selection in element.get_data_selections():
        raw_data_list += [data_selection.get_data(None)]
    
    return render_to_response('dashboard/view_element.html', {'data': raw_data_list, 'dashboard_id': dashboard_id, 'element_id': element_id, 'dashboard': dashboard, 'element': element, 'mode': 'element'}, context_instance = get_context(request))

@login_required
def data(request, dashboard_id = None, element_id = None, column_id = None):
    data = get_data(dashboard_id, element_id, column_id);
        
    return HttpResponse(data, mimetype='text/json')

### View helper functions ###

def get_data(dashboard_id, element_id, column_id, encode = True, explain = False):
    element = get_object_or_404(Element, id = element_id)

    main_data = []
    # Retrieve required data
    if column_id == None:
        data_selections = element.get_data_selections()
        data_list = []
        for data_selection in data_selections:
            data_list += [data_selection.get_data(None, explain = explain)]
        if explain != True:
            formatted_data_list = element.get_child().format_data(data_list)
            main_data = formatted_data_list

# 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

def view_element(request, dashboard_id, element_id, column_id = None):
    dashboard = get_object_or_404(Dashboard, id = dashboard_id)
    element = get_object_or_404(Element, id = element_id)
    raw_data_list = []
    for data_selection in element.get_data_selections():
        raw_data_list += [data_selection.get_data(None)]
    
    return render_to_response('dashboard/view_element.html', {'data': raw_data_list, 'dashboard_id': dashboard_id, 'element_id': element_id, 'dashboard': dashboard, 'element': element, 'mode': 'element'}, context_instance = get_context(request))

@login_required
def data(request, dashboard_id = None, element_id = None, column_id = None):
    data = get_data(dashboard_id, element_id, column_id);
        
    return HttpResponse(data, mimetype='text/json')

### View helper functions ###

def get_data(dashboard_id, element_id, column_id, encode = True, explain = False):
    element = get_object_or_404(Element, id = element_id)

    main_data = []
    # Retrieve required data
    if column_id == None:
        data_selections = element.get_data_selections()
        data_list = []
        for data_selection in data_selections:
            data_list += [data_selection.get_data(None, explain = explain)]
        if explain != True:
            formatted_data_list = element.get_child().format_data(data_list)
            main_data = formatted_data_list

        else: