From f495094eba250d3e8dad36fbd05d980d8f9532d9 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 7 Jul 2012 13:32:33 +0530 Subject: [PATCH] wrapper views for add_object or edit_object --- itf/insidepages/urls.py | 2 ++ itf/insidepages/views.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/itf/insidepages/urls.py b/itf/insidepages/urls.py index 1fd98c0..a2c356e 100755 --- a/itf/insidepages/urls.py +++ b/itf/insidepages/urls.py @@ -6,5 +6,7 @@ urlpatterns = patterns('', # (r'^get_details$', views.get_details), # (r'^get_tab$', views.get_tab), #(r'^static/(?P.*)/$', views.render_object), + (r'^(?P.*?)/(?P.*?)/add', views.add_object), + (r'^(?P.*?)/(?P.*?)/(?Pd+)/edit', views.edit_object), (r'^(?P.*)/$', views.render_object), ) diff --git a/itf/insidepages/views.py b/itf/insidepages/views.py index ec14dbe..6f5f5e6 100755 --- a/itf/insidepages/views.py +++ b/itf/insidepages/views.py @@ -3,6 +3,46 @@ 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 +from django.http import HttpResponseRedirect + + +def add_object(request, module_slug, tab_slug): + tab = get_object_or_404(ModuleTab, slug=tab_slug) + model_class = tab.model_class() + add_form_name = model_class.main_form + form = model_class.get_forms()[add_form_name] + if request.POST: + f = form(request.POST, request.FILES) + if f.is_valid(): + f.save() + return HttpResponseRedirect('/') #TODO: get the saved object and redirect to .get_absolute_url() + else: + f = form() + context = RequestContext(request, { + 'form': f, + }) + return render_to_response("add_form.html", context) + + +def edit_object(request, module_slug, tab_slug, object_id): + tab = get_object_or_404(ModuleTab, slug=tab_slug) + model_class = tab.model_class() + obj = get_object_or_404( + add_form_name = model_class.main_form + form = model_class.get_forms()[add_form_name] + + if request.POST: + f = form(request.POST, request.FILES, instance=obj) + if f.is_valid(): + f.save() + return HttpResponseRedirect(obj.get_absolute_url()) + else: + f = form(instance=obj) + context = RequestContext(request, { + 'form': f, + 'object': obj + }) + return render_to_response("edit_form.html", context) '''