Пример #1
0
def get_vips(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)

    if 'name' in args:
        return Vip.objects(Q(name=args['name']))
    return None
Пример #2
0
def create_sales_record(count, seller_id, book_id, purchaser_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    if book is None:
        return {'message': 'Missing parameter book, or book id is wrong.'}
    if book.remaining < count:
        return {'message': 'This book is not enough'}
    price = book.price
    seller = Account.objects(id=seller_id).first()
    if seller is None:
        return {'message': 'Missing parameter seller, or seller id is wrong.'}
    try:
        purchaser = Vip.objects(id=purchaser_id).first()
    except ValidationError:
        purchaser = None
    if purchaser is not None:
        price *= 0.8
    book.remaining -= count
    book.sales += count
    book.save()
    sales_record = SalesRecord(
        count=count,
        price=price,
        book=book,
        seller=seller,
        purchaser=purchaser,
    ).save()
    return {'id': sales_record.id, 'success': 1}
Пример #3
0
def get_vips(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)

    if 'name' in args:
        return Vip.objects(Q(name=args['name']))
    return None
Пример #4
0
def create_sales_record(count, seller_id, book_id, purchaser_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    if book is None:
        return {'message': 'Missing parameter book, or book id is wrong.'}
    if book.remaining < count:
        return {'message': 'This book is not enough'}
    price = book.price
    seller = Account.objects(id=seller_id).first()
    if seller is None:
        return {'message': 'Missing parameter seller, or seller id is wrong.'}
    try:
        purchaser = Vip.objects(id=purchaser_id).first()
    except ValidationError:
        purchaser = None
    if purchaser is not None:
        price *= 0.8
    book.remaining -= count
    book.sales += count
    book.save()
    sales_record = SalesRecord(
        count=count,
        price=price,
        book=book,
        seller=seller,
        purchaser=purchaser,
    ).save()
    return {
        'id': sales_record.id,
        'success': 1
    }
Пример #5
0
def get_all_accounts(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if is_admin(token):
        condition = Q(username__ne='root')
    else:
        condition = Q(username__ne='root') & Q(role='stuff')
    accounts = Account.objects(condition)
    return accounts
Пример #6
0
def get_all_accounts(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if is_admin(token):
        condition = Q(username__ne='root')
    else:
        condition = Q(username__ne='root') & Q(role='stuff')
    accounts = Account.objects(condition)
    return accounts
Пример #7
0
def get_account_by_id(account_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    condition = Q(id=account_id)
    if is_admin(token) and not is_root(token):
        condition &= Q(username__ne='root')
    elif not is_root(token):
        condition = Q(username__ne='root') & Q(role='stuff')
    account = Account.objects(condition).first()
    return account
Пример #8
0
def get_account_by_id(account_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    condition = Q(id=account_id)
    if is_admin(token) and not is_root(token):
        condition &= Q(username__ne='root')
    elif not is_root(token):
        condition = Q(username__ne='root') & Q(role='stuff')
    account = Account.objects(condition).first()
    return account
Пример #9
0
def create_book_type(name, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if Type.objects(name=name).first() is not None:
        return {'message': 'This type has been exist'}
    book_type = Type(name=name).save()
    return {
        'id': book_type.id,
        'success': 1,
    }
Пример #10
0
def create_book_type(name, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if Type.objects(name=name).first() is not None:
        return {'message': 'This type has been exist'}
    book_type = Type(name=name).save()
    return {
        'id': book_type.id,
        'success': 1,
    }
Пример #11
0
def get_accounts(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if is_admin(token):
        condition = Q(username__ne='root')
    else:
        condition = Q(username__ne='root') & Q(role='stuff')
    if 'username' in args:
        condition &= Q(username=args['username'])
    if 'nickname' in args:
        condition &= Q(nickname=args['nickname'])
    accounts = Account.objects(condition)
    return accounts
Пример #12
0
def get_accounts(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if is_admin(token):
        condition = Q(username__ne='root')
    else:
        condition = Q(username__ne='root') & Q(role='stuff')
    if 'username' in args:
        condition &= Q(username=args['username'])
    if 'nickname' in args:
        condition &= Q(nickname=args['nickname'])
    accounts = Account.objects(condition)
    return accounts
Пример #13
0
def get_book_types(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    condition = None
    if 'id' in args:
        condition = Q(id=args['id'])
    if 'name' in args:
        if condition is None:
            condition = Q(name=args['name'])
        else:
            condition &= Q(name=args['name'])
    types = Type.objects(condition)
    return types
Пример #14
0
def get_book_types(args, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    condition = None
    if 'id' in args:
        condition = Q(id=args['id'])
    if 'name' in args:
        if condition is None:
            condition = Q(name=args['name'])
        else:
            condition &= Q(name=args['name'])
    types = Type.objects(condition)
    return types
Пример #15
0
def create_vip(username, nickname, phone, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if phone is None:
        phone = ""
    if Vip.objects(username=username).first() is not None:
        return {'message': 'The vip\'s name has been used'}
    vip = Vip(
        username=username,
        nickname=nickname,
        phone=phone,
    ).save()
    return {
        'id': vip.id,
        'success': 1,
    }
Пример #16
0
def create_vip(username, nickname, phone, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    if phone is None:
        phone = ""
    if Vip.objects(username=username).first() is not None:
        return {'message': 'The vip\'s name has been used'}
    vip = Vip(
        username=username,
        nickname=nickname,
        phone=phone,
    ).save()
    return {
        'id': vip.id,
        'success': 1,
    }
Пример #17
0
def get_all_vips(token=None):
    if token is None or not is_stuff(token):
        return abort(403)

    vips = Vip.objects()
    return vips
Пример #18
0
def get_sales_records_by_vip(vip_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    vip = Vip.objects(id=vip_id).first()
    sales_records = SalesRecord.objects(purchaser=vip)
    return sales_records
Пример #19
0
def get_book_by_id(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    return book
Пример #20
0
def get_all_books(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    books = Book.objects()
    return books
Пример #21
0
def get_book_by_id(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    return book
Пример #22
0
def get_sales_records_by_account(account_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    account = Account.objects(id=account_id).first()
    sales_records = SalesRecord.objects(seller=account)
    return sales_records
Пример #23
0
def rm_vip(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    vip = Vip.objects(id=book_id)
    vip.delete()
    return {'success': 1}
Пример #24
0
def get_books_by_type(type_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    the_type = Type.objects(id=type_id).first()
    books = Book.objects(type__in=[the_type])
    return books
Пример #25
0
def rm_vip(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    vip = Vip.objects(id=book_id)
    vip.delete()
    return {'success': 1}
Пример #26
0
def get_all_books(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    books = Book.objects()
    return books
Пример #27
0
def get_books_by_type(type_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    the_type = Type.objects(id=type_id).first()
    books = Book.objects(type__in=[the_type])
    return books
Пример #28
0
def get_all_vips(token=None):
    if token is None or not is_stuff(token):
        return abort(403)

    vips = Vip.objects()
    return vips
Пример #29
0
def get_sales_records_by_book(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    sales_records = SalesRecord.objects(book=book)
    return sales_records
Пример #30
0
def get_sales_records_by_account(account_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    account = Account.objects(id=account_id).first()
    sales_records = SalesRecord.objects(seller=account)
    return sales_records
Пример #31
0
def get_all_types(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    types = Type.objects()
    return types
Пример #32
0
def get_sales_records_by_book(book_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    sales_records = SalesRecord.objects(book=book)
    return sales_records
Пример #33
0
def get_sales_records_by_vip(vip_id, token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    vip = Vip.objects(id=vip_id).first()
    sales_records = SalesRecord.objects(purchaser=vip)
    return sales_records
Пример #34
0
def get_all_types(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    types = Type.objects()
    return types