Project

General

Profile

0001-cards-check-on-form-import-if-carddef-custom-view-ex.patch

Lauréline Guerin, 16 Oct 2020 11:40 AM

Download (4.62 KB)

View differences:

Subject: [PATCH] cards: check on form import if carddef custom view exists
 (#47780)

 tests/test_formdef_import.py | 24 ++++++++++++++++++++++++
 wcs/formdef.py               | 27 ++++++++++++++++++++++-----
 2 files changed, 46 insertions(+), 5 deletions(-)
tests/test_formdef_import.py
378 378
    with pytest.raises(FormdefImportError, match='Unknown datasources'):
379 379
        FormDef.import_from_xml(BytesIO(export))
380 380

  
381
    # carddef as datasource
381 382
    carddef = CardDef()
382 383
    carddef.name = 'foo'
383 384
    carddef.fields = [
......
398 399
    with pytest.raises(FormdefImportError):
399 400
        FormDef.import_from_xml(BytesIO(export))
400 401

  
402
    # carddef custom view as datasource
403
    pub.custom_view_class.wipe()
404
    custom_view = pub.custom_view_class()
405
    custom_view.title = 'card view'
406
    custom_view.formdef = carddef
407
    custom_view.columns = {'list': [{'id': 'id'}]}
408
    custom_view.filters = {}
409
    custom_view.visibility = 'datasource'
410
    custom_view.store()
411

  
412
    formdef.fields = [fields.StringField(
413
        id='1', type='string',
414
        data_source={'type': 'carddef:foo:card-view'})]
415
    export = ET.tostring(export_to_indented_xml(formdef))
416
    FormDef.import_from_xml(BytesIO(export))
417

  
418
    formdef.fields = [fields.StringField(
419
        id='1', type='string',
420
        data_source={'type': 'carddef:foo:unknown'})]
421
    export = ET.tostring(export_to_indented_xml(formdef))
422
    with pytest.raises(FormdefImportError):
423
        FormDef.import_from_xml(BytesIO(export))
424

  
401 425

  
402 426
def test_duplicated_field_ids():
403 427
    formdef = FormDef()
wcs/formdef.py
33 33
from quixote.http_request import Upload
34 34

  
35 35
from .qommon import _, N_, force_str, PICKLE_KWARGS
36
from .qommon.storage import StorableObject, fix_key
36
from .qommon.storage import StorableObject, fix_key, Equal
37 37
from .qommon.cron import CronJob
38 38
from .qommon.form import *
39 39
from .qommon.misc import simplify, get_as_datetime, xml_node_text
......
1044 1044
            for field in formdef.fields:
1045 1045
                data_source = getattr(field, 'data_source', None)
1046 1046
                if data_source:
1047
                    data_source_id = data_source.get('type')
1047 1048
                    if isinstance(data_sources.get_object(data_source),
1048 1049
                                  data_sources.StubNamedDataSource):
1049
                        unknown_datasources.add(data_source.get('type'))
1050
                    elif data_source.get('type') and data_source.get('type').startswith('carddef:'):
1050
                        unknown_datasources.add(data_source_id)
1051
                    elif data_source_id and data_source_id.startswith('carddef:'):
1052
                        parts = data_source_id.split(':')
1051 1053
                        # check if carddef exists
1052
                        url_name = data_source['type'][8:]
1054
                        url_name = parts[1]
1053 1055
                        if formdef.xml_root_node == 'carddef' and formdef.url_name == url_name:
1054 1056
                            # reference to itself, it's ok
1055 1057
                            continue
1056 1058
                        try:
1057 1059
                            CardDef.get_by_urlname(url_name)
1058 1060
                        except KeyError:
1059
                            unknown_datasources.add(data_source.get('type'))
1061
                            unknown_datasources.add(data_source_id)
1062
                            continue
1063

  
1064
                        if len(parts) == 2:
1065
                            continue
1066

  
1067
                        lookup_criterias = [
1068
                            Equal('formdef_type', 'carddef'),
1069
                            Equal('visibility', 'datasource'),
1070
                            Equal('slug', parts[2]),
1071
                        ]
1072
                        try:
1073
                            get_publisher().custom_view_class.select(lookup_criterias)[0]
1074
                        except IndexError:
1075
                            unknown_datasources.add(data_source_id)
1076

  
1060 1077
            if unknown_datasources:
1061 1078
                raise FormdefImportError(N_('Unknown datasources'),
1062 1079
                        details=', '.join(sorted(unknown_datasources)))
1063
-