def route_push_poke(user_id): uid = request.headers.get("uid", None) user_from = User.get(uid=uid) user_to = User.get(id=user_id) alarm = Alarm.create_alarm( event=Alarm.Event.POKE, user_from=user_from, user_to=user_to, message="{nickname} 님이 당신을 찔렀습니다.".format(nickname=user_from.nickname) ) alarm_record = alarm.records[-1] data: dict = alarm_record.as_dict() message_service.push(data, user_to) return Response("", mimetype="application/json")
def route_create_thumb_up(post_id, comment_id): uid = request.headers.get("uid", None) if not uid: abort(401) user = User.objects.get_or_404(uid=uid) post = Post.objects.get_or_404(id=post_id) comment = next( (comment for comment in post.comments if str(comment.id) == comment_id), None) if not comment: abort(404) comment.update(add_to_set__thumb_up_user_ids=user.id, pull__thumb_down_user_ids=user.id) comment = Comment.objects.get_or_404(id=comment_id) if user.id == post.author.id: user_from = user user_to = User.get(id=comment.user_id) alarm = Alarm.create_alarm( user_from=user_from, user_to=user_to, event=Alarm.Event.COMMENT_THUMB_UP, post=post, comment=comment, message="{nickname} 님이 당신의 댓글을 좋아합니다.".format( nickname=user_from.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_to) return Response("", mimetype="application/json")
def route_push_look_up(user_id: str): """uid is for a current user and user_id is for an user to watch.""" uid = request.headers.get("uid", None) user_from = User.get(uid=uid) user_to = User.get(id=user_id) message_service.push(dict( event=Alarm.Event.LOOK_UP, user_id=str(user_from.id), nickname=user_from.nickname, image_url=user_from.get_first_image(), message="{nickname} 님이 당신을 조회 중입니다.".format(nickname=user_from.nickname), created_at=str(pendulum.now().int_timestamp) ), user_to) return Response("", mimetype="application/json")
def route_update_star_rating(user_id: str, score: int): """Endpoint for getting users.""" uid = request.headers.get("uid", None) user_from = User.get(uid=uid) user_to = User.get(id=user_id) is_already_rated = StarRating.objects(user_from=user_from, user_to=user_to).first() if not is_already_rated: StarRating( user_from=user_from, user_to=user_to, rated_at=pendulum.now().int_timestamp, score=score ).save() if score > 3: alarm = Alarm.create_alarm( user_from=user_from, user_to=user_to, event=Alarm.Event.STAR_RATING, message="{nickname} 님이 당신을 높게 평가 하였습니다.".format(nickname=user_from.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, user_to) star_ratings = StarRating.objects(user_to=user_to).all() star_rating_sum = 0 for rating in star_ratings: star_rating_sum += rating.score star_rating_avg = star_rating_sum / len(star_ratings) user_to.update(star_rating_avg=star_rating_avg) return Response("", mimetype="application/json")
def route_list_posts(): uid = request.headers.get("uid", None) user = User.get(uid=uid) opposite_sex = "M" if user.sex == "F" else "F" last_id: str = request.args.get("last_id", None) per_page: int = int(request.args.get("per_page", 30)) params = dict(author_sex=opposite_sex, limit=per_page, is_deleted=False) if last_id: params["id__lt"] = last_id result = Post.list_posts(**params) response = encode(list(result)) return Response(response, mimetype="application/json")
def create_user(app, mock_user, sms_token): mock_user["sms_token"] = sms_token mock_user["sms_code"] = "000000" response = app.post("/users", headers=dict(uid=mock_user["uid"]), data=mock_user) user_id = response.get_json().get("_id") user = User.get(id=user_id) mock_user.pop("sms_token") mock_user.pop("sms_code") mock_user.pop("user_images") user.update(**mock_user) user.save() return response
def route_create_comment(post_id: str): uid = request.headers.get("uid", None) if not uid: abort(401) # if exists, create a comment as a sub comment comment_id = request.form.get("comment_id", None) # parent_comment_id comment = request.form.get("comment", "") post = Post.objects.get_or_404(id=post_id) user = User.objects.get_or_404(uid=uid) comment_to_create = Comment( post_id=post_id, user_id=user.id, comment=comment, comments=[], # child comments created_at=pendulum.now().int_timestamp, is_deleted=False).save() post.add_comment(comment_to_create, parent_id=comment_id) alarm = Alarm.create_alarm( user_from=user, user_to=post.author, event=Alarm.Event.COMMENT, post=post, comment=comment_to_create, message="{nickname} 님이 당신의 게시물에 댓글을 남겼습니다.".format( nickname=user.nickname)) alarm_record = alarm.records[-1] data = alarm_record.as_dict() message_service.push(data, post.author) comment = comment_to_create.to_mongo() comment["commenter"] = User.get(id=user.id).to_mongo() response = encode(comment) return Response(response, mimetype="application/json")
def route_list_user_posts(user_id): user = User.get(id=user_id) result = user.list_posts() response = encode(list(result)) return Response(response, mimetype="application/json")
def route_get_user(): """Endpoint for getting user.""" query_params = request.args.to_dict(flat=True) user = User.get(**query_params) response = encode(user.to_mongo()) return Response(response, mimetype="application/json")
def edit_profile(): users = User.get().all() return debug_str(users)