def countrydetail(request, pk): context = {} country = Country.objects.get(pk=pk) context['country'] = country if request.method == 'POST': form = CreateReviewForm(request.POST) context['form'] = form if form.is_valid(): review = form.save(comit=False) review.date = datetime.now() review.country = country review.user =request.user review.save() return redirect('/countrylist/') return render(request, 'country_detail.html', context)
def find_reviews_for_snack(filters): """ Find all reviews given filter For overall rating, and the metrics, all reviews with greater or equal to the given value will be returned Results currently ordered by descending overall rating /snack_reviews/snack_id=abc+overall_rating=3... """ print(f"FILTER IN FIND_REVIEWS {filters}\n", file=sys.stdout) all_filters = filters.split("+") print(f"{all_filters}\n", file=sys.stdout) queryset = Review.objects snack_query = None reviewed = False # all reviews will be returned if nothing specified if "=" in filters: for individual_filter in all_filters: this_filter = individual_filter.split("=") query_index = this_filter[0] query_value = this_filter[1] if query_index == "user_id": queryset = queryset.filter(user_id=query_value) elif query_index == "snack_id": queryset = queryset.filter(snack_id=query_value) snack_query = Snack.objects(id=query_value) elif query_index == "overall_rating": queryset = queryset.filter(overall_rating__gte=query_value) elif query_index == "geolocation": queryset = queryset.filter(geolocation=query_value) elif query_index == "sourness": queryset = queryset.filter(sourness__gte=query_value) elif query_index == "spiciness": queryset = queryset.filter(spiciness__gte=query_value) elif query_index == "bitterness": queryset = queryset.filter(bitterness__gte=query_value) elif query_index == "sweetness": queryset = queryset.filter(sweetness__gte=query_value) elif query_index == "saltiness": queryset = queryset.filter(saltiness__gte=query_value) num_reviews_to_display = 15 # Display a maximum 'num_reviews_to_display' queryset = queryset.order_by("-description", "-overall_rating") print(f"snack_reviews: {queryset}", file=sys.stdout) print(f"snack_reviews: {snack_query}", file=sys.stdout) review_form = CreateReviewForm(request.form) if current_user.is_authenticated: print(len(queryset.filter(user_id=current_user.id))) if len(queryset.filter(user_id=current_user.id)): reviewed = True # Get best and worst reviews! queryset_list = list(queryset) if len(queryset_list) >= 2 * int(1 + num_reviews_to_display / 2): # If we have enough reviews, divide queryset_list = queryset_list[:int(1 + num_reviews_to_display / 2)] + queryset_list[::-1][:int( 1 + num_reviews_to_display / 2)] else: # Just get whatever we can from the best queryset_list = queryset_list[:num_reviews_to_display] # Return results in a table, the metrics such as sourness are not displayed because if they are null, they give # the current simple front end table an error, but it is there for use # Finds reviewers first and last name using their ids # Stored in username as first_name + last_name usernames = {} users = User.objects for review in queryset_list[:num_reviews_to_display]: user_id = review.user_id user = users.with_id(user_id) usernames[user_id] = user.first_name + " " + user.last_name context_dict = { "title": "Delicious Snack", "form": review_form, "query": snack_query, "num_reviews_to_display": num_reviews_to_display, "reviews": queryset_list, "reviewed": reviewed, "usernames": usernames, "user": current_user } return render_template('reviews_for_snack.html', **context_dict)
def create_review(snack): # check authenticated if not current_user.is_authenticated: return redirect(url_for('index')) print(f"is_authenticated\n", file=sys.stdout) review_form = CreateReviewForm(request.form) # post to db if request.method == "POST": user_id = current_user.id snack_id = snack.split('=')[1] snackObject = Snack.objects(id=snack_id) saltiness_review = request.form['saltiness'] sweetness_review = request.form['sweetness'] spiciness_review = request.form['spiciness'] bitterness_review = request.form['bitterness'] sourness_review = request.form['sourness'] overall_rating_review = request.form['overall_rating'] # check if metric review if saltiness_review == 0 and sourness_review == 0 and spiciness_review == 0 \ and bitterness_review == 0 and sweetness_review == 0: try: # user_id comes from current_user # snack_id should come from request sent by frontend # geolocation is found by request new_review = Review(user_id=user_id, snack_id=snack_id, description=request.form['description'], geolocation=request.form['review_country'], overall_rating=overall_rating_review) new_review.save() avg_overall_rating = Review.objects.filter( snack_id=snack_id).average('overall_rating') snackObject.update(set__avg_overall_rating=avg_overall_rating) review_count = snackObject[0].review_count + 1 snackObject.update(set__review_count=review_count) if review_count > 10: snackObject.update(set__is_verified=True) snackObject.update(add_to_set__available_at_locations=request. form['review_country']) except Exception as e: raise Exception( f"Error {e}. \n Couldn't add review {new_review},\n with following review form: {review_form}" ) print(f"A new user submitted the review form: {user_id}", file=sys.stdout) return redirect(url_for('find_reviews_for_snack', filters=snack)) else: try: # user_id comes from current_user # snack_id should come from request sent by frontend # geolocation is found by request snack_metric_review = MetricReview( user_id=user_id, snack_id=snack_id, description=request.form['description'], geolocation=request.form['review_country'], overall_rating=overall_rating_review, sourness=sourness_review, spiciness=spiciness_review, saltiness=saltiness_review, bitterness=bitterness_review, sweetness=sweetness_review) snack_metric_review.save() avg_overall_rating = Review.objects.filter( snack_id=snack_id).average('overall_rating') avg_sourness = Review.objects.filter \ (Q(snack_id=snack_id) & Q(sourness__exists=True)).average("sourness") avg_spiciness = Review.objects.filter \ (Q(snack_id=snack_id) & Q(spiciness__exists=True)).average("spiciness") avg_bitterness = Review.objects.filter \ (Q(snack_id=snack_id) & Q(bitterness__exists=True)).average("bitterness") avg_sweetness = Review.objects.filter \ (Q(snack_id=snack_id) & Q(sweetness__exists=True)).average("sweetness") avg_saltiness = Review.objects.filter \ (Q(snack_id=snack_id) & Q(saltiness__exists=True)).average("saltiness") snackObject.update(set__avg_overall_rating=avg_overall_rating) snackObject.update(set__avg_sourness=avg_sourness) snackObject.update(set__avg_spiciness=avg_spiciness) snackObject.update(set__avg_bitterness=avg_bitterness) snackObject.update(set__avg_sweetness=avg_sweetness) snackObject.update(set__avg_saltiness=avg_saltiness) review_count = snackObject[0].review_count + 1 snackObject.update(set__review_count=review_count) if review_count > 10: snackObject.update(set__is_verified=True) snackObject.update(add_to_set__available_at_locations=request. form['review_country']) except Exception as e: raise Exception( f"Error {e}. \n Couldn't add metric review {snack_metric_review},\n with following review form: {review_form}" ) print(f"A new user submitted the review form: {user_id}", file=sys.stdout) return redirect(url_for('find_reviews_for_snack', filters=snack)) context_dict = { "title": "Create Review", "form": review_form, "user": current_user } # frontend stuff return render_template("reviews_for_snack.html", **context_dict)
def find_reviews_for_snack(filters): """ Find all reviews given filter For overall rating, and the metrics, all reviews with greater or equal to the given value will be returned Results currently ordered by descending overall rating /snack_reviews/snack_id=abc+overall_rating=3... """ all_filters = filters.split("+") print(f"{all_filters}\n", file=sys.stdout) queryset = Review.objects snack_query = None reviewed = False verified = False # all reviews will be returned if nothing specified if "=" in filters: for individual_filter in all_filters: this_filter = individual_filter.split("=") query_index = this_filter[0] query_value = this_filter[1] if query_index == "user_id": queryset = queryset.filter(user_id=query_value) elif query_index == "snack_id": queryset = queryset.filter(snack_id=query_value) snack_query = Snack.objects(id=query_value) if Snack.objects(id=query_value)[0].is_verified: verified = True elif query_index == "overall_rating": queryset = queryset.filter(overall_rating__gte=query_value) elif query_index == "geolocation": queryset = queryset.filter(geolocation=query_value) elif query_index == "sourness": queryset = queryset.filter(sourness__gte=query_value) elif query_index == "spiciness": queryset = queryset.filter(spiciness__gte=query_value) elif query_index == "bitterness": queryset = queryset.filter(bitterness__gte=query_value) elif query_index == "sweetness": queryset = queryset.filter(sweetness__gte=query_value) elif query_index == "saltiness": queryset = queryset.filter(saltiness__gte=query_value) queryset = queryset.order_by("-overall_rating") print(f"snack_reviews: {queryset}", file=sys.stdout) print(f"snack_reviews: {snack_query}", file=sys.stdout) review_form = CreateReviewForm(request.form) if current_user.is_authenticated: if len(queryset.filter(user_id=current_user.id)): reviewed = True # Return results in a table, the metrics such as sourness are not displayed because if they are null, they give # the current simple front end table an error, but it is there for use context_dict = { "title": "Delicious Snack", "form": review_form, "query": snack_query, "reviews": queryset, "reviewed": reviewed, "verified": verified, "user": current_user } return render_template('reviews_for_snack.html', **context_dict)