async def wrapped(self, *args, **kwargs): if self.request.user is not None: add_message( self.request, 'info', '<a href="/logout" class="alert-link">LogOut</a> to continue.') redirect(self.request, 'index') return await func(self, *args, **kwargs)
async def post(self) -> None: data = await self.request.post() roomname = data.get('roomname', '').lower() if not roomname: redirect(self.request, 'create_room') if await self.request.app.objects.count(Room.select().where(Room.name ** roomname)): add_message(self.request, 'danger', f'Room with {roomname} already exists.') redirect(self.request, 'create_room') room = await self.request.app.objects.create(Room, name=roomname) redirect(self.request, 'room', parts=dict(slug=room.name))
async def login_user(self, user: Users) -> None: """ Сетим пользователю сессию """ self.request.session['user'] = str(user.users_id) self.request.session['time'] = str(time()) add_message( self.request, 'info', f'Hello {user}!' ) redirect(self.request, 'index')
async def is_valid(self, username: str, password: str) -> bool: """Валидация парвметров""" if ( re.match(r'^[a-z]\w{0,9}$', username) and re.match(r'^[a-z]\w{0,9}$', password) ): return True add_message( self.request, 'warning', 'username or password not valid' ) return False
async def post(self) -> None: username, password = await self.get_normalize_data() is_valid = await self.is_valid(username, password) if not is_valid: redirect(self.request, 'login') try: user = await self.request.app.objects.get( Users, Users.username ** username, Users.password ** hashlib.md5(password.encode()).hexdigest() ) await self.login_user(user) except Users.DoesNotExist: add_message(self.request, 'danger', f'User {username} not found') redirect(self.request, 'login')
async def post(self) -> None: """Cоздаем пользователя""" username, password = await self.get_normalize_data() is_valid = await self.is_valid(username, password) if not is_valid: redirect(self.request, 'register') if await self.request.app.objects.count( Users.select().where(Users.username ** username) ): add_message( self.request, 'danger', f'{username} already exists' ) redirect(self.request, 'register') user = await self.request.app.objects.create( Users, username=username, password=hashlib.md5(password.encode()).hexdigest() ) await self.login_user(user)
async def wrapped(self, *args, **kwargs): if self.request.user is None: add_message(self.request, 'info', 'LogIn to continue.') redirect(self.request, 'login') return await func(self, *args, **kwargs)
async def get(self) -> None: self.request.session.pop('user') add_message(self.request, 'info', 'You are logged out') redirect(self.request, 'index')