def index_all(): id = get_jwt_identity() favorites = Favorite.select().join( User, on=Favorite.user_id).switch(Favorite).where(Favorite.user_id == id) excluded_chefs = [c.chef_id for c in favorites] recipes = Recipe.select().where((Recipe.user_id != id) & Recipe.user_id.not_in(excluded_chefs)) return jsonify({ "status": "success", "recipe": [{ "id": recipe.id, "id_owner": recipe.user.id, "id_user": str(id), "name": recipe.name, "photo": recipe.photo, "countrys": recipe.countrys, "hour": recipe.hour, "sec": recipe.sec, "directions": recipe.directions, "ingredients": recipe.ingredients, "time": recipe.time, "username": recipe.user.username, "user_id": recipe.user.id, "user_photo": recipe.user.photo } for recipe in recipes] }), 200
def index_number(user_id_param): favorites = Favorite.select().join( User, on=Favorite.user_id).switch(Favorite).where( Favorite.user_id == user_id_param) following = len([c.chef_id for c in favorites]) favorites = Favorite.select().join( User, on=Favorite.user_id).switch(Favorite).where( Favorite.chef_id == user_id_param) followers = len([c.user_id for c in favorites]) my_recipes = Recipe.select().where(Recipe.user_id == user_id_param) recipes = len([c.id for c in my_recipes]) return jsonify({ "status": "success", "number": { "following": following, "followers": followers, "recipes": recipes, } }), 200
def show_search(): id = get_jwt_identity() favorites = Favorite.select().join( User, on=Favorite.user_id).switch(Favorite).where(Favorite.user_id == id) excluded_chefs = [c.chef_id for c in favorites] hour = request.args.get('hour') sec = request.args.get('sec') country = request.args.get('country') order = request.args.get('order') if order == "Time to prepare": ooo = Recipe.prep elif order == "": ooo = Recipe elif order == "Recently": ooo = Recipe.created_at.desc() prepTime = None if sec and hour: prepTime = int(hour) + (int(sec) / 100) elif sec: prepTime = (int(sec) / 100) elif hour: prepTime = int(hour) recipes = False if country and prepTime: recipes = Recipe.select().where( (Recipe.countrys == country) & (Recipe.user_id != id) & (Recipe.prep <= prepTime) & Recipe.user_id.not_in(excluded_chefs)).order_by(ooo) elif country: recipes = Recipe.select().where( (Recipe.countrys == country) & (Recipe.user_id != id) & Recipe.user_id.not_in(excluded_chefs)).order_by(ooo) elif prepTime: recipes = Recipe.select().where( (Recipe.prep <= prepTime) & (Recipe.user_id != id) & Recipe.user_id.not_in(excluded_chefs)).order_by(ooo) else: recipes = Recipe.select().where( (Recipe.user_id != id) & Recipe.user_id.not_in(excluded_chefs)).order_by(ooo) if recipes: return jsonify({ "status": "success", "recipe": [{ "id": recipe.id, "name": recipe.name, "photo": recipe.photo, "countrys": recipe.countrys, "hour": recipe.hour, "sec": recipe.sec, "directions": recipe.directions, "ingredients": recipe.ingredients, "time": recipe.time, "username": recipe.user.username, "user_id": recipe.user.id, "id_owner": recipe.user.id, "user_photo": recipe.user.photo } for recipe in recipes] }), 200 else: return jsonify({"recipe": [], "status": "no recipe"}), 200
def user_profile(user, params, lang): origin_user = user uuid = params.get("uuid", "") if uuid: user = User.select().where(User.uuid == uuid).first() if not user: return {"error_code": 20002, "msg": "user not exist"} profile = user.profile.first() languages = [] ul = UserLanguage.select().where(UserLanguage.user == user) for u in ul: languages.append(dict( language_id=u.id, name=u.name, level=u.level, )) out = dict() # user self can get private information if not uuid: out["reg_step"] = user.reg_step out["id_number"] = "%s********%s" % (profile.id_number[:6], profile.id_number[14:]) if profile.id_number else "" out["alipay"] = profile.alipay out["email"] = user.email out["phone"] = user.phone out["name"] = profile.name out["username"] = user.username out["avatar"] = widget.avatar(profile.avatar) out["completeness"] = profile.completeness out["visibility"] = profile.visibility out["level"] = profile.level out["title"] = profile.title out["overview"] = profile.overview out["hourly"] = profile.hourly out["english"] = profile.english out["skills"] = utils.loads(profile.skills) if profile.skills else [] out["available"] = profile.available out["workload"] = profile.workload location = {} if profile.location_id: city = widget.get_location(profile.location_id) province = widget.get_location(city.parent_id) location["location_id"] = profile.location_id location["name"] = utils.lang_map_name(city.name, city.ename, lang) location["parent_id"] = province.id location["parent_name"] = utils.lang_map_name(province.name, province.ename, lang) out["location"] = location out["address"] = profile.address out["postcode"] = profile.postcode out['id'] = user.uuid out["languages"] = languages out["imid"] = user.id if uuid: favorites = Favorite.select().where(Favorite.user==origin_user, Favorite.target_id==profile.user_id, Favorite.ftype == 'REQ').first() if favorites: out["favorite"] = True else: out["favorite"] = False # 开发者的评价数量和平均得分 score = UserStatistics.select(UserStatistics.eveluate_num, UserStatistics.aver_score).where(UserStatistics.user == user).first() out["eveluate_num"] = score.eveluate_num if score else 0 out["aver_score"] = score.aver_score if score else 0 return {"error_code": 0, "msg": "ok", "profile": out}
def user_profile(user, params, lang): origin_user = user uuid = params.get("uuid", "") if uuid: user = User.select().where(User.uuid == uuid).first() if not user: return {"error_code": 20002, "msg": "user not exist"} profile = user.profile.first() languages = [] ul = UserLanguage.select().where(UserLanguage.user == user) for u in ul: languages.append(dict( language_id=u.id, name=u.name, level=u.level, )) out = dict() # user self can get private information if not uuid: out["reg_step"] = user.reg_step out["id_number"] = "%s********%s" % ( profile.id_number[:6], profile.id_number[14:]) if profile.id_number else "" out["alipay"] = profile.alipay out["email"] = user.email out["phone"] = user.phone out["name"] = profile.name out["username"] = user.username out["avatar"] = widget.avatar(profile.avatar) out["completeness"] = profile.completeness out["visibility"] = profile.visibility out["level"] = profile.level out["title"] = profile.title out["overview"] = profile.overview out["hourly"] = profile.hourly out["english"] = profile.english out["skills"] = utils.loads(profile.skills) if profile.skills else [] out["available"] = profile.available out["workload"] = profile.workload location = {} if profile.location_id: city = widget.get_location(profile.location_id) province = widget.get_location(city.parent_id) location["location_id"] = profile.location_id location["name"] = utils.lang_map_name(city.name, city.ename, lang) location["parent_id"] = province.id location["parent_name"] = utils.lang_map_name(province.name, province.ename, lang) out["location"] = location out["address"] = profile.address out["postcode"] = profile.postcode out['id'] = user.uuid out["languages"] = languages out["imid"] = user.id if uuid: favorites = Favorite.select().where( Favorite.user == origin_user, Favorite.target_id == profile.user_id, Favorite.ftype == 'REQ').first() if favorites: out["favorite"] = True else: out["favorite"] = False # 开发者的评价数量和平均得分 score = UserStatistics.select( UserStatistics.eveluate_num, UserStatistics.aver_score).where(UserStatistics.user == user).first() out["eveluate_num"] = score.eveluate_num if score else 0 out["aver_score"] = score.aver_score if score else 0 return {"error_code": 0, "msg": "ok", "profile": out}