def comment(request, template="generic/comments.html"): """ Handle a ``ReviewForm`` submission and redirect back to its related object. """ response = initial_validation(request, "comment") if isinstance(response, HttpResponse): return response obj, post_data = response form = ReviewForm(request, obj, request.POST ) if form.is_valid(): url = obj.get_absolute_url() if is_spam(request, form, url): return redirect(url) comment = form.save(request) response = redirect(add_cache_bypass(comment.get_absolute_url())) # Store commenter's details in a cookie for 90 days. for field in ReviewForm.cookie_fields: cookie_name = ReviewForm.cookie_prefix + field cookie_value = post_data.get(field, "") set_cookie(response, cookie_name, cookie_value) """ Send activity feed to those who follow this vendor page. """ if request.user.is_authenticated(): action.send(obj, verb=settings.GOT_REVIEW_VERB, target=comment ) return response elif request.is_ajax() and form.errors: return HttpResponse(dumps({"errors": form.errors})) # Show errors with stand-alone comment form. context = {"obj": obj, "posted_comment_form": form} response = render(request, template, context) return response
def edit_review(request, review_id, template="generic/includes/write_review.html"): if not review_id : raise Http404() review_obj = Review.objects.get(id=review_id) if review_obj and review_obj.user != request.user: raise Http404() context = RequestContext(request) parent_obj = review_obj.content_object if request.method == 'POST': form = ReviewForm(request, parent_obj, request.POST ) if form.is_valid(): url = review_obj.get_absolute_url() if is_spam(request, form, url): return redirect(url) review_obj.comment = form.cleaned_data['comment'] review_obj.title = form.cleaned_data['title'] review_obj.overall_value = form.cleaned_data['overall_value'] review_obj.price_value = form.cleaned_data['price_value'] review_obj.website_ex_value = form.cleaned_data['website_ex_value'] review_obj.quality_value = form.cleaned_data['quality_value'] review_obj.service_value = form.cleaned_data['service_value'] exchange_value = form.cleaned_data['exchange_value'] """ exchange_value is not a required field. Can contain null data as well. Therefore need to handle it seperately. """ if exchange_value == '': review_obj.exchange_value = None else: review_obj.exchange_value = exchange_value review_obj.shop_again = form.cleaned_data['shop_again'] blog_category = form.cleaned_data['category'] bought_category = None try: bought_category = BlogCategory.objects.get(slug=slugify(blog_category)) except: pass try: reviewRatingObj = RequiredReviewRating.objects.get(commentid=review_obj.id) reviewRatingObj.overall_value = review_obj.overall_value reviewRatingObj.price_value = review_obj.price_value reviewRatingObj.website_ex_value = review_obj.website_ex_value reviewRatingObj.quality_value = review_obj.quality_value reviewRatingObj.service_value = review_obj.service_value reviewRatingObj.shop_again = review_obj.shop_again reviewRatingObj.save() optReviewRatingObj = OptionalReviewRating.objects.get(commentid=review_obj.id) optReviewRatingObj.exchange_value = review_obj.service_value optReviewRatingObj.save() except: pass review_obj.save() if bought_category: for blog_category in review_obj.bought_category.all(): review_obj.bought_category.remove(blog_category) review_obj.bought_category.add(bought_category) if request.is_ajax(): template = 'generic/includes/comment_ajax.html' review_page = request.GET.get('reviewpage', '0') if review_page == '1': template = 'generic/includes/review_ajax.html' html = render_to_string(template, { 'comment': review_obj, 'request':request }) res = { 'html': html, 'store' : review_obj.content_object.title, 'success':True} response = HttpResponse( simplejson.dumps(res), 'application/json' ) else: response = redirect(add_cache_bypass(review_obj.get_absolute_url())) return response elif form.errors: return HttpResponse(simplejson.dumps({"errors": form.errors}), 'application/json' ) else: data = { "comment" : review_obj.comment, "title" : review_obj.title, "overall_value" : review_obj.overall_value, "price_value" : review_obj.price_value, "website_ex_value" : review_obj.website_ex_value, "quality_value" : review_obj.quality_value, "service_value" : review_obj.service_value, "exchange_value" : review_obj.exchange_value, "shop_again" : review_obj.shop_again, "category" : review_obj.bought_category.all()[0] #we support multiple bought categoires but there's no multiselect option in UI. } form = ReviewForm(request, parent_obj, initial=data) form.fields['overall_value'].widget = forms.HiddenInput() form.fields['price_value'].widget = forms.HiddenInput() form.fields['website_ex_value'].widget = forms.HiddenInput() form.fields['quality_value'].widget = forms.HiddenInput() form.fields['service_value'].widget = forms.HiddenInput() form.fields['exchange_value'].widget = forms.HiddenInput() """ Review pages which list all the reviews require a template different than edit review template on store page. For such a case reviewpage=1 query parameter should be present. """ review_page = request.GET.get('reviewpage', '0') action_url = reverse("edit_review", kwargs={'review_id':review_id}) action_url = action_url + '?reviewpage='+review_page context = { "posted_comment_form": form, "action_url":action_url, "new_review":False, "review_id": review_id } response = render(request, template, context) return response
def write_review(request, content_type_id, object_id, template="generic/includes/write_review.html"): store = request.GET.get('store', 'None') parent = None if store != 'None': store = store.lower() try: parent = BlogPost.objects.get(title__iexact=store) content_type_id = ContentType.objects.get_for_model(parent).pk object_id = parent.pk except: pass if not parent: ctype = get_object_or_404(ContentType, pk=content_type_id) parent = get_object_or_404(ctype.model_class(), pk=object_id) prefix = "write_review" if request.method == 'POST': response = initial_validation_review(request, prefix, content_type_id, object_id, store) if isinstance(response, HttpResponse): return response obj, post_data = response form = ReviewForm(request, obj, request.POST) if form.is_valid() and request.user.is_authenticated(): url = obj.get_absolute_url() if is_spam(request, form, url): return redirect(url) comment = form.save(request) """ Send activity feed to those who follow this vendor page. """ if request.user.is_authenticated(): action.send(obj, verb=settings.GOT_REVIEW_VERB, target=comment ) if request.is_ajax(): html = render_to_string('generic/includes/comment_ajax.html', { 'comment': comment, 'request':request }) res = {'html': html, 'store': obj.title, 'success':True} response = HttpResponse( simplejson.dumps(res), 'application/json' ) # Store commenter's details in a cookie for 90 days. for field in ReviewForm.cookie_fields: cookie_name = ReviewForm.cookie_prefix + field cookie_value = post_data.get(field, "") set_cookie(response, cookie_name, cookie_value) return response else: response = redirect(add_cache_bypass(comment.get_absolute_url())) return response elif request.is_ajax() and form.errors: return HttpResponse( simplejson.dumps({"errors": form.errors, "success":False}), 'application/json') else: post_data = None posted_session_key = "unauthenticated_" + prefix post_session_generic_review_store_key = "unauthenticated_store_" + prefix if posted_session_key in request.session: post_data = request.session.pop(posted_session_key) form = ReviewForm(request, parent, post_data) if post_session_generic_review_store_key in request.session: request.session.pop(post_session_generic_review_store_key) else: form = ReviewForm(request, parent) form.fields['overall_value'].widget = forms.HiddenInput() form.fields['price_value'].widget = forms.HiddenInput() form.fields['website_ex_value'].widget = forms.HiddenInput() form.fields['quality_value'].widget = forms.HiddenInput() form.fields['service_value'].widget = forms.HiddenInput() form.fields['exchange_value'].widget = forms.HiddenInput() action_url = reverse("write_review", kwargs={ 'content_type_id':content_type_id, 'object_id':object_id }) if store != 'None': action_url += '?store=' + store context = {"new_review":True, "obj": parent, "posted_comment_form": form, "action_url": action_url } response = render(request, template, context) return response