diff --git a/src/pretix/base/datasync/datasync.py b/src/pretix/base/datasync/datasync.py index 921e8d14d5..d5bfd304c4 100644 --- a/src/pretix/base/datasync/datasync.py +++ b/src/pretix/base/datasync/datasync.py @@ -25,7 +25,7 @@ import logging from collections import namedtuple from datetime import timedelta from functools import cached_property -from typing import Optional, Protocol +from typing import List, Optional, Protocol import sentry_sdk from django.db import DatabaseError, transaction @@ -176,7 +176,7 @@ class OutboundSyncProvider: - `pretix_id_field`: Which pretix data field should be used to identify the mapped object. Any ``DataFieldInfo.key`` returned by ``sourcefields.get_data_fields()`` for the combination of ``Event`` and ``pretix_model``. - `external_id_field`: Destination identifier field in the target system. - - `property_mappings`: Mapping configuration as generated by ``PropertyMappingFormSet.to_property_mappings_json()``. + - `property_mappings`: Mapping configuration as generated by ``PropertyMappingFormSet.to_property_mappings_list()``. """ raise NotImplementedError @@ -270,8 +270,7 @@ class OutboundSyncProvider: val = ",".join(val) return val - def get_properties(self, inputs: dict, property_mappings: str): - property_mappings = json.loads(property_mappings) + def get_properties(self, inputs: dict, property_mappings: List[dict]): return [ (m["external_field"], self.get_field_value(inputs, m), m["overwrite"]) for m in property_mappings diff --git a/src/pretix/base/datasync/utils.py b/src/pretix/base/datasync/utils.py index da1f22d0de..05aab054ab 100644 --- a/src/pretix/base/datasync/utils.py +++ b/src/pretix/base/datasync/utils.py @@ -19,7 +19,6 @@ # You should have received a copy of the GNU Affero General Public License along with this program. If not, see # . # -import json from typing import List, Tuple from pretix.base.datasync.datasync import SyncConfigError @@ -108,15 +107,17 @@ def translate_property_mappings(property_mappings, checkin_list_map): om.save() """ - mappings = json.loads(property_mappings) + mappings = [] - for mapping in mappings: - if mapping["pretix_field"].startswith("checkin_date_"): - old_id = int(mapping["pretix_field"][len("checkin_date_"):]) + for mapping in property_mappings: + pretix_field = mapping["pretix_field"] + if pretix_field.startswith("checkin_date_"): + old_id = int(pretix_field[len("checkin_date_"):]) if old_id not in checkin_list_map: # old_id might not be in checkin_list_map, because copying of an event series only copies check-in # lists covering the whole series, not individual dates. - mapping["pretix_field"] = "_invalid_" + mapping["pretix_field"] + pretix_field = "_invalid_" + pretix_field else: - mapping["pretix_field"] = "checkin_date_%d" % checkin_list_map[old_id].pk - return json.dumps(mappings) + pretix_field = "checkin_date_%d" % checkin_list_map[old_id].pk + mappings.append({**mapping, "pretix_field": pretix_field}) + return mappings diff --git a/src/pretix/control/forms/mapping.py b/src/pretix/control/forms/mapping.py index 2ff14ac274..7e48971612 100644 --- a/src/pretix/control/forms/mapping.py +++ b/src/pretix/control/forms/mapping.py @@ -19,8 +19,6 @@ # You should have received a copy of the GNU Affero General Public License along with this program. If not, see # . # -import json - from django import forms from django.forms import formset_factory from django.utils.translation import gettext_lazy as _ @@ -76,9 +74,7 @@ class PropertyMappingFormSet(formset_factory( )): template_name = "pretixcontrol/datasync/property_mappings_formset.html" - def __init__(self, pretix_fields, external_fields, available_modes, prefix, *args, initial_json=None, **kwargs): - if initial_json: - kwargs["initial"] = json.loads(initial_json) + def __init__(self, pretix_fields, external_fields, available_modes, prefix, *args, **kwargs): super().__init__( form_kwargs={ "pretix_fields": pretix_fields, @@ -95,9 +91,9 @@ class PropertyMappingFormSet(formset_factory( ctx["external_fields_id"] = self.prefix + "external-fields" return ctx - def to_property_mappings_json(self): + def to_property_mappings_list(self): """ - Returns a property mapping configuration as a JSON-serialized list of dictionaries. + Returns a property mapping configuration as a JSON-serializable list of dictionaries. Each entry specifies how to transfer data from one pretix field to one field in the external system: @@ -113,7 +109,7 @@ class PropertyMappingFormSet(formset_factory( - `MODE_APPEND_LIST` (`"append"`) if the field is an array or a multi-select: add the value to the list. """ mappings = [f.cleaned_data for f in self.ordered_forms] - return json.dumps(mappings) + return mappings QUESTION_TYPE_LABELS = dict(Question.TYPE_CHOICES) diff --git a/src/tests/base/test_datasync.py b/src/tests/base/test_datasync.py index 02c6e6ea87..aed1b93825 100644 --- a/src/tests/base/test_datasync.py +++ b/src/tests/base/test_datasync.py @@ -218,7 +218,7 @@ class SimpleOrderSync(OutboundSyncProvider): id=1, pretix_model='Order', external_object_type='ticketorders', pretix_id_field='event_order_code', external_id_field='ordernumber', - property_mappings=json.dumps([ + property_mappings=[ { "pretix_field": "email", "external_field": "orderemail", @@ -249,7 +249,7 @@ class SimpleOrderSync(OutboundSyncProvider): "value_map": "", "overwrite": MODE_OVERWRITE, }, - ]) + ], ) ] @@ -341,7 +341,7 @@ class OrderAndTicketAssociationSync(OutboundSyncProvider): id=1, pretix_model='OrderPosition', external_object_type='tickets', pretix_id_field='ticket_id', external_id_field='ticketnumber', - property_mappings=json.dumps([ + property_mappings=[ { "pretix_field": "ticket_price", "external_field": "amount", @@ -387,14 +387,14 @@ class OrderAndTicketAssociationSync(OutboundSyncProvider): }), "overwrite": MODE_OVERWRITE, }, - ]), + ], association_mappings=[], ), StaticMappingWithAssociations( id=2, pretix_model='Order', external_object_type='ticketorders', pretix_id_field='event_order_code', external_id_field='ordernumber', - property_mappings=json.dumps([ + property_mappings=[ { "pretix_field": "email", "external_field": "orderemail", @@ -425,7 +425,7 @@ class OrderAndTicketAssociationSync(OutboundSyncProvider): }), "overwrite": MODE_OVERWRITE, }, - ]), + ], association_mappings=[ AssociationMapping(via_mapping_id=1) ],