Ejemplo n.º 1
0
def ajax_add_child(request, part_id):
    part = get_obj_by_id(part_id, request.user)
    data = {}
    if request.GET:
        form = forms.AddChildForm(part.object, initial=request.GET)
    else:
        form = forms.AddChildForm(part.object, request.POST)
        if form.is_valid():
            child = get_obj_from_form(form, request.user)
            part.add_child(child, form.cleaned_data["quantity"],
                           form.cleaned_data["order"],
                           form.cleaned_data["unit"], **form.extensions)
            return {"result": "ok"}
        else:
            data["result"] = "error"
            data["error"] = "invalid form"
    form.fields["type"].widget = widgets.TextInput()
    for field in ("type", "reference", "revision"):
        form.fields[field].widget.attrs['readonly'] = 'on'
    data.update({
        "parent": {
            "id": part.id,
            "type": part.type,
            "reference": part.reference,
            "revision": part.revision,
        },
        "form": form.as_table()
    })
    return data
Ejemplo n.º 2
0
def ajax_attach(request, plmobject_id):
    plmobject = get_obj_by_id(plmobject_id, request.user)
    data = {}
    if request.GET:
        form = forms.PLMObjectForm(initial=request.GET)
    else:
        form = forms.PLMObjectForm(request.POST)
        if form.is_valid():
            attached = get_obj_from_form(form, request.user)
            if hasattr(plmobject, "attach_to_document"):
                plmobject.attach_to_document(attached)
            elif hasattr(plmobject, "attach_to_part"):
                plmobject.attach_to_part(attached)
            return {"result": "ok"}
        else:
            data["result"] = "error"
            data["error"] = "invalid form"
    for field in ("type", "reference", "revision"):
        form.fields[field].widget.attrs['readonly'] = 'on'
    data.update({
        "plmobject": {
            "id": plmobject.id,
            "type": plmobject.type,
            "reference": plmobject.reference,
            "revision": plmobject.revision,
        },
        "form": form.as_table()
    })
    return data
Ejemplo n.º 3
0
def add_child(request, obj_type, obj_ref, obj_revi):
    """
    View to add a child to a part.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`parts/bom_add.html`

    **Context:**

    ``RequestContext``

    ``add_child_form``
        a form to add a child (:class:`.AddChildForm`)

    ``link``
        :class:`.ParentChildLink` being replaced

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "add_child")

    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi,
            load_all=True)

    if request.method == "POST" and request.POST:
        add_child_form = forms.AddChildForm(obj.object, request.POST)
        if add_child_form.is_valid():
            child_obj = get_obj_from_form(add_child_form, request.user)
            obj.add_child(child_obj,
                          add_child_form.cleaned_data["quantity"],
                          add_child_form.cleaned_data["order"],
                          add_child_form.cleaned_data["unit"],
                          **add_child_form.extensions)
            return HttpResponseRedirect(obj.plmobject_url + "BOM-child/")
    else:
        if "type" in request.GET and request.GET["type"] in models.get_all_parts():
            # use GET params only if they seems valid
            initial = request.GET
        else:
            initial = None
        add_child_form = forms.AddChildForm(obj.object, initial=initial)
        ctx['current_page'] = 'BOM-child'
    if ctx["results"]:
        obj.precompute_can_add_child2()
        orders = list(obj.parentchildlink_parent.values_list('order', flat=True))
        initial_order = max(orders) + 10 if orders else 10
        ctx['order'] = initial_order
    ctx.update({'link_creation': True,
                'add_child_form': add_child_form,
                'attach' : (obj, "add_child")})
    return r2r('parts/bom_add.html', ctx, request)
Ejemplo n.º 4
0
def add_management(request, obj_type, obj_ref, obj_revi, reader=False, level=None):
    """
    View to add a manager (notified user or restricted reader).

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/add/`

    or

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/add-reader/`

    .. include:: views_params.txt
    :param reader: True to add a restricted reader instead of a notified user

    **Template:**

    :file:`management_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_manager_form``
        a form to select the new manager (a user)

    ``link_creation``
        Set to True

    ``role``
        role of the new user (:const:`.ROLE_NOTIFIED` or :const:`.ROLE_READER`)

    ``attach``
        set to (*obj*, :samp:`"add_{role}"`)

    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    if level is None:
        role =  models.ROLE_READER if reader else models.ROLE_NOTIFIED
    else:
        role = level_to_sign_str(int(level))
    if request.method == "POST":
        add_management_form = forms.SelectUserForm(request.POST)
        if add_management_form.is_valid():
            if add_management_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(add_management_form, request.user)
                obj.set_role(user_obj.object, role)
            message = _(u"Role %(add_role)s granted." % dict(add_role=role))
            messages.info(request, message)
            return HttpResponseRedirect("../../lifecycle/")
    else:
        add_management_form = forms.SelectUserForm()

    ctx.update({'current_page':'lifecycle',
                'replace_manager_form': add_management_form,
                'link_creation': True,
                'role' : role,
                "attach" : (obj, "add_" + role)})
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 5
0
def ajax_can_add_child(request, part_id):
    part = get_obj_by_id(part_id, request.user)
    data = {"can_add": False}
    if part.is_part and request.GET:
        form = forms.AddPartForm(request.GET)
        if form.is_valid():
            child = get_obj_from_form(form, request.user)
            data["can_add"] = part.can_add_child(child)
    return data
Ejemplo n.º 6
0
def add_management(request, obj_type, obj_ref, obj_revi, reader=False, level=None):
    """
    View to add a manager (notified user or restricted reader).

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/add/`

    or

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/add-reader/`

    .. include:: views_params.txt
    :param reader: True to add a restricted reader instead of a notified user

    **Template:**

    :file:`management_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_manager_form``
        a form to select the new manager (a user)

    ``link_creation``
        Set to True

    ``role``
        role of the new user (:const:`.ROLE_NOTIFIED` or :const:`.ROLE_READER`)

    ``attach``
        set to (*obj*, :samp:`"add_{role}"`)

    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    if level is None:
        role =  models.ROLE_READER if reader else models.ROLE_NOTIFIED
    else:
        role = level_to_sign_str(int(level))
    if request.method == "POST":
        add_management_form = forms.SelectUserForm(request.POST)
        if add_management_form.is_valid():
            if add_management_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(add_management_form, request.user)
                obj.set_role(user_obj.object, role)
            message = _(u"Role %(add_role)s granted." % dict(add_role=role))
            messages.info(request, message)
            return HttpResponseRedirect("../../lifecycle/")
    else:
        add_management_form = forms.SelectUserForm()

    ctx.update({'current_page':'lifecycle',
                'replace_manager_form': add_management_form,
                'link_creation': True,
                'role' : role,
                "attach" : (obj, "add_" + role)})
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 7
0
def replace_management(request, obj_type, obj_ref, obj_revi, link_id):
    """
    View to replace a manager (owner, signer, reader...) by another one.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/replace/{link_id}/`

    .. include:: views_params.txt
    :param link_id: id of the :class:`.PLMObjectUserLink` being replaced

    **Template:**

    :file:`management_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_manager_form``
        a form to select the new manager (a user)

    ``link_creation``
        Set to True

    ``role``
        role of the link being replace

    ``attach``
        set to (*obj*, :samp:`"add_{role}"`)
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    link = obj.users.now().get(id=int(link_id))

    if request.method == "POST":
        replace_manager_form = forms.SelectUserForm(request.POST)
        if replace_manager_form.is_valid():
            if replace_manager_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(replace_manager_form, request.user)
                if link.role.startswith(models.ROLE_SIGN):
                    obj.replace_signer(link.user, user_obj.object, link.role)
                else:
                    obj.set_role(user_obj.object, link.role)
                    if link.role == models.ROLE_NOTIFIED:
                        obj.remove_notified(link.user)
                    elif link.role == models.ROLE_READER:
                        obj.remove_reader(link.user)
            return HttpResponseRedirect("../../../lifecycle/")
    else:
        replace_manager_form = forms.SelectUserForm()

    ctx.update({'current_page':'lifecycle',
                'replace_manager_form': replace_manager_form,
                'link_creation': True,
                'role' : link.role,
                'attach' : (obj, "add_" + link.role)})
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 8
0
def replace_management(request, obj_type, obj_ref, obj_revi, link_id):
    """
    View to replace a manager (owner, signer, reader...) by another one.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/management/replace/{link_id}/`

    .. include:: views_params.txt
    :param link_id: id of the :class:`.PLMObjectUserLink` being replaced

    **Template:**

    :file:`management_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_manager_form``
        a form to select the new manager (a user)

    ``link_creation``
        Set to True

    ``role``
        role of the link being replace

    ``attach``
        set to (*obj*, :samp:`"add_{role}"`)
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    link = obj.users.now().get(id=int(link_id))

    if request.method == "POST":
        replace_manager_form = forms.SelectUserForm(request.POST)
        if replace_manager_form.is_valid():
            if replace_manager_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(replace_manager_form, request.user)
                if link.role.startswith(models.ROLE_SIGN):
                    obj.replace_signer(link.user, user_obj.object, link.role)
                else:
                    obj.set_role(user_obj.object, link.role)
                    if link.role == models.ROLE_NOTIFIED:
                        obj.remove_notified(link.user)
                    elif link.role == models.ROLE_READER:
                        obj.remove_reader(link.user)
            return HttpResponseRedirect("../../../lifecycle/")
    else:
        replace_manager_form = forms.SelectUserForm()

    ctx.update({'current_page':'lifecycle',
                'replace_manager_form': replace_manager_form,
                'link_creation': True,
                'role' : link.role,
                'attach' : (obj, "add_" + link.role)})
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 9
0
def delegate(request, obj_ref, role, sign_level):
    """
    Manage html page for delegations modification of the selected
    :class:`~django.contrib.auth.models.User`.
    It computes a context dictionary based on

    :param request: :class:`django.http.QueryDict`
    :param obj_type: :class:`~django.contrib.auth.models.User`
    :type obj_ref: str
    :param role: :attr:`.DelegationLink.role` if role is not "sign"
    :type role: str
    :param sign_level: used for :attr:`.DelegationLink.role` if role is "sign"
    :type sign_level: str
    :return: a :class:`django.http.HttpResponse`
    """
    obj, ctx = get_generic_data(request, "User", obj_ref)

    if request.method == "POST":
        delegation_form = forms.SelectUserForm(request.POST)
        if delegation_form.is_valid():
            if delegation_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(delegation_form, request.user)
                if role == "notified" or role == "owner":
                    obj.delegate(user_obj.object, role)
                    return HttpResponseRedirect("../..")
                elif role == "sign":
                    if sign_level == "all":
                        obj.delegate(user_obj.object, "sign*")
                        return HttpResponseRedirect("../../..")
                    elif sign_level.isdigit():
                        obj.delegate(user_obj.object,
                                     level_to_sign_str(int(sign_level) - 1))
                        return HttpResponseRedirect("../../..")
    else:
        delegation_form = forms.SelectUserForm()
    if role == 'sign':
        if sign_level.isdigit():
            role = _("signer level") + " " + str(sign_level)
        else:
            role = _("signer all levels")
    elif role == "notified":
        role = _("notified")

    ctx.update({
        'current_page': 'delegation',
        'replace_manager_form': delegation_form,
        'link_creation': True,
        'attach': (obj, "delegate"),
        'role': role
    })
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 10
0
def replace_child(request, obj_type, obj_ref, obj_revi, link_id):
    """
    View to replace a child by another one.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/replace/{link_id}/`

    .. include:: views_params.txt
    :param link_id: id of the :class:`.ParentChildLink` being replaced

    **Template:**

    :file:`parts/bom_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_child_form``
        a form to select the replacement part

    ``link``
        :class:`.ParentChildLink` being replaced

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "add_child")
    """
    link_id = int(link_id)
    obj, ctx = get_generic_data(request,
                                obj_type,
                                obj_ref,
                                obj_revi,
                                load_all=True)
    link = models.ParentChildLink.objects.get(id=link_id)
    if request.method == "POST":
        form = forms.AddPartForm(request.POST)
        if form.is_valid():
            obj.replace_child(link, get_obj_from_form(form, request.user))
            return HttpResponseRedirect("../..")
    else:
        form = forms.AddPartForm()
    if ctx["results"]:
        obj.precompute_can_add_child2()
    ctx["replace_child_form"] = form
    ctx["link"] = link
    ctx["attach"] = (obj, "add_child")
    ctx["link_creation"] = True
    return r2r("parts/bom_replace.html", ctx, request)
Ejemplo n.º 11
0
def ajax_can_attach(request, plmobject_id):
    plmobject = get_obj_by_id(plmobject_id, request.user)
    data = {"can_attach" : False}
    if isinstance(plmobject, PLMObjectController) and request.GET:
        form = forms.PLMObjectForm(request.GET)

        if form.is_valid():
            attached = get_obj_from_form(form, request.user)
            if attached.check_readable(False):
                if hasattr(plmobject, "can_attach_document"):
                    data["can_attach"] = plmobject.can_attach_document(attached)
                elif hasattr(plmobject, "can_attach_part"):
                    data["can_attach"] = plmobject.can_attach_part(attached)
    return data
Ejemplo n.º 12
0
def add_alternate(request, obj_type, obj_ref, obj_revi):
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    if request.POST:
        add_part_form = forms.AddPartForm(request.POST)
        if add_part_form.is_valid():
            part_obj = get_obj_from_form(add_part_form, request.user)
            obj.add_alternate(part_obj)
            return HttpResponseRedirect(obj.plmobject_url + "alternates/")
    else:
        add_part_form = forms.AddPartForm()
    ctx.update({'link_creation': True,
                'add_part_form': add_part_form,
                'attach' : (obj, "add_alternate") })
    return r2r('parts/alternates_add.html', ctx, request)
Ejemplo n.º 13
0
def delegate(request, obj_ref, role, sign_level):
    """
    Manage html page for delegations modification of the selected
    :class:`~django.contrib.auth.models.User`.
    It computes a context dictionary based on

    :param request: :class:`django.http.QueryDict`
    :param obj_type: :class:`~django.contrib.auth.models.User`
    :type obj_ref: str
    :param role: :attr:`.DelegationLink.role` if role is not "sign"
    :type role: str
    :param sign_level: used for :attr:`.DelegationLink.role` if role is "sign"
    :type sign_level: str
    :return: a :class:`django.http.HttpResponse`
    """
    obj, ctx = get_generic_data(request, "User", obj_ref)

    if request.method == "POST":
        delegation_form = forms.SelectUserForm(request.POST)
        if delegation_form.is_valid():
            if delegation_form.cleaned_data["type"] == "User":
                user_obj = get_obj_from_form(delegation_form, request.user)
                if role == "notified" or role == "owner":
                    obj.delegate(user_obj.object, role)
                    return HttpResponseRedirect("../..")
                elif role == "sign":
                    if sign_level == "all":
                        obj.delegate(user_obj.object, "sign*")
                        return HttpResponseRedirect("../../..")
                    elif sign_level.isdigit():
                        obj.delegate(user_obj.object, level_to_sign_str(int(sign_level)-1))
                        return HttpResponseRedirect("../../..")
    else:
        delegation_form = forms.SelectUserForm()
    if role == 'sign':
        if sign_level.isdigit():
            role = _("signer level") + " " + str(sign_level)
        else:
            role = _("signer all levels")
    elif role == "notified":
        role = _("notified")

    ctx.update({'current_page':'delegation',
                'replace_manager_form': delegation_form,
                'link_creation': True,
                'attach' : (obj, "delegate"),
                'role': role})
    return r2r('management_replace.html', ctx, request)
Ejemplo n.º 14
0
def add_alternate(request, obj_type, obj_ref, obj_revi):
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)
    if request.POST:
        add_part_form = forms.AddPartForm(request.POST)
        if add_part_form.is_valid():
            part_obj = get_obj_from_form(add_part_form, request.user)
            obj.add_alternate(part_obj)
            return HttpResponseRedirect(obj.plmobject_url + "alternates/")
    else:
        add_part_form = forms.AddPartForm()
    ctx.update({
        'link_creation': True,
        'add_part_form': add_part_form,
        'attach': (obj, "add_alternate")
    })
    return r2r('parts/alternates_add.html', ctx, request)
Ejemplo n.º 15
0
def replace_child(request, obj_type, obj_ref, obj_revi, link_id):
    """
    View to replace a child by another one.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/replace/{link_id}/`

    .. include:: views_params.txt
    :param link_id: id of the :class:`.ParentChildLink` being replaced

    **Template:**

    :file:`parts/bom_replace.html`

    **Context:**

    ``RequestContext``

    ``replace_child_form``
        a form to select the replacement part

    ``link``
        :class:`.ParentChildLink` being replaced

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "add_child")
    """
    link_id = int(link_id)
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi,
            load_all=True)
    link = models.ParentChildLink.objects.get(id=link_id)
    if request.method == "POST":
        form = forms.AddPartForm(request.POST)
        if form.is_valid():
            obj.replace_child(link, get_obj_from_form(form, request.user))
            return HttpResponseRedirect("../..")
    else:
        form = forms.AddPartForm()
    if ctx["results"]:
        obj.precompute_can_add_child2()
    ctx["replace_child_form"] = form
    ctx["link"] = link
    ctx["attach"] = (obj, "add_child")
    ctx["link_creation"] = True
    return r2r("parts/bom_replace.html", ctx, request)
Ejemplo n.º 16
0
def add_doc_cad(request, obj_type, obj_ref, obj_revi):
    """
    View to attach a document to a part.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/doc-cad/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`parts/doccad_add.html`

    **Context:**

    ``RequestContext``

    ``add_doc_cad_form``
        a form to attach a document (:class:`.AddDocCadForm`)

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "attach_doc")
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)

    if request.POST:
        add_doc_cad_form = forms.AddDocCadForm(request.POST)
        if add_doc_cad_form.is_valid():
            doc_cad_obj = get_obj_from_form(add_doc_cad_form, request.user)
            obj.attach_to_document(doc_cad_obj)
            return HttpResponseRedirect(obj.plmobject_url + "doc-cad/")
    else:
        add_doc_cad_form = forms.AddDocCadForm()
    ctx.update({
        'link_creation': True,
        'add_doc_cad_form': add_doc_cad_form,
        'attach': (obj, "attach_doc")
    })
    return r2r('parts/doccad_add.html', ctx, request)
Ejemplo n.º 17
0
def add_part(request, obj_type, obj_ref, obj_revi):
    """
    View to attach a part to a document.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/parts/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`documents/parts_add.html`

    **Context:**

    ``RequestContext``

    ``add_part_form``
        a form to attach a part (:class:`.AddPartForm`)

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "attach_part")
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)

    if request.POST:
        add_part_form = forms.AddPartForm(request.POST)
        if add_part_form.is_valid():
            part_obj = get_obj_from_form(add_part_form, request.user)
            obj.attach_to_part(part_obj)
            messages.info(request, _(u"The part has been successfully attached to the document."))
            return HttpResponseRedirect(obj.plmobject_url + "parts/")
    else:
        add_part_form = forms.AddPartForm()
    ctx.update({'link_creation': True,
                'add_part_form': add_part_form,
                'attach' : (obj, "attach_part") })
    return r2r('documents/parts_add.html', ctx, request)
Ejemplo n.º 18
0
def attach_plmobject(request, obj_ref):
    obj, ctx = bv.get_generic_data(request, "ECR", obj_ref, "-", search=True)
    if request.method == "POST":
        form = PLMObjectForm(request.POST)
        if form.is_valid():
            plmobject = bv.get_obj_from_form(form, request.user)
            obj.attach_object(plmobject.object)
            return HttpResponseRedirect("..")
    else:
        form = PLMObjectForm()
    if obj.is_editable and ctx["is_owner"]:
        plmobjects = obj.plmobjects.now().values_list("plmobject", flat=True)
        can_attach = lambda x: x.id not in plmobjects
    else:
        can_attach = lambda x: False
    ctx.update({
        "current_page": "part-doc-cads",
        "attach_form": form,
        "link_creation": True,
        "attach": (obj, can_attach),
    })
    return r2r("ecrs/plmobjects_add.html", ctx, request)
Ejemplo n.º 19
0
def add_doc_cad(request, obj_type, obj_ref, obj_revi):
    """
    View to attach a document to a part.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/doc-cad/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`parts/doccad_add.html`

    **Context:**

    ``RequestContext``

    ``add_doc_cad_form``
        a form to attach a document (:class:`.AddDocCadForm`)

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "attach_doc")
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)

    if request.POST:
        add_doc_cad_form = forms.AddDocCadForm(request.POST)
        if add_doc_cad_form.is_valid():
            doc_cad_obj = get_obj_from_form(add_doc_cad_form, request.user)
            obj.attach_to_document(doc_cad_obj)
            return HttpResponseRedirect(obj.plmobject_url + "doc-cad/")
    else:
        add_doc_cad_form = forms.AddDocCadForm()
    ctx.update({'link_creation': True,
                'add_doc_cad_form': add_doc_cad_form,
                'attach' : (obj, "attach_doc")})
    return r2r('parts/doccad_add.html', ctx, request)
Ejemplo n.º 20
0
def add_part(request, obj_type, obj_ref, obj_revi):
    """
    View to attach a part to a document.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/parts/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`documents/parts_add.html`

    **Context:**

    ``RequestContext``

    ``add_part_form``
        a form to attach a part (:class:`.AddPartForm`)

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "attach_part")
    """
    obj, ctx = get_generic_data(request, obj_type, obj_ref, obj_revi)

    if request.POST:
        add_part_form = forms.AddPartForm(request.POST)
        if add_part_form.is_valid():
            part_obj = get_obj_from_form(add_part_form, request.user)
            obj.attach_to_part(part_obj)
            messages.info(request, _(u"The part has been successfully attached to the document."))
            return HttpResponseRedirect(obj.plmobject_url + "parts/")
    else:
        add_part_form = forms.AddPartForm()
    ctx.update({"link_creation": True, "add_part_form": add_part_form, "attach": (obj, "attach_part")})
    return r2r("documents/parts_add.html", ctx, request)
Ejemplo n.º 21
0
def add_child(request, obj_type, obj_ref, obj_revi):
    """
    View to add a child to a part.

    :url: :samp:`/object/{obj_type}/{obj_ref}/{obj_revi}/bom-child/add/`

    .. include:: views_params.txt

    **Template:**

    :file:`parts/bom_add.html`

    **Context:**

    ``RequestContext``

    ``add_child_form``
        a form to add a child (:class:`.AddChildForm`)

    ``link``
        :class:`.ParentChildLink` being replaced

    ``link_creation``
        Set to True

    ``attach``
        set to (*obj*, "add_child")

    """
    obj, ctx = get_generic_data(request,
                                obj_type,
                                obj_ref,
                                obj_revi,
                                load_all=True)

    if request.method == "POST" and request.POST:
        add_child_form = forms.AddChildForm(obj.object, request.POST)
        if add_child_form.is_valid():
            child_obj = get_obj_from_form(add_child_form, request.user)
            obj.add_child(child_obj, add_child_form.cleaned_data["quantity"],
                          add_child_form.cleaned_data["order"],
                          add_child_form.cleaned_data["unit"],
                          **add_child_form.extensions)
            return HttpResponseRedirect(obj.plmobject_url + "BOM-child/")
    else:
        if "type" in request.GET and request.GET[
                "type"] in models.get_all_parts():
            # use GET params only if they seems valid
            initial = request.GET
        else:
            initial = None
        add_child_form = forms.AddChildForm(obj.object, initial=initial)
        ctx['current_page'] = 'BOM-child'
    if ctx["results"]:
        obj.precompute_can_add_child2()
        orders = list(
            obj.parentchildlink_parent.values_list('order', flat=True))
        initial_order = max(orders) + 10 if orders else 10
        ctx['order'] = initial_order
    ctx.update({
        'link_creation': True,
        'add_child_form': add_child_form,
        'attach': (obj, "add_child")
    })
    return r2r('parts/bom_add.html', ctx, request)