from .models import Product, Ingredient, Rating
from .forms import ReviewForm, RatingForm
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView
from django.views.generic.base import View


class ProductsView(ListView):
    """Список продуктов"""
    model = Product
    queryset = Product.objects.filter(draft=False)


class ProductDetailView(DetailView):
    """Полное описание продукта"""
    model = Product
    slug_field = "url"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["star_form"] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""
    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get("parent", None):
                form.parent_id = int(request.POST.get("parent"))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = "name"


class AddStarRating(View):
    """Добавление рейтинга продуктц"""
    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(
                ip=self.get_client_ip(request),
                movie_id=int(request.POST.get("movie")),
                defaults={'star_id': int(request.POST.get("star"))}
            )
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)
2823d4c31690d3a461f19ca30120cfb94b69d77b
<mask token>


class ProductDetailView(DetailView):
    <mask token>
    model = Product
    slug_field = 'url'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['star_form'] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""

    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get('parent', None):
                form.parent_id = int(request.POST.get('parent'))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = 'name'


class AddStarRating(View):
    """Добавление рейтинга продуктц"""

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(ip=self.get_client_ip(request),
                movie_id=int(request.POST.get('movie')), defaults={
                'star_id': int(request.POST.get('star'))})
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)

<mask token>


class ProductDetailView(DetailView):
    """Полное описание продукта"""
    model = Product
    slug_field = 'url'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['star_form'] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""

    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get('parent', None):
                form.parent_id = int(request.POST.get('parent'))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = 'name'


class AddStarRating(View):
    """Добавление рейтинга продуктц"""

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(ip=self.get_client_ip(request),
                movie_id=int(request.POST.get('movie')), defaults={
                'star_id': int(request.POST.get('star'))})
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)

<mask token>


class ProductsView(ListView):
    <mask token>
    model = Product
    queryset = Product.objects.filter(draft=False)


class ProductDetailView(DetailView):
    """Полное описание продукта"""
    model = Product
    slug_field = 'url'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['star_form'] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""

    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get('parent', None):
                form.parent_id = int(request.POST.get('parent'))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = 'name'


class AddStarRating(View):
    """Добавление рейтинга продуктц"""

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(ip=self.get_client_ip(request),
                movie_id=int(request.POST.get('movie')), defaults={
                'star_id': int(request.POST.get('star'))})
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)

<mask token>


class ProductsView(ListView):
    """Список продуктов"""
    model = Product
    queryset = Product.objects.filter(draft=False)


class ProductDetailView(DetailView):
    """Полное описание продукта"""
    model = Product
    slug_field = 'url'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['star_form'] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""

    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get('parent', None):
                form.parent_id = int(request.POST.get('parent'))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = 'name'


class AddStarRating(View):
    """Добавление рейтинга продуктц"""

    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(ip=self.get_client_ip(request),
                movie_id=int(request.POST.get('movie')), defaults={
                'star_id': int(request.POST.get('star'))})
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)

from .models import Product, Ingredient, Rating
from .forms import ReviewForm, RatingForm
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.views.generic import ListView, DetailView
from django.views.generic.base import View


class ProductsView(ListView):
    """Список продуктов"""
    model = Product
    queryset = Product.objects.filter(draft=False)


class ProductDetailView(DetailView):
    """Полное описание продукта"""
    model = Product
    slug_field = "url"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["star_form"] = RatingForm()
        return context


class AddReview(View):
    """Отзывы"""
    def post(self, request, pk):
        form = ReviewForm(request.POST)
        product = Product.objects.get(id=pk)
        if form.is_valid():
            form = form.save(commit=False)
            if request.POST.get("parent", None):
                form.parent_id = int(request.POST.get("parent"))
            form.product = product
            form.save()
        return redirect(product.get_absolute_url())


class IngredientView(DetailView):
    """Вывод информации об ингридиенте"""
    model = Ingredient
    template_name = 'products/ingredient.html'
    slug_field = "name"


class AddStarRating(View):
    """Добавление рейтинга продуктц"""
    def get_client_ip(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

    def post(self, request):
        form = RatingForm(request.POST)
        if form.is_valid():
            Rating.objects.update_or_create(
                ip=self.get_client_ip(request),
                movie_id=int(request.POST.get("movie")),
                defaults={'star_id': int(request.POST.get("star"))}
            )
            return HttpResponse(status=201)
        else:
            return HttpResponse(status=400)