def save_edit(request, comment_id): # if a form has been submitted if request.method=="POST": # if the form is valid form = CommentForm(request.POST) if form.is_valid(): # try to get the old comment try: comment = Comment.objects.get(id=comment_id) # if this user isn't the author, error if request.user != comment.author and not request.user.has_perm("SHIRPI.comment"): return render_to_response('SHIRPI/error.html', {'error': "You are not the author of this comment"}, RequestContext(requet)) restaurant = comment.restaurant # subtract old comment info from the restaurant totals update_values(restaurant, comment, -1) #put in comment info comment.comment = form.cleaned_data['comment'] comment.cleanliness = form.cleaned_data['cleanliness'] comment.food_quality = form.cleaned_data['food_quality'] comment.atmosphere = form.cleaned_data['atmosphere'] comment.overall = form.cleaned_data['overall'] if comment.cleanliness > MAX_RATING or comment.food_quality > MAX_RATING or comment.atmosphere > MAX_RATING or comment.overall > MAX_RATING: return render_to_response("SHIRPI/error.html", {'error': "You cannot have a comment metric greater than " + str(MAX_RATING)}, RequestContest(request)) comment.combined = comment.cleanliness + comment.food_quality + comment.atmosphere + comment.overall comment.ip = request.META['REMOTE_ADDR'] # update restaurant totals with new values update_values(restaurant, comment, 1) # save comment comment.save() #forward to the restaurant browse return HttpResponseRedirect("/cs215/shirpi/view/"+urllib.quote_plus(restaurant.name.replace("/", "%2F"))+"/"+urllib.quote_plus(restaurant.address.replace("/", "%2F"))+"/") #comment doesn't exist, error except Comment.DoesNotExist: return render_to_response('SHIRPI/error.html', {'error': "Comment does not exist"}, RequestContext(request)) # render the comment edit page return render_to_response('SHIRPI/edit_comment.html', {'form': form}, RequestContext(request))
def save(request, restaurant_name, restaurant_address): restaurant_name = urllib.unquote_plus(restaurant_name) restaurant_address = urllib.unquote_plus(restaurant_address) #if the form was submitted properly if request.method == "POST": #determine if the form is valid comment_form = CommentForm(request.POST) if comment_form.is_valid(): #determine if restaurant exists try: restaurant = Restaurant.objects.get( name__iexact=restaurant_name, address__iexact=restaurant_address) except Restaurant.DoesNotExist: return render_to_response( 'SHIRPI/error.html', {'error': "Restaurant or comment does not exist"}, RequestContext(request)) if request.user.is_authenticated(): user = request.user ip = None else: user = User.objects.get(username="******") ip = request.META['REMOTE_ADDR'] #see if the restaurant exists or error if ip == None: comment_set = Comment.objects.filter( author__username=user.username, restaurant=restaurant).order_by('-created')[:1] else: comment_set = Comment.objects.filter( author__username=user.username, restaurant=restaurant, ip=ip).order_by('-created')[:1] if len(comment_set) > 0: comment = comment_set[0] if comment.created + timedelta(days=1) > datetime.now(): return render_to_response( 'SHIRPI/error.html', { 'error': "You can only comment on a restaurant once per day" }, RequestContext(request)) #create the new comment and associate to the restaurant comment = Comment() comment.restaurant = restaurant #try to attach an author, if that fails make the author Anonymous try: author = User.objects.get(username=request.user.username) except User.DoesNotExist: author = User.objects.get(username="******") comment.author = author cleanliness = comment_form.cleaned_data['cleanliness'] food_quality = comment_form.cleaned_data['food_quality'] atmosphere = comment_form.cleaned_data['atmosphere'] overall = comment_form.cleaned_data['overall'] if cleanliness > MAX_RATING or food_quality > MAX_RATING or atmosphere > MAX_RATING or overall > MAX_RATING: return render_to_response( "SHIRPI/error.html", { 'error': "You cannot have a comment metric greater than " + str(MAX_RATING) }, RequestContest(request)) #assign the comment comment.comment = comment_form.cleaned_data['comment'] #update the restaurant info and comment info/count comment.cleanliness = cleanliness comment.food_quality = food_quality comment.atmosphere = atmosphere comment.overall = overall comment.combined = atmosphere + food_quality + cleanliness + overall # add the new values to the restaurant update_values(restaurant, comment, 1) #set datetime comment.created = datetime.now() comment.last_modified = datetime.now() comment.ip = request.META['REMOTE_ADDR'] #save everything restaurant.save() comment.save() #render response return HttpResponseRedirect( "/cs215/shirpi/view/" + urllib.quote_plus(restaurant_name.replace("/", "%2F")) + "/" + urllib.quote_plus(restaurant_address.replace("/", "%2F")) + "/")
def save(request, restaurant_name, restaurant_address): restaurant_name = urllib.unquote_plus(restaurant_name) restaurant_address = urllib.unquote_plus(restaurant_address) #if the form was submitted properly if request.method=="POST": #determine if the form is valid comment_form = CommentForm(request.POST) if comment_form.is_valid(): #determine if restaurant exists try: restaurant = Restaurant.objects.get(name__iexact=restaurant_name, address__iexact=restaurant_address) except Restaurant.DoesNotExist: return render_to_response('SHIRPI/error.html', {'error': "Restaurant or comment does not exist"}, RequestContext(request)) if request.user.is_authenticated(): user = request.user ip = None else: user = User.objects.get( username = "******" ) ip = request.META['REMOTE_ADDR'] #see if the restaurant exists or error if ip == None: comment_set = Comment.objects.filter(author__username = user.username, restaurant = restaurant).order_by('-created')[:1] else: comment_set = Comment.objects.filter(author__username = user.username, restaurant = restaurant, ip = ip).order_by('-created')[:1] if len(comment_set) > 0: comment = comment_set[0] if comment.created + timedelta(days=1) > datetime.now(): return render_to_response('SHIRPI/error.html', {'error': "You can only comment on a restaurant once per day"}, RequestContext(request)) #create the new comment and associate to the restaurant comment = Comment() comment.restaurant = restaurant #try to attach an author, if that fails make the author Anonymous try: author = User.objects.get(username=request.user.username) except User.DoesNotExist: author = User.objects.get(username="******") comment.author = author cleanliness = comment_form.cleaned_data['cleanliness'] food_quality = comment_form.cleaned_data['food_quality'] atmosphere = comment_form.cleaned_data['atmosphere'] overall = comment_form.cleaned_data['overall'] if cleanliness > MAX_RATING or food_quality > MAX_RATING or atmosphere > MAX_RATING or overall > MAX_RATING: return render_to_response("SHIRPI/error.html", {'error': "You cannot have a comment metric greater than " + str(MAX_RATING)}, RequestContest(request)) #assign the comment comment.comment = comment_form.cleaned_data['comment'] #update the restaurant info and comment info/count comment.cleanliness = cleanliness comment.food_quality = food_quality comment.atmosphere = atmosphere comment.overall = overall comment.combined = atmosphere + food_quality + cleanliness + overall # add the new values to the restaurant update_values(restaurant, comment, 1) #set datetime comment.created = datetime.now() comment.last_modified = datetime.now() comment.ip = request.META['REMOTE_ADDR'] #save everything restaurant.save() comment.save() #render response return HttpResponseRedirect("/cs215/shirpi/view/"+urllib.quote_plus(restaurant_name.replace("/", "%2F") )+ "/" + urllib.quote_plus(restaurant_address.replace("/", "%2F")) + "/")
def save_edit(request, comment_id): # if a form has been submitted if request.method == "POST": # if the form is valid form = CommentForm(request.POST) if form.is_valid(): # try to get the old comment try: comment = Comment.objects.get(id=comment_id) # if this user isn't the author, error if request.user != comment.author and not request.user.has_perm( "SHIRPI.comment"): return render_to_response( 'SHIRPI/error.html', {'error': "You are not the author of this comment"}, RequestContext(requet)) restaurant = comment.restaurant # subtract old comment info from the restaurant totals update_values(restaurant, comment, -1) #put in comment info comment.comment = form.cleaned_data['comment'] comment.cleanliness = form.cleaned_data['cleanliness'] comment.food_quality = form.cleaned_data['food_quality'] comment.atmosphere = form.cleaned_data['atmosphere'] comment.overall = form.cleaned_data['overall'] if comment.cleanliness > MAX_RATING or comment.food_quality > MAX_RATING or comment.atmosphere > MAX_RATING or comment.overall > MAX_RATING: return render_to_response( "SHIRPI/error.html", { 'error': "You cannot have a comment metric greater than " + str(MAX_RATING) }, RequestContest(request)) comment.combined = comment.cleanliness + comment.food_quality + comment.atmosphere + comment.overall comment.ip = request.META['REMOTE_ADDR'] # update restaurant totals with new values update_values(restaurant, comment, 1) # save comment comment.save() #forward to the restaurant browse return HttpResponseRedirect( "/cs215/shirpi/view/" + urllib.quote_plus(restaurant.name.replace("/", "%2F")) + "/" + urllib.quote_plus(restaurant.address.replace("/", "%2F")) + "/") #comment doesn't exist, error except Comment.DoesNotExist: return render_to_response('SHIRPI/error.html', {'error': "Comment does not exist"}, RequestContext(request)) # render the comment edit page return render_to_response('SHIRPI/edit_comment.html', {'form': form}, RequestContext(request))