Skip to content

Commit

Permalink
fix(pressure): add referrer_path to shared buttons on post action
Browse files Browse the repository at this point in the history
  • Loading branch information
igr-santos committed Sep 26, 2023
1 parent 154e9a3 commit 0cd5ec4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
7 changes: 6 additions & 1 deletion app/contrib/actions/pressure/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PressurePlugin(CMSPluginBase):

def render(self, context, instance, placeholder):
obj = instance.get_widget()
request = context["request"]
initial = (
{
"email_subject": obj.settings.get("pressure_subject", ""),
Expand All @@ -28,7 +29,11 @@ def render(self, context, instance, placeholder):

if instance.reference_id:
form = PressureAjaxForm(
initial={"reference_id": instance.reference_id, **initial}
initial={
"reference_id": instance.reference_id,
"referrer_path": f"{request.scheme}://{request.get_host()}{request.path}",
**initial,
}
)

context.update(
Expand Down
12 changes: 1 addition & 11 deletions app/contrib/actions/pressure/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import requests

from django import forms
from django.db import transaction
from django.conf import settings

from contrib.bonde.forms import ReferenceBaseModelForm
Expand All @@ -22,6 +21,7 @@ class Meta(ReferenceBaseModelForm.Meta):

class PressureAjaxForm(StyledBaseForm):
reference_id = forms.IntegerField(widget=forms.HiddenInput)
referrer_path = forms.CharField(widget=forms.HiddenInput)

email_address = forms.EmailField(label="Seu e-mail")
name = forms.CharField(label="Seu nome", max_length=80)
Expand All @@ -32,7 +32,6 @@ class PressureAjaxForm(StyledBaseForm):
class Meta(StyledBaseForm.Meta):
readonly_fields = ["email_subject", "email_body"]

@transaction.atomic
def submit(self):
activist = {
"email": self.cleaned_data["email_address"],
Expand Down Expand Up @@ -69,12 +68,3 @@ def submit(self):
print(resp.json())
else:
raise Exception("Query failed to run by returning code of {}. {}".format(resp.status_code, query))

# print(
# "Submitting ->>",
# {
# "activist": activist,
# "input": input,
# "widget_id": self.cleaned_data["reference_id"],
# },
# )
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="px-4 py-4 bg-white rounded-lg">
<h3 class="px-4 pt-4 text-lg font-bold text-center">Valeu, {{form_data.name}}! 🎉 <br/>Sua pressão chegou nos deputados</h3>
<h3 class="px-4 pt-4 text-lg font-bold text-center">Valeu, {{form_data.name}}! 🎉 <br/>Sua pressão foi enviada.</h3>
<p class="py-4 text-center">Agora compartilhe nas redes para mais gente fazer parte desse movimento:</p>
<div class="flex flex-col gap-4 justify-center pb-4">
<a class="w-full btn bg-[#71C16A] border-[#71C16A]" href="https://wa.me/?text={{ form_data.referrer_path }}" target="_blank">
Expand Down
9 changes: 3 additions & 6 deletions app/contrib/actions/pressure/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def form_valid(self, form):
# it might do some processing (in the case of CreateView, it will
# call form.save() for example).
response = super().form_valid(form)

if self.request.is_ajax():
form.submit()
data = {
'success': True,
'html': render(self.request, 'pressure/pressure_success.html', {"form_data": form.cleaned_data}).content.decode('utf-8')
Expand All @@ -49,9 +51,4 @@ class PressureFormAjaxView(AjaxableResponseMixin, FormView):
# this plugin is coming from.
#
def get_success_url(self):
return self.request.path

def form_valid(self, form):
# AjaxableResponseMixin expects our contact object to be 'self.object'.
self.object = form.submit()
return super(PressureFormAjaxView, self).form_valid(form)
return self.request.path

0 comments on commit 0cd5ec4

Please sign in to comment.