Esempio n. 1
0
def pre_insert_bills(bills):
    '''
    Before insert bill:
        1. Make sure now user is at least writer of the bill book of this bill.
        2. Set now user as the creater of this bill.
        3. Check related categorys, create if not existing.
    '''
    user = get_data('user', 409)

    for num, bill in enumerate(bills):
        relation = operator.get('billbook_user_relation', {
            'user': user['_id'],
            'billbook': bill['billbook']
        })

        if not relation:
            billbook = operator.get('billbooks', {'_id': bill['billbook']})
            if billbook['status'] > 0:
                abort(400)
        elif relation['status'] > 2:
            abort(400)

        bills[num]['creater'] = user['_id']
        bills[num]['creater_name'] = user['nickname']

        if get_transfer_billbook(user['_id']) != bill['billbook']:
            _check_bill_cats(bill)
Esempio n. 2
0
def pre_insert_bill_categorys(cats):
    '''
    Before insert category:
        1. If the same category exists, stop
    '''
    for num, cat in enumerate(cats):
        billbook = cat.get('billbook')
        text = cat.get('text')
        if operator.get('bill_categorys', {'billbook': billbook, 'text': text}):
            abort(400)
Esempio n. 3
0
def pre_update_bill_categorys(updates, cat):
    '''
    Before update category:
        1. Remove immutable fields 'billbook'
        2. If text is going to be change, check whether the same
           category exsits. If not, change the all related bills
           and categorys.
    '''
    del_immutable_field(updates, ['billbook'])

    billbook = cat.get('billbook')
    text = updates.get('text', None)
    if text:
        if operator.get('bill_categorys', {'text': text, 'billbook': billbook}):
            abort(400)
Esempio n. 4
0
def pre_insert_relation(relations):
    '''
    Before insert relation:
        1. Only owners or managers can add new user.
        2. Managers can only add writers or readers
    '''
    user = get_data('user', 409)
    for num, relation in enumerate(relations):
        user_relation = operator.get('billbook_user_relation', {
            'user': user['_id'],
            'billbook': relation['billbook']
        })
        if not user_relation or user_relation['status'] > 1:
            abort(409)
        if relation['status'] <= 1 and user_relation['status'] == 1:
            abort(409)
Esempio n. 5
0
def check_billbook_lookup(billbook_lookup, user, relation=None):
    if relation is None:
        relation = get_user_billbook_relation(user)

    if isinstance(billbook_lookup, dict):
        billbooks = billbook_lookup.get('$in', [])
        remove_books = []
        for billbook in billbooks:
            if not check_billbook_readable(billbook, user, relation):
                remove_books.append(billbook)

        billbooks = [
            billbook for billbook in billbooks if billbook not in remove_books
        ]
        billbook_lookup = {'$in': billbooks}
    elif not check_billbook_readable(billbook_lookup, user, relation):
        abort(409)
    return billbook_lookup
Esempio n. 6
0
def pre_delete_billbooks(billbook):
    if billbook.get('default', False):
        abort(400)