-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4168 from open-formulieren/feature/4156-payment-t…
…emplate-2 [#4156] Use a template for payment reference
- Loading branch information
Showing
10 changed files
with
190 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/openforms/config/migrations/0057_migrate_to_order_id_template.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Generated by Django 4.2.11 on 2024-04-12 10:12 | ||
|
||
from django.db import migrations, models | ||
from django.db.migrations.state import StateApps | ||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
from django.core.cache import caches | ||
import openforms.payments.validators | ||
|
||
from django.conf import settings | ||
|
||
|
||
def migrate_to_order_id_template( | ||
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor | ||
) -> None: | ||
"""Migrate from the payment order ID prefix to a customizable template. | ||
If this migration is run on a new instance, the default value of the model field will take effect. | ||
Otherwise, the default value is adapted to have the ``{uid}`` placeholder included. | ||
""" | ||
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration") | ||
VersionInfo = apps.get_model("upgrades", "VersionInfo") | ||
config = GlobalConfiguration.objects.first() | ||
if config is None: | ||
return | ||
|
||
version_info = VersionInfo.objects.first() | ||
|
||
is_new_deploy = version_info is None or version_info.current == settings.RELEASE | ||
if is_new_deploy: | ||
# The default value of the `payment_order_id_template` field | ||
# will take effect | ||
return | ||
|
||
config.payment_order_id_template = config.payment_order_id_prefix + "{uid}" | ||
config.save() | ||
caches[settings.SOLO_CACHE].clear() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("config", "0056_globalconfiguration_enable_backend_formio_validation"), | ||
("upgrades", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="globalconfiguration", | ||
name="payment_order_id_template", | ||
field=models.CharField( | ||
default="{year}/{public_reference}/{uid}", | ||
help_text="Template to use when generating payment order IDs. It should be alpha-numerical and can contain the '/._-' characters. You can use the placeholder tokens: {year}, {public_reference}, {uid}.", | ||
max_length=48, | ||
validators=[ | ||
openforms.payments.validators.validate_payment_order_id_template | ||
], | ||
verbose_name="Payment Order ID template", | ||
), | ||
), | ||
migrations.RunPython(migrate_to_order_id_template, migrations.RunPython.noop), | ||
migrations.RemoveField( | ||
model_name="globalconfiguration", | ||
name="payment_order_id_prefix", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,46 @@ | ||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
from django.test import SimpleTestCase | ||
|
||
from openforms.payments.validators import validate_payment_order_id_prefix | ||
from openforms.payments.validators import validate_payment_order_id_template | ||
|
||
|
||
class PaymentOrderIDValidatorTests(TestCase): | ||
def test_valid_prefixes(self): | ||
class PaymentOrderIDValidatorTests(SimpleTestCase): | ||
def test_uid_present(self): | ||
valid = "{uid}" | ||
with self.subTest(value=valid): | ||
validate_payment_order_id_template(valid) | ||
|
||
invalid = "other_things" | ||
with self.subTest(value=invalid): | ||
with self.assertRaises(ValidationError): | ||
validate_payment_order_id_template(invalid) | ||
|
||
def test_valid_templates(self): | ||
valid = [ | ||
"", | ||
"ab", | ||
"12", | ||
"ab12", | ||
# placeholder | ||
"{year}" "aa{year}" "a1{year}a1", | ||
"{uid}", | ||
"{uid}ab", | ||
"{uid}12", | ||
"{uid}ab12", | ||
"{uid}a-.///---_", | ||
# placeholders: | ||
"{uid}{year}", | ||
"{year}{public_reference}{uid}", | ||
] | ||
|
||
for value in valid: | ||
with self.subTest(value=value): | ||
validate_payment_order_id_prefix(value) | ||
validate_payment_order_id_template(value) | ||
|
||
def test_raises_for_invalid_prefixes(self): | ||
invalid = [ | ||
" ", | ||
"aa ", | ||
" aa", | ||
"a-2", | ||
"{yearrr", | ||
"{bad}", | ||
" {uid}", | ||
"aa {uid}", | ||
" aa{uid}", | ||
"{yearrr{uid}", | ||
"{bad}{uid}", | ||
] | ||
|
||
for value in invalid: | ||
with self.subTest(value=value): | ||
with self.assertRaises(ValidationError): | ||
validate_payment_order_id_prefix(value) | ||
validate_payment_order_id_template(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters