diff --git a/README.md b/README.md index d38079d91f17db19e7f179f16312dfe29382fc05..64104e34c9b549ef8265be18e3f6f9b5ebfb4fba 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/donate/forms.py b/donate/forms.py new file mode 100644 index 0000000000000000000000000000000000000000..970724776f3a014fb335cdcdc6ef23040abec413 --- /dev/null +++ b/donate/forms.py @@ -0,0 +1,41 @@ +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}" diff --git a/donate/migrations/0004_auto_20200621_2349.py b/donate/migrations/0004_auto_20200621_2349.py new file mode 100644 index 0000000000000000000000000000000000000000..7680c1565191f01099a61449c38caf08ee5825ee --- /dev/null +++ b/donate/migrations/0004_auto_20200621_2349.py @@ -0,0 +1,34 @@ +# 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" + ), + ), + ] diff --git a/donate/models.py b/donate/models.py index 156414f4d3b24abfdd4d2e98254fb428c1a8529f..0fe9a272781d983f1cb3fbd6539504404bfe931f 100644 --- a/donate/models.py +++ b/donate/models.py @@ -1,4 +1,5 @@ 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 = [] diff --git a/donate/templates/donate/donate_home_page.html b/donate/templates/donate/donate_home_page.html index 8ea76288b347adfe1dc296a9f6e7156386e58aa1..6983625495ba7859063cbd831d23008186608979 100644 --- a/donate/templates/donate/donate_home_page.html +++ b/donate/templates/donate/donate_home_page.html @@ -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">Kč</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"> diff --git a/donate/templates/donate/donate_project_page.html b/donate/templates/donate/donate_project_page.html index 0abf52d6e241a7b5d19bb02734b9d74a2ad19cfe..3c74fb380bba37323649db774324a446dd69f5c1 100644 --- a/donate/templates/donate/donate_project_page.html +++ b/donate/templates/donate/donate_project_page.html @@ -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">Kč</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> diff --git a/donate/templates/donate/donate_region_page.html b/donate/templates/donate/donate_region_page.html index 8e1fa454b47f1fd4b7fb52f9b787fc318f11d632..fbb174c1557df50e2d8c89dc8fc6743fd4bbc88a 100644 --- a/donate/templates/donate/donate_region_page.html +++ b/donate/templates/donate/donate_region_page.html @@ -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">Kč</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> diff --git a/majak/settings/base.py b/majak/settings/base.py index 3e9df3d40f94060f1067ceade3d2f857b0ddea62..569cb6873021f0def32c70992b5676c63a995e12 100644 --- a/majak/settings/base.py +++ b/majak/settings/base.py @@ -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" +)