def current_profile(): rst = User.query.get(current_user.id) if not rst: api_abort(404) return jsonify( { 'data': rst.to_dict(schema=user_schema) } )
def new_genius(form: new_genius_form): book = Book.query.filter(Book.isbn == form.isbn).first() if not book: book_info_required = ( 'name', 'author', 'douban_id', 'pub_date', 'publisher', 'img_src' ) if g.data_dict and 'book' in g.data_dict and all([x in g.data_dict['book'] for x in book_info_required]): book_info = g.data_dict['book'] book = Book(name=book_info['name'], other_name=book_info['other_name'], authors=book_info['author'], douban_id=book_info['douban_id'], pub_date=book_info['pub_date'], publisher=book_info['publisher'], isbn=form.isbn) book.save() # sending retrieve tasks to message queue BookThumbnail.retrieve_from_url(book_info['img_src'], book=book) else: return api_abort(400, 'Book Information required.') genius = BookOfUser.create_with_uuid() genius.user = current_user genius.book_id = book.id genius.description = form.description genius.deprecation = form.deprecation genius.address_id = form.addressId photos = UserBookImage.query.filter(or_(*[photo_id == UserBookImage.id for photo_id in form.photoIds])).all() if len(photos) == len(form.photoIds): genius.imgs = photos db.session.add(genius) db.session.commit() return jsonify(genius.to_dict(schema=bookofuser_schema)) else: db.session.rollback() print(photos, form.photoIds) return api_abort(400, 'Photos not match.') return api_abort(404, message='Book not exists.')
def wrapper(*args, **kws): filled_dict = {} data_dict = json.loads(request.data) for k, marshal in form_schema.items(): if data_dict.get(k, None): val = data_dict[k] val = marshal(val) if callable( marshal) else val if marshal is no_marshal else marshal filled_dict.setdefault(k, val) else: return api_abort( 400, message=f'{k.capitalize()} field required.') g.data_dict = data_dict return f(*args, form=form_tuple(**filled_dict), **kws)
def update_avator(): avator = request.files['avator'] if avator: user_avator = UserAvator.create_from_file(avator) current_user.avators = [user_avator] current_user.save() return jsonify( { 'data': { 'avator_url': user_avator.url } } ) return api_abort(400, 'Avator file required')
def authenticate(): if not user_logined(): return api_abort(403, 'Login required.')