Ejemplo n.º 1
0
def book_bulk_index(db_session, username):
    logger.info(LogMsg.START, username)
    books = get_all({}, db_session, username)
    for book in books:
        if is_book_already_indexed(book.get('id')):
            logger.debug(LogMsg.BOOK_INDEX_EXISTS, book)
        else:
            roles = persons_of_book(book.get('id'), db_session)
            book['book_id'] = book.get('id')
            del book['roles']
            book.update(roles)
            index_book(book, db_session)
            logger.debug(LogMsg.BOOK_INDEXED, book)
    logger.info(LogMsg.END)
    return {'result': 'successful'}
Ejemplo n.º 2
0
def edit_book(id, db_session, data, username):
    logger.info(LogMsg.START, username)

    logger.info(LogMsg.EDITING_BOOK, id)

    model_instance = db_session.query(Book).filter(Book.id == id).first()
    if model_instance is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': id})
        raise Http_error(404, Message.NOT_FOUND)

    logger.debug(LogMsg.PERMISSION_CHECK, username)
    permission_data = {}
    if model_instance.creator == username:
        permission_data = {Permissions.IS_OWNER.value: True}
    permissions, presses = get_user_permissions(username, db_session)
    has_permit = has_permission_or_not([Permissions.BOOK_EDIT_PREMIUM],
                                       permissions, None, permission_data)
    if not has_permit:
        if model_instance.press in presses:
            has_permission([Permissions.BOOK_EDIT_PRESS], permissions)
        else:
            logger.error(LogMsg.PERMISSION_DENIED)
            raise Http_error(403, Message.ACCESS_DENIED)

    logger.debug(LogMsg.PERMISSION_VERIFIED, username)

    logger.debug(LogMsg.GET_SUCCESS, id)

    if "id" in data.keys():
        del data["id"]
    logger.debug(LogMsg.EDIT_REQUST, {'book_id': id})

    roles = []
    elastic_data = {}

    if 'roles' in data.keys():
        roles = data.get('roles')
        del data['roles']

    for key, value in data.items():
        # TODO  if key is valid attribute of class
        setattr(model_instance, key, value)
    edit_basic_data(model_instance, username, data.get('tags'))
    price = data.get('price', None)
    if price is not None:
        edit_price(model_instance.id, price, db_session)

    if len(roles) > 0:
        logger.debug(LogMsg.DELETING_BOOK_ROLES, id)
        delete_book_roles(model_instance.id, db_session)
        logger.debug(LogMsg.ADDING_ROLES_TO_BOOK, id)
        nroles, elastic_data = add_book_roles(model_instance.id, roles,
                                              db_session, username)
        new_roles = []
        for role in nroles:
            logger.debug(LogMsg.ATTACHING_ROLES_TO_BOOKS)
            new_roles.append(book_role_to_dict(role))
    else:
        elastic_data = persons_of_book(model_instance.id, db_session)

    indexing_data = book_to_dict(db_session, model_instance)
    indexing_data['book_id'] = model_instance.id
    indexing_data.update(elastic_data)
    del indexing_data['roles']

    print(('indexing_data : {}').format(indexing_data))

    logger.debug(LogMsg.ELASTIC_INDEX_DELETE, id)
    delete_book_index(model_instance.id)

    logger.debug(LogMsg.INDEXING_IN_ELASTIC, indexing_data)
    index_book(indexing_data, db_session)

    edited_book = book_to_dict(db_session, model_instance)

    logger.debug(LogMsg.MODEL_ALTERED)

    logger.info(LogMsg.END)

    return edited_book
Ejemplo n.º 3
0
def add_multiple_type_books(db_session, data, username):
    logger.info(LogMsg.START, username)

    logger.debug(LogMsg.BOOK_CHECKING_IF_EXISTS, data)
    types = data.get('types')
    logger.debug(LogMsg.ENUM_CHECK, {'book_types': types})
    check_enums(types, legal_types)

    roles_data = data.get('roles')

    book_data = {k: v for k, v in data.items() if k not in ['roles', 'types']}
    logger.debug(LogMsg.PERMISSION_CHECK, username)
    permissions, presses = get_user_permissions(username, db_session)
    has_permit = has_permission_or_not([Permissions.BOOK_ADD_PREMIUM],
                                       permissions)
    press = None
    for item in roles_data:
        if 'Press' in item.values():
            person = item.get('person', None)
            if person is not None:
                press = person.get('id', None)
    if press is None:
        logger.error(LogMsg.DATA_MISSING, {'press': None})
        raise Http_error(400, Message.MISSING_REQUIERED_FIELD)

    if not has_permit:
        if press in presses:
            has_permission([Permissions.BOOK_ADD_PRESS], permissions)
        else:
            logger.error(LogMsg.PERMISSION_DENIED)
            raise Http_error(403, Message.ACCESS_DENIED)
    logger.debug(LogMsg.PERMISSION_VERIFIED, username)

    result = []
    logger.debug(LogMsg.ADDING_MULTIPLE_BOOKS, data)

    for type in types:
        data['type'] = type
        data['press'] = press
        data['roles'] = roles_data
        unique_code = add_uniquecode(data, db_session)

        book_data.update({
            'type': type,
            'press': press,
            'unique_code': unique_code.UniqueCode
        })

        logger.debug(LogMsg.ADD_BOOK, book_data)
        book = add(db_session, book_data, username)

        logger.debug(LogMsg.ADDING_ROLES_TO_BOOK, roles_data)
        roles, elastic_data = add_book_roles(book.id, roles_data, db_session,
                                             username)

        result.append(book_to_dict(db_session, book))

        index_data = data
        index_data['type'] = type
        index_data['book_id'] = book.id
        index_data['tags'] = book.tags

        if 'roles' in index_data:
            del index_data['roles']
        index_data.update(elastic_data)

        logger.debug(LogMsg.INDEXING_IN_ELASTIC, index_data)
        index_book(index_data, db_session)

    logger.info(LogMsg.END)

    return result
Ejemplo n.º 4
0
def edit_book(id, db_session, data, username):
    logger.info(LogMsg.START, username)

    logger.info(LogMsg.EDITING_BOOK, id)
    schema_validate(data, BOOK_EDIT_SCHEMA_PATH)

    model_instance = db_session.query(Book).filter(Book.id == id).first()
    if model_instance is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': id})
        raise Http_error(404, Message.NOT_FOUND)
    logger.debug(LogMsg.GET_SUCCESS, id)

    logger.debug(LogMsg.PERMISSION_CHECK, username)
    validate_permissions_and_access(username,
                                    db_session,
                                    'BOOK_EDIT',
                                    model=model_instance)
    logger.debug(LogMsg.PERMISSION_VERIFIED)

    logger.debug(LogMsg.EDIT_REQUST, {'book_id': id})

    roles = []
    elastic_data = {}

    if 'roles' in data.keys():
        roles = data.get('roles')
        del data['roles']
    if 'price' in data.keys():
        price = data.get('price')
        if price is None:
            delete_book_price(model_instance.id, db_session)
        else:
            edit_price(model_instance.id, price, db_session)

    for key, value in data.items():
        setattr(model_instance, key, value)
    edit_basic_data(model_instance, username, data.get('tags'))

    if len(roles) > 0:
        logger.debug(LogMsg.DELETING_BOOK_ROLES, id)
        delete_book_roles(model_instance.id, db_session)
        logger.debug(LogMsg.ADDING_ROLES_TO_BOOK, id)
        nroles, elastic_data = add_book_roles(model_instance.id, roles,
                                              db_session, username)
        new_roles = []
        for role in nroles:
            logger.debug(LogMsg.ATTACHING_ROLES_TO_BOOKS)
            new_roles.append(book_role_to_dict(role))
    else:
        elastic_data = persons_of_book(model_instance.id, db_session)

    indexing_data = book_to_dict(db_session, model_instance)
    indexing_data['book_id'] = model_instance.id
    indexing_data.update(elastic_data)
    del indexing_data['roles']

    print(('indexing_data : {}').format(indexing_data))

    logger.debug(LogMsg.ELASTIC_INDEX_DELETE, id)
    delete_book_index(model_instance.id)

    logger.debug(LogMsg.INDEXING_IN_ELASTIC, indexing_data)
    index_book(indexing_data, db_session)

    edited_book = book_to_dict(db_session, model_instance)

    logger.debug(LogMsg.MODEL_ALTERED)

    logger.info(LogMsg.END)

    return edited_book