Sunday, November 28, 2010

Need a tip on how to architect this code to be more extensible

I wrote this open source project that allows you to use the django
admin pop-ups in your own views but I want to give it the ability to
have the view extended so they can pass excludes and instances to the
form before its display... currently the code looks like this:

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from django.utils.html import escape
from tekextensions.forms import get_model_form, normalize_model_name

def add_new_model(request, model_name, form=None):
normal_model_name = normalize_model_name(model_name)

if not form:
form = get_model_form(normal_model_name)

if request.method == 'POST':
form = form(request.POST)
if form.is_valid():
try:
new_obj = form.save()
except forms.ValidationError, error:
new_obj = None

if new_obj:
return HttpResponse('<script type="text/
javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</
script>' % \
(escape(new_obj._get_pk_val()), escape(new_obj)))

else:
form = form()

page_context = {'form': form, 'field': normal_model_name}
return render_to_response('popup.html', page_context,
context_instance=RequestContext(request))

So I'd still like to allow them to get the automatic adding of the
javascript httpresponse and autocreation of the form using the
model_form_factory but would allow them to get the form and pass
instance=... and exclude = ('whatever', ) so that they can
customize/tweak the generated form

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment