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

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

    book_id = data.get('book_id')

    person_id = data.get('person_id')
    if person_id is None:
        logger.error(LogMsg.DATA_MISSING, 'person_id')

    if is_book_in_library(person_id, book_id, db_session):
        logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id})
        raise Http_error(409, Message.ALREADY_EXISTS)

    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    if book.type.name in ONLINE_BOOK_TYPES :
        if data.get('count') > 1:
            logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION)
            raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION)

        content_id = book_has_content(book_id,'Original',db_session)
        if not content_id:
            logger.error(LogMsg.CONTENT_NOT_FOUND,{'book_id':book_id})
            raise Http_error(404,Message.BOOK_HAS_NO_CONTENT)

        if not is_generated(content_id):
            logger.error(LogMsg.CONTENT_NOT_GENERATED, {'content_id': content_id})
            raise Http_error(404, Message.BOOK_NOT_GENERATED)

    model_instance = OrderItem()

    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.order_id = data.get('order_id')
    model_instance.book_id = book_id
    model_instance.count = data.get('count')
    # TODO discount not imposed yet
    model_instance.discount = data.get('discount', 0.0)
    model_instance.description = data.get('description')
    model_instance.price_detail = data.get('price_detail')

    model_instance.unit_price = get_book_price_internal(model_instance.book_id,
                                                        db_session)
    logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price)

    model_instance.net_price = calc_net_price(model_instance.unit_price,
                                              model_instance.count,
                                              model_instance.discount)
    logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price)

    db_session.add(model_instance)
    logger.info(LogMsg.END)

    return model_instance
Beispiel #2
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, RATING_ADD_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)
    user = check_user(username, db_session)
    rated = internal_get(user.person_id, data.get('movie_id'), db_session)
    if rated is not None:
        result = edit(rated.id, data, db_session, username)
        return result
    model_instance = Rating()
    populate_basic_data(model_instance, username, data.get('tags'))
    logger.debug(LogMsg.POPULATING_BASIC_DATA)

    model_instance.movie_id = data.get('movie_id')
    model_instance.person_id = user.person_id
    model_instance.comment = data.get('comment')
    model_instance.overall_rate = data.get('overall_rate')
    model_instance.novel = data.get('novel')
    model_instance.character = data.get('character')
    model_instance.reason = data.get('reason')
    model_instance.directing = data.get('directing')
    model_instance.true_vision_of_enemy = data.get('true_vision_of_enemy')
    model_instance.iranian_life_style = data.get('iranian_life_style')
    model_instance.hope = data.get('hope')
    model_instance.bright_future_exposure = data.get('bright_future_exposure')
    model_instance.theism = data.get('theism')
    model_instance.justice_seeking = data.get('justice_seeking')
    model_instance.feminism_exposure = data.get('feminism_exposure')
    model_instance.individual_social_behavior = data.get('individual_social_behavior')
    model_instance.family_subject = data.get('family_subject')
    model_instance.alcoholic_promotion = data.get('alcoholic_promotion')
    model_instance.gambling_promotion = data.get('gambling_promotion')
    model_instance.breaking_law_encouragement = data.get('breaking_law_encouragement')
    model_instance.suicide_encouragement = data.get('suicide_encouragement')
    model_instance.horror_content = data.get('horror_content')
    model_instance.addiction_promotion = data.get('addiction_promotion')
    model_instance.unsuitable_wearing = data.get('unsuitable_wearing')
    model_instance.sexual_content = data.get('sexual_content')
    model_instance.insulting_range = data.get('insulting_range')
    model_instance.violence_range = data.get('violence_range')
    model_instance.music = data.get('music')
    model_instance.sound = data.get('sound')
    model_instance.visualization = data.get('visualization')
    model_instance.editing = data.get('editing')
    model_instance.acting = data.get('acting')
    model_instance.true_historiography = data.get('true_historiography')
    model_instance.question_1 = data.get('question_1')
    model_instance.question_2 = data.get('question_2')
    model_instance.question_3 = data.get('question_3')
    model_instance.question_4 = data.get('question_4')
    model_instance.question_5 = data.get('question_5')
    model_instance.question_6 = data.get('question_6')
    model_instance.question_7 = data.get('question_7')
    model_instance.question_8 = data.get('question_8')
    model_instance.question_9 = data.get('question_9')
    model_instance.question_10 = data.get('question_10')

    db_session.add(model_instance)
    logger.debug(LogMsg.DB_ADD)
    return model_instance
def add_payment(data, db_session, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, CHECKOUT_ADD_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    logger.debug(LogMsg.PERMISSION_CHECK, username)
    validate_permissions_and_access(username, db_session,
                                    'CHECKOUT_PAYMENT_ADD',
                                    access_level=Access_level.Premium)
    logger.debug(LogMsg.PERMISSION_VERIFIED, username)

    model_instance = CheckoutPressAccount()
    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)

    model_instance.amount = data.get('amount')
    model_instance.payer_id = data.get('payer_id')
    model_instance.receiver_id = data.get('receiver_id')
    model_instance.receiver_account_id = data.get('receiver_account_id')
    model_instance.payment_details = data.get('payment_details')

    db_session.add(model_instance)

    logger.debug(LogMsg.DB_ADD, model_to_dict(model_instance))
    logger.info(LogMsg.END)
    return model_instance
def premium_permission_group(group_id, db_session, username):
    logger.info(LogMsg.START, username)
    logger.debug(LogMsg.PERMISSION_CHECK, username)
    validate_permissions_and_access(username,
                                    db_session,
                                    'ASSIGN_PREMIUM_PERMISSION_GROUP',
                                    access_level=Access_level.Premium)
    logger.debug(LogMsg.PERMISSION_VERIFIED, username)

    permissions = permission_list(db_session, 'PREMIUM')
    result = []
    for permission_id in permissions:
        if group_has_permission(permission_id, group_id, db_session):
            logger.debug(LogMsg.PERMISSION_GROUP_ALREADY_HAS)
            pass
        else:
            model_instance = GroupPermission()
            populate_basic_data(model_instance, username)
            logger.debug(LogMsg.POPULATING_BASIC_DATA)
            model_instance.group_id = group_id
            model_instance.permission_id = permission_id
            db_session.add(model_instance)
            result.append(group_permission_to_dict(model_instance))
    logger.info(LogMsg.END)
    return result
Beispiel #5
0
def add(data, db_session, username=None):
    logger.info(LogMsg.START, username)

    # schema_validate(data,ADD_SCHEMA_PATH)
    if username is not None:
        logger.debug(LogMsg.PERMISSION_CHECK, username)
        validate_permissions_and_access(username,
                                        db_session,
                                        'TRANSACTION_ADD',
                                        access_level=Access_level.Premium)
        logger.debug(LogMsg.PERMISSION_VERIFIED)

    account_id = data.get('account_id')
    logger.debug(LogMsg.GETTING_ACCOUNT_BY_ID, account_id)
    account = get_account(account_id, db_session)
    if account is None:
        logger.error(LogMsg.NOT_FOUND, {'account_id': account_id})
        raise Http_error(404, Message.NOT_FOUND)

    model_instance = Transaction()

    populate_basic_data(model_instance, username)
    model_instance.account_id = account_id
    model_instance.credit = data.get('credit')
    model_instance.debit = data.get('debit')
    model_instance.payment_details = data.get('payment_details')

    db_session.add(model_instance)
    logger.debug(LogMsg.TRANSACTION_ADDED, model_to_dict(model_instance))
    logger.info(LogMsg.END)

    return model_instance
Beispiel #6
0
def add(data, db_session, username=None):
    logger.info(LogMsg.START, username)

    if data.get('credit') and data.get('debit'):
        logger.error(LogMsg.DATA_MISSING)
        raise Http_error(400, Message.CREDIT_DEBIT_ERROR)

    if username is not None:
        permissions, presses = get_user_permissions(username, db_session)

        has_permission([Permissions.TRANSACTION_ADD_PREMIUM], permissions)

    account_id = data.get('account_id')
    logger.debug(LogMsg.GETTING_ACCOUNT_BY_ID, account_id)
    account = get_account(account_id, db_session)
    if account is None:
        logger.error(LogMsg.NOT_FOUND, {'account_id': account_id})
        raise Http_error(404, Message.NOT_FOUND)

    model_instance = Transaction()

    populate_basic_data(model_instance, username)
    model_instance.account_id = account_id
    model_instance.credit = data.get('credit')
    model_instance.debit = data.get('debit')
    model_instance.payment_details = data.get('payment_details')

    db_session.add(model_instance)
    logger.debug(LogMsg.TRANSACTION_ADDED, model_to_dict(model_instance))
    logger.info(LogMsg.END)

    return model_instance
Beispiel #7
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
Beispiel #8
0
def add(data, db_session):
    logger.info(LogMsg.START)

    logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data)

    name = data.get('name')
    last_name = data.get('last_name')
    cell_no = data.get('cell_no')

    try:
        unique_code = ConstraintHandler()
        populate_basic_data(unique_code, 'INTERNAL', None)

        the_code = 'PERSON-{}-{}-{}'.format(name, last_name, cell_no)
        unique_code.UniqueCode = the_code

        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS, the_code)

        db_session.add(unique_code)
        db_session.flush()
    except:
        logger.exception(LogMsg.UNIQUE_CONSTRAINT_EXISTS, exc_info=True)
        raise Http_error(409, Message.ALREADY_EXISTS)

    logger.info(LogMsg.END)

    return unique_code
Beispiel #9
0
def add(db_session, data, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, PERSON_ADD_SCHEMA_PATH)

    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)

    db_session.add(model_instance)
    logger.info(LogMsg.END)
    return model_instance
Beispiel #10
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

    user = check_user(username, db_session)
    if user is None:
        logger.error(LogMsg.INVALID_USER, username)

    model_instance = DiscussionGroup()
    populate_basic_data(model_instance, username, data.get('tags'))
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.title = data.get('title')
    model_instance.description = data.get('description')
    model_instance.image = data.get('image')
    model_instance.status = 'Created'

    db_session.add(model_instance)

    members = data.get('members', [])
    members.append({'person_id': user.person_id, 'type': 'Admin'})
    member_data = {'group_id': model_instance.id, 'members': members}

    logger.debug(LogMsg.DISCUSSION_GROUP_ADD, model_to_dict(model_instance))
    discuss_members = add_disscussuion_members(member_data, db_session,
                                               username)
    logger.debug(LogMsg.DISCUSSION_GROUP_MEMBERS_ADDED, members)
    result = model_to_dict(model_instance)
    result['members'] = discuss_members

    logger.info(LogMsg.END)
    return result
def add(db_session, data):
    logger.info(LogMsg.START, '--INTERNAL--')

    logger.debug(LogMsg.SCHEMA_CHECKED)

    seen = get_internal(data.get('receptor_id'), data.get('sender_id'),
                        data.get('group_id'), db_session)
    if seen is not None:
        seen.last_seen = data.get('last_seen')
        edit_basic_data(seen, 'INTERNAL')
        logger.debug(LogMsg.LAST_SEEN_UPDATE,data)
        return seen

    model_instance = LastSeen()
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    populate_basic_data(model_instance, 'INTERNAL', data.get('tags'))
    model_instance.sender_id = data.get('sender_id')
    model_instance.receptor_id = data.get('receptor_id')
    model_instance.group_id = data.get('group_id', None)
    model_instance.last_seen = data.get('last_seen')

    db_session.add(model_instance)
    logger.debug(LogMsg.LAST_SEEN_ADD,data)
    logger.info(LogMsg.END)

    return model_instance
def add(data, db_session):
    logger.info(LogMsg.START)

    logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, data)

    title = data.get('title')
    person_id = data.get('person_id')

    try:
        unique_code = ConstraintHandler()
        populate_basic_data(unique_code, 'INTERNAL', None)

        the_code = 'COLLECTION-{}-{}'.format(title, person_id)
        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS, the_code)

        unique_code.UniqueCode = the_code

        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS, the_code)

        db_session.add(unique_code)
    except:
        logger.exception(LogMsg.UNIQUE_CONSTRAINT_EXISTS, exc_info=True)
        raise Http_error(409, Message.ALREADY_EXISTS)

    logger.info(LogMsg.END)

    return unique_code
def add(permission_id, group_id, db_session, username):
    logger.info(LogMsg.START, username)

    user = check_user(username, db_session)
    group = get_group(group_id, db_session, username=None)
    permission = get_permission(permission_id, db_session)
    if user.person_id != group.person_id:
        logger.error(LogMsg.PERMISSION_DENIED,
                     {'add_permission_to_group_be user': username})
        raise Http_error(403, Message.ACCESS_DENIED)

    if group_has_permission(permission_id, group_id, db_session):
        logger.error(LogMsg.PERMISSION_GROUP_ALREADY_HAS)
        raise Http_error(409, Message.ALREADY_EXISTS)

    permissions, presses = get_user_permissions(username, db_session)

    if permission.permission not in permissions:
        logger.error(LogMsg.PREMIUM_PERMISSION_ADDITION_RESTRICTION, username)
        raise Http_error(403, Message.ACCESS_DENIED)

    model_instance = GroupPermission()
    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.group_id = group_id
    model_instance.permission_id = permission_id

    db_session.add(model_instance)

    logger.info(LogMsg.END)
    return model_instance
def add(db_session, data, username):
    logger.info(LogMsg.START, username)

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

    book_id = data.get('book_id')

    logger.debug(LogMsg.COMMENT_VALIDATING_BOOK, book_id)
    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    logger.debug(LogMsg.CHECK_USER_EXISTANCE, username)
    user = check_user(username, db_session)
    if user is None:
        logger.error(LogMsg.INVALID_USER, username)
        raise Http_error(400, Message.INVALID_USER)

    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON, username)
        raise Http_error(400, Message.Invalid_persons)

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

    logger.debug(LogMsg.COMMENT_CHECK_FOR_PARENT)
    parent_id = data.get('parent_id', None)
    if parent_id:
        logger.debug(LogMsg.COMMENT_GETTING_PARENT, parent_id)
        parent_comment = get_comment(parent_id, db_session)
        if parent_comment is None:
            logger.error(LogMsg.COMMENT_PARENT_NOT_FOUND, parent_id)
            raise Http_error(404, Message.PARENT_NOT_FOUND)
        if parent_comment.book_id != book_id:
            logger.error(
                LogMsg.COMMENT_PARENT_NOT_MATCH, {
                    'book_id': book_id,
                    'parent': comment_to_dict(db_session, parent_comment)
                })
            raise Http_error(400, Message.ACCESS_DENIED)

    model_instance = Comment()
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    populate_basic_data(model_instance, username, data.get('tags'))
    model_instance.person_id = user.person_id
    model_instance.book_id = book_id
    model_instance.body = data.get('body')
    model_instance.parent_id = data.get('parent_id')

    logger.debug(LogMsg.DATA_ADDITION)

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

    logger.info(LogMsg.END)

    return comment_to_dict(db_session, model_instance, username)
Beispiel #15
0
def add(data, db_session, username):
    logger.debug(LogMsg.START, username)

    permissions, presses = get_user_permissions(username, db_session)
    has_permission([Permissions.ACCOUNT_ADD_PREMIUM], permissions)
    check_schema(['person_id', 'type'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)
    check_enum(data.get('type'), AccountTypes)
    logger.debug(LogMsg.ENUM_CHECK, {
        'enum': data.get('type'),
        'reference_enum': 'AccountTypes'
    })

    user = check_user(username, db_session)
    logger.debug(LogMsg.USER_CHECKING, username)

    if user is None:
        logger.error(LogMsg.INVALID_USER, username)
        raise Http_error(404, Message.INVALID_USER)

    logger.info(LogMsg.USER_XISTS, username)

    if user.person_id is None:
        logger.info(LogMsg.PERSON_NOT_EXISTS, username)

        raise Http_error(404, Message.Invalid_persons)

    validate_person(user.person_id, db_session)
    logger.info(LogMsg.PERSON_EXISTS, username)

    type = data.get('type', 'Main')
    value = data.get('value', 0.0)
    person_id = data.get('person_id')

    logger.info(LogMsg.GETTING_USER_ACCOUNTS, type)

    account = get(person_id, type, db_session)
    if account is not None:
        logger.error(LogMsg.ACCOUNT_BY_TYPE_EXISTS, type)

        raise Http_error(409, Message.ALREADY_EXISTS)

    model_instance = Account()

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

    model_instance.type = type
    model_instance.person_id = person_id
    model_instance.value = value

    db_session.add(model_instance)

    logger.debug(LogMsg.DB_ADD, account_to_dict(model_instance))

    logger.info(LogMsg.END)

    return account_to_dict(model_instance)
Beispiel #16
0
def add(db_session, data, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, ADD_SCHEMA_PATH)

    user = check_user(username, db_session)

    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON, username)
        raise Http_error(400, Message.Invalid_persons)

    book_id = data.get('book_id')
    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(400, Message.NOT_FOUND)

    logger.debug(LogMsg.RATE_CHECK, {
        'book_id': book_id,
        'person_id': user.person_id
    })
    rate = get(book_id, user.person_id, db_session)
    if rate:
        logger.debug(LogMsg.RATE_EXISTS, {
            'book_id': book_id,
            'person_id': user.person_id,
            'rate': rate.rate
        })
        rate.rate = data.get('rate')
        logger.debug(LogMsg.RATE_CHANGED, {
            'book_id': book_id,
            'person_id': user.person_id,
            'rate': rate.rate
        })

        return rate

    else:

        logger.debug(LogMsg.RATE_IS_NEW, {
            'book_id': book_id,
            'person_id': user.person_id
        })

        model_instance = Rate()
        populate_basic_data(model_instance, username, data.get('tags'))
        model_instance.person_id = user.person_id
        model_instance.rate = data.get('rate')
        model_instance.book_id = book_id
        db_session.add(model_instance)
        logger.debug(LogMsg.RATE_ADDED)
        logger.info(LogMsg.END)

        return model_instance
Beispiel #17
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
Beispiel #18
0
def add_initial_account(person_id, db_session, username):
    logger.info(LogMsg.START, username)

    logger.debug(LogMsg.ADD_INITIAL_ACCOUNT, person_id)

    model_instance = Account()
    populate_basic_data(model_instance, username, [])
    model_instance.type = 'Main'
    model_instance.person_id = person_id
    db_session.add(model_instance)
    logger.info(LogMsg.END)

    return account_to_dict(model_instance)
Beispiel #19
0
def add(db_session, data, username):
    logger.info(LogMsg.START, username)

    check_schema(['book_id', 'type', 'content'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)

    book_id = data.get('book_id')
    type = data.get('type')

    logger.debug(LogMsg.ENUM_CHECK, {'BOOK_CONTENT_TYpe': type})
    check_enum(type, BookContentType)

    book = get_book(book_id, db_session)
    if not book:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data)
    content = get_be_data(book_id, type, db_session)
    if content is not None:
        logger.error(LogMsg.CONTENT_EXIST, data)
        raise Http_error(409, Message.ALREADY_EXISTS)

    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)
    if not has_permit:
        if book.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)

    model_instance = BookContent()

    logger.debug(LogMsg.POPULATING_BASIC_DATA)

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

    model_instance.content = data.get('content')
    model_instance.book_id = book_id
    model_instance.type = type
    model_instance.book_press = book.press

    db_session.add(model_instance)

    logger.info(LogMsg.END)

    return model_instance
def add(group_id, person_id, type, db_session, username):
    logger.info(LogMsg.START, username)

    model_instance = DiscussionMember()
    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)

    model_instance.person_id = person_id
    model_instance.group_id = group_id
    model_instance.type = type or 'Normal'

    db_session.add(model_instance)

    return model_instance
Beispiel #21
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, ADD_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)
    user = check_user(username, db_session)
    if user is None:
        raise Http_error(400, Message.INVALID_USER)

    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON, username)
        raise Http_error(400, Message.Invalid_persons)

    validate_person(user.person_id, db_session)
    logger.debug(LogMsg.PERSON_EXISTS, username)
    book_ids = data.get('books')
    for item in book_ids:
        logger.debug(LogMsg.BOOK_CHECKING_IF_EXISTS, {'id': item})
        book = get_book(item, db_session)

        if book is None:
            logger.error(LogMsg.NOT_FOUND, {'book_id': item})
            raise Http_error(400, Message.NOT_FOUND)

        logger.debug(LogMsg.WISH_CHECK_IF_IN_LIST, {
            'person_id': user.person_id,
            'book_id': item
        })
        wish = get(item, user.person_id, db_session)
        if wish is not None:
            logger.error(LogMsg.WISH_ALREADY_EXISTS, {
                'person_id': user.person_id,
                'book_id': item
            })
            raise Http_error(409, Message.ALREADY_EXISTS)

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

        model_instance.person_id = user.person_id
        model_instance.book_id = item

        logger.debug(LogMsg.WISH_ADD, {
            'person_id': user.person_id,
            'book_id': item
        })

        db_session.add(model_instance)
        logger.info(LogMsg.END)

    return data
Beispiel #22
0
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
Beispiel #23
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

    check_schema(['book_id', 'count', 'order_id', 'person_id'], data.keys())
    logger.debug(LogMsg.SCHEMA_CHECKED)
    book_id = data.get('book_id')

    person_id = data.get('person_id')
    if person_id is None:
        logger.error(LogMsg.DATA_MISSING, 'person_id')

    if is_book_in_library(person_id, book_id, db_session):
        logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id})
        raise Http_error(409, Message.ALREADY_EXISTS)

    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    if book.type.name in ONLINE_BOOK_TYPES and data.get('count') > 1:
        logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION)
        raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION)

    model_instance = OrderItem()

    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.order_id = data.get('order_id')
    model_instance.book_id = book_id
    model_instance.count = data.get('count')
    # TODO discount not imposed yet
    model_instance.discount = data.get('discount', 0.0)
    model_instance.description = data.get('description')
    model_instance.price_detail = data.get('price_detail')

    model_instance.unit_price = get_book_price_internal(
        model_instance.book_id, db_session)
    logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price)

    model_instance.net_price = calc_net_price(model_instance.unit_price,
                                              model_instance.count,
                                              model_instance.discount)
    logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price)

    db_session.add(model_instance)
    logger.info(LogMsg.END)

    return model_instance
Beispiel #24
0
def add(data, db_session, username):
    logger.info(LogMsg.START, username)

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

    person_id = data.get('person_id', None)
    if person_id is not None:
        owner_person = validate_person(person_id, db_session)
        if owner_person is not None and not owner_person.is_legal:
            logger.error(LogMsg.PERSON_IS_NOT_LEGAL, username)
            raise Http_error(400, Message.PERSON_IS_NOT_LEGAL)
        owner_user = get_by_person(person_id, db_session)
        if owner_user is None:
            logger.error(LogMsg.PERSON_MUST_HAVE_USER, person_id)
            raise Http_error(404, Message.PERSON_MUST_HAVE_USER)

    user = check_user(username, db_session)

    if username not in ADMINISTRATORS:

        permissions, presses = get_user_permissions(username, db_session)
        permit = has_permission_or_not(
            [Permissions.PERMISSION_GROUP_ADD_PREMIUM], permissions)
        if not permit:
            press_permit = has_permission_or_not(
                [Permissions.PERMISSION_GROUP_ADD_PRESS], permissions)
            if not (press_permit and (person_id == user.person_id)):
                logger.error(LogMsg.PERMISSION_DENIED,
                             {'PERMISSION_GROUP_ADD': username})
                raise Http_error(403, Message.ACCESS_DENIED)

        if check_group_title_exists(data.get('title', None), db_session):
            logger.error(LogMsg.GROUP_EXISTS)
            raise Http_error(409, Message.ALREADY_EXISTS)

    model_instance = Group()
    populate_basic_data(model_instance, username, data.get('tags'))
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.title = data.get('title')
    model_instance.person_id = data.get('person_id')
    db_session.add(model_instance)
    # db_session.flush()
    add_owner_to_group_users(model_instance.id, owner_user.id, db_session,
                             username)

    logger.info(LogMsg.END)
    return model_instance
Beispiel #25
0
def add(book_data, db_session):
    logger.info(LogMsg.START)

    logger.debug(LogMsg.GENERATE_UNIQUE_CONSTRAINT, book_data)

    title = book_data.get('title', None)
    edition = book_data.get('edition', None)
    pub_year = book_data.get('pub_year', None)
    language = book_data.get('language', None)
    roles = book_data.get('roles', [])
    type = book_data.get('type', None)
    role_codes = []
    for item in roles:
        role = item.get('role')
        person = item.get('person')
        person_id = person.get('id')
        if role is None or person_id is None:
            logger.error(LogMsg.DATA_MISSING, {
                'role': role,
                'person_id': person_id
            })
            raise Http_error(400, Message.MISSING_REQUIERED_FIELD)

        role_code = '{}{}'.format(role, person_id)
        role_codes.append(role_code)
    role_codes.sort()
    roles_string = ''.join(role_codes)

    try:
        unique_code = ConstraintHandler()
        populate_basic_data(unique_code, 'INTERNAL', None)

        the_code = 'Book-{}-{}-{}-{}-{}-{}'.format(str(title), str(pub_year),
                                                   str(edition), str(language),
                                                   str(type), roles_string)
        unique_code.UniqueCode = the_code

        logger.debug(LogMsg.UNIQUE_CONSTRAINT_IS, the_code)

        db_session.add(unique_code)
    except:
        logger.exception(LogMsg.UNIQUE_CONSTRAINT_EXISTS, exc_info=True)
        raise Http_error(409, Message.ALREADY_EXISTS)

    logger.info(LogMsg.END)

    return unique_code
Beispiel #26
0
def add(user_id, group_id, db_session, username):
    logger.info(LogMsg.START, username)

    if user_is_in_group(user_id, group_id, db_session):
        logger.error(LogMsg.GROUP_USER_IS_IN_GROUP)
        raise Http_error(409, Message.ALREADY_EXISTS)

    model_instance = GroupUser()
    populate_basic_data(model_instance, username)
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    model_instance.group_id = group_id
    model_instance.user_id = user_id

    db_session.add(model_instance)

    logger.info(LogMsg.END)
    return model_instance
Beispiel #27
0
def add(data, db_session, username):
    logger.debug(LogMsg.START, username)
    schema_validate(data, ADD_SCHEMA_PATH)

    validate_permissions_and_access(username, db_session, 'ACCOUNT_ADD',
                                    access_level=Access_level.Premium)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    check_enum(data.get('type'), AccountTypes)
    logger.debug(LogMsg.ENUM_CHECK,
                 {'enum': data.get('type'), 'reference_enum': 'AccountTypes'})

    logger.debug(LogMsg.USER_CHECKING, username)

    type = data.get('type', 'Main')
    value = data.get('value', 0.0)
    person_id = data.get('person_id')
    person = validate_person(person_id, db_session)
    if person is None:
        logger.error(LogMsg.NOT_FOUND, {'person_id': person_id})
        raise Http_error(404, Message.Invalid_persons)

    logger.info(LogMsg.GETTING_USER_ACCOUNTS, type)

    account = get(person_id, type, db_session)
    if account is not None:
        logger.error(LogMsg.ACCOUNT_BY_TYPE_EXISTS, type)

        raise Http_error(409, Message.ALREADY_EXISTS)

    model_instance = Account()

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

    model_instance.type = type
    model_instance.person_id = person_id
    model_instance.value = value

    db_session.add(model_instance)

    logger.debug(LogMsg.DB_ADD, account_to_dict(model_instance))

    logger.info(LogMsg.END)

    return account_to_dict(model_instance)
def add(db_session, data, username):
    logger.info(LogMsg.START, username)
    schema_validate(data, CONTENT_ADD_SCHEMA_PATH)

    logger.debug(LogMsg.SCHEMA_CHECKED)

    book_id = data.get('book_id')
    type = data.get('type')

    logger.debug(LogMsg.ENUM_CHECK, {'BOOK_CONTENT_TYpe': type})
    check_enum(type, BookContentType)

    book = get_book(book_id, db_session)
    if not book:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    logger.debug(LogMsg.CHECK_UNIQUE_EXISTANCE, data)
    content = get_be_data(book_id, type, db_session)
    if content is not None:
        logger.error(LogMsg.CONTENT_EXIST, data)
        raise Http_error(409, Message.ALREADY_EXISTS)

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

    model_instance = BookContent()

    logger.debug(LogMsg.POPULATING_BASIC_DATA)

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

    model_instance.content = data.get('content')
    model_instance.book_id = book_id
    model_instance.type = type
    model_instance.book_press = book.press

    db_session.add(model_instance)

    logger.info(LogMsg.END)

    return model_instance
Beispiel #29
0
def permissions_to_db(db_session, username):
    permissions = Permissions.__members__
    print(permissions)
    result = []
    for permission in permissions:
        if check_permission_exists(permission, db_session):
            pass
        else:
            model_instance = Permission()
            populate_basic_data(model_instance, username)
            logger.debug(LogMsg.POPULATING_BASIC_DATA)
            model_instance.permission = permission
            db_session.add(model_instance)
            result.append(model_instance)

    logger.info(LogMsg.END)
    return result
Beispiel #30
0
def premium_permission_group(group_id, db_session, username):
    permissions = permission_list(db_session, 'PREMIUM')
    result = []
    for permission_id in permissions:
        if group_has_permission(permission_id, group_id, db_session):
            logger.debug(LogMsg.PERMISSION_GROUP_ALREADY_HAS)
            pass
        else:
            model_instance = GroupPermission()
            populate_basic_data(model_instance, username)
            logger.debug(LogMsg.POPULATING_BASIC_DATA)
            model_instance.group_id = group_id
            model_instance.permission_id = permission_id
            db_session.add(model_instance)
            result.append(group_permission_to_dict(model_instance))
    logger.info(LogMsg.END)
    return result