def put(self, user_id=None): user = User.find_by_id(user_id) if not user: return max_res('', 500, 'Something went wrong.') args = user_update_parser.parse_args() if args['name']: user.name = args['name'] if args['username']: user.username = args['username'] if args['email']: user.email = args['email'] if args['role_id'] is not None: user.role_id = args['role_id'] if args['mobile']: user.mobile = args['mobile'] if args['avatar']: user.avatar = args['avatar'] if args['password']: user.set_password(args['password']) try: user.update() except Exception as e: return max_res('', 500, 'Failed to modify.') return max_res(marshal(user, user_fields))
def delete(self, user_id=None): user = User.find_by_id(user_id) try: user.delete() except Exception as e: return max_res('', 500, 'The user has already deleted.') return max_res('The {} has been deleted.'.format(user.name))
def delete(self, keyword_id=None): keyword = Keyword.find_by_id(keyword_id) try: keyword.delete() except Exception as e: return max_res('', 500, 'The record has already deleted.') return max_res('The keyword has been deleted.')
def delete(self, paper_id=None): paper = Paper.find_by_id(paper_id) try: paper.delete() except Exception as e: return max_res('', 500, 'The record has already deleted.') return max_res('The paper has been deleted.')
def delete(self, logitem_id=None): logitem = Logitem.find_by_id(logitem_id) try: logitem.delete() except Exception as e: return max_res('', 500, 'The record has already deleted.') return max_res('The record has been deleted.')
def post(self): args = paper_post_parser.parse_args() paper = Paper(**args) try: paper.add() except IntegrityError: return max_res('', code=401, errmsg='名称重复') return max_res(marshal(paper, paper_fields))
def post(self): args = logitem_post_parser.parse_args() logitem = Logitem(**args) try: logitem.add() except IntegrityError: return max_res('', code=401, errmsg='名称重复') return max_res(marshal(logitem, logitem_fields))
def post(self): args = keyword_post_parser.parse_args() keyword = Keyword(**args) try: keyword.add() except IntegrityError: return max_res('', code=401, errmsg='名称重复') return max_res(marshal(keyword, keyword_fields))
def post(self): args = login_parser.parse_args() username = args['username'] password = args['password'] user = User.find_by_username(username=username) if not user or not user.check_password(password): return max_res('',401,'Invalid username or password') # Identity can be any data that is json serializable expires = datetime.timedelta(days=10) access_token = create_access_token(user.id, expires_delta=expires) return max_res({'token':access_token}), 200
def get(self): current_userid = get_jwt_identity() print(current_userid) user = User.find_by_id(current_userid) if not user: return max_res('', 500, 'Invalid User'), 500 res = { "id": user.id, "account": user.username, "name": user.name, "avatar": "" } return max_res(res)
def post(self): args = user_post_parser.parse_args() password = args['password'] user = User(name=args['name'], username=args['username']) user.set_password(password) try: user.add() except IntegrityError: return max_res('', code=401, errmsg='用户已存在') return max_res(marshal(user, user_fields))
def get(self): current_userid = get_jwt_identity() print(current_userid) user = User.find_by_id(current_userid) if not user: return max_res('',500,'Invalid User'), 500 res = { "id": user.id, "email": user.email, "name": user.name, "avatar": user.avatar, "intro": user.intro, } return max_res(res)
def get(self, user_id=None): if user_id: user = User.find_by_id(user_id) return max_res(marshal(user, user_fields)) else: conditions = [] args = user_query_parser.parse_args() page = args['page'] per_page = args['pagesize'] if args['name'] is not None: conditions.append(User.name.like('%' + args['name'] + '%')) if args['username'] is not None: conditions.append( User.username.like('%' + args['username'] + '%')) if args['email'] is not None: conditions.append(User.email == args['email']) if args['mobile'] is not None: conditions.append(User.mobile == args['mobile']) if args['role_id'] is not None: conditions.append(User.role_id == args['role_id']) if args['orderby'] not in sortable_fields: return max_res('', code=500, errmsg='非法字段') sort = args['orderby'] if args['desc'] > 0: sort = args['orderby'] + ' desc' if conditions is []: pagination = User.query.order_by(text(sort)).paginate( page, per_page, error_out=False) else: pagination = User.query.filter(*conditions).order_by( text(sort)).paginate(page, per_page, error_out=False) paginate = { 'total': pagination.total, 'pageSize': pagination.per_page, 'current': pagination.page } return max_res( marshal( { 'pagination': paginate, 'list': [marshal(u, user_fields) for u in pagination.items] }, user_list_fields))
def post(self): args = login_parser.parse_args() email = args['email'] password = args['password'] user = User.find_by_email(email=email) if not user or not user.check_password(password): return max_res('', 401, 'Invalid email or password') user_j = {'name': user.name, 'email': user.email, 'id': user.id} # Identity can be any data that is json serializable expires = datetime.timedelta(days=10) access_token = create_access_token(user.id, expires_delta=expires) return max_res({'token': access_token, 'user': user_j}), 200
def put(self, logitem_id=None): logitem = Logitem.find_by_id(logitem_id) args = logitem_update_parser.parse_args() logitem = update_all_fields(args, logitem) #可以在这里继续添加 需要更新的字段 如 # if args['name']: # o.name = args['name'] # db.session.commit() try: logitem.update() except Exception as e: return max_res('', 500, 'Failed to modify.') return max_res(marshal(logitem, logitem_fields))
def get(self, course_id=None): res = Course.query.all() res_list = [] if (res != None): for item in res: res_list.append({'id': item.id, 'name': item.name}) return max_res(res_list)
def get(self, article_id=None): article = Article.find_by_id(article_id) res = { 'id': article.id, 'title': article.title, 'body': article.body, 'front_pic': article.front_pic } return max_res(res)
def put(self, paper_id=None): paper = Paper.find_by_id(paper_id) args = paper_update_parser.parse_args() paper = update_all_fields(args, paper) #可以在这里继续添加 需要更新的字段 如 # if args['name']: # o.name = args['name'] # db.session.commit() try: paper.update() except Exception as e: return max_res('', 500, 'Failed to modify.') return max_res(marshal(paper, paper_fields))
def put(self, keyword_id=None): keyword = Keyword.find_by_id(keyword_id) args = keyword_update_parser.parse_args() keyword = update_all_fields(args, keyword) #可以在这里继续添加 需要更新的字段 如 # if args['name']: # o.name = args['name'] # db.session.commit() try: keyword.update() except Exception as e: return max_res('', 500, 'Failed to modify.') return max_res(marshal(keyword, keyword_fields))
def get(self, good_id=None): conditions = [] conditions.append(Goodsize.good_id == good_id) res = Goodsize.query.filter(*conditions).all() temp = [] for item in res: temp.append(item.json()) return max_res(temp)
def get(self, keyword_id=None): if keyword_id: keyword = Keyword.find_by_id(keyword_id) return max_res(marshal(keyword, keyword_fields)) else: conditions = [] args = keyword_query_parser.parse_args() page = args['page'] per_page = args['pagesize'] if args['orderby'] not in sortable_fields: return max_res('', code=500, errmsg='排序非法字段') sort = args['orderby'] if args['desc'] > 0: sort = args['orderby'] + ' desc' conditions = make_conditions(conditions, args) # 在这里添加更多的 条件查询 例如 # if args['name'] is not None: # conditions.append(Keyword.name.like('%'+args['name']+'%')) if conditions is []: pagination = Keyword.query.order_by(text(sort)).paginate( page, per_page, error_out=False) else: pagination = Keyword.query.filter(*conditions).order_by( text(sort)).paginate(page, per_page, error_out=False) paginate = { 'total': pagination.total, 'pageSize': pagination.per_page, 'current': pagination.page } print(pagination.items) return max_res( marshal( { 'pagination': paginate, 'list': [marshal(u, keyword_fields) for u in pagination.items] }, keyword_list_fields))
def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return max_res('',404, 'No file part') file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return max_res('',404, 'No selected file') if file and allowed_file(file.filename): today = datetime.utcnow().strftime("%Y%m%d") new_filename = datetime.utcnow().strftime("%H%M%S%f") + '.' + file.filename.rsplit('.', 1)[1].lower() upload_dir = app.config['UPLOAD_FOLDER']+ '/' + today mkdir(upload_dir) filename = secure_filename(file.filename) file.save(os.path.join(upload_dir, new_filename)) return max_res('/uploads/'+ today + '/' +new_filename) return '''
def put(self, user_id=None): user = User.find_by_id(user_id) user_update_parser.add_argument('password', type=str) args = user_update_parser.parse_args() user = update_all_fields(args, user) # 可以在这里继续添加 需要更新的字段 如 # if args['name']: # o.name = args['name'] # if args['password']: user.set_password(args['password']) db.session.commit() try: user.update() except Exception as e: return max_res('', 500, 'Failed to modify.') return max_res(marshal(user, user_fields))
def post(self): user_post_parser.add_argument( 'password', type=str, ) args = user_post_parser.parse_args() password = args['password'] user = User(name=args['name'], email=args['email'], intro=args['intro'], avatar=args['avatar']) if (user.avatar == ''): user.avatar = 'https://avatars.dicebear.com/v2/female/janefaddsdfkk.svg' if (user.intro == ''): user.intro = 'Hi all.' user.set_password(password) try: user.add() except IntegrityError: return max_res('', code=500, errmsg='Record existed.') return max_res(marshal(user, user_fields))
def get(self, catalog_id=None): conditions = [] conditions.append(Article.catalog_id == catalog_id) res = Article.query.filter(*conditions).all() temp = [] for item in res: temp.append({ 'id': item.id, 'title': item.title, 'author': item.author }) return max_res(temp)
def get(self, good_id=None): conditions = [] conditions.append(Goodsize.good_id == good_id) good = Goodsize.query.filter(*conditions).first() return max_res(good.json())