From 6cf06e915ee3d7f2ba709a424bd7569973fec2c3 Mon Sep 17 00:00:00 2001 From: Jason Robinson Date: Mon, 5 Sep 2016 23:19:34 +0300 Subject: [PATCH] Add Diaspora entity utility get_full_xml_representation --- CHANGELOG.md | 1 + federation/entities/diaspora/utils.py | 13 +++++++++++++ federation/tests/entities/diaspora/test_utils.py | 14 +++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37da5c5..13d5fff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ## Added - `Post.provider_display_name` is now supported in the entity outbound/inbound mappers. [#44](https://github.com/jaywink/social-federation/pull/44) - Add utility method `federation.utils.network.send_document` which is just a wrapper around `requests.post`. User agent will be added to the headers and exceptions will be silently captured and returned instead. [#45](https://github.com/jaywink/social-federation/pull/45) +- Add Diaspora entity utility `federation.entities.diaspora.utils.get_full_xml_representation`. Renders the entity XML document and wraps it in `...`. [#46](https://github.com/jaywink/social-federation/pull/46) ## [0.4.1] - 2016-09-04 diff --git a/federation/entities/diaspora/utils.py b/federation/entities/diaspora/utils.py index 28f1571..ee51cb5 100644 --- a/federation/entities/diaspora/utils.py +++ b/federation/entities/diaspora/utils.py @@ -48,3 +48,16 @@ def get_base_attributes(entity): if not attr.startswith("_"): attributes[attr] = getattr(entity, attr) return attributes + + +def get_full_xml_representation(entity): + """Get full XML representation of an entity. + + This contains the .. wrapper. + + Accepts either a Base entity or a Diaspora entity. + """ + from federation.entities.diaspora.mappers import get_outbound_entity + diaspora_entity = get_outbound_entity(entity) + xml = diaspora_entity.to_xml() + return "%s" % etree.tostring(xml).decode("utf-8") diff --git a/federation/tests/entities/diaspora/test_utils.py b/federation/tests/entities/diaspora/test_utils.py index 81db5b8..544054e 100644 --- a/federation/tests/entities/diaspora/test_utils.py +++ b/federation/tests/entities/diaspora/test_utils.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- +import re + from federation.entities.base import Post -from federation.entities.diaspora.utils import get_base_attributes +from federation.entities.diaspora.utils import get_base_attributes, get_full_xml_representation class TestGetBaseAttributes(object): @@ -10,3 +12,13 @@ class TestGetBaseAttributes(object): assert set(attrs) == { 'created_at', 'guid', 'handle', 'location', 'photos', 'provider_display_name', 'public', 'raw_content' } + + +class TestGetFullXMLRepresentation(object): + def test_returns_xml_document(self): + entity = Post() + document = get_full_xml_representation(entity) + document = re.sub(r".*", "", document) # Dates are annoying to compare + assert document == "" \ + "false" \ + ""