Exemple #1
0
 def render(self, djp, wrapper, prefix, for_model=None, ajax=False, method="get", **kwargs):
     appmodel, ok = app_model_from_ct(for_model)
     if not ok:
         return appmodel
     filters = appmodel.search_fields
     if not filters:
         return u""
     request = djp.request
     search_url = appmodel.searchurl(request)
     if not search_url:
         return u""
     model = appmodel.model
     initial = dict((request.GET or request.POST).items())
     form = forms.modelform_factory(model, appmodel.form, fields=filters, exclude=[])
     form.layout = FormLayout()
     f = UniForm(form(initial=initial), method=method, action=search_url)
     if ajax:
         f.addClass(djp.css.ajax)
     f.inputs.append(input(value="filter", name="_filter"))
     return f.render()
Exemple #2
0
def get_form(djp,
             form_class,
             method = 'POST',
             initial = None,
             prefix = None,
             addinputs = None,
             withdata = True,
             instance  = None,
             model = None,
             form_withrequest = None,
             template = None,
             form_ajax = False,
             withinputs = False):
    '''Comprehensive method for building a
:class:`djpcms.utils.uniforms.UniForm` instance:
    
:parameter djp: instance of :class:`djpcms.views.DjpResponse`.
:parameter form_class: required form class.
:parameter method: optional string indicating submit method. Default ``POST``.
:parameter initial: If not none, a dictionary of initial values.
:parameter prefix: Optional prefix string to use in the form.
:parameter addinputs: An optional function for creating inputs.
                      If available, it is called if the
                      available form class as no inputs associated with it.
                      Default ``None``.
'''
    from djpcms.utils.uniforms import UniForm
    request  = djp.request
    own_view = djp.own_view()
    
    data = request.POST if request.method == 'POST' else request.GET
    prefix = data.get('_prefixed',None)
    
    save_as_new = data.has_key('_save_as_new')
    initial  = update_initial(request, form_class, initial,
                              own_view = own_view)
    
    inputs = getattr(form_class,'submits',None)
    if inputs:
        inputs = [input(value = val, name = nam) for val,nam in inputs]
    elif addinputs:
        inputs = addinputs(instance, own_view)
        
    if not prefix:
        prefix = gen_unique_id()
        inputs.append(input(value = prefix, name = '_prefixed', type = 'hidden'))
                
    f     = form_class(**form_kwargs(request     = request,
                                     initial     = initial,
                                     instance    = instance,
                                     prefix      = prefix,
                                     withdata    = withdata,
                                     withrequest = form_withrequest,
                                     method      = method,
                                     own_view    = own_view,
                                     save_as_new = save_as_new,
                                     inputs      = inputs if withinputs else None))
        
    wrap = UniForm(f,
                   request  = request,
                   instance = instance,
                   action   = djp.url,
                   inputs   = inputs,
                   template = template,
                   save_as_new = save_as_new)
    if form_ajax:
        wrap.addClass(djp.css.ajax)
    wrap.is_ajax = request.is_ajax()
    if model:
        wrap.addClass(str(model._meta).replace('.','-'))
    return wrap