async def register(self, request): user_id = await authorized_userid(request) if user_id: return redirect(request, 'index') form = await request.post() error = await validate_register_form(self.mongo, form) if error is None: await self.mongo.user.insert_one({ 'username': form['username'], 'pw_hash': generate_password_hash(form['password']) }) return redirect(request, 'login') return {'error': error, 'form': form}
async def remove_user(self, request): user_id = request.match_info['user_id'] if request.method == 'POST': response = redirect(request, 'index') await forget(request, response) await self.mongo.user.delete_one({'_id': ObjectId(user_id)}) await self.mongo.link.delete_many({'user_id': ObjectId(user_id)}) return response
async def login(self, request): user_id = await authorized_userid(request) if user_id: return redirect(request, 'index') form = await request.post() user = await self.mongo.user.find_one({'username': form['username']}) if user is None: error = 'Invalid username' elif not check_password_hash(user['pw_hash'], form['password']): error = 'Invalid password' else: response = redirect(request, 'index') await remember(request, response, str(user['_id'])) return response return {'error': error, 'form': form}
async def change_password(self, request): user_id = await authorized_userid(request) if not user_id: return redirect(request, 'index') form = await request.post() error = await validate_password_form(form) if request.method == 'POST': if error is None: await self.mongo.user.update_one({'_id': ObjectId(user_id)}, { '$set': { 'pw_hash': generate_password_hash(form['password']) } }) return redirect(request, 'index') return {'form': form, 'error': error}
async def index(self, request): user_id = await authorized_userid(request) if user_id is None: router = request.app.router location = router['public_homepage'].url_for() raise web.HTTPFound(location=location) user = await self.mongo.user.find_one({'_id': ObjectId(user_id)}) links = await self.mongo.link.find({ 'user_id': ObjectId(user_id) }).to_list(300) form = await request.post() error_msg = None if request.method == 'POST': if form['url']: url_pattern = re.compile( r"^(https://drive\.google\.com/open\?id=.+)") url = form['url'] if url_pattern.match(string=url): file_id = url_parser.parse_qsl(url)[0][1] await self.mongo.link.insert_one({ 'user_id': ObjectId(user_id), 'url': form['url'], 'filename': form['filename'], 'file_id': file_id }) return redirect(request, 'index') else: error_msg = "It seems like URL doesnt match the pattern" endpoint = request.match_info.route.name return { 'endpoint': endpoint, 'user': user, 'links': links, 'form': form, 'error_msg': error_msg, }
async def change_password_page(self, request): user_id = await authorized_userid(request) if not user_id: return redirect(request, 'index') return {'error': None, 'form': None}
async def remove_link(self, request): link_id = request.match_info['link_id'] if request.method == 'POST': await self.mongo.link.delete_one({'_id': ObjectId(link_id)}) return redirect(request, 'index')
async def register_page(self, request): user_id = await authorized_userid(request) if user_id: return redirect(request, 'index') return {'error': None, 'form': None}
async def logout(self, request): response = redirect(request, 'index') await forget(request, response) return response
async def public_homepage(self, request): user_id = await authorized_userid(request) if user_id: return redirect(request, 'index') return {'endpoint': request.match_info.route.name}