from app.controllers import paths
from app.models import schemas, models
from app.controllers.base_controller import crud

paths['genre'] = crud(schemas.Genre, schemas.Genre, models.Genre, 'id')
Ejemplo n.º 2
0
from app.controllers import paths
from app.models import schemas, models
from app.controllers.base_controller import crud

paths['watchlist'] = crud(schemas.Watchlist, schemas.Watchlist,
                          models.Watchlist, 'movie_user')
Ejemplo n.º 3
0
def create(db, instance, model):
    movie_instance: Movie = instance_existence(Movie,
                                               'id')(id_value=instance.movie,
                                                     db=db)
    old_rating_sum = movie_instance.rating * movie_instance.vote_count
    new_rating_sum = old_rating_sum + instance.rating
    movie_instance.rating = new_rating_sum / (movie_instance.vote_count + 1)
    movie_instance.vote_count += 1
    save_instance(movie_instance, db=db)

    return create_instance(db=db, instance=instance, model=model)


router = crud(schemas.Rating,
              schemas.Rating,
              models.Rating,
              'movie_user',
              post=create)
paths['rating'] = router


@router.get("/rolling-avg/{id}",
            responses={
                200: RatingsHistory().definition,
                **error_docs('Movie', ResourceDoesNotExist)
            })
@error_handling
def get_movie_ratings_history_by_year(db: Session = Depends(get_db),
                                      movie: Movie = Depends(
                                          instance_existence(Movie,
                                                             id_field='id'))):

def create_review(db, instance, model):
    rating_model = db.query(models.Rating).filter_by(
        user=instance.user).filter_by(movie=instance.movie).first()
    if rating_model is None:
        rating_keys = list(schemas.Rating.schema().get('properties').keys())
        rating = schemas.Rating(
            **{k: v
               for k, v in instance.dict().items() if k in rating_keys})
        rating_model = create_instance(db=db,
                                       instance=rating,
                                       model=models.Rating)
    instance.rating = rating_model
    return create_instance(db=db, instance=instance, model=model)


def update_review(db, data, instance):
    data.fields.pop('rating', None)
    if getattr(data, 'rating', None) is not None:
        delattr(data, 'rating')
    return update_instance_data(db=db, data=data, instance=instance)


paths['review'] = crud(schemas.ReviewRead,
                       schemas.ReviewWrite,
                       models.Review,
                       'movie_user',
                       post=create_review,
                       put=update_review)
Ejemplo n.º 5
0
from app.controllers import paths
from app.controllers.base_controller import crud
from app.models import schemas, models

paths['user'] = crud(schemas.UserRead, schemas.UserWrite, models.User, 'id')
Ejemplo n.º 6
0
from app.controllers import paths
from app.models import schemas, models
from app.controllers.base_controller import crud

paths['tag'] = crud(schemas.Tag, schemas.Tag, models.Tag, 'name')
        if db.query(models.Genre).filter_by(id=g).first() is None
    ]
    [save_instance(genre, db=db) for genre in new_genres]

    return create_instance(db=db, instance=instance, model=model)


def get_all(db,
            limit: int = 10,
            page: int = 1,
            sort: List[str] = (),
            filters: List[str] = (),
            user_id=None):
    watchlist = get_user_watchlist_ids(db, user_id)
    query = query_objects(db=db,
                          query_model=models.Movie,
                          filters=filters,
                          sort=sort,
                          filter_model=MovieFilter)
    result = paginator(query, page_number=page, per_page_limit=limit)
    [setattr(i, 'in_watchlist', i.id in watchlist) for i in result.items]
    return result


paths['movie'] = crud(schemas.MovieRead,
                      schemas.Movie,
                      models.Movie,
                      'id',
                      get_all=get_all,
                      post=create)
    section.poster_path, section.description = eval(
        section.poster_function)(db)
    return section


def read_all(db,
             limit: int = 10,
             page: int = 1,
             sort: List[str] = (),
             filters: List[str] = (),
             **_):
    query = query_objects(db=db,
                          query_model=models.Section,
                          filters=filters,
                          sort=sort)
    page = paginator(query, page_number=page, per_page_limit=limit)
    page.items = [update_section(db, item) for item in page.items]
    return page


def read_one(db, instance: models.Section):
    return update_section(db=db, section=instance)


paths['section'] = crud(schemas.SectionRead,
                        schemas.Section,
                        models.Section,
                        'id',
                        get_all=read_all,
                        get_one=read_one)