def register(_username, _password): """ Register new user in database with couple(username, password), return True if add, False if doesnt """ db = Database() if db.select_one('''SELECT username FROM user WHERE username LIKE ?''', (_username, )) is not None: return "Username already taken" else: pwh = str(bcrypt.hashpw(_password.encode('utf-8'), bcrypt.gensalt())) db.update("INSERT INTO user(username,password) VALUES(?,?)", (_username, pwh[2:(len(pwh) - 1)])) return User.login(_username, _password)
def load_to_db(self): """ Persist instance to database """ db = Database() db.update( ''' UPDATE step SET textOfTheStep = ? WHERE questId = ? AND stepNumber = ? ''', (self.m_text if self.m_text else "", self.quest_id, self.m_number))
def load_to_db(self, _recursive=True): """ Persist instance to database, if _recursive is True persist each steps too """ db = Database() db.update( ''' UPDATE quest SET questNumber = ? WHERE questId = ? ''', (self.m_name if self.m_name else "", self.m_id)) if _recursive: for step in self.m_steps: step.load_to_db()
def load_to_db(self): """ Persist instance to database, if _recursive is True persist each steps too """ db = Database() if db.select_one("SELECT idHero FROM hero WHERE nameOfTheHero = ?", (self.name,)): db.update( ''' UPDATE hero SET lvl = ?,weapon = ?, armor = ?, passive = ?, sex = ?, idUser = ?,numQuest = ?, numStep = ? WHERE nameOfTheHero = ? ''', (self.lvl, self.weapon["name"], self.armor, self.passive, 1 if self.m_sex else 0, self.user_id, self.current_quest, self.current_step, self.name) ) else: db.add( ''' INSERT INTO hero(nameOfTheHero, lvl, weapon, armor, passive, sex, idUser, numQuest, numStep) VALUES(?,?,?,?,?,?,?,?,?) ''', (self.name, self.lvl, self.weapon["name"], self.armor, self.passive, 1 if self.m_sex else 0, self.user_id, self.current_quest, self.current_step) )