def sign_up(self, data): user = User(name=data.get('name')) local_oauth = LocalOAuth() local_oauth.password = data.get('password') user.local_oauth = local_oauth try: self.db.add(user) self.db.commit() return msg(True, "注册成功") except exc.IntegrityError as e: return msg(False, str(e))
def get(self, *args, **kwargs): data, count = UserModel.all() kwargs = { "count": count, "data": data } self.render("passport/accounts/index.html", **kwargs)
def sign_in(self, data): user = User.by_name(name=data.get('name')) password = user.local_oauth if user and password.auth_password(other_password=data.get('password')): self.session.set('user_name', user.name) return msg(True, "登陆成功") return msg(False, '用户名和密码不正确')
def validate_name(self, field): """ 如果用户已经有了,就不让注册 :param self: :param field: :return: """ if User.by_name(field.data) is None: raise ValidationError("用户没注册")
def get(self, user_id): data, count = RoleModel.all() user = UserModel.by_id(user_id) roles = user.roles rlist = list() for i in roles: rlist.append(i.id) kwargs = { "data": data, "user": user, "rlist": rlist } self.render("passport/accounts/set_role.html", **kwargs)
def create_user(): user = User() user.name = "admin" user.password = "******" dbSession.add(user) dbSession.commit()
def get(self, user_id): kwargs = { "form": EditForm(), "data": UserModel.by_id(user_id) } self.render("passport/accounts/edit.html", **kwargs)
def get_current_user(self): user_name = self.session.get("user_name") user = None if user_name: user = User.by_name(name=user_name) return user if user else None