Beispiel #1
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}
Beispiel #2
0
def get_all_sales_records(token=None):
    if token is None or not is_admin(token):
        return abort(403)
    sales_records = SalesRecord.objects()
    return sales_records
Beispiel #3
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
Beispiel #4
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
Beispiel #5
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
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
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
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
def get_all_sales_records(token=None):
    if token is None or not is_admin(token):
        return abort(403)
    sales_records = SalesRecord.objects()
    return  sales_records