Changeset 2505

Show
Ignore:
Timestamp:
01/27/10 16:36:36 (7 weeks ago)
Author:
JensDiemer
Message:

form_generator: handle "max_length" and "min_length" and don't add defaults

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/pylucid_plugins/internals/admin_views.py

    r2400 r2505  
    121121#----------------------------------------------------------------------------- 
    122122 
    123 def textform_for_model(model): 
     123def _textform_for_model(model, request, debug=False): 
    124124    """ 
    125125    based on http://www.djangosnippets.org/snippets/458/ 
    126126    """ 
     127    defaults = {"required": True, "initial": None, "min_length": None} 
     128 
    127129    opts = model._meta 
    128130    field_list = [] 
     
    133135        if formfield: 
    134136            kw = [] 
    135             for a in  ('queryset', 'maxlength', 'label', 'initial', 'help_text', 'required'): 
     137            if debug: 
     138                request.page_msg(dir(formfield)) 
     139            for a in ('queryset', 'max_length', 'min_length', 'label', 'initial', 'help_text', 'required'): 
    136140                if hasattr(formfield, a): 
    137141                    attr = getattr(formfield, a) 
     142 
     143                    if a in defaults and attr == defaults[a]: 
     144                        # Don't add default key/value combinations into form 
     145                        continue 
     146 
    138147                    if a in ("label", "help_text"): # "translate" lazy text 
    139148                        attr = unicode(attr) 
    140149 
    141                     if attr in [True, False, None]: 
    142                         if a == 'required' and attr == True: # Don't add default 
    143                             continue 
    144                         if a == 'initial' and attr == None: # Don't add default 
    145                             continue 
     150                    if a == 'queryset': 
     151                        kw.append("%s=%s" % (a, "%s.objects.all()" % attr.model.__name__)) 
     152                    elif attr in [True, False, None]: 
    146153                        kw.append("%s=%s" % (a, attr)) 
    147                     elif a == 'queryset': 
    148                         kw.append("%s=%s" % (a, "%s.objects.all()" % attr.model.__name__)) 
    149154                    elif attr: 
    150155                        kw.append("%s=_('%s')" % (a, attr)) 
     
    169174    if model_no: 
    170175        model = models_dict[int(model_no)] 
    171         sourcecode = textform_for_model(model) 
     176        sourcecode = _textform_for_model(model, request, debug=True) 
    172177 
    173178        output = hightlighter.make_html(sourcecode, source_type="py")