<s> from django . shortcuts import get_object_or_404 , render <EOL> from django . http import HttpResponseRedirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib . auth . models import User <EOL> from . models import Review , Wine , Cluster <EOL> from . forms import ReviewForm <EOL> from . suggestions import update_clusters <EOL> import datetime <EOL> from django . contrib . auth . decorators import login_required <EOL> def review_list ( request ) : <EOL> latest_review_list = Review . objects . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:9> ] <EOL> context = { '<STR_LIT>' : latest_review_list } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def review_detail ( request , review_id ) : <EOL> review = get_object_or_404 ( Review , pk = review_id ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : review } ) <EOL> def wine_list ( request ) : <EOL> wine_list = Wine . objects . order_by ( '<STR_LIT>' ) <EOL> context = { '<STR_LIT>' : wine_list } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> def wine_detail ( request , wine_id ) : <EOL> wine = get_object_or_404 ( Wine , pk = wine_id ) <EOL> form = ReviewForm ( ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : wine , '<STR_LIT>' : form } ) <EOL> @ login_required <EOL> def add_review ( request , wine_id ) : <EOL> wine = get_object_or_404 ( Wine , pk = wine_id ) <EOL> form = ReviewForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> rating = form . cleaned_data [ '<STR_LIT>' ] <EOL> comment = form . cleaned_data [ '<STR_LIT>' ] <EOL> user_name = request . user . username <EOL> review = Review ( ) <EOL> review . wine = wine <EOL> review . user_name = user_name <EOL> review . rating = rating <EOL> review . comment = comment <EOL> review . pub_date = datetime . datetime . now ( ) <EOL> review . save ( ) <EOL> update_clusters ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( wine . id , ) ) ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : wine , '<STR_LIT>' : form } ) <EOL> def user_review_list ( request , username = None ) : <EOL> if not username : <EOL> username = request . user . username <EOL> latest_review_list = Review . objects . filter ( user_name = username ) . order_by ( '<STR_LIT>' ) <EOL> context = { '<STR_LIT>' : latest_review_list , '<STR_LIT:username>' : username } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ login_required <EOL> def user_recommendation_list ( request ) : <EOL> user_reviews = Review . objects . filter ( user_name = request . user . username ) . prefetch_related ( '<STR_LIT>' ) <EOL> user_reviews_wine_ids = set ( map ( lambda x : x . wine . id , user_reviews ) ) <EOL> try : <EOL> user_cluster_name = User . objects . get ( username = request . user . username ) . cluster_set . first ( ) . name <EOL> except : <EOL> update_clusters ( ) <EOL> user_cluster_name = User . objects . get ( username = request . user . username ) . cluster_set . first ( ) . name <EOL> user_cluster_other_members = Cluster . objects . get ( name = user_cluster_name ) . users . exclude ( username = request . user . username ) . all ( ) <EOL> other_members_usernames = set ( map ( lambda x : x . username , user_cluster_other_members ) ) <EOL> other_users_reviews = Review . objects . filter ( user_name__in = other_members_usernames ) . exclude ( wine__id__in = user_reviews_wine_ids ) <EOL> other_users_reviews_wine_ids = set ( map ( lambda x : x . wine . id , other_users_reviews ) ) <EOL> wine_list = sorted ( <EOL> list ( Wine . objects . filter ( id__in = other_users_reviews_wine_ids ) ) , <EOL> key = lambda x : x . average_rating , <EOL> reverse = True <EOL> ) <EOL> return render ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT:username>' : request . user . username , '<STR_LIT>' : wine_list } <EOL> ) </s>
