Beispiel #1
0
    def writeCollections(self, collections):
        collectionsArray = []
        for collection in collections:
            collectionKey = collection.get('collectionKey')
            if collectionKey == '' or collectionKey == None:
                newCollectionKey = getKey()
                collection.set('collectionKey', newCollectionKey)
                collection.set('collectionVersion', 0)
            collectionsArray.append(collection)

        aparams = {'target': 'collections', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [collectionsArray[i:i + 50] for i in range(0, len(collectionsArray), 50)]
        for chunk in chunks:
            writeArray = []
            for collection in chunk:
                writeArray.append(collection.writeApiObject())
            requestData = json.dumps({'collections': writeArray})

            writeResponse = self.owningLibrary._request(reqUrl, 'POST', requestData, {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeCollections Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for collection in chunk:
                    collection.writeFailure = {'key': collection.get('key'), 'code': writeResponse.status_code, 'message': writeResponse.text}
            else:
                updateObjectsFromWriteResponse(chunk, writeResponse)

        return collections
Beispiel #2
0
    def writeItems(self, items):
        writeItems = []

        for item in items:
            itemKey = item.get('itemKey')
            if itemKey == '':
                newItemKey = getKey()
                item.set('itemKey', newItemKey)
                item.set('itemVersion', 0)
            writeItems.append(item)

            #add separate note items if this item has any
            itemNotes = item.get('notes')
            if itemNotes and len(itemNotes) > 0:
                for note in itemNotes:
                    note.set('parentItem', item.get('itemKey'))
                    note.set('itemKey', getKey())
                    note.set('itemVersion', 0)
                    writeItems.append(note)

        aparams = {'target': 'items', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [writeItems[i:i + 50] for i in range(0, len(writeItems), 50)]
        for chunk in chunks:
            writeArray = []
            for item in chunk:
                writeArray.append(item.writeApiObject())
            requestData = json.dumps({'items': writeArray})

            writeResponse = self.owningLibrary._request(
                reqUrl, 'POST', requestData,
                {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeItems Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for item in chunk:
                    item.writeFailure = {
                        'key': item.get('itemKey'),
                        'code': writeResponse.status_code,
                        'message': writeResponse.text
                    }
            else:
                updateObjectsFromWriteResponse(chunk, writeResponse)
        return writeItems
Beispiel #3
0
    def writeItems(self, items):
        writeItems = []

        for item in items:
            itemKey = item.get('itemKey')
            if itemKey == '':
                newItemKey = getKey()
                item.set('itemKey', newItemKey)
                item.set('itemVersion', 0)
            writeItems.append(item)

            #add separate note items if this item has any
            itemNotes = item.get('notes')
            if itemNotes and len(itemNotes) > 0:
                for note in itemNotes:
                    note.set('parentItem', item.get('itemKey'))
                    note.set('itemKey', getKey())
                    note.set('itemVersion', 0)
                    writeItems.append(note)

        aparams = {'target': 'items', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [writeItems[i:i + 50] for i in range(0, len(writeItems), 50)]
        for chunk in chunks:
            writeArray = []
            for item in chunk:
                writeArray.append(item.writeApiObject())
            requestData = json.dumps({'items': writeArray})

            writeResponse = self.owningLibrary._request(reqUrl, 'POST', requestData, {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeItems Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for item in chunk:
                    item.writeFailure = {'key': item.get('itemKey'), 'code': writeResponse.status_code, 'message': writeResponse.text}
            else:
                updateObjectsFromWriteResponse(chunk, writeResponse)
        return writeItems
    def writeCollections(self, collections):
        collectionsArray = []
        for collection in collections:
            collectionKey = collection.get('collectionKey')
            if collectionKey == '' or collectionKey == None:
                newCollectionKey = getKey()
                collection.set('collectionKey', newCollectionKey)
                collection.set('collectionVersion', 0)
            collectionsArray.append(collection)

        aparams = {'target': 'collections', 'content': 'json'}
        reqUrl = self.owningLibrary.apiRequestString(aparams)

        chunks = [
            collectionsArray[i:i + 50]
            for i in range(0, len(collectionsArray), 50)
        ]
        for chunk in chunks:
            writeArray = []
            for collection in chunk:
                writeArray.append(collection.writeApiObject())
            requestData = json.dumps({'collections': writeArray})

            writeResponse = self.owningLibrary._request(
                reqUrl, 'POST', requestData,
                {'Content-Type': 'application/json'})
            if responseIsError(writeResponse):
                logging.info('writeCollections Error')
                logging.info(writeResponse.status_code)
                #entire request failed but we get no per-item write failure messages
                #so update all items with writeFailure manually
                for collection in chunk:
                    collection.writeFailure = {
                        'key': collection.get('key'),
                        'code': writeResponse.status_code,
                        'message': writeResponse.text
                    }
            else:
                updateObjectsFromWriteResponse(chunk, writeResponse)

        return collections