Uploading a document and immediately adding labels/cabinets/metadata...

Anything concerning integration via the REST API
Post Reply
noses
Posts: 8
Joined: Thu Sep 24, 2020 12:05 am

Uploading a document and immediately adding labels/cabinets/metadata...

Post by noses »

And the second problem came up immediately...

I'm trying to write a command line tool to upload documents and add all the things I know about it in one pass as the web interface is driving me nuts.

The uploading is not the problem but how do I go on from here?

Code: Select all

  def document_upload (self, filename, label = None, doctype = None, description = "", language = None):
    if file_is_readable (filename):
      payload = {"description" : description,
                 "document_type" : doctype if doctype else self.__default_document_type,
                 "label" : label if label else filename,
                 "language" : language if language else self.__default_language
                }
      files = {'file': open (filename, 'rb')} 
      response = self.session.post (self.__base_URL + "/documents/", files = files, data = payload)

      return response.json () ["id"]
    else:
      return (-1)

  def document_metadata_create (self, document_pk, metadata_type_pk, data):
    url =self.__base_URL + "/documents/" + str (document_pk) + "/metadata/"

    payload = {"metadata_type_pk" : metadata_type_pk,
               "value" : str (data)
              }
    response = self.session.post (url, data = payload)

    print ("Response:", response)
    print ("Response.json ():", response.json ())
  
    return response
and

Code: Select all

  document_id = dms.document_upload (filename = filename, label = label, description = args.description, language = "deu")

  if document_id > 0:
    # optionally set metadata -- as this is a new document it is not necessary to check if it already exists and just create it.
    if args.metadata_from_filename:
      metadata_type_id = dms.metadata_type_id_from_label ("Document Date")
      filedate = get_date_from_filename (filename)
      if filedate is not None:
        result = dms.document_metadata_create (document_id, metadata_type_id, filedate)
        print (result)
and guess what? I'm getting a 400 response back:

Response: <Response [400]>
Response.json (): {'non_field_errors': ["{'__all__': ['Value is not one of the provided options.']}"]}

So where did I go wrong?


Achim
noses
Posts: 8
Joined: Thu Sep 24, 2020 12:05 am

Re: Uploading a document and immediately adding labels/cabinets/metadata...

Post by noses »

To answer my own question: If a metadata type has a "Default" or a "Lookup" defined it is impossible to create it giving it a value. And it is also impossible to create it without a value.

PS: If file names turned into labels start with a date in the format of YYYYMMDD using

{{ document.label|slice:"0:4" }}-{{ document.label|slice:"4:6" }}-{{ document.label|slice:"6:8" }}

as Default or Lookup does not put anything usable into the metadata field.
Post Reply