Skip to content
Snippets Groups Projects
Commit c9f9183e authored by jan.bednarik's avatar jan.bednarik
Browse files

donate: Processing forms for donations

parent 99e91450
Branches
No related tags found
1 merge request!35donate: Processing forms for donations
Pipeline #740 passed
...@@ -77,6 +77,13 @@ V produkci musí být navíc nastaveno: ...@@ -77,6 +77,13 @@ V produkci musí být navíc nastaveno:
| `DJANGO_SECRET_KEY` | | tajný šifrovací klíč | | `DJANGO_SECRET_KEY` | | tajný šifrovací klíč |
| `DJANGO_ALLOWED_HOSTS` | | allowed hosts (více hodnot odděleno čárkami) | | `DJANGO_ALLOWED_HOSTS` | | allowed hosts (více hodnot odděleno čárkami) |
Settings pro appky na weby:
| proměnná | default | popis |
| --- | --- | --- |
| `DONATE_PORTAL_REDIRECT_URL` | "" | URL pro přesměrování z darovacího formuláře |
| `DONATE_PORTAL_REDIRECT_SOURCE` | dary.pirati.cz | identifikátor zdroje pro přesměrování na darovací portál |
### Management commands ### Management commands
Přes CRON je třeba na pozadí spouštět Django `manage.py` commandy: Přes CRON je třeba na pozadí spouštět Django `manage.py` commandy:
......
import urllib.parse
from django import forms
from django.conf import settings
class DonateForm(forms.Form):
CUSTOM_AMOUNT = -1
DEFAULT_CUSTOM_AMOUNT = 1000
ALLOWED_PERIODICITY = [730, 99999]
amount = forms.IntegerField()
custom_amount = forms.IntegerField(required=False)
periodicity = forms.IntegerField()
def clean_periodicity(self):
value = self.cleaned_data["periodicity"]
if value not in self.ALLOWED_PERIODICITY:
raise forms.ValidationError("Wrong periodicity!")
return value
def get_amount(self):
amount = self.cleaned_data["amount"]
if amount == self.CUSTOM_AMOUNT:
amount = (
abs(self.cleaned_data["custom_amount"]) or self.DEFAULT_CUSTOM_AMOUNT
)
return amount
def get_redirect_url(self, portal_project_id):
amount = self.get_amount()
periodicity = self.cleaned_data["periodicity"]
query = urllib.parse.urlencode(
{
"amount": amount,
"periodicity": periodicity,
"projectAccount": portal_project_id,
"source": settings.DONATE_PORTAL_REDIRECT_SOURCE,
}
)
return f"{settings.DONATE_PORTAL_REDIRECT_URL}?{query}"
# Generated by Django 3.0.6 on 2020-06-21 21:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("donate", "0003_donatecookiespage_donateinfopage"),
]
operations = [
migrations.AddField(
model_name="donatehomepage",
name="portal_project_id",
field=models.IntegerField(
blank=True, null=True, verbose_name="ID projektu v darovacím portálu"
),
),
migrations.AddField(
model_name="donateprojectpage",
name="portal_project_id",
field=models.IntegerField(
blank=True, null=True, verbose_name="ID projektu v darovacím portálu"
),
),
migrations.AddField(
model_name="donateregionpage",
name="portal_project_id",
field=models.IntegerField(
blank=True, null=True, verbose_name="ID projektu v darovacím portálu"
),
),
]
from django.db import models from django.db import models
from django.shortcuts import redirect
from django.utils.translation import gettext_lazy from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import ( from wagtail.admin.edit_handlers import (
FieldPanel, FieldPanel,
...@@ -14,6 +15,8 @@ from wagtailmetadata.models import MetadataPageMixin ...@@ -14,6 +15,8 @@ from wagtailmetadata.models import MetadataPageMixin
from tuning import help from tuning import help
from .forms import DonateForm
class SubpageMixin: class SubpageMixin:
"""Must be used in class definition before MetadataPageMixin!""" """Must be used in class definition before MetadataPageMixin!"""
...@@ -28,6 +31,29 @@ class SubpageMixin: ...@@ -28,6 +31,29 @@ class SubpageMixin:
return self.search_image or self.root_page.get_meta_image() return self.search_image or self.root_page.get_meta_image()
class DonateFormMixin(models.Model):
"""Pages which has donate form. Must be in class definition before Page!"""
portal_project_id = models.IntegerField(
"ID projektu v darovacím portálu", blank=True, null=True
)
class Meta:
abstract = True
def serve(self, request):
if request.method == "POST":
form = DonateForm(request.POST)
if form.is_valid():
url = form.get_redirect_url(self.portal_project_id)
return redirect(url)
return super().serve(request)
@property
def show_donate_form(self):
return bool(self.portal_project_id)
def get_url(page, dest_page_type): def get_url(page, dest_page_type):
try: try:
return page.get_children().type(dest_page_type).live().get().get_url() return page.get_children().type(dest_page_type).live().get().get_url()
...@@ -35,7 +61,7 @@ def get_url(page, dest_page_type): ...@@ -35,7 +61,7 @@ def get_url(page, dest_page_type):
return "#" return "#"
class DonateHomePage(Page, MetadataPageMixin): class DonateHomePage(DonateFormMixin, Page, MetadataPageMixin):
# lead section # lead section
lead_title = models.CharField("hlavní nadpis", max_length=250, blank=True) lead_title = models.CharField("hlavní nadpis", max_length=250, blank=True)
lead_body = models.TextField("hlavní popis", blank=True) lead_body = models.TextField("hlavní popis", blank=True)
...@@ -111,6 +137,7 @@ class DonateHomePage(Page, MetadataPageMixin): ...@@ -111,6 +137,7 @@ class DonateHomePage(Page, MetadataPageMixin):
"sociální sítě", "sociální sítě",
), ),
FieldPanel("matomo_id"), FieldPanel("matomo_id"),
FieldPanel("portal_project_id"),
] ]
subpage_types = [ subpage_types = [
...@@ -199,7 +226,7 @@ class DonateRegionIndexPage(Page, SubpageMixin, MetadataPageMixin): ...@@ -199,7 +226,7 @@ class DonateRegionIndexPage(Page, SubpageMixin, MetadataPageMixin):
return context return context
class DonateRegionPage(Page, SubpageMixin, MetadataPageMixin): class DonateRegionPage(DonateFormMixin, Page, SubpageMixin, MetadataPageMixin):
perex = models.TextField("krátký popis do přehledu krajů") perex = models.TextField("krátký popis do přehledu krajů")
main_title = models.CharField("hlavní nadpis na stránce", max_length=250) main_title = models.CharField("hlavní nadpis na stránce", max_length=250)
body = RichTextField("obsah") body = RichTextField("obsah")
...@@ -229,7 +256,7 @@ class DonateRegionPage(Page, SubpageMixin, MetadataPageMixin): ...@@ -229,7 +256,7 @@ class DonateRegionPage(Page, SubpageMixin, MetadataPageMixin):
), ),
] ]
settings_panels = [] settings_panels = [FieldPanel("portal_project_id")]
parent_page_types = ["donate.DonateRegionIndexPage"] parent_page_types = ["donate.DonateRegionIndexPage"]
subpage_types = [] subpage_types = []
...@@ -288,7 +315,7 @@ class DonateProjectIndexPage(Page, SubpageMixin, MetadataPageMixin): ...@@ -288,7 +315,7 @@ class DonateProjectIndexPage(Page, SubpageMixin, MetadataPageMixin):
return context return context
class DonateProjectPage(Page, SubpageMixin, MetadataPageMixin): class DonateProjectPage(DonateFormMixin, Page, SubpageMixin, MetadataPageMixin):
date = models.DateField("běží od") date = models.DateField("běží od")
perex = models.TextField("krátký popis") perex = models.TextField("krátký popis")
body = RichTextField("obsah") body = RichTextField("obsah")
...@@ -337,6 +364,8 @@ class DonateProjectPage(Page, SubpageMixin, MetadataPageMixin): ...@@ -337,6 +364,8 @@ class DonateProjectPage(Page, SubpageMixin, MetadataPageMixin):
), ),
] ]
settings_panels = Page.settings_panels + [FieldPanel("portal_project_id")]
parent_page_types = ["donate.DonateProjectIndexPage"] parent_page_types = ["donate.DonateProjectIndexPage"]
subpage_types = [] subpage_types = []
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
</div> <!-- /container --> </div> <!-- /container -->
</section> </section>
{% if page.show_donate_form %}
<section class="section--primary" id="strana"> <section class="section--primary" id="strana">
<div class="container"> <div class="container">
<h2 class="lead page-subheading mb-4">{{ page.support_title }}</h2> <h2 class="lead page-subheading mb-4">{{ page.support_title }}</h2>
...@@ -45,7 +46,8 @@ ...@@ -45,7 +46,8 @@
</div><!-- /donate-form__icon --> </div><!-- /donate-form__icon -->
</div><!-- /donate-form__left --> </div><!-- /donate-form__left -->
<div class="donate-form__right"> <div class="donate-form__right">
<form id="js-donate-form"> <form id="js-donate-form" method="post">
{% csrf_token %}
<div class="form-group row mb-4 align-items-center"> <div class="form-group row mb-4 align-items-center">
<legend class="col-form-label col-md-4 col-form-label-lg">Částka</legend> <legend class="col-form-label col-md-4 col-form-label-lg">Částka</legend>
<div class="col-md-8"> <div class="col-md-8">
...@@ -62,7 +64,7 @@ ...@@ -62,7 +64,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label> <label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="amount4" name="amount" value="custom" class="custom-control-input"> <input type="radio" id="amount4" name="amount" value="-1" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label> <label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label>
</div> </div>
</div> </div>
...@@ -70,7 +72,7 @@ ...@@ -70,7 +72,7 @@
<div class="form-group row mb-4 align-items-center" id="js-custom-amount-input" style="display: none;"> <div class="form-group row mb-4 align-items-center" id="js-custom-amount-input" style="display: none;">
<div class="offset-md-4 col-md-8"> <div class="offset-md-4 col-md-8">
<div class="input-group input-group-lg mb-3 custom-amount"> <div class="input-group input-group-lg mb-3 custom-amount">
<input type="number" class="form-control" id="customamount" name="customamount" placeholder="1000" aria-describedby="customamount-currency"> <input type="number" class="form-control" id="customamount" name="custom_amount" placeholder="1000" aria-describedby="customamount-currency">
<div class="input-group-append"> <div class="input-group-append">
<span class="input-group-text" id="customamount-currency"></span> <span class="input-group-text" id="customamount-currency"></span>
</div> </div>
...@@ -81,12 +83,12 @@ ...@@ -81,12 +83,12 @@
<legend class="col-form-label col-md-4 col-form-label-lg">Typ příspěvku</legend> <legend class="col-form-label col-md-4 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-8"> <div class="col-md-8">
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked required> <input type="radio" id="periodicity1" name="periodicity" value="730" class="custom-control-input" checked required>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label> <label class="custom-control-label col-form-label-lg" for="periodicity1">Měsíční</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input"> <input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label> <label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div> </div>
</div> </div>
</div> </div>
...@@ -101,6 +103,7 @@ ...@@ -101,6 +103,7 @@
</div><!-- /donate-form --> </div><!-- /donate-form -->
</div> <!-- /container --> </div> <!-- /container -->
</section> </section>
{% endif %}
{% if page.has_projects %} {% if page.has_projects %}
<section class="section--alternate" id="projekty"> <section class="section--alternate" id="projekty">
......
...@@ -67,7 +67,10 @@ ...@@ -67,7 +67,10 @@
<h5><strong>40 dní</strong> do konce</h5> <h5><strong>40 dní</strong> do konce</h5>
<hr> <hr>
{% endcomment %} {% endcomment %}
<form id="js-donate-form">
{% if page.show_donate_form %}
<form id="js-donate-form" method="post">
{% csrf_token %}
<div class="form-group row mb-2 align-items-center"> <div class="form-group row mb-2 align-items-center">
<legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend> <legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend>
<div class="col-md-12"> <div class="col-md-12">
...@@ -84,7 +87,7 @@ ...@@ -84,7 +87,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label> <label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="amount4" name="amount" value="custom" class="custom-control-input"> <input type="radio" id="amount4" name="amount" value="-1" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label> <label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label>
</div> </div>
</div> </div>
...@@ -92,7 +95,7 @@ ...@@ -92,7 +95,7 @@
<div class="form-group row mb-2 align-items-center" id="js-custom-amount-input" style="display: none;"> <div class="form-group row mb-2 align-items-center" id="js-custom-amount-input" style="display: none;">
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group input-group-lg mb-3 custom-amount"> <div class="input-group input-group-lg mb-3 custom-amount">
<input type="number" class="form-control" id="customamount" name="customamount" placeholder="1000" aria-describedby="customamount-currency"> <input type="number" class="form-control" id="customamount" name="custom_amount" placeholder="1000" aria-describedby="customamount-currency">
<div class="input-group-append"> <div class="input-group-append">
<span class="input-group-text" id="customamount-currency"></span> <span class="input-group-text" id="customamount-currency"></span>
</div> </div>
...@@ -103,12 +106,12 @@ ...@@ -103,12 +106,12 @@
<legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend> <legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-12"> <div class="col-md-12">
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked> <input type="radio" id="periodicity1" name="periodicity" value="730" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label> <label class="custom-control-label col-form-label-lg" for="periodicity1">Měsíční</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input"> <input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label> <label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div> </div>
</div> </div>
</div> </div>
...@@ -118,6 +121,7 @@ ...@@ -118,6 +121,7 @@
</div> </div>
</div> </div>
</form> </form>
{% endif %}
</div><!-- /project-donate-form__right --> </div><!-- /project-donate-form__right -->
</div> </div>
......
...@@ -30,7 +30,9 @@ ...@@ -30,7 +30,9 @@
</div><!-- /region-donate-form__left --> </div><!-- /region-donate-form__left -->
<div class="region-donate-form__right"> <div class="region-donate-form__right">
<form id="js-donate-form"> {% if page.show_donate_form %}
<form id="js-donate-form" method="post">
{% csrf_token %}
<div class="form-group row mb-2 align-items-center"> <div class="form-group row mb-2 align-items-center">
<legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend> <legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend>
<div class="col-md-12"> <div class="col-md-12">
...@@ -47,7 +49,7 @@ ...@@ -47,7 +49,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label> <label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="amount4" name="amount" value="custom" class="custom-control-input"> <input type="radio" id="amount4" name="amount" value="-1" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label> <label class="custom-control-label col-form-label-lg" for="amount4">Jiná částka</label>
</div> </div>
</div> </div>
...@@ -55,7 +57,7 @@ ...@@ -55,7 +57,7 @@
<div class="form-group row mb-2 align-items-center" id="js-custom-amount-input" style="display: none;"> <div class="form-group row mb-2 align-items-center" id="js-custom-amount-input" style="display: none;">
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group input-group-lg mb-3 custom-amount"> <div class="input-group input-group-lg mb-3 custom-amount">
<input type="number" class="form-control" id="customamount" name="customamount" placeholder="1000" aria-describedby="customamount-currency"> <input type="number" class="form-control" id="customamount" name="custom_amount" placeholder="1000" aria-describedby="customamount-currency">
<div class="input-group-append"> <div class="input-group-append">
<span class="input-group-text" id="customamount-currency"></span> <span class="input-group-text" id="customamount-currency"></span>
</div> </div>
...@@ -66,12 +68,12 @@ ...@@ -66,12 +68,12 @@
<legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend> <legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-12"> <div class="col-md-12">
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked> <input type="radio" id="periodicity1" name="periodicity" value="730" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label> <label class="custom-control-label col-form-label-lg" for="periodicity1">Měsíční</label>
</div> </div>
<div class="custom-control custom-radio custom-control-inline"> <div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input"> <input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label> <label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div> </div>
</div> </div>
</div> </div>
...@@ -81,6 +83,7 @@ ...@@ -81,6 +83,7 @@
</div> </div>
</div> </div>
</form> </form>
{% endif %}
</div><!-- /region-donate-form__right --> </div><!-- /region-donate-form__right -->
</div> </div>
......
...@@ -202,3 +202,10 @@ WAGTAIL_EMAIL_MANAGEMENT_ENABLED = False ...@@ -202,3 +202,10 @@ WAGTAIL_EMAIL_MANAGEMENT_ENABLED = False
# Base URL to use when referring to full URLs within the Wagtail admin backend - # Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash # e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = env.str("BASE_URL", default="https://majak.pirati.cz") BASE_URL = env.str("BASE_URL", default="https://majak.pirati.cz")
# CUSTOM APPS SETTINGS
# ------------------------------------------------------------------------------
DONATE_PORTAL_REDIRECT_URL = env.str("DONATE_PORTAL_REDIRECT_URL", default="")
DONATE_PORTAL_REDIRECT_SOURCE = env.str(
"DONATE_PORTAL_REDIRECT_SOURCE", default="dary.pirati.cz"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment