Exemple #1
0
def update_order(gid, order_id):
    # confirm order_id is exit
    ObjectIdStructure(order_id)
    order_id = ObjectId(order_id)
    order = current_app.mongodb_conn.Order.find_one_by_id(order_id)
    if order is None:
        raise OrderNotFound('Update Order Error, Order Not Found')
    # confirm order belongs to current_group
    if not order['group'] == g.current_group['_id']:
        raise VerifyGroupError('order not in group')

    req = parse_request_body()
    desc = req.get_params('desc')
    payer, total_fee, items = req.get_required_params('payer', 'total_fee',
                                                      'items')
    # verify order params
    ObjectIdStructure(payer)
    payer_id = ObjectId(payer)
    PriceStructure(total_fee)
    MinorDescStructure(desc)
    # verify items params
    for item in items:
        if not 'user' and 'fee' and 'note' in item:
            raise ValidationParameterRequired
        ObjectIdStructure(item['user'])
        item['user'] = ObjectId(item['user'])
        PriceStructure(item['fee'])
        MinorDescStructure(item['note'])
    # end

    # delete old items
    for old_item_id in order['items']:
        old_item = current_app.mongodb_conn.Item.find_one_by_id(old_item_id)
        old_item.delete()

    # save new items
    item_id_list = []
    for item in items:
        new_item = current_app.mongodb_conn.Item()
        new_item['user'] = item['user']
        new_item['fee'] = item['fee']
        new_item['note'] = item['note']
        new_item.save()
        item_id_list.append(new_item['_id'])

    # save order
    order['payer'] = payer_id
    order['total_fee'] = total_fee
    order['desc'] = desc
    order['items'] = item_id_list
    order.save()
    return {'id': order['_id']}
Exemple #2
0
def add_order(gid):
    req = parse_request_body()
    payer, total_fee, desc, items = req.get_required_params(
        'payer', 'total_fee', 'desc', 'items')

    # verify order params
    ObjectIdStructure(payer)
    payer_id = ObjectId(payer)
    PriceStructure(total_fee)
    MinorDescStructure(desc)

    # verify items params
    for item in items:
        if not 'user' and 'fee' and 'note' in item:
            raise ValidationParameterRequired
        ObjectIdStructure(item['user'])
        item['user'] = ObjectId(item['user'])
        PriceStructure(item['fee'])
        MinorDescStructure(item['note'])
    # end

    # save order
    group_id = g.current_group['_id']
    createAt = generate_current_second()
    order = current_app.mongodb_conn.Order()
    order['group'] = group_id
    order['createAt'] = createAt
    order['payer'] = payer_id
    order['total_fee'] = total_fee
    order['desc'] = desc
    order.save()

    # save items
    for item in items:
        new_item = current_app.mongodb_conn.Item()
        new_item['order'] = order['_id']
        new_item['user'] = item['user']
        new_item['fee'] = item['fee']
        new_item['note'] = item['note']
        new_item.save()

    return {'id': order['_id']}
Exemple #3
0
def get_request_group():
    gid = request.view_args.get('gid')
    if gid is None:
        raise BadRequest('GroupID is Required')

    ObjectIdStructure(gid)
    group_id = ObjectId(gid)

    group = current_app.mongodb_conn.Group.find_one_by_id(group_id)
    if group is None:
        raise NotFound('GroupID not Exist')
    return group
Exemple #4
0
def delete_order(gid, order_id):
    # verify params
    ObjectIdStructure(order_id)
    order_id = ObjectId(order_id)
    # end

    order = current_app.mongodb_conn.Order.find_one_by_id(order_id)
    if order is None:
        raise OrderNotFound('Delete Group Order Error')
    items = current_app.mongodb_conn.Item.find_all_by_id_list(order['items'])
    for item in items:
        item.delete()

    order.delete()
    return {'id': order['_id']}
Exemple #5
0
def verify_token():
    token = request.headers.get('Authorization', None)
    if token is None:
        raise AuthenticationFailed(
            'Authorization Required, Authorization header was missing')
    uid = extract_token(token)

    ObjectIdStructure(uid)
    user_id = ObjectId(uid)

    current_user = current_app.mongodb_conn.User.find_one({'_id': user_id})
    if current_user is None:
        raise AuthenticationFailed("User Not Exist")
    g.current_user = current_user
    return
Exemple #6
0
def transfer_owner_permission(gid):
    req = parse_request_body()
    uid = req.get_required_params("uid")

    # verify params
    ObjectIdStructure(uid, 'uid')
    user_id = ObjectId(uid)
    # end

    user = current_app.mongodb_conn.User.find_one_by_id(user_id)
    if user is None:
        raise NotFound('transfer_owner_permission error: User Not Found')
    group = g.current_group
    if not user['_id'] in group['member']:
        raise MemberNotFound(
            'transfer owner permission error: Member Not Found In Group')
    group['owner'] = user['_id']
    group.save()
    return {
        'owner': group['owner'],
    }
Exemple #7
0
def delete_group_member(gid, uid):
    # verify params
    ObjectIdStructure(uid, 'uid')
    user_id = ObjectId(uid)
    # end

    # owner can not delete self
    if user_id == g.current_user['_id']:
        raise CanNotDeleteSelf(
            'owner can not delete self before transform RoleOfOwner')
    group_id = g.current_group['_id']
    user = current_app.mongodb_conn.User.find_one_by_id(user_id)
    if user is None:
        raise NotFound('Add group member error: User not found')
    members = g.current_group_members
    if not user['_id'] in members:
        raise MemberNotFound(
            'delete group member error: Member Not Found In Group')
    relation = current_app.mongodb_conn.Relation.find_one_by_group_and_user(
        group_id, user_id)
    relation.delete()
    return {'id': user['_id']}