test rating

This commit is contained in:
Götz 2017-10-22 00:48:23 +02:00
parent 34a818ecbb
commit c6babd7613
3 changed files with 55 additions and 35 deletions

View File

@ -23,5 +23,6 @@ urlpatterns = [
url(r'^daily/$', views.daily_food, name='daily-food'),
url(r'^weekly/$', views.weekly_food, name='weekly-food'),
url(r'^all/$', views.food, name='all-food'),
url(r'^daily/$', views.daily_food, name='daily-food'),
url(r'^daily/rating/$', views.food_rating, name='rating-food'),
url(r'^weekly/rating/$', views.food_rating, name='rating-food'),
]

View File

@ -6,42 +6,12 @@ import datetime
from django.shortcuts import render
from apps.food.models import Menu, HappyHour, SingleFood
from django.http import HttpResponse
# Create your views here.
def daily_food(request):
print(
"REQUEST------------------------------------------------------------------------------------------------------")
id = request.GET.get('food_id', None)
rating = request.GET.get('rating', None)
print("ID: %s, RATING: %s" % (id, rating))
if id and rating:
food = SingleFood.objects.get(id=id)
if rating == str(1):
print("First Start")
food.first_star = food.first_star + 1
if rating == str(2):
print("First Start")
food.second_star += 1
if rating == str(3):
print("First Start")
food.third_star += 1
if rating == str(4):
print("First Start")
food.fourth_star += 1
if rating == str(5):
print("First Start")
food.fifth_star += 1
global_count = food.first_star + food.second_star + food.third_star + food.fourth_star + food.fifth_star
print("GLOBAL_COUNT: " + str(global_count))
sum = food.first_star * 1 + food.second_star * 2 + food.third_star * 3 + food.fourth_star * 4 + food.fifth_star * 5
print("SUM: " + str(sum))
food.rating = sum / global_count
print("SUMME:------------------" + str(sum / global_count))
food.save()
print("DONE")
today = datetime.datetime.now() - datetime.timedelta(1)
today = datetime.datetime.now() - datetime.timedelta(2)
feki_menu = Menu.objects.filter(date__exact=today).filter(location__contains="Feldkirchenstraße").last()
austr_menu = Menu.objects.filter(date__exact=today).filter(location__contains="Austraße").last()
erba_cafete = Menu.objects.filter(date__exact=today).filter(location__contains="Erba").last()
@ -97,3 +67,27 @@ def all_food(request):
def food(request):
return render(request, "food/home.jinja", {
})
def food_rating(request):
food_id = request.GET.get('food_id', None)
rating = request.GET.get('rating', None)
if food_id and rating:
print("ID: %s, RATING: %s" % (food_id, rating))
food = SingleFood.objects.get(id=food_id)
if rating == str(1):
food.first_star = food.first_star + 1
if rating == str(2):
food.second_star += 1
if rating == str(3):
food.third_star += 1
if rating == str(4):
food.fourth_star += 1
if rating == str(5):
food.fifth_star += 1
global_count = food.first_star + food.second_star + food.third_star + food.fourth_star + food.fifth_star
sum = food.first_star * 1 + food.second_star * 2 + food.third_star * 3 + food.fourth_star * 4 + food.fifth_star * 5
food.rating = sum / global_count
print("SUMME: " + str(sum / global_count))
food.save()
return HttpResponse(status=200)

View File

@ -2,16 +2,22 @@
* Created by michigg on 20.10.17.
*/
document.addEventListener('DOMContentLoaded', rate_init);
/**
* setup page, Add event listener
*/
function rate_init() {
add_Stars();
$('.star').on("mouseenter mouseleave", function () {
showRating(this);
}).on("click", function () {
console.log('Click')
sendRating(this);
})
}
/**
* add rating stars to each food-item
*/
function add_Stars() {
console.log($('.food-item'));
$('.food-item').each(function () {
@ -25,6 +31,12 @@ function add_Stars() {
});
}
/**
* get user Rating
*
* @param obj
* chosen rate star
*/
function showRating(obj) {
splitted_id = $(obj).attr('class').split(' ')[0].split('-');
console.log(splitted_id);
@ -33,6 +45,11 @@ function showRating(obj) {
buildRating(food_id, rating);
}
/**
* build rating-stars for given rating
* @param food_id food which shall be rated
* @param rating int or double between 1-5; count of rating-stars
*/
function buildRating(food_id, rating) {
for (var i = 1; i < 6; i++) {
var icon_id = '.star-' + i + '-' + food_id;
@ -44,18 +61,26 @@ function buildRating(food_id, rating) {
}
}
/**
* Sends user Rating to server
*
* @param obj user Rating
*/
function sendRating(obj) {
console.log('Send');
splitted_id = $(obj).attr('class').split(' ')[0].split('-');
var rating = splitted_id[1];
var food_id = splitted_id[2];
//TODO: Better URL handling
var url = window.location.href;
var url = window.location.href + "rating/";
console.log(url);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
}
};
console.log(url + "?rating=" + rating + "&food_id=" + food_id);
xhttp.open("GET", url + "?rating=" + rating + "&food_id=" + food_id, true);
xhttp.send();
}