Ignore:
Timestamp:
01/18/12 11:59:51 (9 years ago)
Author:
pcosquer
Message:

3D branch: merge changes from trunk (rev [661])

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3D/openPLM/plmapp/forms.py

    r595 r662  
    3636 
    3737import openPLM.plmapp.models as m 
     38from openPLM.plmapp.units import UNITS, DEFAULT_UNIT 
    3839from openPLM.plmapp.controllers import rx_bad_ref, DocumentController 
    3940from openPLM.plmapp.controllers.user import UserController 
     
    6768        " to this group.") 
    6869 
    69 def get_creation_form(user, cls=m.PLMObject, data=None, empty_allowed=False): 
     70def auto_complete_fields(form, cls): 
     71    """ 
     72    Replaces textinputs field of *form* with auto complete fields. 
     73 
     74    :param form: a :class:`Form` instance or class 
     75    :param cls: class of the source that provides suggested values 
     76    """ 
     77    for field, form_field in form.base_fields.iteritems(): 
     78        if field not in ("reference", "revision") and \ 
     79                isinstance(form_field.widget, forms.TextInput): 
     80            source = '/ajax/complete/%s/%s/' % (cls.__name__, field) 
     81            form_field.widget = JQueryAutoComplete(source) 
     82 
     83def get_new_reference(cls, start=0): 
     84    u""" 
     85    Returns a new reference for creating a :class:`.PLMObject` of type 
     86    *cls*. 
     87 
     88    The formatting is ``PART_000XX`` if *cls* is a subclass of :class:`.Part` 
     89    and ``DOC_000XX`` otherwise. 
     90     
     91    The number is the count of Parts or Documents plus *start* plus 1. 
     92    It is incremented while an object with the same reference aleady exists. 
     93    *start* can be used to create several creation forms at once. 
     94 
     95    .. note:: 
     96        The returned referenced may not be valid if a new object has been 
     97        created after the call to this function. 
     98    """ 
     99    if issubclass(cls, m.Part): 
     100        base_cls, name = m.Part, "PART" 
     101    else: 
     102        base_cls, name = m.Document, "DOC" 
     103    nb = base_cls.objects.count() + start + 1 
     104    reference = "%s_%05d" % (name, nb) 
     105    while base_cls.objects.filter(reference=reference).exists(): 
     106        nb += 1 
     107        reference = "%s_%05d" % (name, nb) 
     108    return reference 
     109 
     110def get_initial_creation_data(cls, start=0): 
     111    u""" 
     112    Returns initial data to create a new object (from :func:`get_creation_form`). 
     113 
     114    :param cls: class of the created object 
     115    :param start: used to generate the reference,  see :func:`get_new_reference` 
     116    """ 
     117    if issubclass(cls, m.PLMObject): 
     118        data = { 
     119                'reference' : get_new_reference(cls),  
     120                'revision' : 'a', 
     121                'lifecycle' : str(m.get_default_lifecycle().pk), 
     122        } 
     123    else: 
     124        data = {} 
     125    return data 
     126 
     127def get_creation_form(user, cls=m.PLMObject, data=None, start=0): 
    70128    u""" 
    71129    Returns a creation form suitable to creates an object 
     
    77135    :class:`~plmapp.controllers.PLMObjectController`. 
    78136 
    79     If *initial* is provided, it will be used to fill the form. 
     137    If *data* is provided, it will be used to fill the form. 
     138 
     139    *start* is used if *data* is ``None``, it's usefull if you need to show 
     140    several initial creation forms at once and you want different references. 
    80141    """ 
    81142    Form = get_creation_form.cache.get(cls) 
     
    83144        fields = cls.get_creation_fields() 
    84145        Form = modelform_factory(cls, fields=fields, exclude=('type', 'state')) 
     146        # replace textinputs with autocomplete inputs, see ticket #66 
     147        auto_complete_fields(Form, cls) 
    85148        if issubclass(cls, m.PLMObject): 
    86149            Form.clean_reference = _clean_reference 
     
    95158            Form.clean = _clean 
    96159        get_creation_form.cache[cls] = Form 
    97     form = Form(data=data, empty_permitted=empty_allowed) if data else Form() 
     160    if data is None: 
     161        form = Form(initial=get_initial_creation_data(cls, start)) 
     162    else: 
     163        form = Form(data=data) 
    98164    if issubclass(cls, m.PLMObject): 
    99165        # display only valid groups 
     
    110176        fields = cls.get_modification_fields() 
    111177        Form = modelform_factory(cls, fields=fields) 
     178        auto_complete_fields(Form, cls) 
    112179        get_modification_form.cache[cls] = Form 
    113180    if data: 
     
    225292get_search_form.cache = {}     
    226293 
    227 from haystack.query import EmptySearchQuerySet 
    228 from openPLM.plmapp.search import SmartSearchQuerySet 
    229  
    230294class SimpleSearchForm(forms.Form): 
    231295    q = forms.CharField(label=_("Query"), required=False) 
    232296 
    233297    def search(self, *models): 
     298        from haystack.query import EmptySearchQuerySet 
     299        from openPLM.plmapp.search import SmartSearchQuerySet 
     300 
    234301        if self.is_valid(): 
    235302            query = self.cleaned_data["q"].strip() 
     
    246313    quantity = forms.FloatField() 
    247314    order = forms.IntegerField() 
     315    unit = forms.ChoiceField(choices=UNITS, initial=DEFAULT_UNIT) 
    248316 
    249317 
     
    265333    class Meta: 
    266334        model = m.ParentChildLink 
    267         fields = ["order", "quantity", "child", "parent"] 
     335        fields = ["order", "quantity", "unit", "child", "parent",] 
    268336 
    269337ChildrenFormset = modelformset_factory(m.ParentChildLink, 
     
    526594get_headers_formset = memoize(get_headers_formset, {}, 1) 
    527595 
     596from openPLM.plmapp.archive import ARCHIVE_FORMATS 
     597class ArchiveForm(forms.Form): 
     598    format = forms.TypedChoiceField(choices=zip(ARCHIVE_FORMATS, ARCHIVE_FORMATS)) 
     599 
Note: See TracChangeset for help on using the changeset viewer.