Exemple #1
0
def add(db_session, data, username):
    logger.info(LogMsg.START, username)

    logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data)
    unique_code = add_uniquecode(data, db_session)

    model_instance = BookRole()

    logger.debug(LogMsg.POPULATING_BASIC_DATA)

    populate_basic_data(model_instance, username, data.get('tags'))

    model_instance.person_id = data.get('person_id')
    model_instance.book_id = data.get('book_id')
    model_instance.role = data.get('role')

    logger.debug(LogMsg.DATA_ADDITION, book_role_to_dict(model_instance))

    db_session.add(model_instance)
    logger.debug(LogMsg.DB_ADD)

    add_connector(model_instance.id, unique_code.UniqueCode, db_session)
    logger.debug(
        LogMsg.UNIQUE_CONNECTOR_ADDED, {
            'book_role_id': model_instance.id,
            'unique_constraint': unique_code.UniqueCode
        })

    logger.info(LogMsg.END)

    return model_instance
Exemple #2
0
def edit(id, db_session, data, username):
    logger.info(LogMsg.START, username)

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

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

    logger.debug(LogMsg.EDITING_BOOK, id)

    files = data.get('files', None)
    images = data.get('images', None)
    if files:
        logger.debug(LogMsg.DELETE_BOOK_FILES, id)
        delete_files(model_instance.files)

    if images:
        logger.debug(LogMsg.DELETE_BOOK_IMAGES)
        delete_files(model_instance.images)

    for key, value in data.items():
        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)

    logger.debug(LogMsg.MODEL_ALTERED)

    logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING)
    unique_data = book_to_dict(db_session, model_instance)
    del unique_data['roles']
    unique_data['roles'] = data['roles']

    unique_connector = get_connector(id, db_session)
    if unique_connector:
        logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
        delete_uniquecode(unique_connector.UniqueCode, db_session)
        logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, unique_data)
        code = add_uniquecode(unique_data, db_session)
        delete_connector(id, db_session)
        add_connector(id, code.UniqueCode, db_session)

    logger.debug(LogMsg.EDIT_SUCCESS, book_to_dict(db_session, model_instance))

    logger.info(LogMsg.END)

    return book_to_dict(db_session, model_instance)
Exemple #3
0
def add(db_session, data, username, **kwargs):
    logger.debug(LogMsg.START, username)

    genre = data.get('genre', [])
    if genre and len(genre) > 0:
        check_enums(genre, Genre)
    logger.debug(LogMsg.ENUM_CHECK, {'genre': genre})

    model_instance = Book()

    populate_basic_data(model_instance, username, data.get('tags'))
    logger.debug(LogMsg.POPULATING_BASIC_DATA)

    model_instance.title = data.get('title')
    model_instance.edition = data.get('edition')
    model_instance.pub_year = data.get('pub_year')
    model_instance.type = data.get('type')
    model_instance.genre = genre
    model_instance.images = data.get('images')
    model_instance.files = data.get('files')
    model_instance.language = data.get('language')
    model_instance.rate = data.get('rate')
    model_instance.description = data.get('description')
    model_instance.pages = data.get('pages')
    model_instance.duration = data.get('duration')
    model_instance.size = data.get('size')
    model_instance.isben = data.get('isben')
    model_instance.description = data.get('description')
    model_instance.from_editor = data.get('from_editor')
    model_instance.press = data.get('press')

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

    db_session.add(model_instance)
    logger.debug(LogMsg.DB_ADD)

    add_connector(model_instance.id, data.get('unique_code'), db_session)
    logger.debug(LogMsg.UNIQUE_CONNECTOR_ADDED, {
        'book_id': model_instance.id,
        'unique_constraint': data.get('unique_code')
    })

    price = data.get('price', None)
    if price:
        add_price_internal(price, model_instance.id, db_session, username)

    logger.info(LogMsg.END)
    return model_instance
Exemple #4
0
def rename_collection(data, db_session, username):
    logger.info(LogMsg.START, username)

    check_schema(['title', 'new_title'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)
    person_id = data.get('person_id')
    if person_id is None:
        user = check_user(username, db_session)
        person_id = user.person_id
        if person_id is None:
            logger.error(LogMsg.USER_HAS_NO_PERSON, username)
            raise Http_error(401, Message.INVALID_USER)

    validate_person(person_id, db_session)
    logger.debug(LogMsg.PERSON_EXISTS)

    new_title = data.get('new_title')
    # db_session.update(Collection).Where(Collection.title == title,
    #                                     Collection.person_id == person_id).values(
    #     title=new_title)
    result = db_session.query(Collection).filter(
        Collection.title == data.get('title'),
        Collection.person_id == person_id).all()
    final_res = []
    for item in result:
        item.title = new_title
        unique_connector = get_connector(item.id, db_session)
        if unique_connector:
            logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
            delete_uniquecode(unique_connector.UniqueCode, db_session)
            delete_connector(item.id, db_session)
            db_session.flush()
        model_dict = collection_to_dict(db_session, item)
        logger.debug(LogMsg.MODEL_ALTERED, model_dict)
        print(model_dict)

        logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, model_dict)
        unique_code = unique_code_exists(model_dict, db_session)
        if unique_code is None:
            logger.debug(LogMsg.UNIQUE_NOT_EXISTS)
            unique_code = add_uniquecode(model_dict, db_session)
            db_session.flush()
        add_connector(item.id, unique_code.UniqueCode, db_session)
        logger.debug(LogMsg.UNIQUE_CONNECTOR_ADDED, {
            'collection_id': item.id,
            'unique_constraint': unique_code.UniqueCode
        })
        final_res.append(model_dict)

    return final_res
def add(db_session, data, username):
    logger.info(LogMsg.START, username)
    schema_validate(data,PERSON_ADD_SCHEMA_PATH)

    if username is not None and username != SIGNUP_USER:
        permissions, presses = get_user_permissions(username, db_session)
        has_permission([Permissions.PERSON_ADD_PREMIUM],
                       permissions)
        logger.debug(LogMsg.PERMISSION_VERIFIED)

    cell_no = data.get('cell_no')
    if cell_no and person_cell_exists(db_session, cell_no):
        logger.error(LogMsg.PERSON_EXISTS, {'cell_no': cell_no})
        raise Http_error(409, Message.ALREADY_EXISTS)

    email = data.get('email')

    if email and person_mail_exists(db_session, email):
        logger.error(LogMsg.PERSON_EXISTS, {'email': email})
        raise Http_error(409, Message.ALREADY_EXISTS)

    logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data)

    model_instance = Person()
    populate_basic_data(model_instance, username, data.get('tags'))
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.name = data.get('name')
    model_instance.last_name = data.get('last_name')
    model_instance.full_name = full_name(model_instance.name,model_instance.last_name)
    model_instance.address = data.get('address')
    model_instance.phone = data.get('phone')
    model_instance.email = data.get('email')
    model_instance.cell_no = data.get('cell_no')
    model_instance.bio = data.get('bio')
    model_instance.image = data.get('image')
    model_instance.is_legal = data.get('is_legal', False)

    unique_code = add_uniquecode(model_instance, db_session)
    db_session.add(model_instance)

    db_session.flush()
    logger.debug(LogMsg.DB_ADD)
    add_initial_account(model_instance.id, db_session, username)
    logger.debug(LogMsg.PERSON_ADD_ACCOUNT, {'person_id': model_instance.id})

    add_connector(model_instance.id, unique_code.UniqueCode, db_session)
    logger.debug(LogMsg.UNIQUE_CONNECTOR_ADDED, {'person_id': model_instance.id,
                                                 'unique_constraint': unique_code.UniqueCode})
    logger.info(LogMsg.END)
    return model_instance
Exemple #6
0
def edit(id, db_session, data, username):
    logger.info(LogMsg.START, username)

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

    logger.debug(LogMsg.MODEL_GETTING, id)

    model_instance = db_session.query(BookRole).filter(
        BookRole.id == id).first()
    if model_instance:
        logger.debug(LogMsg.GET_SUCCESS, book_role_to_dict(model_instance))
    else:
        logger.debug(LogMsg.GET_FAILED, id)
        raise Http_error(404, Message.NOT_FOUND)

    for key, value in data.items():
        # TODO  if key is valid attribute of class
        setattr(model_instance, key, value)

    logger.debug(LogMsg.EDITING_BASIC_DATA, id)
    edit_basic_data(model_instance, username, data.get('tags'))

    logger.debug(LogMsg.MODEL_ALTERED, id)

    logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING)

    unique_connector = get_connector(id, db_session)
    if unique_connector:
        logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
        delete_uniquecode(unique_connector.UniqueCode, db_session)
        logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data)
        code = add_uniquecode(data, db_session)
        delete_connector(id, db_session)
        add_connector(id, code.UniqueCode, db_session)

    logger.debug(LogMsg.EDIT_SUCCESS, book_role_to_dict(model_instance))

    logger.info(LogMsg.END)

    return model_instance
Exemple #7
0
def edit(id, db_session, data, username):
    logger.info(LogMsg.START, username)
    if "id" in data.keys():
        del data["id"]
    logger.debug(LogMsg.EDIT_REQUST)
    logger.debug(LogMsg.MODEL_GETTING, {'book_id': id})
    model_instance = db_session.query(Book).filter(Book.id == id).first()
    if model_instance:
        logger.debug(LogMsg.GET_SUCCESS)
    else:
        logger.debug(LogMsg.NOT_FOUND, {'book_id': id})
        raise Http_error(404, Message.NOT_FOUND)

    # 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 and  model_instance.press in presses:
    #     has_permission([Permissions.BOOK_EDIT_PRESS], permissions)

    logger.debug(LogMsg.EDITING_BOOK, id)

    files = data.get('files', None)
    images = data.get('images', None)
    if files:
        logger.debug(LogMsg.DELETE_BOOK_FILES, id)
        delete_files(model_instance.files)

    if images:
        logger.debug(LogMsg.DELETE_BOOK_IMAGES)
        delete_files(model_instance.images)

    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)

    logger.debug(LogMsg.MODEL_ALTERED)

    logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING)
    unique_data = book_to_dict(db_session, model_instance)
    del unique_data['roles']
    unique_data['roles'] = data['roles']

    unique_connector = get_connector(id, db_session)
    if unique_connector:
        logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
        delete_uniquecode(unique_connector.UniqueCode, db_session)
        logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, unique_data)
        code = add_uniquecode(unique_data, db_session)
        delete_connector(id, db_session)
        add_connector(id, code.UniqueCode, db_session)

    logger.debug(LogMsg.EDIT_SUCCESS, book_to_dict(db_session, model_instance))

    logger.info(LogMsg.END)

    return book_to_dict(db_session, model_instance)
Exemple #8
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

    check_schema(['title'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)
    title = data.get('title')

    if 'person_id' in data:
        person_id = data.get('person_id')
    else:
        user = check_user(username, db_session)
        if user is None:
            raise Http_error(404, Message.INVALID_USER)

        if user.person_id is None:
            logger.error(LogMsg.USER_HAS_NO_PERSON, username)
            raise Http_error(404, Message.Invalid_persons)
        person_id = user.person_id
        data['person_id'] = person_id

    validate_person(person_id, db_session)
    logger.debug(LogMsg.PERSON_EXISTS, person_id)

    logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data)
    unique_code = unique_code_exists(data, db_session)
    if unique_code is not None and username != 'internal':
        logger.error(LogMsg.COLLECTION_EXISTS, {
            'collection_tilte': data.get('title'),
            'person_id': person_id
        })
        raise Http_error(409, Message.ALREADY_EXISTS)

    if unique_code is None:
        unique_code = add_uniquecode(data, db_session)
        db_session.flush()
    logger.debug(LogMsg.COLLECTION_ADD_NEW_COLLECTION, {'title': title})
    book_ids = data.get('book_ids', None)

    if book_ids is None:
        logger.debug(LogMsg.COLLECTION_ADD_EMPTY_COLLECTION, {'title': title})

        model_instance = Collection()
        logger.debug(LogMsg.POPULATING_BASIC_DATA)
        populate_basic_data(model_instance, username, data.get('tags'))
        model_instance.person_id = person_id
        model_instance.title = title

        db_session.add(model_instance)
        add_connector(model_instance.id, unique_code.UniqueCode, db_session)
        logger.debug(
            LogMsg.UNIQUE_CONNECTOR_ADDED, {
                'collection_id': model_instance.id,
                'unique_constraint': unique_code.UniqueCode
            })

        logger.debug(LogMsg.DB_ADD)

        return data

    for item in book_ids:
        log_data = {'book_id': item, 'collection': title}
        logger.debug(LogMsg.COLLECTION_ADD_BOOKS_TO_COLLECTION, title)
        data['book_id'] = item

        collection_item = get(item, person_id, title, db_session)
        if collection_item is not None:
            logger.error(LogMsg.COLLECTION_BOOK_ALREADY_EXISTS, log_data)
            raise Http_error(409, Message.ALREADY_EXISTS)

        if not is_book_in_library(person_id, item, db_session):
            logger.error(LogMsg.COLLECTION_BOOK_IS_NOT_IN_LIBRARY,
                         {'book_id': item})
            raise Http_error(403, Message.BOOK_NOT_IN_LIB)

        logger.debug(LogMsg.COLLECTION_CHECK_BOOK_IS_IN_COLLECTION, log_data)

        model_instance = Collection()
        logger.debug(LogMsg.POPULATING_BASIC_DATA)
        populate_basic_data(model_instance, username, data.get('tags'))
        model_instance.person_id = person_id
        model_instance.book_id = item
        model_instance.title = title

        logger.debug(LogMsg.DB_ADD)
        db_session.add(model_instance)
        add_connector(model_instance.id, unique_code.UniqueCode, db_session)
        logger.debug(
            LogMsg.UNIQUE_CONNECTOR_ADDED, {
                'collection_id': model_instance.id,
                'unique_constraint': unique_code.UniqueCode
            })

        logger.info(LogMsg.END)

    return data
def edit(id, db_session, data, username):
    logger.info(LogMsg.START, username)

    schema_validate(data,PERSON_EDIT_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

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


    user = check_user(username, db_session)
    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON, username)
        raise Http_error(404, Message.INVALID_USER)

    model_instance = db_session.query(Person).filter(Person.id == id).first()
    if model_instance:
        logger.debug(LogMsg.MODEL_GETTING)
    else:
        logger.debug(LogMsg.MODEL_GETTING_FAILED, {'person_id': id})
        raise Http_error(404, Message.NOT_FOUND)

    per_data = {}
    permissions, presses = get_user_permissions(username, db_session)
    if user.person_id == id:
        per_data.update({Permissions.IS_OWNER.value: True})
    has_permission([Permissions.PERSON_EDIT_PREMIUM],
                   permissions, None, per_data)
    logger.debug(LogMsg.PERMISSION_VERIFIED)

    if 'current_book_id' in data.keys():
        if not is_book_in_library(model_instance.id, data.get('current_book_id'),
                                  db_session):
            logger.error(LogMsg.COLLECTION_BOOK_IS_NOT_IN_LIBRARY,
                         {'current_book_id': data.get('current_book_id')})
            raise Http_error(404, Message.BOOK_NOT_IN_LIB)
    if 'cell_no' in data.keys():
        cell_person = person_cell_exists(db_session, data.get('cell_no'))
        if cell_person is not None:
            if cell_person.id != model_instance.id:
                logger.error(LogMsg.ANOTHER_PERSON_BY_CELL)
                raise Http_error(403, Message.CELL_EXISTS)

    try:
        for key, value in data.items():
            # TODO  if key is valid attribute of class
            setattr(model_instance, key, value)
        model_instance.full_name = full_name(model_instance.name,model_instance.last_name)

        edit_basic_data(model_instance, username, data.get('tags'))
        db_session.flush()

        logger.debug(LogMsg.MODEL_ALTERED,
                     person_to_dict(model_instance, db_session))

        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING)
        unique_connector = get_connector(id, db_session)
        if unique_connector:
            if persons_code(model_instance) != unique_connector.UniqueCode:
                logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
                delete_uniquecode(unique_connector.UniqueCode, db_session)
                db_session.flush()
                logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data)
                code = add_uniquecode(model_instance, db_session)
                delete_connector(id, db_session)
                add_connector(id, code.UniqueCode, db_session)
    except:
        logger.exception(LogMsg.EDIT_FAILED, exc_info=True)
        raise Http_error(500, Message.DELETE_FAILED)

    logger.info(LogMsg.END)
    return model_instance
Exemple #10
0
def edit(id, db_session, data, username):
    logger.info(LogMsg.START, username)

    # TODO: you never checked version of passed data, we have version field in our
    #      records, to prevent conflict when we received two different edit request
    #      concurrently. check KAVEH codes (edit functions) to better understanding
    #      version field usage

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

    if "id" in data.keys():
        del data["id"]
    user = check_user(username,db_session)
    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON,username)
        raise Http_error(404,Message.INVALID_USER)

    model_instance = db_session.query(Person).filter(Person.id == id).first()
    if model_instance:
        logger.debug(LogMsg.MODEL_GETTING)
    else:
        logger.debug(LogMsg.MODEL_GETTING_FAILED, {'person_id': id})
        raise Http_error(404, Message.NOT_FOUND)

    per_data = {}
    permissions, presses = get_user_permissions(username, db_session)
    if user.person_id == id:
        per_data.update({Permissions.IS_OWNER.value: True})
    has_permission([Permissions.PERSON_EDIT_PREMIUM],
                   permissions, None, per_data)
    logger.debug(LogMsg.PERMISSION_VERIFIED)

    if 'current_book' in data.keys():
        if not is_book_in_library(model_instance.id, data.get('current_book'),
                                  db_session):
            logger.error(LogMsg.COLLECTION_BOOK_IS_NOT_IN_LIBRARY,
                         {'current_book_id': data.get('current_book')})
            raise Http_error(404, Message.BOOK_NOT_IN_LIB)
    if 'cell_no' in data.keys():
        cell_person  = person_cell_exists(db_session,data.get('cell_no'))
        if cell_person is not None:
            if cell_person.id !=model_instance.id:
                logger.error(LogMsg.ANOTHER_PERSON_BY_CELL)
                raise Http_error(403,Message.CELL_EXISTS)

    try:
        for key, value in data.items():
            # TODO  if key is valid attribute of class
            setattr(model_instance, key, value)
        model_instance.full_name ='{} {}'.format(model_instance.last_name or '',model_instance.name or '')
        edit_basic_data(model_instance, username, data.get('tags'))
        db_session.flush()

        logger.debug(LogMsg.MODEL_ALTERED,
                     person_to_dict(model_instance, db_session))

        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS_CHANGING)
        unique_connector = get_connector(id, db_session)
        if unique_connector:
            logger.debug(LogMsg.DELETE_UNIQUE_CONSTRAINT)
            delete_uniquecode(unique_connector.UniqueCode, db_session)
            logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data)
            db_session.flush()
            code = add_uniquecode(data, db_session)
            delete_connector(id, db_session)
            add_connector(id, code.UniqueCode, db_session)
    except:
        logger.exception(LogMsg.EDIT_FAILED,exc_info=True)
        raise Http_error(403,Message.DELETE_FAILED)

    logger.info(LogMsg.END)
    return model_instance