def post(self): try: login = self.json["login"] email = self.json["email"] is_admin = bool(self.json.get("is_admin", 0)) password = self.json["password"] assert password and len(password) > 3 assert LOGIN_EXP.match(login) assert EMAIL_EXP.match(email) except (KeyError, AssertionError, TypeError): raise HTTPError(400) if Users.select().where(Users.login == login).count(): raise HTTPError(409) user = Users( login=login, email=email, is_admin=is_admin, password=password, ) user.save() self.response({ 'id': user.id, 'login': user.login, 'email': user.email, 'is_admin': user.is_admin, })
def post(self): try: login, password = self.json['login'], self.json['password'] except KeyError: raise HTTPError(400) try: user = Users.check(login, password) except DoesNotExist: raise HTTPError(403) self.set_secure_cookie( 'session', ( user.id, self.request.remote_ip, IOLoop.current().time() ), SESSION_DAYS ) return self.response({ 'login': user.login, 'is_admin': user.is_admin, })
def check_password(login, password): try: user = Users.check(login, password) except DoesNotExist: raise LookupError('User not found') return user
def delete(self, uid): try: user = Users.get(id=uid) user.disabled = True except (KeyError, AssertionError): raise HTTPError(400) except DoesNotExist: raise HTTPError(404) else: user.save() self.set_status(204)
def get(self): self.response( list( map( lambda x: dict( id=x.id, login=x.login, email=x.email, is_admin=x.is_admin, disabled=x.disabled, ), Users.select(Users.id, Users.login, Users.email, Users.is_admin, Users.disabled))))
def get(self, uid): try: user = Users.get(id=uid) except DoesNotExist: raise HTTPError(404) self.response( dict(id=user.id, login=user.login, email=user.email, is_admin=user.is_admin, disabled=user.disabled, packages=list(map(lambda x: x.name, user.package_set))))
def find_user(uid, is_admin=False): cond = ( Users.disabled == False, Users.id == uid, ) if is_admin: cond += (Users.is_admin == is_admin,) q = Users.select().where(*cond) if not q.count(): raise DoesNotExist("User doesn't exists") return q.limit(1)[0]
def find_user(uid, is_admin=False): cond = ( Users.disabled == False, Users.id == uid, ) if is_admin: cond += (Users.is_admin == is_admin, ) q = Users.select().where(*cond) if not q.count(): raise DoesNotExist("User doesn't exists") return q.limit(1)[0]
def get(self, uid): try: user = Users.get(id=uid) except DoesNotExist: raise HTTPError(404) self.response(dict( id=user.id, login=user.login, email=user.email, is_admin=user.is_admin, disabled=user.disabled, packages=list( map( lambda x: x.name, user.package_set ) ) ))
def post(self): try: login, password = self.json['login'], self.json['password'] except KeyError: raise HTTPError(400) try: user = Users.check(login, password) except DoesNotExist: raise HTTPError(403) self.set_secure_cookie( 'session', (user.id, self.request.remote_ip, IOLoop.current().time()), SESSION_DAYS) return self.response({ 'login': user.login, 'is_admin': user.is_admin, })
def get(self): self.response( list( map( lambda x: dict( id=x.id, login=x.login, email=x.email, is_admin=x.is_admin, disabled=x.disabled, ), Users.select( Users.id, Users.login, Users.email, Users.is_admin, Users.disabled ) ) ) )
def put(self, uid): try: user = Users.get(id=uid) except DoesNotExist: raise HTTPError(404) try: user.login = self.json.get("login", user.login) user.email = self.json.get("email", user.email) user.is_admin = bool(self.json.get("is_admin", user.is_admin)) password = self.json.get("password") if password: user.password = password disabled = self.json.get("disabled") if disabled is False: user.disabled = False if not all(( isinstance(user.login, text_type), isinstance(user.email, text_type), LOGIN_EXP.match(str(user.login)) is not None, user.password and len(user.password) > 3, EMAIL_EXP.match(str(user.email)) is not None, )): raise HTTPError(400) except: raise HTTPError(400) user.save() self.response({ 'id': user.id, 'login': user.login, 'email': user.email, 'disabled': user.disabled, 'is_admin': user.is_admin, })
def create_default_user(migrator, db): Users(login='******', password='******', email="*****@*****.**", is_admin=True).save()