Package expedient :: Package common :: Package utils :: Module views
[hide private]
[frames] | no frames]

Source Code for Module expedient.common.utils.views

 1  ''' 
 2  Created on Jun 18, 2010 
 3   
 4  @author: jnaous 
 5  ''' 
 6  from django.shortcuts import get_object_or_404 
 7  from django.views.generic.create_update import get_model_and_form_class 
 8  from django.views.generic import simple 
 9  from django.http import HttpResponseRedirect, HttpResponseNotAllowed 
10  from expedient.common.messaging.models import DatedMessage 
11   
12 -def generic_crud(request, obj_id, model, template, redirect, 13 extra_context={}, form_class=None, extra_form_params={}, 14 template_object_name="object", pre_save=None, 15 post_save=None, success_msg=None):
16 """ 17 Generic way to create/update a bit more advanced than the django's generic 18 views. The context will contain the form as C{form}, the object when 19 updating as the value in C{template_object_name} which defaults to 20 "object", and anything specified in C{extra_context}. 21 22 @param request: The request object 23 @param obj_id: The object's id to update or None if creating an object 24 @param model: The model class 25 @param template: the name of the template to use 26 @param redirect: callable that takes the created/saved instance as argument 27 and returns a URL to redirect to. 28 @keyword extra_context: dict of fields to add to the template context. 29 @keyword form_class: the form class to use for the object 30 (ModelForm subclass) 31 @keyword extra_form_params: dict of additional keyword parameters to pass 32 to the form when init'ing the form instance. 33 @keyword template_object_name: name of the object field in the template 34 context. This is only available when updating. 35 @keyword pre_save: function to call before saving the object instantiated 36 from the form. Called with arguments (C{instance}, C{created}). 37 @keyword post_save: function to call after saving the object instantiated 38 from the form but before calling m2m_save() on it. Called with argument 39 (C{instance}, C{created}). 40 @keyword success_msg: callable that should take instance as parameter and 41 return a string to be put in a success DatedMessage and sent to the 42 user on success. 43 """ 44 45 model, form_class = get_model_and_form_class(model, form_class) 46 47 if obj_id != None: 48 instance = get_object_or_404(model, pk=obj_id) 49 else: 50 instance = None 51 52 if request.method == "GET": 53 form = form_class(instance=instance, **extra_form_params) 54 elif request.method == "POST": 55 form = form_class(request.POST, instance=instance, **extra_form_params) 56 if form.is_valid(): 57 instance = form.save(commit=False) 58 if pre_save: pre_save(instance, obj_id == None) 59 instance.save() 60 if post_save: post_save(instance, obj_id == None) 61 form.save_m2m() 62 if success_msg: 63 DatedMessage.objects.post_message_to_user( 64 success_msg(instance), request.user, 65 msg_type=DatedMessage.TYPE_SUCCESS) 66 return HttpResponseRedirect(redirect(instance)) 67 else: 68 return HttpResponseNotAllowed("GET", "POST") 69 70 context = {"form": form} 71 context.update(extra_context) 72 if obj_id != None: context[template_object_name] = instance 73 74 return simple.direct_to_template( 75 request, 76 template=template, 77 extra_context=context, 78 form=form, 79 )
80