def user_profile(self, user_id): db = ORMUtil.initDB() Profile = ORMUtil.getProfileORM() if (db and Profile) is None: return None, 100 try: result = db.session.query(Profile).filter( Profile.user_id == user_id).first() except Exception as exc: return None, 141 if result is None: return None, 142 msg = { "user_id": result.user_id, "nickname": result.nickname, "icon_path": result.icon_path, "email": result.email, "department": result.department, "introduction": result.introduction } return msg, 0
def user_login(self, user_id, password): db = ORMUtil.initDB() Authentication = ORMUtil.getAuthenticationORM() Profile = ORMUtil.getProfileORM() if (db and Authentication and Profile) is None: return None, 100 try: result = db.session.query( Authentication.hmac_key, Authentication.encrypted_password).filter( Authentication.user_id == user_id).first() except Exception as exc: return None, 122 if result is None: return None, 121 hmac_key = result.hmac_key.encode('ascii') encrypted_password = Util.getEncryptedPassword(hmac_key, password) if result.encrypted_password == encrypted_password: try: result = db.session.query(Profile.nickname).filter( Profile.user_id == user_id).first() if result.nickname is None: return FlaskUser(user_id), 0 else: return FlaskUser(user_id), 0 except Exception as exc: return None, 124 else: return None, 123
def account_save_profile(self, user_id, nickname=None, email=None, department=None, introduction=None): db = ORMUtil.initDB() Profile = ORMUtil.getProfileORM() if (db and Profile) is None: return None, 100 #プロフィール取得 try: result = db.session.query(Profile).filter(Profile.user_id == user_id).first() except Exception as exc: return None, 141 if result is None: return None, 145 #変更しない値は現在の値を使う(=>変更なし) if nickname is None: nickname = result.nickname if email is None: email = result.email if department is None: department = result.department if introduction is None: introduction = result.introduction #コミット try: db.session.query(Profile).filter(Profile.user_id == user_id).update({'nickname': nickname, 'email': email, 'department': department, 'introduction': introduction}) db.session.commit() except Exception as exc: return None, 143 return "ok", 0
def user_register(self, user_id, password): db = ORMUtil.initDB() Authentication = ORMUtil.getAuthenticationORM() User = ORMUtil.getUserORM() UserHash = ORMUtil.getUserHashORM() Profile = ORMUtil.getProfileORM() if (db and Authentication and User and UserHash and Profile) is None: return None, 100 hmac_key = Util.generateRandomBytes(32) encrypted_password = Util.getEncryptedPassword(hmac_key, password) user_hash = Util.generateUserHash(user_id) try: db.session.add( Authentication(user_id, encrypted_password, hmac_key)) db.session.add( User(user_id, email=None, gift=0, stock=Util.DefaultStockValue)) db.session.add(UserHash(user_id, user_hash)) db.session.add( Profile(user_id, nickname=None, icon_path=None, email=None, department=None, introduction=None)) db.session.commit() except sqlalchemy.exc.IntegrityError as exc: current_app.logger.critical("user_register: Integrity error: %s" % exc) return None, 110 except Exception as exc: current_app.logger.critical("user_register: Unknown error: %s" % exc) return None, 199 return "ok", 0
def __set_profile(self, user_id): db = ORMUtil.initDB() Profile = ORMUtil.getProfileORM() if (db and Profile) is None: return user_id try: result = db.session.query(Profile).filter( Profile.user_id == user_id).first() except Exception as exc: self.nickname = user_id self.department = "現在参照できません" if result.nickname is None: self.nickname = user_id else: self.nickname = result.nickname if result.department is None: self.department = "所属未設定" else: self.department = result.department
def user_delete(self, user_id): db = ORMUtil.initDB() Authentication = ORMUtil.getAuthenticationORM() User = ORMUtil.getUserORM() UserHash = ORMUtil.getUserHashORM() Profile = ORMUtil.getProfileORM() if (db and Authentication and User and Profile) is None: return None, 100 try: db.session.query(Profile).filter( Profile.user_id == user_id).delete() db.session.query(User).filter(User.user_id == user_id).delete() db.session.query(UserHash).filter( UserHash.user_id == user_id).delete() db.session.query(Authentication).filter( Authentication.user_id == user_id).delete() db.session.commit() except Exception as exc: current_app.logger.critical("user_delete: Unknown error: %s" % exc) return None, 199 return "ok", 0
def user_list(self, page=None): db = ORMUtil.initDB() Profile = ORMUtil.getProfileORM() if (db and Profile) is None: return None, 100 try: result = db.session.query(Profile).all() except Exception as exc: return None, 122 users = [] for user in result: user_dict = { "user_id": user.user_id, "nickname": user.nickname, "icon_path": user.icon_path, "department": user.department, "introduction": user.introduction } users.append(user_dict) return users, 0
def gifts(self, user_id, mode=None): db = ORMUtil.initDB() GiftTransaction = ORMUtil.getGiftTransactionORM() User = ORMUtil.getUserORM() UserHash = ORMUtil.getUserHashORM() Profile = ORMUtil.getProfileORM() if (db and GiftTransaction and User and UserHash) is None: return None, 100 try: result = db.session.query(UserHash.user_hash).filter(UserHash.user_id == user_id).first() except Exception as exc: return None, 123 if result is None: return None, 124 user_hash = result.user_hash try: if mode == "send": result = db.session.query(GiftTransaction).filter(GiftTransaction.sender == user_hash).all() elif mode == "receive": result = db.session.query(GiftTransaction).filter(GiftTransaction.receiver == user_hash).all() else: result = db.session.query(GiftTransaction).filter(or_(GiftTransaction.sender == user_hash, GiftTransaction.receiver == user_hash)).all() except Exception as exc: return None, 122 if result is None: return None, 121 events = [] #ニックネーム取得 nicknames = {} user_ids = {} user_ids[user_hash] = user_id nicknames[user_hash] = "あなた" for event in result: for target_hash in [event.sender, event.receiver]: if target_hash in nicknames: continue else: try: tmp_result = db.session.query(UserHash.user_id).filter(UserHash.user_hash == target_hash).first() if tmp_result is None: user_ids[target_hash] = "" nicknames[target_hash] = "(退会したユーザ)" else: peer_user_id = tmp_result.user_id user_ids[target_hash] = peer_user_id tmp_result = db.session.query(Profile.nickname).filter(Profile.user_id == peer_user_id).first() peer_nickname = tmp_result.nickname #ニックネームが設定されていない場合はuser_idを用いる if peer_nickname is None: nicknames[target_hash] = peer_user_id else: nicknames[target_hash] = peer_nickname except Exception as exc: current_app.logger.critical("gifts getNickname error: %s" % exc) return None, 125 else: continue for event in result: json = {"date": event.created_at, "sender": user_ids[event.sender], "sender_nickname": nicknames[event.sender], "receiver":user_ids[event.receiver], "receiver_nickname": nicknames[event.receiver], "value": event.value, "message": event.message} events.append(json) if events is not None: return events, 0 else: return None, 0