Esempio n. 1
0
def get(obj):
    if is_file(obj):
        if 'md5' in obj:
            obj['md5'] = utils.normalize_md5(obj['md5'])
        if 'sha1' in obj:
            obj['sha1'] = utils.normalize_sha1(obj['sha1'])
        o = files.find_one(obj)
    else:
        o = works.find_one(obj)
    return o
Esempio n. 2
0
def get(obj):
    if is_file(obj):
        if 'md5' in obj:
            obj['md5'] = utils.normalize_md5(obj['md5'])
        if 'sha1' in obj:
            obj['sha1'] = utils.normalize_sha1(obj['sha1'])
        o = files.find_one(obj)
    else:
        o = works.find_one(obj)
    return o
Esempio n. 3
0
def set(query, update, user=None, description=''):
    table = getattr(db, is_file(query) and 'files' or 'works')
    o = get_or_create(query)
    w = None
    o_is_file = is_file(query)
    updated = False
    for key in update:
        if key.startswith('_') or key in ('id', 'work', 'files'):
            continue
        value = update[key]
        if key == 'md5':
            value = utils.normalize_md5(value)
        elif key == 'sha1':
            value = utils.normalize_sha1(value)
        elif key == 'oshash':
            value = utils.normalize_oshash(value)
        elif key == 'isbn':
            if isinstance(value, list):
                value = filter(None, [utils.check_isbn(i) for i in value])
            else:
                value = utils.check_isbn(value)
        elif key == 'openlibrary':
            value = utils.check_openlibrary(value)
        if o_is_file:
            if not is_file_key(key):
                if not w:
                    if not '_work' in o:
                        if not isinstance(value, list):
                           _value = [value]
                        else:
                           _value = value
                        for v in _value:
                            obj = {}
                            obj[key] = v
                            w = get(obj)
                            if w: break
                        if not w:
                            w = get_or_create(obj)

                        o['_work'] = w['_id']
                        db.files.save(o)
                    else:
                        w = db.works.find_one({'_id': o['_work']})
                #fixme: enforce uniqueness for ids
                update_key(w, key, value)
            else:
                update_key(o, key, value)
                updated = True
        else:
            if is_file_key(key):
                raise Exception, 'can not set file key on work'
            update_key(o, key, value)
            updated = True
    if w:
        db.works.save(w)
        if user:
            log(user, w, description)
    if updated:
        table.save(o)
        if user:
            log(user, o, description)
    return o
Esempio n. 4
0
def set(query, update, user=None, description=''):
    table = getattr(db, is_file(query) and 'files' or 'works')
    o = get_or_create(query)
    w = None
    o_is_file = is_file(query)
    updated = False
    for key in update:
        if key.startswith('_') or key in ('id', 'work', 'files'):
            continue
        value = update[key]
        if key == 'md5':
            value = utils.normalize_md5(value)
        elif key == 'sha1':
            value = utils.normalize_sha1(value)
        elif key == 'oshash':
            value = utils.normalize_oshash(value)
        elif key == 'isbn':
            if isinstance(value, list):
                value = filter(None, [utils.check_isbn(i) for i in value])
            else:
                value = utils.check_isbn(value)
        elif key == 'openlibrary':
            value = utils.check_openlibrary(value)
        if o_is_file:
            if not is_file_key(key):
                if not w:
                    if not '_work' in o:
                        if not isinstance(value, list):
                            _value = [value]
                        else:
                            _value = value
                        for v in _value:
                            obj = {}
                            obj[key] = v
                            w = get(obj)
                            if w: break
                        if not w:
                            w = get_or_create(obj)

                        o['_work'] = w['_id']
                        db.files.save(o)
                    else:
                        w = db.works.find_one({'_id': o['_work']})
                #fixme: enforce uniqueness for ids
                update_key(w, key, value)
            else:
                update_key(o, key, value)
                updated = True
        else:
            if is_file_key(key):
                raise Exception, 'can not set file key on work'
            update_key(o, key, value)
            updated = True
    if w:
        db.works.save(w)
        if user:
            log(user, w, description)
    if updated:
        table.save(o)
        if user:
            log(user, o, description)
    return o