Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Need a tip on how to architect this code to be more extensible [RSS Feed]

#1 Nov. 29, 2010 06:26:01

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 5, 2010 05:53:31

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


I still haven't came up with a creative solution on this, is there
anyone that might be able to give me some ideas?

On Nov 29, 12:25 am, Sontek <son...@gmail.com> wrote:
> 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 5, 2010 15:25:20

Rodrigo &.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


Hi.
There's a really good talk by James Bennett at the Google I/O 2008
that may help you. James talk mostly about the best practices when
writing a "Reusable App".http://www.youtube.com/watch?v=A-S0tqpPga4I'm sorry for my rusty english.
-- Rodrigo Chacon


On Dec 5, 3:53 am, Sontek <son...@gmail.com> wrote:
> I still haven't came up with a creative solution on this, is there
> anyone that might be able to give me some ideas?
>
> On Nov 29, 12:25 am, Sontek <son...@gmail.com> wrote:
>
>
>
>
>
>
>
> > 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-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Need a tip on how to architect this code to be more extensible [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support