def token_user_group_post(group): response = dict() status = 200 (response["msg"], status) = ("완료되었습니다", 200) trainee = None token = request.headers.get("x-gs-token", None) try: if token is None: raise RuntimeError("토큰이(가) 존재하지 않습니다.", 403) trainee = models.User.get_by_token(token) t = type(trainee) if t is models.NotFoundAccount: raise RuntimeError("토큰이(가) 유효하지 않습니다.", 403) if t is models.Trainer: raise RuntimeError("트레이니이(가) 아닙니다.", 403) group = models.Group.find(group) if group is None: raise RuntimeError("그룹이(가) 존재하지 않습니다.", 404) if group.start_date < datetime.now().date(): raise RuntimeError("그룹 참가이(가) 마감되었습니다.", 404) res = trainee.enter_group(group) if not res: (response["msg"], status) = ("그룹에 들어갈 수 없습니다.", 404) except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def groups_UID_trainings_get(uid): response = dict() try: content_type = request.headers.get("content-type", "") token = request.headers.get("x-gs-token") (response["msg"], status) = ("완료되었습니다", 200) user = None if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) else: user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) group = models.Group.find(uid) if group is None: raise RuntimeError("그룹이 유효하지 않습니다.", 403) response = models.Training.get_list(group) except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=200, mimetype="application/json") return r
def groups_UID_users_UUID_results_after_get(uid, u_uid): response = dict() try: content_type = request.headers.get("content-type", "") token = request.headers.get("x-gs-token") (response["msg"], status) = ("완료되었습니다", 200) user = None if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) else: user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) group = models.Group.find(uid) if group is None: raise RuntimeError("그룹이 유효하지 않습니다.", 403) trainee = models.User.find(u_uid) if type(trainee) is not models.Trainee: raise RuntimeError("유저가 잘못되었습니다.", 403) if not group in trainee.get_groups(): raise RuntimeError("해당 그룹에 유저가 가입하지 않았습니다.", 403) response = models.MeasurementInfoList.get_after(group, trainee) except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=200, mimetype="application/json") return r
def trainers_UID_images_delete(uid, name): response = dict() try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 400) user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) trainer = models.Trainer.find(uid) if type(trainer) is not models.Trainer: raise RuntimeError("해당 uid는 트레이너가 아닙니다.", 403) res = models.Image.get_list(trainer) if not name in {x.image_name for x in res}: raise RuntimeError("이미지 파일이 존재하지 않습니다.", 403) img = models.Image(name, trainer, None, None) img.delete() response["images"] = res except RuntimeError as e: return e.to_response() (response["msg"], status) = ("완료되었습니다", 200) r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def groups_UID_trainings_UDATE_post(uid, udate): response = dict() try: content_type = request.headers.get("content-type", "") token = request.headers.get("x-gs-token") (response["msg"], status) = ("완료되었습니다", 200) user = None if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) else: user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) group = models.Group.find(uid) if group is None: raise RuntimeError("그룹이 유효하지 않습니다.", 403) if group.opener.uid != user.uid: raise RuntimeError("그룹의 개설자가 아닙니다.", 403) form = request.data form = json.loads(form.decode("utf-8")) if len(form) > 6: raise RuntimeError("등록할 운동 갯수가 6개를 초과합니다.", 403) err_m = list() fields = {"name", "count", "set"} for idx, training in enumerate(form): for field in fields: if training.get(field) is None: err_m.append("{}번째 운동 기록에 {}이(가) 누락되었습니다.".format( idx, field)) def filter_training(training): fields = {"name", "count", "set"} for field in fields: if training.get(field) is None: return False return True #trainings = (models.Training(i, udate, group, form[i]["name"], form[i]["count"], form[i]["set"]) for i in range(len(form))) for t in map( lambda it: models.Training(it[0], udate, group, it[1]["name"], it[1]["count"], it[1]["set"]), enumerate(filter(filter_training, form))): t.insert() from flask import g connection = g.connection connection.commit() response["msg"] = "완료되었습니다." except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=200, mimetype="application/json") return r
def groups_UID_get(uid): response = dict() try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) if type(models.User.get_by_token(token)) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) group = models.Group.find(uid) if group is None: raise RuntimeError("그룹이 존재하지 않습니다.", 404) response = group except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=200, mimetype="application/json") return r
def trainers_group_get(uid): response = dict() (response["msg"], status) = ("완료되었습니다", 200) token = request.headers.get("x-gs-token", None) try: if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) r = models.User.get_by_token(token) if type(r) == models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) r = models.Trainer(uid, None, None) response = r.get_groups() except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def trainers_UID_reviews_get(uid: int): response = dict() response["msg"] = "완료되었습니다" status = 200 try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 400) user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) trainer = models.Trainer.find(uid) if type(trainer) is not models.Trainer: raise RuntimeError("해당 uid는 트레이너가 아닙니다.", 403) res = models.Review.get_list(trainer) response["reviews"] = res except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def trainers_UID_images_post(uid): response = dict() try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 400) user = models.User.get_by_token(token) if type(user) is not models.Trainer: raise RuntimeError("토큰이 유효하지 않습니다.", 403) trainer = models.Trainer.find(uid) if type(trainer) is not models.Trainer: raise RuntimeError("해당 uid는 트레이너가 아닙니다.", 403) if user.uid != trainer.uid: raise RuntimeError("업로드 권한이 없습니다.", 403) img = request.data content_type = request.headers.get("content-type") if img is None: raise RuntimeError("이미지가 없습니다.", 403) import base64 import hashlib temp = content_type.split(";") mime = temp[0].split("/")[1].strip() b = temp[1].strip() hash512 = hashlib.sha512() img = base64.decodebytes(img) hash512.update(img) image_name = hash512.hexdigest() + "." + mime res = models.Image(image_name, trainer, img, None) res.upload() except RuntimeError as e: return e.to_response() (response["msg"], status) = ("완료되었습니다", 200) r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def trainers_UID_reviews_post(uid: int): response = dict() response["msg"] = "완료되었습니다" status = 200 try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 400) user = models.User.get_by_token(token) if type(user) is not models.Trainee: raise RuntimeError("토큰이 유효하지 않습니다.", 403) trainer = models.Trainer.find(uid) if type(trainer) is not models.Trainer: raise RuntimeError("해당 uid는 트레이너가 아닙니다.", 403) #해당 유저가 트레이너가 개최한 그룹에 속해 있어야 한다. trainer_groups = set(trainer.get_groups()) user_groups = set(user.get_groups()) if len(user_groups & trainer_groups) == 0: raise RuntimeError("해당 유저는 트레이너를 리뷰할 수 없습니다.", 403) form = request.data form = json.loads(form.decode("utf-8")) comments = form.get("comments", None) grade = form.get("grade", None) if comments is None or grade is None: raise RuntimeError("리뷰를 제대로 전송하지 않았습니다.", 403) review = models.Review(None, trainer, user, comments, grade) review.insert() except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def trainers_UID_images_get(uid): response = dict() try: token = request.headers.get("x-gs-token", None) if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 400) user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) trainer = models.Trainer.find(uid) if type(trainer) is not models.Trainer: raise RuntimeError("해당 uid는 트레이너가 아닙니다.", 403) res = models.Image.get_list(trainer) for it in res: it.image_name = "https://gym.hehehee.net/images/{}".format( it.image_name) response["images"] = res except RuntimeError as e: return e.to_response() (response["msg"], status) = ("완료되었습니다", 200) r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r
def groups_GROUPUID_users_TRAINEE_UID_bodymeasurements_post( group_uid, trainee_uid): response = dict() try: content_type = request.headers.get("content-type", "") token = request.headers.get("x-gs-token") (response["msg"], status) = ("완료되었습니다", 200) user = None if token is None: raise RuntimeError("토큰이 존재하지 않습니다.", 403) else: user = models.User.get_by_token(token) if type(user) is models.NotFoundAccount: raise RuntimeError("토큰이 유효하지 않습니다.", 403) if status != 200: r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r #토큰이 올바른 지 확인했다면, 이제 이 토큰이 해당 그룹의 개설자인지 확인해야 함 group = models.Group.find(group_uid) if group is None: raise RuntimeError("그룹이 유효하지 않습니다.", 403) if group.opener.uid != user.uid: raise RuntimeError("그룹의 개설자가 아닙니다.", 403) #TODO: 날짜까지 반영해야 함 trainee = models.User.find(trainee_uid) if type(trainee) is not models.Trainee: raise RuntimeError("유저가 잘못되었습니다.", 403) if not group in trainee.get_groups(): raise RuntimeError("해당 그룹에 유저가 가입하지 않았습니다.", 403) form = request.data form = json.loads(form.decode("utf-8")) print(form) img = form.get("img", None) d = form.get("date", None) image_name = None weight = form.get("weight", None) muscle = form.get("muscle", None) fat = form.get("fat", None) comment = form.get("comment", None) img_type = None if img is not None: import base64 import hashlib hash512 = hashlib.sha512() img_type = str(img["type"]) img = base64.decodebytes(img["data"].encode()) hash512.update(img) image_name = hash512.hexdigest() measurement_log = models.MeasurementInfo(image_name, user, trainee, img, img_type, weight, muscle, fat, comment) measurement_log.upload() except RuntimeError as e: return e.to_response() r = Response(response=json.dumps(response, default=json_handler), status=status, mimetype="application/json") return r