Beispiel #1
0
def attach_to_part(request, doc_id, part_id):
    """
    Links the :class:`.Document` identified by *doc_id* with the :class:`.Part`
    identified by *part_id*.

    :implements: :func:`http_api.attach_to_part`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :param part_id: id of a :class:`.Part`
    :returned fields: None 
    """
    doc = get_obj_by_id(doc_id, request.user)
    part = get_obj_by_id(part_id, request.user)
    doc.attach_to_part(part)
    return {}
Beispiel #2
0
def ajax_attach(request, plmobject_id):
    plmobject = get_obj_by_id(plmobject_id, request.user)
    data = {}
    if request.GET:
        form = forms.AddRelPartForm(initial=request.GET)
    else:
        form = forms.AddRelPartForm(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
Beispiel #3
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"
    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
Beispiel #4
0
def revise(request, doc_id):
    """
    Makes a new revision of the :class:`.Document` identified by *doc_id*.

    :implements: :func:`http_api.revise`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :returned fields:
        * doc, the new document (see :ref:`http-api-object`)
        * files, a list of files (see :ref:`http-api-file`) 
    """
    
    doc = get_obj_by_id(doc_id, request.user)
    form = forms.AddRevisionForm(request.POST)
    if form.is_valid():
        rev = doc.revise(form.cleaned_data["revision"])
        ret = {"doc" : object_to_dict(rev)}
        files = []
        for df in rev.files:
            files.append(dict(id=df.id, filename=df.filename, size=df.size))
        ret["files"] = files
        return ret
    else:
        return {"result" : 'error'}
Beispiel #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.AddRelPartForm(request.GET)
        if form.is_valid():
            child = get_obj_from_form(form, request.user)
            data["can_add"] = part.can_add_child(child)
    return data
Beispiel #6
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.AddRelPartForm(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
Beispiel #7
0
def is_revisable(request, doc_id):
    """
    Returns True if the :class:`.Document` identified by *doc_id* can be revised.

    :implements: :func:`http_api.is_revisable`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :returned fields: revisable, True if it can be revised
    """

    doc = get_obj_by_id(doc_id, request.user)
    return {"revisable" : doc.is_revisable()}
Beispiel #8
0
def add_file(request, doc_id, thumbnail=False):
    """
    Adds a file to the :class:`.Document` identified by *doc_id*.

    :implements: :func:`http_api.add_file`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :returned fields: doc_file, the file that has been had,
                      see :ref:`http-api-file`.
    """
    doc = get_obj_by_id(doc_id, request.user)
    form = forms.AddFileForm(request.POST, request.FILES)
    df = doc.add_file(request.FILES["filename"], thumbnail=thumbnail)
    return {"doc_file" : dict(id=df.id, filename=df.filename, size=df.size)}
Beispiel #9
0
def next_revision(request, doc_id):
    """
    Returns a possible new revision for the :class:`.Document` identified by
    *doc_id*.
    
    :implements: :func:`http_api.next_revision`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :returned fields: revision, the new revision (may be an empty string)

    .. seealso:: :func:`.utils.get_next_revision` for possible results
    """

    doc = get_obj_by_id(doc_id, request.user)
    return {"revision" : get_next_revision(doc.revision)}
Beispiel #10
0
def is_locked(request, doc_id, df_id):
    """
    Returns True if the :class:`.DocumentFile` identified by *df_id* from
    the :class:`.Document` identified by *doc_id* is locked.

    :implements: :func:`http_api.is_locked`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :param df_id: id of a :class:`.DocumentFile`
    :returned fields: locked, True if the file is locked.
    """

    doc = get_obj_by_id(doc_id, request.user)
    df = models.DocumentFile.objects.get(id=df_id)
    return {"locked" : df.locked}
Beispiel #11
0
def check_out(request, doc_id, df_id):
    """
    Locks the :class:`.DocumentFile` identified by *df_id* from
    the :class:`.Document` identified by *doc_id*.

    :implements: :func:`http_api.lock`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :param df_id: id of a :class:`.DocumentFile`
    :returned fields: None 
    """
    doc = get_obj_by_id(doc_id, request.user)
    df = models.DocumentFile.objects.get(id=df_id)
    doc.lock(df)
    return {}
Beispiel #12
0
def add_thumbnail(request, doc_id, df_id):
    """
    Add a thumbnail the :class:`.DocumentFile` identified by *df_id* from
    the :class:`.Document` identified by *doc_id*.

    :implements: :func:`http_api.add_thumbnail`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :param df_id: id of a :class:`.DocumentFile`
    :returned fields: None
    """

    doc = get_obj_by_id(doc_id, request.user)
    form = forms.AddFileForm(request.POST, request.FILES)
    df = models.DocumentFile.objects.get(id=df_id)
    doc.add_thumbnail(df, request.FILES["filename"])
    return {}
Beispiel #13
0
def check_in(request, doc_id, df_id, thumbnail=False):
    """
    Checks-in the :class:`.DocumentFile` identified by *df_id* from
    the :class:`.Document` identified by *doc_id*

    :implements: :func:`http_api.check_in`
    
    :param request: the request
    :param doc_id: id of a :class:`.Document`
    :param df_id: id of a :class:`.DocumentFile`
    :returned fields: None
    """
    doc = get_obj_by_id(doc_id, request.user)
    df = models.DocumentFile.objects.get(id=df_id)
    form = forms.AddFileForm(request.POST, request.FILES)
    if form.is_valid():
        doc.checkin(df, request.FILES['filename'], thumbnail=thumbnail)
    return {}
Beispiel #14
0
def download(request, docfile_id, filename=""):
    """
    View to download a document file.
    
    :param request: :class:`django.http.QueryDict`
    :param docfile_id: :attr:`.DocumentFile.id`
    :type docfile_id: str
    :return: a :class:`django.http.HttpResponse`
    """
    doc_file = models.DocumentFile.objects.get(id=docfile_id)
    ctrl = get_obj_by_id(int(doc_file.document.id), request.user)
    ctrl.check_readable()
    name = doc_file.filename.encode("utf-8", "ignore")
    mimetype = guess_type(name, False)[0]
    if not mimetype:
        mimetype = 'application/octet-stream'
    response = HttpResponse(file(doc_file.file.path), mimetype=mimetype)
    response["Content-Length"] = doc_file.file.size
    if not filename:
        response['Content-Disposition'] = 'attachment; filename="%s"' % name
    return response