def get_auth_token(self): if self._token is None: db = get_db() cursor = db.cursor() sql = """ SELECT token FROM token WHERE user_id=%s """ try: cursor.execute(sql, self.id) result = cursor.fetchone() except: db.rollback() abort(500) if result is None: self._token = make_secure_token( str(self._id), self._password, self._salt, key=current_app.config['SECRET_KEY']) sql = """ INSERT INTO token (user_id,token) VALUES(%s,%s) """ try: cursor.execute(sql, (self.id, self._token)) db.commit() except: db.rollback() abort(500) else: self._token = result[0] return self._token
def create_user(**kwargs): assert 'username' in kwargs user = User.get_user_by_username(kwargs['username']) if user is not None: raise UsernameDuplicate(kwargs['username']) user = User(**kwargs) assert 'password' in kwargs user.password = kwargs['password'] db = get_db() cursor = db.cursor() try: cursor.execute( "INSERT INTO user (username,password,salt,email,activation,type,status,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s,CURRENT_TIME())", (user.username, user._password, user.salt, user.email, user.activation, user.type, user.status)) print( cursor.mogrify( "INSERT INTO user (username,password,salt,email,activation,type,status,create_time) VALUES (%s,%s,%s,%s,%s,%s,%s,CURRENT_TIME())", (user.username, user._password, user.salt, user.email, user.activation, user.type, user.status))) db.commit() return User.get_user_by_username(user.username) except: db.rollback() abort(500)
def delete_user(self): db = get_db() cursor = db.cursor() try: cursor.execute("DELETE FROM user WHERE id=%s", self._id) db.commit() except: db.rollback() abort(500)
def salt(self, new_val): if self._salt == new_val: return self._salt = new_val db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET salt=%s WHERE id=%s", (self._salt, self._id)) db.commit() except: db.rollback() abort(500)
def password(self, new_val): if self.check_password(new_val): return self._password = generate_password_hash(new_val) db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET password=%s WHERE id=%s", (self._password, self._id)) db.commit() except: db.rollback() abort(500)
def status(self, new_val): # 如果新值等于原始值则跳过修改 if self._status == new_val: return self._status = new_val db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET status=%s WHERE id=%s", (self._status, self._id)) db.commit() except: db.rollback() abort(500)
def activation(self, new_val): # 如果新值等于原始值则跳过修改 if self._activation == new_val: return self._activation = new_val db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET activation=%s WHERE id=%s", (self._activation, self._id)) db.commit() except: db.rollback() abort(500)
def username(self, new_val): if self._username == new_val: return user = User.get_user_by_username(new_val) if user is not None: raise UsernameDuplicate(new_val) self._username = new_val db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET username=%s WHERE id=%s", (self._username, self._id)) db.commit() except: db.rollback() abort(500)
def email(self, new_val): # 如果新值等于原始值则跳过修改 from free_shark.utils import is_email if self._email == new_val: return if not is_email(new_val): raise UserEmailInvalid(new_val) self._email = new_val db = get_db() cursor = db.cursor() try: cursor.execute("UPDATE user SET email=%s WHERE id=%s", (self._email, self._id)) db.commit() self.status = 2 except: db.rollback() abort(500)
def wrapper(self, *args, **kwargs): if self.id is None: print('User id should not None!') abort(500) else: func(self, *args, **kwargs)