def update_review(self, review_id: int) -> Response: with create_session() as session: new_review = request.args.get('review_text') review = session.query(Review).filter(Review.id == review_id).one() review.text = new_review return redirect(url_for('users.user_page', user_id=review.user_id), code=302)
def create_data(tmp_database): h1 = b'\xe40:\xb4\xd1g\x89\xd9\x04\x85G\xf7(pH:\xf0\x0f\x90\x99\xb5\xf00\xb2' h2 = b'qn\x03\x01\x08~\\"\x13\xf9\xe2\x81p\xc0\x01$@\xdd' session_factory = sessionmaker(tmp_database) with create_session(session_factory) as session: session.bulk_save_objects(data) session.add(User(login='******', password_hash=h1 + h2))
def film_top( min_rating: int = 1, max_rating: int = 10, credentials: HTTPBasicCredentials = Depends(cred_check), ) -> List[FilmModel]: with create_session() as session: return get_film_top(session, min_rating, max_rating)
def user_page(self, user_id: int) -> Response: with create_session() as session: user_reviews = (session.query( Film.name, Film.year, Review.text, Review.id, Review.rating).join(Review, Film.id == Review.film_id).filter( Review.user_id == user_id).all()) return self.render('user_page.html', review_list=user_reviews)
def film_search( name_contains: Optional[str] = None, year: Optional[int] = None, min_id: int = 0, max_id: int = 5, credentials: HTTPBasicCredentials = Depends(cred_check), ) -> List[FilmModel]: with create_session() as session: query = FilmSearchValidator(name_contains=name_contains, year=year, min_id=min_id, max_id=max_id) return get_film_list(session, query)
def register(user: RegisterRequest) -> RegisterResponse: salt: bytes = os.urandom(10) password_hash: bytes = salt + hashlib.pbkdf2_hmac( 'sha256', user.password.encode('utf-8'), salt, 100000) try: with create_session() as session: session.add(User(login=user.login, password_hash=password_hash)) except IntegrityError as e: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail='Login is already taken', ) from e return RegisterResponse(login=user.login)
def cred_check( credentials: HTTPBasicCredentials = Depends(security), ) -> HTTPBasicCredentials: with create_session() as session: try: user: User = (session.query(User).filter( User.login == credentials.username).one()) except NoResultFound as e: raise WrongPassword from e salt = user.password_hash[:10] correct_password_hash = user.password_hash[10:] possible_password_hash = hashlib.pbkdf2_hmac( 'sha256', credentials.password.encode('utf-8'), salt, 100000) if not possible_password_hash == correct_password_hash: raise WrongPassword return credentials
def rate( film_id: int, params: UserRate, credentials: HTTPBasicCredentials = Depends(cred_check), ) -> UserRate: with create_session() as session: user = session.query(User).filter( User.login == credentials.username).one() session.add( Review( user_id=user.id, film_id=film_id, rating=params.rating, text=params.text, timestamp=datetime.now(), )) return UserRate(rating=params.rating, text=params.text)
def film_info( film_id: int, credentials: HTTPBasicCredentials = Depends(cred_check) ) -> FilmStatsModel: with create_session() as session: return get_film(session, film_id)
def preview_review(self, review_id: int) -> Response: with create_session() as session: review = session.query(Review).filter(Review.id == review_id).one() return self.render('review_edit_form.html', review=review)
def index(self) -> Response: with create_session() as session: users = session.query(User).all() return self.render('user_list.html', userlist=users)
def index(self) -> Response: with create_session() as session: (films, ) = session.query(func.count(Film.id)).one() (reviews, ) = session.query(func.count(Review.id)).one() return self.render('index.html', films=films, reviews=reviews)
Film.name, Film.year, Review.text, Review.id, Review.rating).join(Review, Film.id == Review.film_id).filter( Review.user_id == user_id).all()) return self.render('user_page.html', review_list=user_reviews) @expose('/review/<review_id>') def preview_review(self, review_id: int) -> Response: with create_session() as session: review = session.query(Review).filter(Review.id == review_id).one() return self.render('review_edit_form.html', review=review) @expose('/update_review/<review_id>') def update_review(self, review_id: int) -> Response: with create_session() as session: new_review = request.args.get('review_text') review = session.query(Review).filter(Review.id == review_id).one() review.text = new_review return redirect(url_for('users.user_page', user_id=review.user_id), code=302) admin = Admin( app, name='filmrating admin panel', template_mode='bootstrap3', index_view=CustomIndexView(name='Старт', url='/'), ) with create_session() as s: admin.add_view(ModelView(Film, s, name='Фильмы')) admin.add_view(ReviewsView(name='Пользователи', endpoint='users'))