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)
def create_and_save_rating(): """Creates a new rating and saves it in the database.""" form_data = { 'score': 9, 'beer_name': "Test Beer", 'brewer_name': "Test Co", 'notes': "Test Notes" } form = RatingForm(data=form_data) rating = form.save() return rating
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")
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)+'/')
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) + '/')
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)
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)