Exemplo n.º 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}
Exemplo n.º 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
    }
Exemplo n.º 3
0
def update_book(book_id, delta, price, des, type_id_list, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    if book is None:
        return {'message': 'This book does not exist.'}
    remaining = book.remaining
    if des is None:
        des = ""
    book.update(
        price=price,
        remaining=remaining+delta,
        description=des,
    )
    del book.type[:]
    if type_id_list is not None:
        for i in type_id_list:
            try:
                term = Type.objects(id=i).first()
            except ValidationError:
                continue
            if term is None:
                continue
            book.type.append(term)
    book.save()
    return {
        'success': 1,
        'id': book_id,
    }
Exemplo n.º 4
0
def update_book(book_id, delta, price, des, type_id_list, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    book = Book.objects(id=book_id).first()
    if book is None:
        return {'message': 'This book does not exist.'}
    remaining = book.remaining
    if des is None:
        des = ""
    book.update(
        price=price,
        remaining=remaining + delta,
        description=des,
    )
    del book.type[:]
    if type_id_list is not None:
        for i in type_id_list:
            try:
                term = Type.objects(id=i).first()
            except ValidationError:
                continue
            if term is None:
                continue
            book.type.append(term)
    book.save()
    return {
        'success': 1,
        'id': book_id,
    }
Exemplo n.º 5
0
def rm_ref_book2type(type_id, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    the_type = Type.objects(id=type_id).first()
    all_books = Book.objects()
    for book in all_books:
        if the_type in book.type:
            book.type.remove(the_type)
        book.save()
    return {'success': 1}
Exemplo n.º 6
0
def rm_ref_book2type(type_id, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    the_type = Type.objects(id=type_id).first()
    all_books = Book.objects()
    for book in all_books:
        if the_type in book.type:
            book.type.remove(the_type)
        book.save()
    return {'success': 1}
Exemplo n.º 7
0
def get_books(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'])
    books = Book.objects(condition)
    return books
Exemplo n.º 8
0
def get_books(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'])
    books = Book.objects(condition)
    return books
Exemplo n.º 9
0
def create_book(name, price, count, description, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    if Book.objects(name=name).first() is not None:
        return {'message': 'this book has been existed'}
    book = Book(
        name=name,
        price=price,
        remaining=count,
        description=description,
    )
    book = book.save()
    return {'success': 1, 'id': book.id}
Exemplo n.º 10
0
def create_book(name, price, count, description, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    if Book.objects(name=name).first() is not None:
        return {
            'message': 'this book has been existed'
        }
    book = Book(
        name=name,
        price=price,
        remaining=count,
        description=description,
    )
    book = book.save()
    return {
        'success': 1,
        'id': book.id
    }
Exemplo n.º 11
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
Exemplo n.º 12
0
def get_all_books(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    books = Book.objects()
    return books
Exemplo n.º 13
0
def rm_book(book_id, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    book = Book.objects(id=book_id)
    book.delete()
    return {'success': 1}
Exemplo n.º 14
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
Exemplo n.º 15
0
def get_all_books(token=None):
    if token is None or not is_stuff(token):
        return abort(403)
    books = Book.objects()
    return books
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
0
def rm_book(book_id, token=None):
    if token is None or not is_admin(token):
        return abort(403)
    book = Book.objects(id=book_id)
    book.delete()
    return {'success': 1}
Exemplo n.º 20
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