Exemplo n.º 1
0
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))
Exemplo n.º 2
0
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})
Exemplo n.º 3
0
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})
Exemplo n.º 4
0
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})
Exemplo n.º 5
0
Arquivo: views.py Projeto: gridl/qexx
def add_rating(request):

    result = {}

    rating_form = RatingForm(request.POST)

    if rating_form.is_valid():
        new_rating = rating_form.save(commit=False)
        new_rating.user = request.user
        new_rating.save()

    result['html'] = render_to_string('ratings/rating_form.html', {
        'rating_form': rating_form,
        'success': rating_form.is_valid()
    })

    return JsonResponse(result)
Exemplo n.º 6
0
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
    })
Exemplo n.º 7
0
def professor_rating(request, professor_id, professor_name):
    if request.method == "GET":
        form = RatingForm()
        prof = Professor.objects.get(id=professor_id, prof_name=professor_name)
        args = {
            'form': form,
            'prof': prof,
        }
        return render(request, 'ratings/professor_rating.html', args)

    if request.method == 'POST':
        form = RatingForm(request.POST)
        if form.is_valid():
            rating = form.save(commit=False)
            prof = Professor.objects.get(id=professor_id)
            rating.prof_rating = prof
            rating.save()
            ratings = Rating.objects.filter(prof_rating=prof).order_by('-date')
            posts = Post.objects.filter(
                rating_post__in=ratings).order_by('-date')
            form = ProfessorForm()
            args = {
                'form': form,
                'posts': posts,
                'prof': prof,
                'ratings': ratings,
            }
            return redirect(
                reverse('searchings:professor',
                        args=(
                            professor_id,
                            professor_name,
                        )))
        else:
            return redirect(
                reverse('searchings:professor',
                        args=(
                            professor_id,
                            professor_name,
                        )))
Exemplo n.º 8
0
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)