

def generic_crud(request, obj_id, model, template, redirect, extra_context={
    
}, form_class=None, template_object_name='object', pre_save=None, post_save=None, success_msg=None):
    '\n    Generic way to create/update a bit more advanced than the django\'s generic\n    views. The context will contain the form as C{form}, the object when\n    updating as the value in C{template_object_name} which defaults to\n    "object", and anything specified in C{extra_context}.\n    \n    @param request: The request object\n    @param obj_id: The object\'s id to update or None if creating an object\n    @param model: The model class\n    @param template: the name of the template to use\n    @param redirect: callable that takes the created/saved instance as argument\n        and returns a URL to redirect to.\n    @keyword extra_context: dict of fields to add to the template context.\n    @keyword form_class: the form class to use for the object (ModelForm subclass)\n    @keyword template_object_name: name of the object field in the template\n        context. This is only available when updating.\n    @keyword pre_save: function to call before saving the object instantiated\n        from the form. Called with arguments (C{instance}, C{created}).\n    @keyword post_save: function to call after saving the object instantiated\n        from the form but before calling m2m_save() on it. Called with argument\n        (C{instance}, C{created}).\n    @keyword success_msg: callable that should take instance as parameter and\n        return a string to be put in a success DatedMessage and sent to the\n        user on success. \n    '
    (model, form_class) = get_model_and_form_class(model, form_class)
    if (obj_id != None):
        instance = get_object_or_404(model, pk=obj_id)
    else:
        instance = None
    if (request.method == 'GET'):
        form = form_class(instance=instance)
    elif (request.method == 'POST'):
        form = form_class(request.POST, instance=instance)
        if form.is_valid():
            instance = form.save(commit=False)
            if pre_save:
                pre_save(instance, (obj_id == None))
            instance.save()
            if post_save:
                post_save(instance, (obj_id == None))
            form.save_m2m()
            if success_msg:
                DatedMessage.objects.post_message_to_user(success_msg(instance), request.user, msg_type=DatedMessage.TYPE_SUCCESS)
            return HttpResponseRedirect(redirect(instance))
    else:
        return HttpResponseNotAllowed('GET', 'POST')
    context = {
        'form': form,
    }
    context.update(extra_context)
    if (obj_id != None):
        context[template_object_name] = instance
    return simple.direct_to_template(request, template=template, extra_context=context, form=form)

dataset/ETHPy150Open fp7-ofelia/ocf/vt_manager/src/python/vt_manager/common/utils/views.py generic_crud/original