def edit_comment(request, comment_id): # anything passed through the url is a potential problem - unquote the comment_id comment_id = urllib.unquote_plus(comment_id) # get the comment, check that the logged in user is the author then render the edit form # any error in this process will return the appropriate error message on the error template try: comment = Comment.objects.get(id=comment_id) 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(request)) form = CommentForm( initial={ 'comment': comment.comment, 'cleanliness': int(comment.cleanliness), 'atmosphere': int(comment.atmosphere), 'overall': int(comment.overall), 'food_quality': int(comment.food_quality) }) return render_to_response('SHIRPI/edit_comment.html', { 'form': form, 'comment': comment }, RequestContext(request)) except Comment.DoesNotExist: return render_to_response('SHIRPI/error.html', {'error': "That comment does not exist"}, RequestContext(request))
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 comment(request, restaurant_name, restaurant_address): restaurant_name = urllib.unquote_plus(restaurant_name) restaurant_address = urllib.unquote_plus(restaurant_address) 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 try: restaurant = Restaurant.objects.get(name__iexact=restaurant_name, address__iexact=restaurant_address) 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)) except Restaurant.DoesNotExist: return render_to_response('SHIRPI/error.html', { 'error': "The restaurant you are trying to comment on does not exist" }, RequestContext(request)) except Comment.DoesNotExist: pass # create comment form and render form = CommentForm() return render_to_response('SHIRPI/comment.html', { 'restaurant': restaurant, '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))
def view_restaurant(request, restaurant_name, restaurant_address): ''' Function : view_restaurant Description : view the information from a specific restaurant Parameter(s) : request - HttpRequest : restaurant_name - the name of the restaurant to display : restaurant_address - the address of the restaurant to display Return : HttpResponse ''' try: # get the restaurant, reports associated with that restaurant and comments restaurant = Restaurant.objects.get( name__iexact=urllib.unquote_plus(restaurant_name), address__iexact=urllib.unquote_plus(restaurant_address)) reports = HealthReport.objects.filter(restaurant=restaurant) comments = Comment.objects.filter( restaurant=restaurant).order_by('-created')[0:5] form = CommentForm() commentable = True if request.user.is_authenticated(): user = request.user ip = None else: user = User.objects.get(username="******") ip = request.META['REMOTE_ADDR'] #context defined here so that isn't ugly context = { 'restaurant': restaurant, 'reports': reports, 'comments': comments } #context defined here so that isn't ugly context = { 'restaurant': restaurant, 'reports': reports, 'comments': comments } #determine if the user can comment, put a form in the context if they can 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(): context['form'] = form else: context['form'] = form return render_to_response("SHIRPI/view_restaurant.html", context, RequestContext(request)) except Restaurant.DoesNotExist: # redirect return HttpResponseRedirect("/cs215/shirpi/")