from models import Module, ModuleTab from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from ox.django.shortcuts import render_to_json_response import re def main(request, module_slug): m = get_object_or_404(Module, slug=module_slug) tabs = m.moduletab_set.all().order_by('order') default_tab = tabs[0] try: formStr = default_tab.model_class().add_form has_add = True add_form_class = default_tab.model_class().get_add_form() add_form = add_form_class() except: add_form = None has_add = False list_options = {} #get some options as GET params, etc. to potentially pass to get_list default_tab_list = default_tab.model_class().get_list(list_options) context = RequestContext(request, { 'title': m.title, 'about': m.about, 'has_add': has_add, 'add_form': add_form, 'default_tab': tabs[0], 'default_list': default_tab_list, 'default_sorts': default_tab.get_dict()['sorts'], 'tabs': tabs[1:] }) return render_to_response("noel/insidepage.html", context) def get_tab(request): tab_slug = request.GET.get("tab", "") tab = get_object_or_404(ModuleTab, slug=tab_slug) return render_to_json_response(tab.get_dict()) def get_list(request): numre = re.compile("[0-9]*") tab_slug = request.GET.get("tab", "") tab = get_object_or_404(ModuleTab, slug=tab_slug) object_id = request.GET.get("object_id", False) object_id = object_id[numre.match(object_id).start():numre.match(object_id).end()] #FIXME - why is the front-end sending this? sortString = request.GET.get("sort", "") if sortString == "" or sortString == 'null': sortArray = [] else: sortOperator = sortString[0] sortField = sortString[1:] sortArray = [{ 'operator': sortOperator, 'key': sortField }] list_options = { 'search': request.GET.get("search", ""), 'sort': sortArray, 'page': request.GET.get("page", 1), 'object_id': object_id, 'count': request.GET.get("count", 15) #FIXME: make list_length either in settings.py or config per model } object_list = tab.get_list(list_options) return render_to_json_response(object_list) def get_details(request): # import pdb tab_slug = request.GET.get("tab", '') tab = get_object_or_404(ModuleTab, slug=tab_slug) model_class = tab.model_class() object_id = request.GET.get("object_id", 0) obj = get_object_or_404(model_class, pk=object_id) ret = obj.insidepage_dict(request) # ret['csrf_token'] = c # pdb.set_trace() return render_to_json_response(ret) def render_object(request, module_slug): module = get_object_or_404(Module, slug=module_slug) tab_slug = request.GET.get("tab", '') try: tab = get_object_or_404(ModuleTab, slug=tab_slug) except: try: tab = module.moduletab_set.filter(is_default=True)[0] except: tab = module.moduletab_set.all()[0] model_class = tab.model_class() object_id = request.GET.get("object_id", 0) sortString = request.GET.get("sort", "") if sortString == "" or sortString == 'null': sortArray = [] else: sortOperator = sortString[0] sortField = sortString[1:] sortArray = [{ 'operator': sortOperator, 'key': sortField }] list_options = { 'search': request.GET.get("search", ""), 'sort': sortArray, 'page': request.GET.get("page", 1), 'object_id': object_id, 'count': request.GET.get("count", 15) #FIXME: make list_length either in settings.py or config per model } object_list = tab.get_list(list_options) if object_id == 0: object_id = object_list['items'][0]['id'] obj = get_object_or_404(model_class, pk=object_id) item_data = obj.insidepage_dict(request) context = RequestContext(request, { 'item': obj, 'item_data': item_data, 'tab': tab, 'object_list': object_list, 'tabs': tab.module.moduletab_set.all() }) return render_to_response("noel/render_object.html", context)