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
No related branches found
No related tags found
No related merge requests found
......@@ -77,6 +77,13 @@ V produkci musí být navíc nastaveno:
| `DJANGO_SECRET_KEY` | | tajný šifrovací klíč |
| `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
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.shortcuts import redirect
from django.utils.translation import gettext_lazy
from wagtail.admin.edit_handlers import (
FieldPanel,
......@@ -14,6 +15,8 @@ from wagtailmetadata.models import MetadataPageMixin
from tuning import help
from .forms import DonateForm
class SubpageMixin:
"""Must be used in class definition before MetadataPageMixin!"""
......@@ -28,6 +31,29 @@ class SubpageMixin:
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):
try:
return page.get_children().type(dest_page_type).live().get().get_url()
......@@ -35,7 +61,7 @@ def get_url(page, dest_page_type):
return "#"
class DonateHomePage(Page, MetadataPageMixin):
class DonateHomePage(DonateFormMixin, Page, MetadataPageMixin):
# lead section
lead_title = models.CharField("hlavní nadpis", max_length=250, blank=True)
lead_body = models.TextField("hlavní popis", blank=True)
......@@ -111,6 +137,7 @@ class DonateHomePage(Page, MetadataPageMixin):
"sociální sítě",
),
FieldPanel("matomo_id"),
FieldPanel("portal_project_id"),
]
subpage_types = [
......@@ -199,7 +226,7 @@ class DonateRegionIndexPage(Page, SubpageMixin, MetadataPageMixin):
return context
class DonateRegionPage(Page, SubpageMixin, MetadataPageMixin):
class DonateRegionPage(DonateFormMixin, Page, SubpageMixin, MetadataPageMixin):
perex = models.TextField("krátký popis do přehledu krajů")
main_title = models.CharField("hlavní nadpis na stránce", max_length=250)
body = RichTextField("obsah")
......@@ -229,7 +256,7 @@ class DonateRegionPage(Page, SubpageMixin, MetadataPageMixin):
),
]
settings_panels = []
settings_panels = [FieldPanel("portal_project_id")]
parent_page_types = ["donate.DonateRegionIndexPage"]
subpage_types = []
......@@ -288,7 +315,7 @@ class DonateProjectIndexPage(Page, SubpageMixin, MetadataPageMixin):
return context
class DonateProjectPage(Page, SubpageMixin, MetadataPageMixin):
class DonateProjectPage(DonateFormMixin, Page, SubpageMixin, MetadataPageMixin):
date = models.DateField("běží od")
perex = models.TextField("krátký popis")
body = RichTextField("obsah")
......@@ -337,6 +364,8 @@ class DonateProjectPage(Page, SubpageMixin, MetadataPageMixin):
),
]
settings_panels = Page.settings_panels + [FieldPanel("portal_project_id")]
parent_page_types = ["donate.DonateProjectIndexPage"]
subpage_types = []
......
......@@ -34,6 +34,7 @@
</div> <!-- /container -->
</section>
{% if page.show_donate_form %}
<section class="section--primary" id="strana">
<div class="container">
<h2 class="lead page-subheading mb-4">{{ page.support_title }}</h2>
......@@ -45,7 +46,8 @@
</div><!-- /donate-form__icon -->
</div><!-- /donate-form__left -->
<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">
<legend class="col-form-label col-md-4 col-form-label-lg">Částka</legend>
<div class="col-md-8">
......@@ -62,7 +64,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div>
<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>
</div>
</div>
......@@ -70,7 +72,7 @@
<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="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">
<span class="input-group-text" id="customamount-currency"></span>
</div>
......@@ -81,12 +83,12 @@
<legend class="col-form-label col-md-4 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-8">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked required>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label>
<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="periodicity1">Měsíční</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label>
<input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div>
</div>
</div>
......@@ -101,6 +103,7 @@
</div><!-- /donate-form -->
</div> <!-- /container -->
</section>
{% endif %}
{% if page.has_projects %}
<section class="section--alternate" id="projekty">
......
......@@ -67,7 +67,10 @@
<h5><strong>40 dní</strong> do konce</h5>
<hr>
{% 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">
<legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend>
<div class="col-md-12">
......@@ -84,7 +87,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div>
<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>
</div>
</div>
......@@ -92,7 +95,7 @@
<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="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">
<span class="input-group-text" id="customamount-currency"></span>
</div>
......@@ -103,12 +106,12 @@
<legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-12">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label>
<input type="radio" id="periodicity1" name="periodicity" value="730" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="periodicity1">Měsíční</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label>
<input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div>
</div>
</div>
......@@ -118,6 +121,7 @@
</div>
</div>
</form>
{% endif %}
</div><!-- /project-donate-form__right -->
</div>
......
......@@ -30,7 +30,9 @@
</div><!-- /region-donate-form__left -->
<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">
<legend class="col-form-label col-md-12 col-form-label-lg">Částka</legend>
<div class="col-md-12">
......@@ -47,7 +49,7 @@
<label class="custom-control-label col-form-label-lg" for="amount3">500 Kč</label>
</div>
<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>
</div>
</div>
......@@ -55,7 +57,7 @@
<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="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">
<span class="input-group-text" id="customamount-currency"></span>
</div>
......@@ -66,12 +68,12 @@
<legend class="col-form-label col-md-12 col-form-label-lg">Typ příspěvku</legend>
<div class="col-md-12">
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type1" name="type" value="monthly" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="type1">Měsíční</label>
<input type="radio" id="periodicity1" name="periodicity" value="730" class="custom-control-input" checked>
<label class="custom-control-label col-form-label-lg" for="periodicity1">Měsíční</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="type2" name="type" value="onetime" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="type2">Jednorázový</label>
<input type="radio" id="periodicity2" name="periodicity" value="99999" class="custom-control-input">
<label class="custom-control-label col-form-label-lg" for="periodicity2">Jednorázový</label>
</div>
</div>
</div>
......@@ -81,6 +83,7 @@
</div>
</div>
</form>
{% endif %}
</div><!-- /region-donate-form__right -->
</div>
......
......@@ -202,3 +202,10 @@ WAGTAIL_EMAIL_MANAGEMENT_ENABLED = False
# 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
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