Esempio n. 1
0
def film_view(request, film_id=None):
    if request.method == 'GET':
        if film_id:
            film = Film.objects.get(id=film_id)
            response = JsonResponse(serialize(film))
        else:
            query = request.GET.dict()
            page = int(query['page']) if query.get('page') else 1

            languages = list(
                set([film.language for film in Film.objects.all()]))
            countries = list(set([film.country
                                  for film in Film.objects.all()]))
            genres = [
                serialize_demo_genre(genre) for genre in Genre.objects.all()
            ]

            name_filter = query.get('nameFilter') or ''
            budget_from_filter = query.get('budgetFromFilter') or 0
            budget_to_filter = query.get('budgetToFilter') or 1000000000
            language_filter = request.GET.getlist(
                'languagesFilter') or languages
            country_filter = request.GET.getlist(
                'countriesFilter') or countries
            genre_filter = request.GET.getlist('genresFilter') or [
                genre['id'] for genre in genres
            ]
            with_reviews_filter = query.get('withReviewsFilter') or 'all'

            films = Film.objects.filter(
                name__contains=name_filter,
                budget__gte=budget_from_filter,
                budget__lte=budget_to_filter,
                language__in=language_filter,
                country__in=country_filter,
            )

            if with_reviews_filter == 'yes':
                films = films.annotate(
                    review_count=Count('review')).filter(~Q(review_count=0))
            elif with_reviews_filter == 'no':
                films = films.annotate(review_count=Count('review')).filter(
                    review_count=0)

            if genre_filter:
                genre_filter = Genre.objects.filter(id__in=genre_filter)
                films = [
                    film for film in films
                    if len(set(film.genres.all()) & set(genre_filter)) != 0
                ]

            total_pages = math.ceil(len(films) / globals.PAGE_SIZE)

            if len(films) and page > total_pages or page < 0:
                return HttpResponseNotFound('<h1>Page not found</h1>')

            prev_page = page - 1 if page > 1 else None
            next_page = page + 1 if page < total_pages else None

            response = JsonResponse(
                {
                    'data': globals.get_page(page, films, serialize_demo),
                    'page': page,
                    'total_pages': total_pages,
                    'prev_page': prev_page,
                    'next_page': next_page,
                    'languages': languages,
                    'countries': countries,
                    'genres': genres,
                },
                safe=False)

    elif request.method == 'POST':
        data = json.loads(request.body)
        film = Film.objects.create(
            budget=data['budget'],
            name=data['name'],
            language=data['language'],
            overview=data['overview'],
            country=data['country'],
            date=datetime.strptime(data['date'], '%d/%m/%Y'),
            revenue=data['revenue'],
            runtime=data['runtime'],
            tagline=data['tagline'],
            image=data['image'],
        )
        film.save()

        actors = Actor.objects.filter(id__in=data['actors'])
        film.actors.add(*actors)

        genres = Genre.objects.filter(id__in=data['genres'])
        film.genres.add(*genres)

        companies = Company.objects.filter(id__in=data['companies'])
        film.companies.add(*companies)
        film.save()

        response = JsonResponse(serialize(film))

    elif request.method == 'PUT':
        film = Film.objects.get(id=film_id)
        data = json.loads(request.body)
        update_fields = []

        if data.get('budget'):
            film.budget = int(data['budget'])
            update_fields.append('budget')

        if data.get('name'):
            film.name = data['name']
            update_fields.append('name')

        if data.get('language'):
            film.language = data['language']
            update_fields.append('language')

        if data.get('overview'):
            film.overview = data['overview']
            update_fields.append('overview')

        if data.get('country'):
            film.country = data['country']
            update_fields.append('country')

        if data.get('date'):
            film.date = datetime.strptime(data['date'], '%d/%m/%Y')
            update_fields.append('date')

        if data.get('revenue'):
            film.revenue = int(data['revenue'])
            update_fields.append('revenue')

        if data.get('runtime'):
            film.runtime = int(data['runtime'])
            update_fields.append('runtime')

        if data.get('tagline'):
            film.tagline = data['tagline']
            update_fields.append('tagline')

        if data.get('image'):
            film.image = data['image']
            update_fields.append('image')

        if data.get('actors'):
            film.actors.clear()
            actors = Actor.objects.filter(id__in=data['actors'])
            film.actors.add(*actors)

        if data.get('genres'):
            film.genres.clear()
            genres = Genre.objects.filter(id__in=data['genres'])
            film.genres.add(*genres)

        if data.get('companies'):
            film.companies.clear()
            companies = Company.objects.filter(id__in=data['companies'])
            film.companies.add(*companies)

        film.save(update_fields=update_fields)
        response = JsonResponse(serialize(film))

    elif request.method == 'DELETE':
        film = Film.objects.get(id=film_id)
        film.delete()
        response = JsonResponse(dict())

    else:
        response = JsonResponse(dict())

    response = globals.ser_cors_headers(response)
    return response
Esempio n. 2
0
def review_view(request, review_id=None):
    if request.method == 'GET':
        if review_id:
            review = Review.objects.get(id=review_id)
            response = JsonResponse(serialize(review))
        else:
            total_pages = math.ceil(
                len(Review.objects.all()) / globals.PAGE_SIZE)
            query = request.GET.dict()
            page = int(query['page']) if query.get('page') else 1

            if page > total_pages:
                return HttpResponseNotFound('<h1>Page not found</h1>')

            prev_page = str(page - 1) if page > 1 else None
            next_page = str(page + 1) if page < total_pages else None

            response = JsonResponse(
                {
                    'data': globals.get_page(page, Review.objects.all(),
                                             serialize),
                    'page': page,
                    'total_pages': total_pages,
                    'prev_page': prev_page,
                    'next_page': next_page,
                },
                safe=False)

    elif request.method == 'POST':
        data = json.loads(request.body)
        review = Review.objects.create(
            text=data['text'],
            is_positive=data['is_positive'],
            film=Film.objects.get(id=data['film']),
        )
        response = JsonResponse(serialize(review))

    elif request.method == 'PUT':
        review = Review.objects.get(id=review_id)
        data = json.loads(request.body)
        update_fields = []

        if data.get('text'):
            review.text = data['text']
            update_fields.append('text')

        if data.get('is_positive'):
            review.is_positive = data['is_positive']
            update_fields.append('is_positive')

        if data.get('film'):
            review.film = Film.objects.get(id=data['film'])
            update_fields.append('film')

        review.save(update_fields=update_fields)
        response = JsonResponse(serialize(review))

    elif request.method == 'DELETE':
        review = Review.objects.get(id=review_id)
        review.delete()
        response = JsonResponse(dict())

    else:
        response = JsonResponse(dict())

    response = globals.ser_cors_headers(response)
    return response
Esempio n. 3
0
def company_view(request, company_id=None):
    if request.method == 'GET':
        if company_id:
            company = Company.objects.get(id=company_id)
            response = JsonResponse(serialize(company))
        else:
            query = request.GET.dict()
            page = int(query['page']) if query.get('page') else 1

            name_filter = query['nameFilter'] if query.get('nameFilter') else ''
            date_filter_from = query.get('dateFilterFrom') or 0
            date_filter_to = query.get('dateFilterTo') or 3000
            film_id_filter = query.get('filmId') or None
            genre_id_filter = query.get('genreId') or None

            if film_id_filter is not None:
                film = Film.objects.get(id=film_id_filter)
                companies = film.companies.all()
            elif genre_id_filter is not None:
                genre = Genre.objects.get(id=genre_id_filter)
                companies = genre.companies.all()
            else:
                companies = Company.objects.all()

            companies = companies.filter(name__contains=name_filter,
                                         founding_date__gte=date_filter_from,
                                         founding_date__lte=date_filter_to,
                                         )
            total_pages = math.ceil(len(companies)/globals.PAGE_SIZE)

            if len(companies) and page > total_pages or page < 0:
                return HttpResponseNotFound('<h1>Page not found</h1>')

            prev_page = page - 1 if page > 1 else None
            next_page = page + 1 if page < total_pages else None

            response = JsonResponse({
                'data': globals.get_page(page, companies, serialize_demo),
                'page': page,
                'total_pages': total_pages,
                'prev_page': prev_page,
                'next_page': next_page,
            }, safe=False)

    elif request.method == 'POST':
        """
            name: string
            founding_date: year, integer
            genres: ids of genres
        """

        data = json.loads(request.body)
        company = Company.objects.create(
            name=data['name'],
            founding_date=int(data['founding_date'])
        )
        company.save()

        genres = Genre.objects.filter(id__in=data['genres'])
        company.genre_set.add(*genres)

        response = JsonResponse(serialize(company))

    elif request.method == 'PUT':
        """
            name: string
            founding_date: year, integer
            genres: ids of genres
            films: ids of films
        """

        company = Company.objects.get(id=company_id)
        data = json.loads(request.body)
        update_fields = []

        if data.get('name'):
            company.name = data['name']
            update_fields.append('name')

        if data.get('founding_date'):
            company.founding_date = int(data['founding_date'])
            update_fields.append('founding_date')

        if data.get('genres'):
            genres = Genre.objects.filter(id__in=data['genres'])
            company.genre_set.clear()
            company.genre_set.add(*genres)

        if data.get('films'):
            films = Film.objects.filter(id__in=data['films'])
            company.film_set.clear()
            company.film_set.add(*films)

        company.save(update_fields=update_fields)
        response = JsonResponse(serialize(company))

    elif request.method == 'DELETE':
        company = Company.objects.get(id=company_id)
        company.delete()
        response = JsonResponse(dict())

    else:
        response = JsonResponse(dict())

    response = globals.ser_cors_headers(response)
    return response
Esempio n. 4
0
def actor_view(request, actor_id=None):
    if request.method == 'GET':
        if actor_id:
            actor = Actor.objects.get(id=actor_id)
            response = JsonResponse(serialize(actor))
        else:
            query = request.GET.dict()
            page = int(query['page']) if query.get('page') else 1
            name_filter = query.get('filter') or ''
            film_id_filter = query.get('filmId')
            genre_id_filter = query.get('genreId')

            if film_id_filter is not None:
                film = Film.objects.get(id=film_id_filter)
                actors = film.actors.all()
            elif genre_id_filter is not None:
                genre = Genre.objects.get(id=genre_id_filter)
                actors = genre.actors.all()
            else:
                actors = Actor.objects.all()

            actors = actors.filter(name__contains=name_filter)
            total_pages = math.ceil(len(actors) / globals.PAGE_SIZE)

            if len(actors) and page > total_pages or page < 0:
                return HttpResponseNotFound('<h1>Page not found</h1>')

            prev_page = page - 1 if page > 1 else None
            next_page = page + 1 if page < total_pages else None

            response = JsonResponse(
                {
                    'data': globals.get_page(page, actors, serialize_demo),
                    'page': page,
                    'total_pages': total_pages,
                    'prev_page': prev_page,
                    'next_page': next_page,
                },
                safe=False)

    elif request.method == 'POST':
        """
            name: string
            birthday: string as %d/%m/%Y
            genres: ids of genres
        """

        data = json.loads(request.body)
        actor = Actor.objects.create(
            name=data['name'],
            image=data['image'],
        )
        actor.save()

        genres = Genre.objects.filter(id__in=data['genres'])
        actor.genre_set.add(*genres)

        response = JsonResponse(serialize(actor))

    elif request.method == 'PUT':
        """
            name: string
            birthday: string as %d/%m/%Y
            genres: ids of genres
            films: ids of films
        """

        actor = Actor.objects.get(id=actor_id)
        data = json.loads(request.body)

        update_fields = []

        if data.get('name'):
            actor.name = data['name']
            update_fields.append('name')

        if data.get('image'):
            actor.image = data['image']
            update_fields.append('image')

        if data.get('films'):
            films = Film.objects.filter(id__in=data['films'])
            actor.film_set.clear()
            actor.film_set.add(*films)

        if data.get('genres'):
            genres = Genre.objects.filter(id__in=data['genres'])
            actor.genre_set.clear()
            actor.genre_set.add(*genres)

        actor.save(update_fields=update_fields)
        response = JsonResponse(serialize(actor))

    elif request.method == 'DELETE':
        actor = Actor.objects.get(id=actor_id)
        actor.delete()
        response = JsonResponse(dict())

    else:
        response = JsonResponse(dict())

    response = globals.ser_cors_headers(response)
    return response
Esempio n. 5
0
def genre_view(request, genre_id=None):
    if request.method == 'GET':
        if genre_id:
            genre = Genre.objects.get(id=genre_id)
            response = JsonResponse(serialize(genre))
        else:
            query = request.GET.dict()
            page = int(query['page']) if query.get('page') else 1

            filter_name = query.get('filter') or ''
            film_id_filter = query.get('filmId')
            actor_id_filter = query.get('actorId')
            company_id_filter = query.get('companyId')

            if film_id_filter is not None:
                film = Film.objects.get(id=film_id_filter)
                genres = film.genres.all()
            elif actor_id_filter is not None:
                actor = Actor.objects.get(id=actor_id_filter)
                genres = actor.genre_set.all()
            elif company_id_filter is not None:
                company = Company.objects.get(id=company_id_filter)
                genres = company.genre_set.all()
            else:
                genres = Genre.objects.all()

            genres = genres.filter(name__contains=filter_name)
            total_pages = math.ceil(len(genres) / globals.PAGE_SIZE)

            if len(genres) and page > total_pages or page < 0:
                return HttpResponseNotFound('<h1>Page not found</h1>')

            prev_page = page - 1 if page > 1 else None
            next_page = page + 1 if page < total_pages else None

            response = JsonResponse(
                {
                    'data': globals.get_page(page, genres, serialize_demo),
                    'page': page,
                    'total_pages': total_pages,
                    'prev_page': prev_page,
                    'next_page': next_page,
                },
                safe=False)

    elif request.method == 'POST':
        """
            name: string
            description: string
        """
        data = json.loads(request.body)
        genre = Genre.objects.create(
            name=data['name'],
            description=data['description'],
            image=data['image'],
        )
        response = JsonResponse(serialize(genre))

    elif request.method == 'PUT':
        """
            name: string
            description: string
            actors: ids of actors
            companies: ids of companies
            films: ids of films
        """
        genre = Genre.objects.get(id=genre_id)
        data = json.loads(request.body)
        update_fields = []

        if data.get('name'):
            genre.name = data['name']
            update_fields.append('name')

        if data.get('description'):
            genre.description = data['description']
            update_fields.append('description')

        if data.get('image'):
            genre.image = data['image']
            update_fields.append('image')

        if data.get('actors'):
            actors = Actor.objects.filter(id__in=data['actors'])
            genre.actors.clear()
            genre.actors.add(*actors)

        if data.get('companies'):
            companies = Company.objects.filter(id__in=data['companies'])
            genre.companies.clear()
            genre.companies.add(*companies)

        if data.get('films'):
            films = Film.objects.filter(id__in=data['films'])
            genre.film_set.clear()
            genre.film_set.add(*films)

        genre.save(update_fields=update_fields)
        response = JsonResponse(serialize(genre))

    elif request.method == 'DELETE':
        genre = Genre.objects.get(id=genre_id)
        genre.delete()
        response = JsonResponse({})

    else:
        response = JsonResponse({})

    response = globals.ser_cors_headers(response)
    return response