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

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

Addition of files upload/deletion in a document

  • Property svn:executable set to *
Line 
1from django.shortcuts import render_to_response, get_object_or_404
2import datetime
3from operator import attrgetter
4
5import openPLM.plmapp.models as models
6from openPLM.plmapp.controllers import PLMObjectController, get_controller, DocumentController
7
8from django.db.models import Q
9from django.http import HttpResponseRedirect, QueryDict, HttpResponse
10
11from openPLM.plmapp.forms import *
12from openPLM.plmapp.utils import get_next_revision
13
14from django.contrib import auth
15from django.contrib.auth.decorators import login_required
16
17##########################################################################################
18def replace_white_spaces(Chain):
19    """ Replace all whitespace characteres by %20 in order to be compatible with an URL"""
20    return Chain.replace(" ","%20")
21
22##########################################################################################
23def get_obj(object_type_value, object_reference_value, object_revision_value):
24    """ Get Type, Reference and Revision and return an object """
25    obj = get_object_or_404(models.PLMObject, type=object_type_value,
26                            reference=object_reference_value,
27                            revision=object_revision_value)
28    # guess what kind of PLMObject (Part, Document) obj is
29    cls = models.PLMObject
30    find = True
31    while find:
32        find = False
33        for c in cls.__subclasses__():
34            if hasattr(obj, c.__name__.lower()):
35                cls  = c
36                obj = getattr(obj, c.__name__.lower())
37                find = True
38    user = models.User.objects.all()[0]
39    controller_cls = get_controller(object_type_value)
40    return controller_cls(obj, user)
41
42##########################################################################################
43def init_context_dict(init_type_value, init_reference_value, init_revision_value):
44    """ Initiate context_dict we used after to transfer parameters to html pages"""
45    now = datetime.datetime.now()
46    return {
47        'current_date': now,
48        'object_reference': init_reference_value,
49        'object_revision': init_revision_value,
50        'object_type': init_type_value,
51        }
52
53##########################################################################################
54###                   Manage html pages for Home Page                                  ###
55##########################################################################################
56
57def display_global_page(request_dict):
58    """ Get a request and return a dictionnary with elements common to all pages """
59    log_in_person="pjoulaud"
60    context_dict = {'log_in_person' : request_dict.user}
61    query_dict = {}
62    type_form_instance = type_form()
63    attributes_form_instance = attributes_form()
64    extra_attributes_form_instance = get_search_form()
65    results=[]
66    if request_dict.GET and "type" in request_dict.GET:
67        type_form_instance = type_form(request_dict.GET)
68        attributes_form_instance = attributes_form(request_dict.GET)
69        for key, value in request_dict.GET.items():
70            request_dict.session[key] = value
71        if attributes_form_instance.is_valid():
72            cls = models.get_all_plmobjects()[attributes_form_instance.cleaned_data["type"]]
73            extra_attributes_form_instance = get_search_form(cls, request_dict.GET)
74            for field, value in attributes_form_instance.cleaned_data.items():
75                if value and field != "type":
76                    query_dict["%s__icontains"%field]=value
77            results = cls.objects.filter(**query_dict)
78            request_dict.session["results"] = results
79            query_dict = {}
80            if extra_attributes_form_instance.is_valid():
81                for field, value in extra_attributes_form_instance.cleaned_data.items():
82                    if value:
83                        query_dict[field]=value
84                results = extra_attributes_form_instance.search(results)
85                request_dict.session["results"] = results
86    elif request_dict.session:
87        type_form_instance = type_form(request_dict.session)
88        attributes_form_instance = attributes_form(request_dict.session)
89        request_dict.session.update(request_dict.session)
90        if attributes_form_instance.is_valid():
91            cls = models.get_all_plmobjects()[attributes_form_instance.cleaned_data["type"]]
92            extra_attributes_form_instance = get_search_form(cls, request_dict.session)
93            for field, value in attributes_form_instance.cleaned_data.items():
94                if value:
95                    query_dict["%s__icontains"%field]=value
96            results = cls.objects.filter(**query_dict)
97            query_dict = {}
98            if extra_attributes_form_instance.is_valid():
99                for field, value in extra_attributes_form_instance.cleaned_data.items():
100                    if value:
101                        query_dict[field]=value
102                results = extra_attributes_form_instance.search(results)
103     
104    context_dict.update({'results': results, 'type_form': type_form_instance, 'attributes_form': attributes_form_instance, 'extra_attributes_form': extra_attributes_form_instance})
105
106    return context_dict, request_dict.session
107#    return render_to_response('display_home_page.htm', context_dict)
108
109##########################################################################################
110###                    Function which manage the html login page                        ###
111##########################################################################################
112
113def display_login_page(request):
114    if request.method == 'POST':
115        if request.POST:
116            username_value = request.POST['username']
117            password_value = request.POST['password']
118            user = auth.authenticate(username=username_value, password=password_value)
119            if user is not None and user.is_active:
120                auth.login(request, user)
121                return HttpResponseRedirect("/home/")
122            else:
123                return HttpResponse('Mauvais login, mauvais mot de passe ou compte inactif')
124        else:
125            return HttpResponse('mauvaise requete post')
126    else:
127        return render_to_response('DisplayLoginPage.htm', {})
128
129##########################################################################################
130###                    Function which manage the html home page                        ###
131##########################################################################################
132@login_required
133def display_home_page(request):
134    now = datetime.datetime.now()
135    context_dict, SessionDictionnary= display_global_page(request)
136    class_for_div="NavigateBox4Part"
137    context_dict.update({'class4div': class_for_div, 'current_date': now,})
138    request.session.update(SessionDictionnary)
139    return render_to_response('DisplayHomePage.htm', context_dict)
140
141##########################################################################################
142###     All functions which manage the different html pages related to a part          ###
143##########################################################################################
144@login_required
145def display_object(request, object_type_value, object_reference_value, object_revision_value):
146    """ Manage html page for attributes """
147    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
148    if isinstance(obj, DocumentController):
149        class_for_div="NavigateBox4Doc"
150    else:
151        class_for_div="NavigateBox4Part"
152    menu_list = obj.menu_items
153    object_attributes_list = []
154    for attr in obj.attributes:
155        item = obj._meta.get_field(attr).verbose_name
156        object_attributes_list.append((item, getattr(obj, attr)))
157    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
158    context_dict.update({'current_page':'attributes', 'class4div': class_for_div, 'object_menu': menu_list, 'object_attributes': object_attributes_list})
159    var_dict, request_dict = display_global_page(request)
160    request.session.update(request_dict)
161    print request.user
162    context_dict.update(var_dict)
163    return render_to_response('DisplayObject.htm', context_dict)
164
165##########################################################################################
166@login_required
167def display_object_lifecycle(request, object_type_value, object_reference_value, object_revision_value):
168    """ Manage html page for Lifecycle """
169    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
170    if isinstance(obj, DocumentController):
171        class_for_div="NavigateBox4Doc"
172    else:
173        class_for_div="NavigateBox4Part"
174    if request.method == 'POST':
175        if request.POST["action"] == "DEMOTE":
176            obj.demote()
177        elif request.POST["action"] == "PROMOTE":
178            obj.promote()
179    menu_list = obj.menu_items
180    state = obj.state.name
181    lifecycle = obj.lifecycle
182    object_lifecycle_list = []
183    for st in lifecycle:
184        object_lifecycle_list.append((st, st == state))
185    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
186    context_dict.update({'current_page':'lifecycle', 'class4div': class_for_div, 'object_menu': menu_list, 'object_lifecycle': object_lifecycle_list})
187    var_dict, request_dict = display_global_page(request)
188    request.session.update(request_dict)
189    context_dict.update(var_dict)
190    return render_to_response('DisplayObjectLifecycle.htm', context_dict)
191
192##########################################################################################
193@login_required
194def display_object_revisions(request, object_type_value, object_reference_value, object_revision_value):
195    """Manage html page for revisions"""
196    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
197    if isinstance(obj, DocumentController):
198        class_for_div="NavigateBox4Doc"
199    else:
200        class_for_div="NavigateBox4Part"
201    menu_list = obj.menu_items
202    if obj.is_revisable():
203        if request.method == "POST" and request.POST:
204            add_form = AddRevisionForm(request.POST)
205            if add_form.is_valid():
206                obj.revise(add_form.cleaned_data["revision"])
207        else:
208            add_form = AddRevisionForm({"revision" : get_next_revision(object_revision_value)})
209    else:
210        add_form = None
211    revisions = obj.get_all_revisions()
212    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
213    context_dict.update({'current_page':'revisions', 'class4div': class_for_div, 'object_menu': menu_list, 'revisions': revisions,
214                         'add_revision_form' : add_form})
215    var_dict, request_dict = display_global_page(request)
216    request.session.update(request_dict)
217    context_dict.update(var_dict)
218    return render_to_response('DisplayObjectRevisions.htm', context_dict)
219
220##########################################################################################
221@login_required
222def display_object_history(request, object_type_value, object_reference_value, object_revision_value):
223    """Manage html page for history"""
224    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
225    if isinstance(obj, DocumentController):
226        class_for_div="NavigateBox4Doc"
227    else:
228        class_for_div="NavigateBox4Part"
229    menu_list = obj.menu_items
230    histos = models.History.objects.filter(plmobject=obj.object).order_by('date')
231    object_history_list = []
232    for histo in histos:
233        object_history_list.append((histo.date, histo.action, histo.details))
234    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
235    context_dict.update({'current_page':'history', 'class4div': class_for_div, 'object_menu': menu_list, 'object_history': object_history_list})
236    var_dict, request_dict = display_global_page(request)
237    request.session.update(request_dict)
238    context_dict.update(var_dict)
239    return render_to_response('DisplayObjectHistory.htm', context_dict)
240
241##########################################################################################
242@login_required
243def display_object_child(request, object_type_value, object_reference_value, object_revision_value):
244    """ Manage html page for BOM and children of the part """
245    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
246    if isinstance(obj, DocumentController):
247        class_for_div="NavigateBox4Doc"
248    else:
249        class_for_div="NavigateBox4Part"
250    menu_list = obj.menu_items
251    if not hasattr(obj, "get_children"):
252        # TODO
253        raise TypeError()
254    date = None
255    level = "first"
256    if request.GET:
257        display_form = DisplayChildrenForm(request.GET)
258        if display_form.is_valid():
259            date = display_form.cleaned_data["date"]
260            level = display_form.cleaned_data["level"]
261    else:
262        display_form = DisplayChildrenForm(initial={"date" : datetime.datetime.now(),
263                                                    "level" : "first"})
264    max_level = 1 if level == "first" else -1
265    children = obj.get_children(max_level, date=date)
266    if level == "last" and children:
267        maximum = max(children, key=attrgetter("level")).level
268        children = (c for c in children if c.level == maximum)
269    # convert level to html space
270    children = ((" " * 2 * (level-1), link) for level, link in children)
271
272    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
273    context_dict.update({'current_page':'BOM-child', 'class4div': class_for_div, 'object_menu': menu_list, 'obj' : obj,
274                                 'children': children, "display_form" : display_form})
275    var_dict, request_dict = display_global_page(request)
276    request.session.update(request_dict)
277    context_dict.update(var_dict)
278    return render_to_response('DisplayObjectChild.htm', context_dict)
279
280##########################################################################################
281@login_required
282def edit_children(request, object_type_value, object_reference_value, object_revision_value):
283    """ Manage html page for BOM and children of the part : edition"""
284    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
285    if isinstance(obj, DocumentController):
286        class_for_div="NavigateBox4Doc"
287    else:
288        class_for_div="NavigateBox4Part"
289    menu_list = obj.menu_items
290    if not hasattr(obj, "get_children"):
291        # TODO
292        raise TypeError()
293    if request.method == "POST":
294        if request.POST.get("action", "Undo") == "Undo":
295            return HttpResponseRedirect("..")
296        formset = get_children_formset(obj, request.POST)
297        if formset.is_valid():
298            obj.update_children(formset)
299            return HttpResponseRedirect("..")
300    else:
301        formset = get_children_formset(obj)
302    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
303    context_dict.update({'current_page':'BOM-child', 'class4div': class_for_div, 'object_menu': menu_list, 'obj' : obj,
304                                 'children_formset': formset, })
305    var_dict, request_dict = display_global_page(request)
306    request.session.update(request_dict)
307    context_dict.update(var_dict)
308    return render_to_response('DisplayObjectChildEdit.htm', context_dict)
309
310##########################################################################################   
311@login_required
312def add_children(request, object_type_value, object_reference_value, object_revision_value):
313    """ Manage html page for BOM and children of the part : add new link"""
314    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
315    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
316    if isinstance(obj, DocumentController):
317        class_for_div="NavigateBox4Doc"
318    else:
319        class_for_div="NavigateBox4Part"
320    menu_list = obj.menu_items
321    var_dict, request_dict = display_global_page(request)
322    request.session.update(request_dict)
323    context_dict.update(var_dict)
324    if request.POST:
325        add_child_form_instance = add_child_form(request.POST)
326        if add_child_form_instance.is_valid():
327            child_obj = get_obj(add_child_form_instance.cleaned_data["type"], \
328                        add_child_form_instance.cleaned_data["reference"], \
329                        add_child_form_instance.cleaned_data["revision"])
330            obj.add_child(child_obj, \
331                            add_child_form_instance.cleaned_data["quantity"], \
332                            add_child_form_instance.cleaned_data["order"])
333            context_dict.update({'object_menu': menu_list, 'add_child_form': add_child_form_instance, })
334            return HttpResponseRedirect("/object/%s/%s/%s/BOM-child/" \
335                                        % (object_type_value, object_reference_value, object_revision_value) )
336        else:
337            add_child_form_instance = add_child_form(request.POST)
338            context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_child_form': add_child_form_instance, })
339            return render_to_response('DisplayObjectChildAdd.htm', context_dict)
340    else:
341        add_child_form_instance = add_child_form()
342        context_dict.update({'current_page':'BOM-child', 'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_child_form': add_child_form_instance, })
343        return render_to_response('DisplayObjectChildAdd.htm', context_dict)
344   
345##########################################################################################   
346@login_required
347def display_object_parents(request, object_type_value, object_reference_value, object_revision_value):
348    """ Manage html page for "where is used / parents" of the part """
349    obj = get_obj( object_type_value, object_reference_value, object_revision_value)
350    if isinstance(obj, DocumentController):
351        class_for_div="NavigateBox4Doc"
352    else:
353        class_for_div="NavigateBox4Part"
354    menu_list = obj.menu_items
355    if not hasattr(obj, "get_parents"):
356        # TODO
357        raise TypeError()
358    date = None
359    level = "first"
360    if request.GET:
361        display_form = DisplayChildrenForm(request.GET)
362        if display_form.is_valid():
363            date = display_form.cleaned_data["date"]
364            level = display_form.cleaned_data["level"]
365    else:
366        display_form = DisplayChildrenForm(initial={"date" : datetime.datetime.now(),
367                                                    "level" : "first"})
368    max_level = 1 if level == "first" else -1
369    parents = obj.get_parents(max_level, date=date)
370    if level == "last" and parents:
371        maximum = max(parents, key=attrgetter("level")).level
372        parents = (c for c in parents if c.level == maximum)
373
374    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
375    context_dict.update({'current_page':'parents', 'class4div': class_for_div, 'object_menu': menu_list, 'parents' :  parents,
376                                 'display_form' : display_form, 'obj': obj})
377    var_dict, request_dict = display_global_page(request)
378    request.session.update(request_dict)
379    context_dict.update(var_dict)
380    return render_to_response('DisplayObjectParents.htm', context_dict)
381
382##########################################################################################
383@login_required
384def display_object_doc_cad(request, object_type_value, object_reference_value, object_revision_value):
385    """ Manage html page for related documents and CAD of the part"""
386    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
387    if isinstance(obj, DocumentController):
388        class_for_div="NavigateBox4Doc"
389    else:
390        class_for_div="NavigateBox4Part"
391    menu_list = obj.menu_items
392    object_doc_cad_list = [
393        (["Type", "Reference", "Revision", "Name", "Status"], ""),
394        (["Rapport essais", "Doc0045", "a", "Essais de compatibilite electro-magnetique", "official"], replace_white_spaces("/object/Rapport essais/Doc0045/a/")),
395        (["Incident Qualite", "Doc0066", "b", "Probleme de casse chaine", "obsolete"], replace_white_spaces("/object/Incident Qualite/Doc0066/b/")),
396        (["CatDrawing", "Cad00123", "a", "Vue d'ensemble", "official"], replace_white_spaces("/object/CatDrawing/Cad00123/a/")),
397        ]
398    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
399    context_dict.update({'current_page':'doc-cad', 'class4div': class_for_div, 'object_menu': menu_list, 'object_doc_cad': object_doc_cad_list})
400    var_dict, request_dict = display_global_page(request)
401    request.session.update(request_dict)
402    context_dict.update(var_dict)
403    return render_to_response('DisplayObjectDocCad.htm', context_dict)
404   
405   
406
407    if not hasattr(obj, "get_attached_parts"):
408        # TODO
409        raise TypeError()
410    if request.method == "POST":
411        print request.POST.items()
412        formset = get_rel_part_formset(obj, request.POST)
413        if formset.is_valid():
414            obj.update_rel_part(formset)
415            return HttpResponseRedirect(".")
416    else:
417        formset = get_rel_part_formset(obj)
418    object_rel_part_list = obj.get_attached_parts()
419    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
420    context_dict.update({'current_page':'parts', 'class4div': class_for_div, 'object_menu': menu_list, 'object_rel_part': object_rel_part_list, 'rel_part_formset': formset})
421    var_dict, request_dict = display_global_page(request)
422    request.session.update(request_dict)
423    context_dict.update(var_dict)
424    return render_to_response('DisplayObjectRelPart.htm', context_dict)
425
426
427##########################################################################################   
428@login_required
429def add_doc_cad(request, object_type_value, object_reference_value, object_revision_value):
430    """ Manage html page for BOM and children of the part : add new link"""
431    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
432    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
433    if isinstance(obj, DocumentController):
434        class_for_div="NavigateBox4Doc"
435    else:
436        class_for_div="NavigateBox4Part"
437    menu_list = obj.menu_items
438    var_dict, request_dict = display_global_page(request)
439    request.session.update(request_dict)
440    context_dict.update(var_dict)
441    if request.POST:
442        add_doc_cad_form_instance = add_doc_cad_form(request.POST)
443        if add_doc_cad_form_instance.is_valid():
444            doc_cad_obj = get_obj(add_doc_cad_form_instance.cleaned_data["type"], \
445                        add_doc_cad_form_instance.cleaned_data["reference"], \
446                        add_doc_cad_form_instance.cleaned_data["revision"])
447            obj.add_doc_cad(doc_cad_obj)
448            context_dict.update({'object_menu': menu_list, 'add_doc_cad_form': add_doc_cad_form_instance, })
449            return HttpResponseRedirect("/object/%s/%s/%s/doc-cad/" \
450                                        % (object_type_value, object_reference_value, object_revision_value) )
451        else:
452            add_doc_cad_form_instance = add_doc_cad_form(request.POST)
453            context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_doc_cad_form': add_doc_cad_form_instance, })
454            return render_to_response('DisplayDocCadAdd.htm', context_dict)
455    else:
456        add_doc_cad_form_instance = add_doc_cad_form()
457        context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_doc_cad_form': add_doc_cad_form_instance, })
458        return render_to_response('DisplayDocCadAdd.htm', context_dict)
459   
460##########################################################################################
461@login_required
462def display_related_part(request, object_type_value, object_reference_value, object_revision_value):
463    """ Manage html page for related parts of the document"""
464    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
465    if isinstance(obj, DocumentController):
466        class_for_div="NavigateBox4Doc"
467    else:
468        class_for_div="NavigateBox4Part"
469    menu_list = obj.menu_items
470    if not hasattr(obj, "get_attached_parts"):
471        # TODO
472        raise TypeError()
473    if request.method == "POST":
474        print request.POST.items()
475        formset = get_rel_part_formset(obj, request.POST)
476        if formset.is_valid():
477            obj.update_rel_part(formset)
478            return HttpResponseRedirect(".")
479    else:
480        formset = get_rel_part_formset(obj)
481    object_rel_part_list = obj.get_attached_parts()
482    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
483    context_dict.update({'current_page':'parts', 'class4div': class_for_div, 'object_menu': menu_list, 'object_rel_part': object_rel_part_list, 'rel_part_formset': formset})
484    var_dict, request_dict = display_global_page(request)
485    request.session.update(request_dict)
486    context_dict.update(var_dict)
487    return render_to_response('DisplayObjectRelPart.htm', context_dict)
488
489##########################################################################################   
490@login_required
491def add_rel_part(request, object_type_value, object_reference_value, object_revision_value):
492    """ Manage html page for related part of the document : add new link"""
493    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
494    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
495    if isinstance(obj, DocumentController):
496        class_for_div="NavigateBox4Doc"
497    else:
498        class_for_div="NavigateBox4Part"
499    menu_list = obj.menu_items
500    var_dict, request_dict = display_global_page(request)
501    request.session.update(request_dict)
502    context_dict.update(var_dict)
503    if request.POST:
504        add_rel_part_form_instance = AddRelPartForm(request.POST)
505        if add_rel_part_form_instance.is_valid():
506            part_obj = get_obj(add_rel_part_form_instance.cleaned_data["type"], \
507                        add_rel_part_form_instance.cleaned_data["reference"], \
508                        add_rel_part_form_instance.cleaned_data["revision"])
509            obj.attach_to_part(part_obj)
510            context_dict.update({'object_menu': menu_list, 'add_rel_part_form': add_rel_part_form_instance, })
511            return HttpResponseRedirect("/object/%s/%s/%s/parts/" \
512                                        % (object_type_value, object_reference_value, object_revision_value) )
513        else:
514            add_rel_part_form_instance = add_rel_part_form(request.POST)
515            context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_rel_part_form': add_rel_part_form_instance, })
516            return render_to_response('DisplayRelPartAdd.htm', context_dict)
517    else:
518        add_rel_part_form_instance = AddRelPartForm()
519        context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_rel_part_form': add_rel_part_form_instance, })
520        return render_to_response('DisplayRelPartAdd.htm', context_dict)
521
522##########################################################################################
523@login_required
524def display_files(request, object_type_value, object_reference_value, object_revision_value):
525    """ Manage html page for files contained by the document"""
526    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
527    if isinstance(obj, DocumentController):
528        class_for_div="NavigateBox4Doc"
529    else:
530        class_for_div="NavigateBox4Part"
531    menu_list = obj.menu_items
532
533    if not hasattr(obj, "files"):
534        # TODO
535        raise TypeError()
536    if request.method == "POST":
537        formset = get_file_formset(obj, request.POST)
538        if formset.is_valid():
539            obj.update_file(formset)
540            return HttpResponseRedirect(".")
541    else:
542        formset = get_file_formset(obj)
543
544    object_files_list = obj.files
545    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
546    context_dict.update({'current_page':'files', 'class4div': class_for_div, 'object_menu': menu_list, 'object_files': object_files_list, 'file_formset': formset})
547    var_dict, request_dict = display_global_page(request)
548    request.session.update(request_dict)
549    context_dict.update(var_dict)
550    return render_to_response('DisplayObjectFiles.htm', context_dict)
551
552##########################################################################################
553@login_required
554def add_file(request, object_type_value, object_reference_value, object_revision_value):
555    """ Manage html page for file addition in the document"""
556    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
557    obj = get_obj(object_type_value, object_reference_value, object_revision_value)
558    if isinstance(obj, DocumentController):
559        class_for_div="NavigateBox4Doc"
560    else:
561        class_for_div="NavigateBox4Part"
562    menu_list = obj.menu_items
563    var_dict, request_dict = display_global_page(request)
564    request.session.update(request_dict)
565    context_dict.update(var_dict)
566    if request.POST:
567        print request.FILES
568        add_file_form_instance = AddFileForm(request.POST, request.FILES)
569        if add_file_form_instance.is_valid():
570            obj.add_file(request.FILES["filename"])
571            context_dict.update({'object_menu': menu_list, 'add_file_form': add_file_form_instance, })
572            return HttpResponseRedirect("/object/%s/%s/%s/files/" \
573                                        % (object_type_value, object_reference_value, object_revision_value) )
574        else:
575            add_file_form_instance = AddFileForm(request.POST)
576            context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_file_form': add_file_form_instance, })
577            return render_to_response('DisplayRelPartAdd.htm', context_dict)
578    else:
579        add_file_form_instance = AddFileForm()
580        context_dict.update({'class4search_div': True, 'class4div': class_for_div, 'object_menu': menu_list, 'add_file_form': add_file_form_instance, })
581        return render_to_response('DisplayFileAdd.htm', context_dict)
582
583##########################################################################################
584###             Manage html pages for part creation / modification                     ###
585##########################################################################################
586def create_non_modifyable_attributes_list(Classe=models.PLMObject):
587    """ Create a list of an object's attributes we can't modify' and set them a value """
588    non_modifyable_fields_list = Classe.excluded_creation_fields()
589    non_modifyable_attributes_list=[]
590    non_modifyable_attributes_list.append((non_modifyable_fields_list[0], 'Person'))
591    non_modifyable_attributes_list.append((non_modifyable_fields_list[1], 'Person'))
592    non_modifyable_attributes_list.append((non_modifyable_fields_list[2], 'Date'))
593    non_modifyable_attributes_list.append((non_modifyable_fields_list[3], 'Date'))
594    return non_modifyable_attributes_list
595
596##########################################################################################
597@login_required
598def create_object(request):
599    """ Manage html page for the part creation """
600    now = datetime.datetime.now()
601    context_dict = {'current_date': now}
602    var_dict, request_dict = display_global_page(request)
603    request.session.update(request_dict)
604    context_dict.update(var_dict)
605    log_in_person="pjoulaud"
606    context_dict.update({'log_in_person' : log_in_person})
607    if request.method == 'GET':
608        if request.GET:
609            type_form_instance = type_form(request.GET)
610            if type_form_instance.is_valid():
611                cls = models.get_all_plmobjects()[type_form_instance.cleaned_data["type"]]
612                if issubclass(cls, models.Document):
613                    class_for_div="NavigateBox4Doc"
614                else:
615                    class_for_div="NavigateBox4Part"
616                creation_form_instance = get_creation_form(cls, {'revision':'a', 'lifecycle': str(models.get_default_lifecycle()), }, True)
617                non_modifyable_attributes_list = create_non_modifyable_attributes_list(cls)
618    elif request.method == 'POST':
619        if request.POST:
620            type_form_instance = type_form(request.POST)
621            if type_form_instance.is_valid():
622                type_name = type_form_instance.cleaned_data["type"]
623                cls = models.get_all_plmobjects()[type_name]
624                if issubclass(cls, models.Document):
625                    class_for_div="NavigateBox4Doc"
626                else:
627                    class_for_div="NavigateBox4Part"
628                non_modifyable_attributes_list = create_non_modifyable_attributes_list(cls)
629                creation_form_instance = get_creation_form(cls, request.POST)
630                if creation_form_instance.is_valid():
631                    user = models.User.objects.get(username=context_dict['log_in_person'])
632                    controller_cls = get_controller(type_name)
633                    controller = PLMObjectController.create_from_form(creation_form_instance, user)
634                    return HttpResponseRedirect("/object/%s/%s/%s/" % (controller.type, controller.reference, controller.revision) )
635    context_dict.update({'class4div': class_for_div, 'creation_form': creation_form_instance, 'object_type': type_form_instance.cleaned_data["type"], 'non_modifyable_attributes': non_modifyable_attributes_list })
636    return render_to_response('DisplayObject4creation.htm', context_dict)
637
638##########################################################################################
639@login_required
640def modify_object(request, object_type_value, object_reference_value, object_revision_value):
641    """ Manage html page for part modification """
642    log_in_person="pjoulaud"
643    now = datetime.datetime.now()
644    context_dict = init_context_dict(object_type_value, object_reference_value, object_revision_value)
645    context_dict.update({'log_in_person' : log_in_person})
646    cls = models.get_all_plmobjects()[object_type_value]
647    non_modifyable_attributes_list = create_non_modifyable_attributes_list(cls)
648    current_object = get_obj(object_type_value, object_reference_value, object_revision_value)
649    if isinstance(obj, DocumentController):
650        class_for_div="NavigateBox4Doc"
651    else:
652        class_for_div="NavigateBox4Part"
653    if request.method == 'POST':
654        if request.POST:
655            modification_form_instance = get_modification_form(cls, request.POST)
656            if modification_form_instance.is_valid():
657                user = models.User.objects.get(username=log_in_person)
658                current_object.update_from_form(modification_form_instance)
659                return HttpResponseRedirect("/object/%s/%s/%s/" % (current_object.type, current_object.reference, current_object.revision) )
660            else:
661                pass
662        else:
663            modification_form_instance = get_modification_form(cls, instance = current_object.object)
664    else:
665        modification_form_instance = get_modification_form(cls, instance = current_object.object)
666    var_dict, request_dict = display_global_page(request)
667    request.session.update(request_dict)
668    context_dict.update(var_dict)
669    context_dict.update({'class4div': class_for_div, 'modification_form': modification_form_instance, 'non_modifyable_attributes': non_modifyable_attributes_list})
670    return render_to_response('DisplayObject4modification.htm', context_dict)
671   
672##########################################################################################
673def display_bollox(request):
674    context_dict={}
675    return render_to_response('bollox.htm', context_dict)
676   
677   
678   
679   
680   
681   
682   
683   
684   
Note: See TracBrowser for help on using the repository browser.