Example #1
0
def ajax_creation_form(request):
    """
    Simple view which returns the html of a creation form with the data
    of :attr:`request.GET` as initial values.

    The request must contains a get parameter *type* with a valid type,
    otherwise, a :class:`.HttpResponseForbidden` is returned.
    """
    tf = forms.TypeForm(request.GET)
    if tf.is_valid():
        type_ = tf.cleaned_data["type"]
        request.session["type"] = type_
        request.session.save()
        cls = models.get_all_users_and_plmobjects()[type_]
        view = get_creation_view(cls)
        if view is not None:
            return {"reload" : True}
        initial = dict(request.GET.iteritems())
        if "pfiles" in request.GET:
            initial["pfiles"] = request.GET.getlist("pfiles")
        if "reference" in initial:
            # gets a new reference if the type switches from a part to a document
            # and vice versa, see ticket #99
            ref = initial["reference"]
            if (ref.startswith("DOC_") and type_ in models.get_all_parts()) or \
               (ref.startswith("PART_") and type_ in models.get_all_documents()):
                del initial["reference"]
        form = forms.get_creation_form(request.user, cls, initial=initial)
        return {"reload" : False, "form" : form.as_table(),
                "type" : type_, "form_media": form.media.render(), }
    else:
        return HttpResponseForbidden()
Example #2
0
def create_object(request, from_registered_view=False, creation_form=None):
    """
    View to create a :class:`.PLMObject` or a :class:`.GroupInfo`.

    :url: ``/object/create/``

    Requests (POST and GET) must contain a ``type`` variable that validates a
    :class:`.TypeForm`.

    POST requests must validate the creation form, fields depend on the
    given type. If the creation form is valid, an object is created and
    in case of success, this view redirects to the created object.

    Requests may contain a ``__next__`` variable. A successful creation will
    redirect to this URL. Some special strings are replaced:

        * ``##type##`` with the created object's type
        * ``##ref##`` with the created object's reference
        * ``##rev##`` with the created object's reference

    Requests may also contain other special variables (at most one of
    them):

        ``related_doc``
            Id of a document. The created part will be attached to this
            document. Object's type is restricted to part types.
            Two context variables (``related_doc`` and ``related``)
            are set to the document controller.

        ``related_part``
            Id of a part. The created document will be attached to this
            part. Object's type is restricted to document types.
            Two context variables (``related_part`` and ``related``)
            are set to the part controller.

        ``related_parent``
            Id of a part. Object's type is restricted to part types.
            Two context variables (``related_parent`` and ``related``)
            are set to the part controller.

    .. note::
        If *from_registered_view* is False, this view delegates its
        treatment to a registered view that handles creation of
        objects of the given type.
        (see :func:`.get_creation_view` and :func:`.register_creation_view`)

    :param from_registered_view: True if this function is called by another
         creation view
    :param creation_form: a creation form that will be used instead of the
         default one

    **Template:**

    :file:`create.html`

    **Context:**

    ``RequestContext``

    ``creation_form``

    ``creation_type_form``
        :class:`.TypeForm` to select the type of the created object

    ``object_type``
        type of the created object

    ``next``
        value of the ``__next__`` request variable if given
    """

    obj, ctx = get_generic_data(request)
    Form = forms.TypeForm
    # it is possible that the created object must be attached to a part
    # or a document
    # related_doc and related_part should be a plmobject id
    # If the related_doc/part is not a doc/part, we let python raise
    # an AttributeError, since a user should not play with the URL
    # and openPLM must be smart enough to produce valid URLs
    attach = related = None
    if "related_doc" in request.REQUEST:
        Form = forms.PartTypeForm
        doc = get_obj_by_id(int(request.REQUEST["related_doc"]), request.user)
        attach = doc.attach_to_part
        ctx["related_doc"] = request.REQUEST["related_doc"]
        related = ctx["related"] = doc
    elif "related_part" in request.REQUEST:
        Form = forms.DocumentTypeForm
        part = get_obj_by_id(int(request.REQUEST["related_part"]),
                             request.user)
        attach = part.attach_to_document
        ctx["related_part"] = request.REQUEST["related_part"]
        related = ctx["related"] = part
    elif "related_parent" in request.REQUEST:
        Form = forms.PartTypeForm
        parent = get_obj_by_id(int(request.REQUEST["related_parent"]),
                               request.user)
        ctx["related_parent"] = request.REQUEST["related_parent"]
        related = ctx["related"] = parent
    if "pfiles" in request.REQUEST:
        Form = forms.Document2TypeForm

    if "__next__" in request.REQUEST:
        redirect_to = request.REQUEST["__next__"]
        ctx["next"] = redirect_to
    else:
        # will redirect to the created object
        redirect_to = None

    type_form = Form(request.REQUEST)
    if type_form.is_valid():
        type_ = type_form.cleaned_data["type"]
        cls = models.get_all_users_and_plmobjects()[type_]
        if not from_registered_view:
            view = get_creation_view(cls)
            if view is not None:
                # view has been registered to create an object of type 'cls'
                return view(request)
    else:
        ctx["creation_type_form"] = type_form
        return r2r('create.html', ctx, request)

    if request.method == 'GET' and creation_form is None:
        creation_form = forms.get_creation_form(request.user,
                                                cls,
                                                template="pfiles"
                                                not in request.GET)
        if related is not None:
            creation_form.fields["group"].initial = related.group
            creation_form.initial["lifecycle"] = related.lifecycle
        if "pfiles" in request.GET:
            pfiles = request.GET.getlist("pfiles")
            creation_form.initial["pfiles"] = pfiles
            try:
                name = filename_to_name(
                    obj.files.get(id=int(pfiles[0])).filename)
                creation_form.initial["name"] = name
            except Exception:
                pass
    elif request.method == 'POST':
        if creation_form is None:
            creation_form = forms.get_creation_form(request.user, cls,
                                                    request.POST)
        if creation_form.is_valid():
            ctrl_cls = get_controller(type_)
            ctrl = ctrl_cls.create_from_form(creation_form, request.user)
            message = _(u"The %(Object_type)s has been created") % dict(
                Object_type=type_)
            messages.info(request, message)
            if attach is not None:
                try:
                    attach(ctrl)
                    message = _(u"The %(Object_type)s has been attached"
                                ) % dict(Object_type=type_)
                    messages.info(request, message)
                except (ControllerError, ValueError) as e:
                    # crtl cannot be attached (maybe the state of the
                    # related object as changed)
                    # alerting the user using the messages framework since
                    # the response is redirected
                    message = _(u"Error: %(details)s") % dict(
                        details=unicode(e))
                    messages.error(request, message)
                    # redirecting to the ctrl page that lists its attached
                    # objects
                    if ctrl.is_document:
                        return HttpResponseRedirect(ctrl.plmobject_url +
                                                    "parts/")
                    else:
                        return HttpResponseRedirect(ctrl.plmobject_url +
                                                    "doc-cad/")
            if redirect_to:
                redirect_to = redirect_to.replace("##ref##", ctrl.reference)
                redirect_to = redirect_to.replace("##rev##", ctrl.revision)
                redirect_to = redirect_to.replace("##type##", ctrl.type)
            return HttpResponseRedirect(redirect_to or ctrl.plmobject_url)
    ctx.update({
        'creation_form': creation_form,
        'object_type': type_,
        'creation_type_form': type_form,
    })
    return r2r('create.html', ctx, request)
Example #3
0
def create_object(request, from_registered_view=False, creation_form=None):
    """
    View to create a :class:`.PLMObject` or a :class:`.GroupInfo`.

    :url: ``/object/create/``

    Requests (POST and GET) must contain a ``type`` variable that validates a
    :class:`.TypeForm`.

    POST requests must validate the creation form, fields depend on the
    given type. If the creation form is valid, an object is created and
    in case of success, this view redirects to the created object.

    Requests may contain a ``__next__`` variable. A successful creation will
    redirect to this URL. Some special strings are replaced:

        * ``##type##`` with the created object's type
        * ``##ref##`` with the created object's reference
        * ``##rev##`` with the created object's reference

    Requests may also contain other special variables (at most one of
    them):

        ``related_doc``
            Id of a document. The created part will be attached to this
            document. Object's type is restricted to part types.
            Two context variables (``related_doc`` and ``related``)
            are set to the document controller.

        ``related_part``
            Id of a part. The created document will be attached to this
            part. Object's type is restricted to document types.
            Two context variables (``related_part`` and ``related``)
            are set to the part controller.

        ``related_parent``
            Id of a part. Object's type is restricted to part types.
            Two context variables (``related_parent`` and ``related``)
            are set to the part controller.

    .. note::
        If *from_registered_view* is False, this view delegates its
        treatment to a registered view that handles creation of
        objects of the given type.
        (see :func:`.get_creation_view` and :func:`.register_creation_view`)

    :param from_registered_view: True if this function is called by another
         creation view
    :param creation_form: a creation form that will be used instead of the
         default one

    **Template:**

    :file:`create.html`

    **Context:**

    ``RequestContext``

    ``creation_form``

    ``creation_type_form``
        :class:`.TypeForm` to select the type of the created object

    ``object_type``
        type of the created object

    ``next``
        value of the ``__next__`` request variable if given
    """

    obj, ctx = get_generic_data(request)
    Form = forms.TypeForm
    # it is possible that the created object must be attached to a part
    # or a document
    # related_doc and related_part should be a plmobject id
    # If the related_doc/part is not a doc/part, we let python raise
    # an AttributeError, since a user should not play with the URL
    # and openPLM must be smart enough to produce valid URLs
    attach = related = None
    if "related_doc" in request.REQUEST:
        Form = forms.PartTypeForm
        doc = get_obj_by_id(int(request.REQUEST["related_doc"]), request.user)
        attach = doc.attach_to_part
        ctx["related_doc"] = request.REQUEST["related_doc"]
        related = ctx["related"] = doc
    elif "related_part" in request.REQUEST:
        Form = forms.DocumentTypeForm
        part = get_obj_by_id(int(request.REQUEST["related_part"]), request.user)
        attach = part.attach_to_document
        ctx["related_part"] = request.REQUEST["related_part"]
        related = ctx["related"] = part
    elif "related_parent" in request.REQUEST:
        Form = forms.PartTypeForm
        parent = get_obj_by_id(int(request.REQUEST["related_parent"]), request.user)
        ctx["related_parent"] = request.REQUEST["related_parent"]
        related = ctx["related"] = parent
    if "pfiles" in request.REQUEST:
        Form = forms.Document2TypeForm

    if "__next__" in request.REQUEST:
        redirect_to = request.REQUEST["__next__"]
        ctx["next"] = redirect_to
    else:
        # will redirect to the created object
        redirect_to = None

    type_form = Form(request.REQUEST)
    if type_form.is_valid():
        type_ = type_form.cleaned_data["type"]
        cls = models.get_all_users_and_plmobjects()[type_]
        if not from_registered_view:
            view = get_creation_view(cls)
            if view is not None:
                # view has been registered to create an object of type 'cls'
                return view(request)
    else:
        ctx["creation_type_form"] = type_form
        return r2r('create.html', ctx, request)

    if request.method == 'GET' and creation_form is None:
        creation_form = forms.get_creation_form(request.user, cls,
             template="pfiles" not in request.GET)
        if related is not None:
            creation_form.fields["group"].initial = related.group
            creation_form.initial["lifecycle"] = related.lifecycle
        if "pfiles" in request.GET:
            pfiles = request.GET.getlist("pfiles")
            creation_form.initial["pfiles"] = pfiles
            try:
                name = filename_to_name(obj.files.get(id=int(pfiles[0])).filename)
                creation_form.initial["name"] = name
            except Exception:
                pass
    elif request.method == 'POST':
        if creation_form is None:
            creation_form = forms.get_creation_form(request.user, cls, request.POST)
        if creation_form.is_valid():
            ctrl_cls = get_controller(type_)
            ctrl = ctrl_cls.create_from_form(creation_form, request.user)
            message = _(u"The %(Object_type)s has been created") % dict(Object_type = type_)
            messages.info(request, message)
            if attach is not None:
                try:
                    attach(ctrl)
                    message = _(u"The %(Object_type)s has been attached") % dict(Object_type = type_)
                    messages.info(request, message)
                except (ControllerError, ValueError) as e:
                    # crtl cannot be attached (maybe the state of the
                    # related object as changed)
                    # alerting the user using the messages framework since
                    # the response is redirected
                    message = _(u"Error: %(details)s") % dict(details=unicode(e))
                    messages.error(request, message)
                    # redirecting to the ctrl page that lists its attached
                    # objects
                    if ctrl.is_document:
                        return HttpResponseRedirect(ctrl.plmobject_url + "parts/")
                    else:
                        return HttpResponseRedirect(ctrl.plmobject_url + "doc-cad/")
            if redirect_to:
                redirect_to = redirect_to.replace("##ref##", ctrl.reference)
                redirect_to = redirect_to.replace("##rev##", ctrl.revision)
                redirect_to = redirect_to.replace("##type##", ctrl.type)
            return HttpResponseRedirect(redirect_to or ctrl.plmobject_url)
    ctx.update({
        'creation_form' : creation_form,
        'object_type' : type_,
        'creation_type_form' : type_form,
    })
    return r2r('create.html', ctx, request)