def sign_up(): ''' auth_key, auth_value, phone, password 로 계정 하나를 생성합니다. ''' user = afr(User(request.json.get('password'), phone=request.json.get('phone'))) user_session = afr(UserSession(user)) Session().commit() return {'user_id': user.id, 'token': user_session.token, 'expiry': DateTimeHelper.full_datetime(user_session.expiry)}, Status.HTTP_200_OK
def upload_image(): ''' 삭제된 유저가 bucket 에 남긴 사진은 나중에 수동으로라도 지울 수 있도록 기록을 납깁니다. 2020-08-19 ''' file = request.files.get('image') url = s3.upload_file(file, file.filename) afr(Pic(user_id=g.user_session.user.id, url=url)) Session().commit() return {'url': url}, Status.HTTP_200_OK
def recharge_heart_apple(): ''' 애플 영수증을 검증하고 하트를 충전합니다. ''' heart_recharge = afr(HeartRecharge(**request.json.get('recharge_info'))) afr(UserPointTx(user_id=g.user_session.user.id, hp=heart_recharge.amount)) UserPoint.heart_point(user_id=g.user_session.user.id, hp=heart_recharge.amount) Session().commit() return {'okay': True}, Status.HTTP_200_OK
def report(): ''' 신고를 때립니다. ''' user = Session().query(User).filter( (User.id == request.json.get('user_id'))).one_or_none() if user is None: raise ClientError(f'No User Found id #{request.json.get("user_id")}', status_code=Status.HTTP_404_NOT_FOUND) afr( Report(from_user_id=g.user_session.user.id, to_user_id=request.json.get('user_id'), memo=request.json.get('memo'))) return {'okay': True}, Status.HTTP_200_OK
def login(): ''' 새로운 토큰을 발급해 줍니다. ''' user_session = afr(UserSession(g.pr_result.get('user'))) Session().commit() return {'token': user_session.token, 'expiry': DateTimeHelper.full_datetime(user_session.expiry)}, Status.HTTP_200_OK
def login_google(): oauth = Session().query(OauthGoogle).filter(OauthGoogle.party_id == g.info.get('id')).first() is_new = True if oauth is None else False if is_new: # TODO 여기서 블랙리스트 조회에서 에러 리턴. user = afr(User(email=g.info.get('email'))) oauth = afr(OauthGoogle(user, g.info)) user.oauth_google_id = oauth.id user_session = UserSession(oauth.user, third_party_token=request.json.get('token')) Session(changed=True).add(user_session) return {'okay':True, 'user_id':oauth.user.id, 'token': user_session.token, 'is_new': is_new, 'expiry': DateTimeHelper.full_datetime(user_session.expiry)}, Status.HTTP_200_OK
def register_match_question(): ''' question_ids 를 받아서 set question_ids 의 길이가 5를 초과하면 raise ClientError 하나라도 답변 안 한 질문이 있다면 raise ClientError ''' Session().query(MatchQuestion).filter( (MatchQuestion.user_id == g.user_session.user.id)).delete() Session().flush() match_questions = [ MatchQuestion(user_id=g.user_session.user.id, question_id=a.question_id) for a in g.pr_result.get('answers') ] afr(*match_questions) Session().commit() return {'okay': True}, Status.HTTP_200_OK
def heart_point(cls, user_id, hp): ''' hp 는 음수일 수 있습니다. 음수라면 hp 가 감소합니다. ''' from libs.database.engine import Session, afr tx = afr(UserPointTx(user_id=user_id, hp=hp)) point = Session().query(UserPoint).filter( (UserPoint.user_id == user_id)).with_for_update().one() point.hp += hp Session().flush() return point
def manner_point(cls, user_id, mp): ''' mp 는 음수일 수 있습니다. 음수라면 mp 가 감소합니다. ''' from libs.database.engine import Session, afr tx = afr(UserPointTx(user_id=user_id, mp=mp)) point = Session().query(UserPoint).filter( (UserPoint.user_id == user_id)).with_for_update().one() point.mp += mp Session().flush() return point
def login_kakao(): oauth = Session().query(OauthKakao).filter( OauthKakao.party_id == g.info.get('id')).first() is_new = True if oauth is None else False if is_new: user = afr(User(email=g.info['kakao_account'].get('email'))) oauth = afr(OauthKakao(user, g.info)) afr(UserPoint(user_id=user.id)) user_session = UserSession(oauth.user, third_party_token=request.json.get('token')) Session(changed=True).add(user_session) return { 'user_id': oauth.user.id, 'token': user_session.token, 'is_new': is_new, 'expiry': DateTimeHelper.full_datetime(user_session.expiry) }, Status.HTTP_200_OK
def generate_random_matches(): user = Session().query(User).filter( (User.sex != g.user_session.user.sex) & (User.id.notin_( Match.matched_user_ids(g.user_session.user.id, Session()))) ).order_by(func.random()).limit(1).one() match = afr( Match(from_user_id=user.id, to_user_id=g.user_session.user.id, type_=Match.TYPE_RANDOM)) Session().commit() return {'match': match.json()}, Status.HTTP_200_OK
def upload_picture(): file = request.files.get('image') url = s3.upload_file(file, file.filename) picture = afr( Picture( user_id=g.user_session.user.id, url=url, date=datetime.strptime(request.form.get('date'), '%Y-%m-%d').date(), )) Session().commit() return picture.json(), Status.HTTP_200_OK
def send_sms(): ''' 문자 인증을 보냅니다. ''' auth = afr(SmsAuth(g.user_session.user.id, request.json.get('phone_num'))) result = sms_helper.send_auth_sms(auth) if not result.get('header').get('isSuccessful'): raise ServerError('could not send sms') Session().commit() return {'auth_key': str(auth.auth_key)}, Status.HTTP_200_OK
def send_double_heart(): ''' 더블하트를 보냅니다. ''' heart = afr( Heart(from_user_id=g.user_session.user.id, to_user_id=request.json.get('user_id'), message=request.json.get('message'), double=True)) g.pr_result['to_match'].heart_id = heart.id g.pr_result['from_match'].heart_id = heart.id Session().add(heart) Session().commit() return {'okay': True, 'heart_id': heart.id}, Status.HTTP_200_OK
def notify_register_confirmed(): ''' TODO: 이것도 관리자페이지 기능 ''' notification = afr( Notification(from_user_id=g.user_session.user.id, to_user_id=request.json.get('user_id'), body={ 'kind': 'REGISTER_CONFIRMED', 'title': '가입이 승인되었습니다!', })) notification.notify() Session().commit() return {'okay': True}, Status.HTTP_200_OK
def notify_heart_accepted(): heart = Session().query(Heart).filter( (Heart.id == request.json.get('heart_id'))).one() match = Session().query(Match).filter( (Match.heart_id == heart.id) & (Match.from_user_id == g.user_session.user.id)).one() notification = afr( Notification( from_user_id=g.user_session.user.id, to_user_id=match.to_user_id, notification={ 'title': f'{g.user_session.user.nick_name if g.user_session.user.nick_name else "???"} ' f'님이 하트를 수락하셨습니다' }, body={ 'kind': 'HEART_ACCEPTED', 'match_id': str(match.id), 'click_action': 'FLUTTER_NOTIFICATION_CLICK', })) notification.notify() Session().commit() return {'okay': True}, Status.HTTP_200_OK
def post_user_troubles(): afr( UserTrouble(user_id=g.user_session.user.id, trouble_id=request.json.get('trouble_id'))) Session().commit() return {'okay'}, Status.HTTP_200_OK
def post_life_style_answers(): afr( LifeStyleAnswer(answers=request.json.get('answers'), user_id=g.user_session.user.id)) Session().commit() return {'okay': True}, Status.HTTP_200_OK