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
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)
Response: <Response [400]>
Response.json (): {'non_field_errors': ["{'__all__': ['Value is not one of the provided options.']}"]}
So where did I go wrong?
Achim