upload documents and link to item

This commit is contained in:
j 2015-09-24 11:49:47 +01:00
parent 97bbd595bd
commit 733b909981

View file

@ -32,6 +32,8 @@ socket.setdefaulttimeout(300)
CHUNK_SIZE = 1024*1024 CHUNK_SIZE = 1024*1024
default_media_cache = os.environ.get('oxMEDIA', '~/.ox/media') default_media_cache = os.environ.get('oxMEDIA', '~/.ox/media')
DOCUMENT_FORMATS = ('jpg', 'pdf', 'png')
def get_frames(filename, prefix, info, force=False): def get_frames(filename, prefix, info, force=False):
oshash = info['oshash'] oshash = info['oshash']
cache = os.path.join(prefix, os.path.join(*utils.hash_prefix(oshash))) cache = os.path.join(prefix, os.path.join(*utils.hash_prefix(oshash)))
@ -646,6 +648,7 @@ class Client(object):
print "you need to login" print "you need to login"
return return
conn, c = self._conn() conn, c = self._conn()
documents = []
if args: if args:
data = [] data = []
for arg in args: for arg in args:
@ -678,6 +681,7 @@ class Client(object):
data = r['data']['data'] data = r['data']['data']
files = r['data']['file'] files = r['data']['file']
info = r['data']['info'] info = r['data']['info']
documents = self._get_documents()
if info: if info:
r = self.update_info(info) r = self.update_info(info)
@ -691,6 +695,12 @@ class Client(object):
if os.path.exists(path): if os.path.exists(path):
self.api.uploadData(path, oshash) self.api.uploadData(path, oshash)
break break
if documents:
print 'uploading %s documents' % len(documents)
for oshash, item in documents:
for path in self.path(oshash):
if os.path.exists(path):
self._add_document(path, item)
if data: if data:
print 'encoding and uploading %s videos' % len(data) print 'encoding and uploading %s videos' % len(data)
@ -751,21 +761,41 @@ class Client(object):
if r.get('status', {}).get('code') != 200: if r.get('status', {}).get('code') != 200:
print r print r
def upload_document(self, args): def _get_documents(self):
if not self.user: files = self.api.findMedia({
print "you need to login" 'query': {
return 'conditions': [
conn, c = self._conn() {'key': 'filename', 'operator': '', 'value': value}
for f in args: for value in DOCUMENT_FORMATS
if f.split('.')[-1] not in ('jpg', 'pdf', 'png'): ],
print 'unsupported format', f 'operator': '|'
continue },
'keys': ['item', 'id'],
'range': [0, 5000]
})['data']['items']
d = self.api.findDocuments({
'query': {
'conditions': [
{'key': 'oshash', 'operator': '==', 'value': f['id']}
for f in files
],
'operator': '|'
},
'keys': ['id', 'oshash'],
'range': [0, len(files)]
})['data']['items']
available = set(f['oshash'] for f in d)
missing = [(f['id'], f['item']) for f in files if f['id'] not in available]
return missing
def _add_document(self, f, item=None):
if f.split('.')[-1] not in DOCUMENT_FORMATS:
return False
url = '%supload/document/' % self._config['url'] url = '%supload/document/' % self._config['url']
r = self.api.upload_chunks(url, f, { r = self.api.upload_chunks(url, f, {
'filename': os.path.basename(f) 'filename': os.path.basename(f)
}) })
''' if r and item:
if r:
oshash = ox.oshash(f) oshash = ox.oshash(f)
r = self.api.findDocuments({ r = self.api.findDocuments({
"keys": ['id'], "keys": ['id'],
@ -775,17 +805,22 @@ class Client(object):
} }
}) })
did = r['data']['items'][0]['id'] did = r['data']['items'][0]['id']
if description: r = self.api.addDocument({
r = api.editDocument({
'id': did,
'description': description
})
if item:
r = api.addDocument({
'id': did, 'id': did,
'item': item 'item': item
}) })
''' return True
def upload_document(self, args):
if not self.user:
print "you need to login"
return
conn, c = self._conn()
for f in args:
r = self._add_document(f)
if not r:
print 'unsupported format', f
continue
def files(self, prefix): def files(self, prefix):
if not prefix.endswith('/'): if not prefix.endswith('/'):