Skip to content

Commit

Permalink
Merge pull request #1000 from rdmorganiser/fix_full_export
Browse files Browse the repository at this point in the history
Add parent attributes to full export
  • Loading branch information
jochenklar committed Jun 20, 2024
2 parents 0aa4f2e + f0a3969 commit a0b05ed
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
10 changes: 7 additions & 3 deletions rdmo/domain/renderers/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class AttributeRendererMixin:

def render_attribute(self, xml, attribute):
def render_attribute(self, xml, attribute, include_children=True):
if attribute['uri'] not in self.uris:
self.uris.add(attribute['uri'])

Expand All @@ -12,6 +12,10 @@ def render_attribute(self, xml, attribute):
self.render_text_element(xml, 'parent', {'dc:uri': attribute['parent']}, None)
xml.endElement('attribute')

if 'children' in attribute and attribute['children']:
for child in attribute['children']:
if include_children:
for child in attribute.get('children', []):
self.render_attribute(xml, child)

parent_data = attribute.get('parent_data')
if parent_data:
self.render_attribute(xml, parent_data, include_children=False)
8 changes: 7 additions & 1 deletion rdmo/domain/serializers/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class AttributeExportSerializer(serializers.ModelSerializer):

parent = serializers.CharField(source='parent.uri', default=None, read_only=True)
parent_data = serializers.SerializerMethodField()

class Meta:
model = Attribute
Expand All @@ -15,5 +16,10 @@ class Meta:
'key',
'path',
'comment',
'parent'
'parent',
'parent_data'
)

def get_parent_data(self, obj):
if obj.parent is not None:
return AttributeExportSerializer(obj.parent).data
17 changes: 17 additions & 0 deletions rdmo/questions/tests/test_viewset_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,20 @@ def test_detail_export(db, client, username, password, export_format):
assert root.tag == 'rdmo'
for child in root:
assert child.tag in ['catalog', 'section', 'page', 'questionset', 'question']


def test_detail_export_full(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['detail_export'], args=[1]) + 'xml/?full=true'
response = client.get(url)
assert response.status_code == status_map['detail']['editor'], response.content

root = et.fromstring(response.content)
assert root.tag == 'rdmo'

uris = [child.attrib[r'{http://purl.org/dc/elements/1.1/}uri'] for child in root]
assert 'http://example.com/terms/conditions/options_empty' in uris
assert 'http://example.com/terms/domain/conditions' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris
17 changes: 17 additions & 0 deletions rdmo/questions/tests/test_viewset_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,20 @@ def test_detail_export(db, client, username, password, export_format):
assert root.tag == 'rdmo'
for child in root:
assert child.tag in ['page', 'questionset', 'question']


def test_detail_export_full(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['detail_export'], args=[71]) + 'xml/?full=true'
response = client.get(url)
assert response.status_code == status_map['detail']['editor'], response.content

root = et.fromstring(response.content)
assert root.tag == 'rdmo'

uris = [child.attrib[r'{http://purl.org/dc/elements/1.1/}uri'] for child in root]
assert 'http://example.com/terms/conditions/options_empty' in uris
assert 'http://example.com/terms/domain/conditions' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris
15 changes: 15 additions & 0 deletions rdmo/questions/tests/test_viewset_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,18 @@ def test_detail_export(db, client, username, password, export_format):
assert root.tag == 'rdmo'
for child in root:
assert child.tag in ['question']


def test_detail_export_full(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['detail_export'], args=[104]) + 'xml/?full=true'
response = client.get(url)
assert response.status_code == status_map['detail']['editor'], response.content

root = et.fromstring(response.content)
assert root.tag == 'rdmo'

uris = [child.attrib[r'{http://purl.org/dc/elements/1.1/}uri'] for child in root]
assert 'http://example.com/terms/conditions/set_bool_is_true' in uris
assert 'http://example.com/terms/domain/conditions' in uris
16 changes: 16 additions & 0 deletions rdmo/questions/tests/test_viewset_questionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,19 @@ def test_detail_export(db, client, username, password, export_format):
assert root.tag == 'rdmo'
for child in root:
assert child.tag in ['questionset', 'question']


def test_detail_export_full(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['detail_export'], args=[90]) + 'xml/?full=true'
response = client.get(url)
assert response.status_code == status_map['detail']['editor'], response.content

root = et.fromstring(response.content)
assert root.tag == 'rdmo'

uris = [child.attrib[r'{http://purl.org/dc/elements/1.1/}uri'] for child in root]
assert 'http://example.com/terms/domain/blocks' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris
17 changes: 17 additions & 0 deletions rdmo/questions/tests/test_viewset_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,20 @@ def test_detail_export(db, client, username, password, export_format):
assert root.tag == 'rdmo'
for child in root:
assert child.tag in ['section', 'page', 'questionset', 'question']


def test_detail_export_full(db, client):
client.login(username='editor', password='editor')

url = reverse(urlnames['detail_export'], args=[5]) + 'xml/?full=true'
response = client.get(url)
assert response.status_code == status_map['detail']['editor'], response.content

root = et.fromstring(response.content)
assert root.tag == 'rdmo'

uris = [child.attrib[r'{http://purl.org/dc/elements/1.1/}uri'] for child in root]
assert 'http://example.com/terms/conditions/options_empty' in uris
assert 'http://example.com/terms/domain/conditions' in uris
assert 'http://example.com/terms/options/one_two_three' in uris
assert 'http://example.com/terms/options/one_two_three/one' in uris

0 comments on commit a0b05ed

Please sign in to comment.