From fda7230bce53dbbda8d728d102b26b6a7bb4b106 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 4 Aug 2019 22:14:15 -0500 Subject: [PATCH] Fix form handling for user feedback issue was noticed on demo site, but possibly existed elsewhere? also not 100% sure about this fix, but it's believed to be okay... --- tailbone/forms/core.py | 17 +++++++---------- .../themes/falafel/js/tailbone.feedback.js | 9 +++++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/tailbone/forms/core.py b/tailbone/forms/core.py index 8c77dfa4..c5ce1bb7 100644 --- a/tailbone/forms/core.py +++ b/tailbone/forms/core.py @@ -918,16 +918,13 @@ class Form(object): if self.request.method != 'POST': return False - # we assume data is in the form of POST params - controls = self.request.POST.items() - - # but if not, we can try to parse request body as JSON - if not controls: - try: - controls = self.request.json_body.items() - except ValueError: - # not a valid JSON body, so no data - controls = [] + # use POST or JSON body, whichever is present + # TODO: per docs, some JS libraries may not set this flag? + # https://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request.is_xhr + if self.request.is_xhr: + controls = self.request.json_body.items() + else: + controls = self.request.POST.items() dform = self.make_deform_form() try: diff --git a/tailbone/static/themes/falafel/js/tailbone.feedback.js b/tailbone/static/themes/falafel/js/tailbone.feedback.js index d60c27a5..a3cd2af2 100644 --- a/tailbone/static/themes/falafel/js/tailbone.feedback.js +++ b/tailbone/static/themes/falafel/js/tailbone.feedback.js @@ -27,8 +27,13 @@ let FeedbackForm = { } this.$http.post(this.action, params, {headers: headers}).then(({ data }) => { - this.showDialog = false - alert("Message successfully sent.\n\nThank you for your feedback.") + if (data.ok) { + alert("Message successfully sent.\n\nThank you for your feedback.") + this.showDialog = false + } else { + alert("Sorry! Your message could not be sent.\n\n" + + "Please try to contact the site admin some other way.") + } }) }, }