def rating_edit(request, pk): """Allow user to edit rating.""" rating = get_object_or_404(Rating, pk=pk) if request.method == "POST": form = RatingForm(request.POST, instance=rating) if form.is_valid(): post = form.save() post.save() return redirect('rating_detail', pk=rating.pk) else: form = RatingForm(instance=rating) return render(request, 'ratings/rating_edit.html', {'form': form})
def home(request): """ Show the entry point to the ratings app :param request: Django request object :return: rendered homepage """ form = RatingForm() if request.method == "POST": form = RatingForm(request.POST) if form.is_valid(): rating = form.save() rating.save() return redirect('rating_detail', pk=rating.pk) else: print form.errors return render(request, 'ratings/home.html', {'ratings': Rating.objects.all().order_by('-score'), 'form': form})
def rater_page(request, rater_id): try: rater = Rater.objects.get(rater=rater_id) rated_mov = Rater.movies_rated(rater) except Movie.DoesNotExist: raise Http404 context = {'rater': rater, 'rated_movies': rated_mov} if request.user == rater.user: if request.POST: rate_instance = Rating(rater=rater) form = RatingForm(request.POST, instance=rate_instance) if form.is_valid(): form.save() return HttpResponseRedirect(reverse("best_movies")) form = RatingForm() context['form'] = form return render_to_response('ratings/rater.html', context, context_instance=RequestContext(request))
def movie_view(request, movie_id): movie = Movie.objects.get(id=movie_id) genres = Movie_Genre.objects.filter(movie=movie) tags = Tag.objects.filter(id=movie_id) link = Link.objects.get(movie=movie) if request.POST: if "delete" in request.POST: Rating.objects.get(movie=movie, rater=request.user.rater).delete() else: Rating.objects.get_or_create(movie=movie, rater=request.user.rater, defaults={"rating": request.POST["rating"], "timestamp": int(datetime.now().timestamp())}) movie.calculate_average_rating() avg_rating = round(movie.average_rating, 1) context = {"movie": movie, "tags": tags, "genres": genres, "link": link, "avg_rating": avg_rating} if request.user: rating = Rating.objects.filter(movie=movie, rater=request.user.rater) context["form"] = RatingForm() if rating: context["rating"] = rating[0] context["form"].rating = rating[0] return render_to_response("movies.html", context, context_instance=RequestContext(request))
def rating(context, *args, **kwargs): """ {% load rating %} {% rating %} """ obj = kwargs.get('object') rating_only = kwargs.get('rating_only') user = None request = context['request'] if request.user.is_authenticated: user = request.user app_label = obj._meta.app_label model_name = obj._meta.model_name if app_label == "playlists": if model_name == "movieproxy" or "tvshowproxy": model_name = "playlist" c_type = ContentType.objects.get(app_label=app_label, model=model_name) avg_rating = Rating.objects.filter(content_type=c_type, object_id=obj.id).rating() context = {'value': avg_rating, 'form': None} display_form = False if user is not None: display_form = True if rating_only is True: display_form = False if display_form: context['form'] = RatingForm( initial={ "object_id": obj.id, 'content_type_id': c_type.id, "next": request.path }) return context
def home(request): """ Show the entry point to the ratings app :param request: Django request object :return: rendered homepage """ form = RatingForm() if request.method == "POST": form = RatingForm(request.POST) if form.is_valid(): rating = form.save() rating.save() return redirect('rating_detail', pk=rating.pk) else: print form.errors return render(request, 'ratings/home.html', { 'ratings': Rating.objects.all().order_by('-score'), 'form': form })
def get_ratings_form(product): return RatingForm(initial={'product': product})
def item_detail(request, slug): context = {} try: item = Item.objects.get(is_deleted=False, slug=slug) item_stock_count = item.stock_record.quantity except Item.DoesNotExist: item = None item_stock_count = None except Item.MultipleObjectsReturned: qs = Item.objects.filter(slug=slug, is_deleted=False) item = qs.first() item_stock_count = item.stock_record.quantity context['item_count'] = item_stock_count context['quantity'] = range(1, item_stock_count + 1) if item: cart_obj, new_obj = Cart.objects.new_or_get(request) context['cart'] = cart_obj if cart_obj: context['items'] = cart_obj.cart_items.all() form = CommentForm(request.POST or None) quantity_form = forms.QuantityForm(request.POST or None) rating_form = RatingForm(request.POST or None) if request.method == 'POST': if quantity_form.is_valid(): data = quantity_form.save(commit=False) data.cart = cart_obj data.item = item data.save() item.stock_record.quantity -= decimal.Decimal(data.quantity) item.stock_record.save() item.save() request.session['item_count'] = cart_obj.cart_items.all( ).count() messages.success(request, 'Item Added to Cart') return redirect('carts:cart') if rating_form.is_valid(): rating = rating_form.save(commit=False) rating.item = item rating.fiscal_year = FiscalYear.get_active_fiscal_year() if request.user.is_authenticated: if request.user.is_customer(): rating.user = request.user else: if request.session['guest_email_id']: guest_email_id = request.session['guest_email_id'] try: g_mail = GuestEmail.objects.get(id=guest_email_id) except GuestEmail.MultipleObjectsReturned: g_mail = GuestEmail.objects.filter( id=guest_email_id).last() except GuestEmail.DoesNotExist: g_mail = None rating.guest_user = g_mail rating.save() return HttpResponseRedirect("") context['form'] = form context['rating_form'] = rating_form context['quantity_form'] = quantity_form try: data = CartItem.objects.get(item=item, cart=cart_obj) if data: context['data'] = 1 except CartItem.MultipleObjectsReturned: data = CartItem.objects.filter(item=item, cart=cart_obj).first() if data: context['data'] = 1 except CartItem.DoesNotExist: data = None if data: context['data'] = 1 else: context['data'] = 0 comments = Comment.objects.filter(item=item).order_by('-created_at') related_products = Item.objects.filter(is_deleted=False, category=item.category) context['related_products'] = related_products context['comments'] = comments ratings5 = Rating.objects.filter(is_deleted=False, item=item, rating=5).count() ratings4 = Rating.objects.filter(is_deleted=False, item=item, rating=4).count() ratings3 = Rating.objects.filter(is_deleted=False, item=item, rating=3).count() ratings2 = Rating.objects.filter(is_deleted=False, item=item, rating=2).count() ratings1 = Rating.objects.filter(is_deleted=False, item=item, rating=1).count() context['ratings5'] = ratings5 context['ratings4'] = ratings4 context['ratings3'] = ratings3 context['ratings2'] = ratings2 context['ratings1'] = ratings1 if request.user.is_authenticated: if request.user.is_customer(): try: is_rated = Rating.objects.get(is_deleted=False, item=item, user=request.user) except Rating.DoesNotExist: is_rated = None if is_rated: context['rate'] = is_rated context['is_rated'] = True try: ratings = Rating.objects.filter(item=item, is_deleted=False) except: ratings = None if ratings: total_ratings = ratings.aggregate(Sum('rating')) rating_total = total_ratings['rating__sum'] avg_rating = rating_total / ratings.count() rating_percentage = (avg_rating / 5) * 100 print(rating_percentage) context['rating_percentage'] = rating_percentage context['avg_rating'] = avg_rating context['rating_total'] = rating_total context['item'] = item template_name = 'items/item_detail.html' return render(request, template_name, context)