Пример #1
0
def api_add_rating(request, course_id):
    if request.method == 'POST':
        form = RatingForm(request.POST)
        results = []

        if form.is_valid():
            rating = form.save(commit=False)
            rating.user = UserProfile.objects.get(user=int(request.user.id))
            rating.date = datetime.datetime.now()
            try:
                c = Course.objects.get(id=int(course_id))
                rating.course = c
                rating.save()

                #update the course averages
                c.number_of_ratings += 1
                c.average_difficulty = ((c.average_difficulty if c.average_difficulty else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['difficulty_rating'])) \
                                       / Decimal(c.number_of_ratings)

                c.average_teaching = ((c.average_teaching if c.average_teaching else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['teaching_rating'])) \
                                     / Decimal(c.number_of_ratings)

                c.average_materials = ((c.average_materials if c.average_materials else 0) * (c.number_of_ratings - 1) +
                                       Decimal(form.cleaned_data['materials_rating'])) / Decimal(c.number_of_ratings)

                c.average_overall = ((c.average_overall if c.average_overall else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['overall_rating'])) \
                                    / Decimal(c.number_of_ratings)

                c.average_satisfaction = ((c.average_satisfaction if c.average_satisfaction else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['satisfaction_rating'])) \
                                         / Decimal(c.number_of_ratings)

                c.save()

                #response contains averages for the course, and the username of the submitter
                msg = {"data": {
                    "overall": float(c.average_overall),
                    "satisfaction": float(c.average_satisfaction),
                    "difficulty": float(c.average_difficulty),
                    "teaching": float(c.average_teaching),
                    "materials": float(c.average_materials),
                    "ratings": c.number_of_ratings,
                    "username": request.user.username
                }
                }
                results.append(msg)
            except Exception as e:
                return HttpResponseRedirect('/')
        else:
            print form.errors
            errors = {"errors": form.errors
            }
            results.append(errors)
        return HttpResponse(json.dumps(results), content_type="application/json")

    else:
        return course(request, course_id)
Пример #2
0
def add_rating(request, profID):
	if request.method == 'POST':
		form = RatingForm(request.POST)
		if form.is_valid():
			rating = Rating()
			rating.professor = Professor.objects.get(id=profID)
			rating.poster = request.user
			rating.overall = form.cleaned_data['overall']
			rating.humor = form.cleaned_data['humor']
			rating.difficulty = form.cleaned_data['difficulty']
			rating.availability = form.cleaned_data['availability']
			if Rating.objects.filter(poster=request.user).filter(professor=rating.professor).count > 0:
				params = {
					'message': 'You cannot rate a professor multiple times',
				}
				return render(request, 'message.html', params)
			rating.save()
		return HttpResponseRedirect('/accounts/profs/profID/' + profID + '/')
	else:
		form = RatingForm()
		prof = Professor.objects.get(id=profID)
		params = {
			'form': form,
			'formTitle': 'Add a rating for ' + str(prof.firstName) + ' ' + str(prof.lastName),
		}
		return render(request, 'profSearch.html', params)
Пример #3
0
    def test_form_fails_with_invalid_data(self):
        """Test that form will fail if given invalid data."""

        form_data = {
            'score': 11,
            'beer_name': "Test Beer",
            'brewer_name': "Test Co",
            'notes': "Test Notes"
        }
        form = RatingForm(data=form_data)
        self.assertFalse(form.is_valid())
Пример #4
0
def process_rating(request):
    if request.method == "POST":
        form = RatingForm(request.POST)
        if form.is_valid():
            practice_item = get_object_or_404(FlashCard,
                                              pk=int(form.cleaned_data['id']))
            practice_item.set_next_practice(int(form.cleaned_data['rating']))
            practice_item.save()

            return redirect(to='list_flashcards')
    else:
        return HttpResponseBadRequest("There's nothing here. Haha.")
Пример #5
0
def process_rating(request):
    if request.method == "POST":
        form = RatingForm(request.POST)
        if form.is_valid():
            practice_item = get_object_or_404(FlashCard,
                                              pk=int(form.cleaned_data['id']))
            practice_item.set_next_practice(int(form.cleaned_data['rating']))
            practice_item.save()

            return redirect(to='list_flashcards')
    else:
        return HttpResponseBadRequest("There's nothing here. Haha.")
Пример #6
0
    def test_form_data_is_valid(self):
        """Test that rating form is working."""

        form_data = {
            'score': 9,
            'beer_name': "Test Beer",
            'brewer_name': "Test Co",
            'notes': "Test Notes"
        }
        form = RatingForm(data=form_data)
        self.assertTrue(form.is_valid())
        rating = form.save()
        self.assertEqual(rating.score, 9)
        self.assertEqual(rating.beer_name, "Test Beer")
        self.assertEqual(rating.brewer_name, "Test Co")
        self.assertEqual(rating.notes, "Test Notes")
Пример #7
0
def demo_rate(request, demoid):
    context = RequestContext(request)
    context_dict ={}
    t = get_team(request.user)
    add_isteam_to_context_dict(context_dict, t)

    rater = get_rater(request.user)

    if rater:
        if request.method == 'POST':
            rating_form = RatingForm(data=request.POST)
            if rating_form.is_valid():
                demo = Demo.objects.get(id=demoid)

                # check if the rater has already rated this app, if so, remove this entry and replace with new rating
                rating_check = Rating.objects.filter(rater=rater,demo=demo).count()
                if rating_check == 1:
                    past_rating = Rating.objects.get(rater=rater,demo=demo)
                    demo.rating_count = demo.rating_count - 1
                    demo.rating_sum = demo.rating_sum - past_rating.score
                    past_rating.delete()
                    demo.save()

                rating = rating_form.save(commit=False)
                rating.rater = rater
                rating.demo = demo
                rating.save()
                demo.rating_count = demo.rating_count + 1
                demo.rating_sum = demo.rating_sum + rating.score
                demo.save()

                return HttpResponseRedirect('/showcase/demo/show/'+str(demoid)+'/')
            else:
                print rating_form.errors
        else:
            rating_form = RatingForm()

            context_dict['rating_form']=rating_form
            return render_to_response(
            'showcase/demo.html',
            context_dict,
            context)

    return HttpResponseRedirect('/showcase/demo/show/'+str(demoid)+'/')
Пример #8
0
def demo_rate(request, demoid):
    context = RequestContext(request)
    context_dict = {}
    t = get_team(request.user)
    add_isteam_to_context_dict(context_dict, t)

    rater = get_rater(request.user)

    if rater:
        if request.method == 'POST':
            rating_form = RatingForm(data=request.POST)
            if rating_form.is_valid():
                demo = Demo.objects.get(id=demoid)

                # check if the rater has already rated this app, if so, remove this entry and replace with new rating
                rating_check = Rating.objects.filter(rater=rater,
                                                     demo=demo).count()
                if rating_check == 1:
                    past_rating = Rating.objects.get(rater=rater, demo=demo)
                    demo.rating_count = demo.rating_count - 1
                    demo.rating_sum = demo.rating_sum - past_rating.score
                    past_rating.delete()
                    demo.save()

                rating = rating_form.save(commit=False)
                rating.rater = rater
                rating.demo = demo
                rating.save()
                demo.rating_count = demo.rating_count + 1
                demo.rating_sum = demo.rating_sum + rating.score
                demo.save()

                return HttpResponseRedirect('/showcase/demo/show/' +
                                            str(demoid) + '/')
            else:
                print rating_form.errors
        else:
            rating_form = RatingForm()

            context_dict['rating_form'] = rating_form
            return render_to_response('showcase/demo.html', context_dict,
                                      context)

    return HttpResponseRedirect('/showcase/demo/show/' + str(demoid) + '/')
Пример #9
0
    def test_edit_rating(self):
        """Test that edit rating feature is working by checking database values before and after edit of beer name."""

        rating = create_and_save_rating()
        rating_in_db = Rating.objects.filter(pk=rating.pk).exists()
        self.assertEqual(rating.beer_name, "Test Beer")

        response = self.client.post(reverse('rating_edit', args=(rating.pk, )))

        form_data = {
            'score': 9,
            'beer_name': "Testing Edit",
            'brewer_name': "Test Co",
            'notes': "Test Notes"
        }
        form = RatingForm(data=form_data)
        self.assertTrue(form.is_valid())
        rating = form.save()

        self.assertEqual(rating_in_db, True)
        self.assertEqual(rating.beer_name, "Testing Edit")
        self.assertEqual(response.status_code, 200)
Пример #10
0
def api_add_rating(request, course_id):
    if request.method == 'POST':
        form = RatingForm(request.POST)
        results = []

        if form.is_valid():
            rating = form.save(commit=False)
            rating.user = UserProfile.objects.get(user=int(request.user.id))
            rating.date = datetime.datetime.now()
            try:
                c = Course.objects.get(id=int(course_id))
                rating.course = c
                rating.save()

                #update the course averages
                c.number_of_ratings += 1
                c.average_difficulty = ((c.average_difficulty if c.average_difficulty else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['difficulty_rating'])) \
                                       / Decimal(c.number_of_ratings)

                c.average_teaching = ((c.average_teaching if c.average_teaching else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['teaching_rating'])) \
                                     / Decimal(c.number_of_ratings)

                c.average_materials = (
                    (c.average_materials if c.average_materials else 0) *
                    (c.number_of_ratings - 1) +
                    Decimal(form.cleaned_data['materials_rating'])) / Decimal(
                        c.number_of_ratings)

                c.average_overall = ((c.average_overall if c.average_overall else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['overall_rating'])) \
                                    / Decimal(c.number_of_ratings)

                c.average_satisfaction = ((c.average_satisfaction if c.average_satisfaction else 0) * (
                    c.number_of_ratings - 1) + Decimal(form.cleaned_data['satisfaction_rating'])) \
                                         / Decimal(c.number_of_ratings)

                c.save()

                #response contains averages for the course, and the username of the submitter
                msg = {
                    "data": {
                        "overall": float(c.average_overall),
                        "satisfaction": float(c.average_satisfaction),
                        "difficulty": float(c.average_difficulty),
                        "teaching": float(c.average_teaching),
                        "materials": float(c.average_materials),
                        "ratings": c.number_of_ratings,
                        "username": request.user.username
                    }
                }
                results.append(msg)
            except Exception as e:
                return HttpResponseRedirect('/')
        else:
            print form.errors
            errors = {"errors": form.errors}
            results.append(errors)
        return HttpResponse(json.dumps(results),
                            content_type="application/json")

    else:
        return course(request, course_id)