示例#1
0
def save_package(metadata, id=None):
    metadata['last_updated'] = datetime.now()
    package = {}
    if id is not None:
        existing_package = retrieve_package(id).to_python()
        for k in metadata:
            try:
                existing_package[k] = metadata[k]
            except:
                continue
        package = Package(**existing_package)
    else:
        package = Package(**metadata)

    # parse the metadata
    try:        
        package.validate()
    except:
        raise HelperException("The metadata used to create the package object was not in the expected form.", 400)

    # create the package object
    try:
        return _save_from_model(package)
    except Exception as e:
        raise HelperException("The learning package could not be saved. " + str(e), 500)
示例#2
0
def retrieve_json_package(id):
    pkg = json.loads(Package.make_json_publicsafe(retrieve_package(id)))

    for item in pkg['files']:
        item['type'] = 'file'

    for item in pkg['urls']:
        item['type'] = 'url'

    if pkg['files'] is None:
        pkg['files'] = []

    pkg['resources'] = pkg['files'] + pkg['urls']
    del pkg['files']
    del pkg['urls']

    return json.dumps(pkg)