source: main/trunk/openPLM/plmapp/forms.py @ 65

Revision 65, 6.3 KB checked in by pjoulaud, 11 years ago (diff)

Addition of files upload/deletion in a document

Line 
1from django import forms
2from django.forms.models import modelform_factory, modelformset_factory
3
4import openPLM.plmapp.models as m
5
6
7def get_creation_form(cls=m.PLMObject, data=None, empty_allowed=False):
8    u"""
9    Returns a creation form suitable to creates an object
10    of type *cls*.
11
12    The returned form can be used, if it is valid, with the function
13    :meth:`~plmapp.controllers.PLMObjectController.create_from_form`
14    to create a :class:`~plmapp.models.PLMObject` and his associated
15    :class:`~plmapp.controllers.PLMObjectController`.
16
17    If *initial* is provided, it will be used to fill the form.
18    """
19
20    fields = cls.get_creation_fields()
21    Form = modelform_factory(cls, fields=fields, exclude=('type', 'state'))
22    if data:
23        return Form(data=data, empty_permitted=empty_allowed)
24    else:
25        return Form()
26       
27def get_modification_form(cls=m.PLMObject, data=None, instance=None):
28    fields = cls.get_modification_fields()
29    Form = modelform_factory(cls, fields=fields)
30    if data:
31        return Form(data)
32    elif instance:
33        return Form(instance=instance)
34    else:
35        return Form()
36
37def get_search_form(cls=m.PLMObject, data=None):
38    fields = set(cls.get_creation_fields())
39    fields.update(set(cls.get_modification_fields()))
40    fields.difference_update(("revision", "type", "reference", "lifecycle"))
41    fields_dict = {}
42    for field in fields:
43        model_field = cls._meta.get_field(field)
44        form_field = model_field.formfield()
45        form_field.help_text = ""
46        if isinstance(form_field.widget, forms.Textarea):
47            form_field.widget = forms.TextInput()
48        form_field.required = False
49        fields_dict[field] = form_field
50   
51    def search(self, query_set=None):
52        if self.is_valid():
53            query = {}
54            for field in self.changed_data:
55                if isinstance(cls._meta.get_field(field),
56                              (m.models.CharField, m.models.TextField)):
57                    query[field + "__icontains"] = self.cleaned_data[field]
58                else:
59                    query[field] = self.cleaned_data[field]
60            if query_set is not None:
61                return query_set.filter(**query)
62            else:
63                return []
64   
65    Form = type("Search%sForm" % cls.__name__,
66                (forms.BaseForm,),
67                {"base_fields" : fields_dict, "search" : search})
68    if data is not None:
69        return Form(data=data, empty_permitted=True)
70    else:
71        return Form(empty_permitted=True)
72   
73class type_form(forms.Form):
74    DICT = m.get_all_plmobjects()
75    TYPES = [(v, v) for v in DICT]
76    type = forms.TypedChoiceField(choices=TYPES)
77
78class attributes_form(type_form):
79    reference = forms.CharField(widget=forms.TextInput(), required=False)
80    revision = forms.CharField(widget=forms.TextInput(), required=False)
81   
82def get_attr_search_form(cls=m.PLMObject, data=None, instance=None):
83    fields = cls.get_modification_fields()
84    Form = modelform_factory(cls, fields=fields)
85    if data:
86        return Form(data)
87    elif instance:
88        return Form(instance=instance)
89    else:
90        return Form()
91       
92class add_child_form(forms.Form):
93    type = forms.CharField()
94    reference = forms.CharField()
95    revision = forms.CharField()
96    quantity = forms.FloatField()
97    order = forms.IntegerField()
98
99class DisplayChildrenForm(forms.Form):
100    LEVELS = (("all", "All levels",),
101              ("first", "First level",),
102              ("last", "Last level"),)
103    level = forms.ChoiceField(choices=LEVELS, widget=forms.RadioSelect())
104    date = forms.DateTimeField(required=False)
105
106class ModifyChildForm(forms.ModelForm):
107    delete = forms.BooleanField(required=False, initial=False)
108    parent = forms.ModelChoiceField(queryset=m.Part.objects.all(),
109                                   widget=forms.HiddenInput())
110    child = forms.ModelChoiceField(queryset=m.Part.objects.all(),
111                                   widget=forms.HiddenInput())
112    quantity = forms.FloatField(widget=forms.TextInput(attrs={'size':'4'}))
113    order = forms.IntegerField(widget=forms.TextInput(attrs={'size':'2'}))
114    class Meta:
115        model = m.ParentChildLink
116        fields = ["order", "quantity", "child", "parent"]
117
118def get_children_formset(controller, data=None):
119    Formset = modelformset_factory(m.ParentChildLink, form=ModifyChildForm, extra=0)
120    if data is None:
121        queryset = m.ParentChildLink.objects.filter(parent=controller,
122                                                    end_time__exact=None)
123        formset = Formset(queryset=queryset)
124    else:
125        formset = Formset(data=data)
126    return formset
127
128class AddRevisionForm(forms.Form):
129    revision = forms.CharField()
130   
131class AddRelPartForm(forms.Form):
132    type = forms.CharField()
133    reference = forms.CharField()
134    revision = forms.CharField()
135   
136class ModifyRelPartForm(forms.ModelForm):
137    delete = forms.BooleanField(required=False, initial=False)
138    document = forms.ModelChoiceField(queryset=m.Document.objects.all(),
139                                   widget=forms.HiddenInput())
140    part = forms.ModelChoiceField(queryset=m.Part.objects.all(),
141                                   widget=forms.HiddenInput())
142    class Meta:
143        model = m.DocumentPartLink
144        fields = ["document", "part"]
145       
146def get_rel_part_formset(controller, data=None):
147    Formset = modelformset_factory(m.DocumentPartLink, form=ModifyRelPartForm, extra=0)
148    if data is None:
149        queryset = controller.get_attached_parts()
150        formset = Formset(queryset=queryset)
151    else:
152        formset = Formset(data=data)
153    return formset
154
155
156
157
158
159class AddFileForm(forms.Form):
160    filename = forms.FileField()
161   
162class ModifyFileForm(forms.ModelForm):
163    delete = forms.BooleanField(required=False, initial=False)
164    document = forms.ModelChoiceField(queryset=m.Document.objects.all(),
165                                   widget=forms.HiddenInput())
166    class Meta:
167        model = m.DocumentFile
168        fields = ["document"]
169       
170def get_file_formset(controller, data=None):
171    Formset = modelformset_factory(m.DocumentFile, form=ModifyFileForm, extra=0)
172    if data is None:
173        queryset = controller.files
174        formset = Formset(queryset=queryset)
175    else:
176        formset = Formset(data=data)
177    return formset
178
179
180
181
Note: See TracBrowser for help on using the repository browser.