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, }
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, }
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, }
def rm_book_type(book_type_id, token=None): if token is None or not is_admin(token): return abort(403) book_type = Type.objects(id=book_type_id) try: book_type.delete() except OperationError: return {"message": "please dereference before delete the type."} else: return {'success': 1}
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}
def rm_book_type(book_type_id, token=None): if token is None or not is_admin(token): return abort(403) book_type = Type.objects(id=book_type_id) try: book_type.delete() except OperationError: return { "message": "please dereference before delete the type." } else: return {'success': 1}
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
def get_all_types(token=None): if token is None or not is_stuff(token): return abort(403) types = Type.objects() return types
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