def leave_feedback(request, template_name='feedback/feedback_form.html'): if request.method == "GET": nexturl = request.GET.get('next') else: nexturl = request.POST.get('next') if request.user.is_authenticated(): form = FeedbackForm(request.POST or None) else: form = AnonymousFeedbackForm(request.POST or None) if form.is_valid(): feedback = form.save(commit=False) if request.user.is_anonymous(): feedback.user = None else: feedback.user = request.user feedback.save() messages.add_message(request, messages.SUCCESS, 'Your feedback was submitted.') return HttpResponseRedirect(nexturl) return render_to_response( template_name, { 'feedback_form': form, 'next': nexturl }, context_instance=RequestContext(request))
def feedback_form(request): feedback_form = None if request.user.is_authenticated(): feedback_form = FeedbackForm() elif getattr(settings, 'ALLOW_ANONYMOUS_FEEDBACK', True): feedback_form = AnonymousFeedbackForm() return {'feedback_form': feedback_form}
def leave_feedback(request, template_name='feedback/feedback_form.html'): ''' Generate and manage the form for feedback, and ulterior AJAX exchanges Only respond to Ajax call. Will return Http404 in case of non-xhr request Parameters ---------- request : HttpRequest user request sent by browser template_url : string Url of the template to use for rendering ''' if request.is_ajax(): # Create Form if request.user.is_authenticated(): form = FeedbackForm(request.POST or None) elif getattr(settings, 'ALLOW_ANONYMOUS_FEEDBACK', False): form = AnonymousFeedbackForm(request.POST or None) else: raise Http404 # POST if request.method == 'POST': if form.is_valid(): feedback = form.save(commit=False) if request.user.is_anonymous(): feedback.user = None else: feedback.user = request.user try: feedback.context_url = request.META['HTTP_REFERER'] feedback.browser = request.META['HTTP_USER_AGENT'] except KeyError: pass feedback.save() data = { 'success': _('Your feedback has been sent to us! Thanks a lot.') } else: data = { 'success': False, 'errors': dict([(k, [unicode(e) for e in v]) for k, v in form.errors.items()]) } return JSONResponse(request, data) else: context = { 'feedback_form': form, } return render(request, template_name, context) else: raise Http404
def feedback_form(request): ''' Add `feedback_form`, the feedback formular to the context ''' feedback_form = None if request.user.is_authenticated(): feedback_form = FeedbackForm() elif getattr(settings, 'ALLOW_ANONYMOUS_FEEDBACK', False): feedback_form = AnonymousFeedbackForm() return {'feedback_form': feedback_form}
def leave_feedback(request, template_name='feedback/feedback_form.html'): if request.user.is_authenticated(): form = FeedbackForm(request.POST or None) else: form = AnonymousFeedbackForm(request.POST or None) if form.is_valid(): feedback = form.save(commit=False) if request.user.is_anonymous(): feedback.user = None else: feedback.user = request.user feedback.save() messages.add_message(request, messages.SUCCESS, "Your feedback was submitted.") return HttpResponseRedirect( request.POST.get('next', request.META.get('HTTP_REFERER', '/'))) return render_to_response(template_name, {'feedback_form': form}, context_instance=RequestContext(request))