Пример #1
0
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
Пример #2
0
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