diff --git a/donate/blocks.py b/donate/blocks.py index 6dcf2c5caee3a300716d927dd059df69b9568f4f..9bbeb34badab7b9b049ea7be667c4f5a51ec0e40 100644 --- a/donate/blocks.py +++ b/donate/blocks.py @@ -1,19 +1,7 @@ -from django.forms.utils import ErrorList from wagtail.blocks import CharBlock, DateBlock, IntegerBlock, ListBlock, StructBlock -from wagtail.blocks.struct_block import StructBlockValidationError from wagtail.images.blocks import ImageChooserBlock -class CrowdfundingVariantBlock(StructBlock): - reward_id = IntegerBlock(label="ID odměny") - title = CharBlock( - label="Název varianty", max_length=12, help_text="Například velikost: S" - ) - - class Meta: - icon = "radio-empty" - - class CrowdfundingRewardBlock(StructBlock): title = CharBlock(label="Název odměny") description = CharBlock(label="Popis", max_length=255, required=False) @@ -22,26 +10,14 @@ class CrowdfundingRewardBlock(StructBlock): delivery_date = DateBlock(label="Datum dodání", required=False) reward_id = IntegerBlock( label="ID odměny", - help_text="Vyplňte pouze v případě, že odměna nemá varianty", - required=False, - ) - crm_product_description = CharBlock( - label="Popis produktu pro CRM", - help_text='Bude předán systému v parametru "product"', - required=False, + required=True, ) variant_list = ListBlock( - CrowdfundingVariantBlock(), label="Varianty", required=False + CharBlock(label="Varianta", max_length=12, help_text="Například velikost: S"), + label="Varianty", ) class Meta: template = "donate/blocks/crowdfunding_reward_block.html" icon = "pick" label = "Odměna" - - def clean(self, value): - errors = {} - if not value["reward_id"] and not value["variant_list"]: - errors["reward_id"] = ErrorList(["Musíte vyplnit ID odměny"]) - raise StructBlockValidationError(errors) - return super().clean(value) diff --git a/donate/forms.py b/donate/forms.py index a329d75bb3e5851aea06d2c3d936a5f78f047e7c..bb68cf76499cf931d9b5595971d75488a28a8148 100644 --- a/donate/forms.py +++ b/donate/forms.py @@ -2,6 +2,7 @@ import urllib.parse from django import forms from django.conf import settings +from django.utils.text import slugify class DonateForm(forms.Form): @@ -21,6 +22,8 @@ class DonateForm(forms.Form): value = self.cleaned_data["periodicity"] if value not in self.ALLOWED_PERIODICITY: raise forms.ValidationError("Wrong periodicity!") + if self.cleaned_data["crowdfunding"] and value != 99999: + raise forms.ValidationError("Wrong periodicity!") return value def clean(self): @@ -32,6 +35,10 @@ class DonateForm(forms.Form): raise forms.ValidationError("Není zadán účel daru.") if not cleaned_data["amount"] and not cleaned_data["custom_amount"]: raise forms.ValidationError("Nebyla zadána částka.") + if cleaned_data["crowdfunding"] and not cleaned_data["product"]: + raise forms.ValidationError("Nebyla zadána produkt.") + if cleaned_data["product"] and not cleaned_data["crowdfunding"]: + raise forms.ValidationError("Nebyla zadána typ odměny.") return cleaned_data def get_amount(self): @@ -60,9 +67,8 @@ class DonateForm(forms.Form): } if crowdfunding: - query_dict.update(crowdfunding=crowdfunding) - if product: - query_dict.update(product=product) + product = slugify(product) + query_dict.update(crowdfunding=crowdfunding, product=product) query = urllib.parse.urlencode(query_dict) diff --git a/donate/migrations/0024_donateprojectpage_crowdfunding.py b/donate/migrations/0024_donateprojectpage_crowdfunding.py index cee136b861c536f30333db4a13fe8d1edaeef284..74a075c256fc0aeb7fbe52d2f4dbfb8165b3dc1b 100644 --- a/donate/migrations/0024_donateprojectpage_crowdfunding.py +++ b/donate/migrations/0024_donateprojectpage_crowdfunding.py @@ -1,4 +1,4 @@ -# Generated by Django 4.0.4 on 2022-07-26 10:35 +# Generated by Django 4.0.4 on 2022-07-31 05:26 import wagtail.blocks import wagtail.fields @@ -48,42 +48,18 @@ class Migration(migrations.Migration): ( "reward_id", wagtail.blocks.IntegerBlock( - help_text="Vyplňte pouze v případě, že odměna nemá varianty", - label="ID odměny", - required=False, - ), - ), - ( - "crm_product_description", - wagtail.blocks.CharBlock( - help_text='Bude předán systému v parametru "product"', - label="Popis produktu pro CRM", - required=False, + label="ID odměny", required=True ), ), ( "variant_list", wagtail.blocks.ListBlock( - wagtail.blocks.StructBlock( - [ - ( - "reward_id", - wagtail.blocks.IntegerBlock( - label="ID odměny" - ), - ), - ( - "title", - wagtail.blocks.CharBlock( - help_text="Například velikost: S", - label="Název varianty", - max_length=12, - ), - ), - ] + wagtail.blocks.CharBlock( + help_text="Například velikost: S", + label="Varianta", + max_length=12, ), label="Varianty", - required=False, ), ), ] diff --git a/donate/templates/donate/blocks/crowdfunding_reward_block.html b/donate/templates/donate/blocks/crowdfunding_reward_block.html index 59c54796b2173eac91e543dc5633b14f0b007db6..2b9b26389ada4f2806adec5f5af6b3be5cc1d058 100644 --- a/donate/templates/donate/blocks/crowdfunding_reward_block.html +++ b/donate/templates/donate/blocks/crowdfunding_reward_block.html @@ -6,7 +6,7 @@ <input type="hidden" name="portal_project_id" value="{{ page.portal_project_id }}"> <input type="hidden" name="periodicity" value="99999"> <input type="hidden" name="amount" value="{{ self.amount }}"> - <input type="hidden" name="product" value="{{ self.crm_product_description }}"> + <input type="hidden" name="crowdfunding" value="{{ self.reward_id }}"> <h3 class="lead mb-3">{{ self.title }}</h3> <div class="row"> <figure class="col-12 col-md-7"> @@ -15,22 +15,24 @@ </figure> <div class="col-12 col-md-5"> {{ self.description }}<br> - {% if self.variant_list %} - <small>Zvolte si varaintu:</small> - {% for variant in self.variant_list %} - <div class="custom-control form-control-md custom-radio"> - <input type="radio" id="reward-{{ variant.reward_id }}" name="crowdfunding" - value="{{ variant.reward_id }}" - class="custom-control-input form-control-md" required> - <label class="custom-control-label col-form-label-lg" - for="reward-{{ variant.reward_id }}">{{ variant.title }}</label> - </div> - {% endfor %} - + {% if self.variant_list %} + <small>Zvolte si varaintu:</small> + {% for variant in self.variant_list %} + <div class="custom-control form-control-md custom-radio"> + <input type="radio" id="product-{{ forloop.counter }}" name="product" + value="{{ self.title }}-{{ variant }}" + class="custom-control-input form-control-md" required> + <label class="custom-control-label col-form-label-lg" + for="product-{{ forloop.counter }}">{{ variant }}</label> + </div> + {% endfor %} {% else %} - <input id="reward-{{ self.reward_id }}" name="crowdfunding" - value="{{ self.reward_id }}" hidden - class="custom-control-input form-control-md" required> + <input name="product" + value="{{ self.title }}" + hidden + class="custom-control-input form-control-md" + required + > {% endif %} </div> </div> @@ -38,10 +40,12 @@ <div class="col-7 mb-2 mb-md-0"> <button type="submit" class="btn btn-danger btn-md">Darovat {{ self.amount }} Kč</button> </div> - <p class="col-12 col-md-5"> - Dodání do: <br> - <time datetime="2019-06-18">{{ self.delivery_date|date:"d. m. Y" }}</time> - </p> + {% if self.delivery_date %} + <p class="col-12 col-md-5"> + Dodání do: <br> + <time datetime="2019-06-18">{{ self.delivery_date|date:"d. m. Y" }}</time> + </p> + {% endif %} </div> </form> </div>