8000 Allow appending plain text to twiml nodes · chiewxia/twilio-python@cb28ddc · GitHub
[go: up one dir, main page]

Skip to content

Commit cb28ddc

Browse files
author
Ethan Karson
committed
Allow appending plain text to twiml nodes
1 parent 47b66ad commit cb28ddc

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

tests/unit/twiml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def strip(self, xml):
1818
@raises(TwiMLException)
1919
def test_append_fail(self):
2020
t = TwiML()
21-
t.append('foobar')
21+
t.append(12345)
2222

2323
def test_format_language_none(self):
2424
language = None

tests/unit/twiml/test_messaging_response.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,24 @@ def test_redirect(self):
8888
self.strip(r),
8989
'<?xml version="1.0" encoding="UTF-8"?><Response><Redirect>example.com</Redirect></Response>'
9090
)
91+
92+
class TestTest(TwilioTest):
93+
def test_text(self):
94+
r = MessagingResponse()
95+
r.append('No tags!')
96+
97+
assert_equal(
98+
self.strip(r),
99+
'<?xml version="1.0" encoding="UTF-8"?><Response>No tags!</Response>'
100+
)
101+
102+
def text_mixed(self):
103+
r = MessagingResponse()
104+
r.append('before')
105+
r.append(Body('Content'))
106+
r.append('after')
107+
108+
assert_equal(
109+
self.strip(r),
110+
'<?xml version="1.0" encoding="UTF-8"?><Response>before<Body>Content</Body>after</Response>'
111+
)

twilio/twiml/__init__.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
# coding=utf-8
2-
"""
3-
This code was generated by
4-
\ / _ _ _| _ _
5-
| (_)\/(_)(_|\/| |(/_ v1.0.0
6-
/ /
7-
"""
8-
91
import json
102
import re
113
import xml.etree.ElementTree as ET
@@ -79,10 +71,7 @@ def append(self, verb):
7971
8072
:returns: self
8173
"""
82-
if not isinstance(verb, TwiML):
83-
raise TwiMLException('Only appending of TwiML is allowed')
84-
85-
self.verbs.append(verb)
74+
self.nest(verb)
8675
return self
8776

8877
def nest(self, verb):
@@ -93,8 +82,8 @@ def nest(self, verb):
9382
9483
:returns: the TwiML verb
9584
"""
96-
if not isinstance(verb, TwiML):
97-
raise TwiMLException('Only nesting of TwiML is allowed')
85+
if not isinstance(verb, TwiML) and not isinstance(verb, str):
86+
raise TwiMLException('Only nesting of TwiML and strings are allowed')
9887

9988
self.verbs.append(verb)
10089
return verb
@@ -118,7 +107,16 @@ def xml(self):
118107

119108
el.text = self.value
120109

110+
last_child = None
111+
121112
for verb in self.verbs:
122-
el.append(verb.xml())
113+
if isinstance(verb, str):
114+
if last_child:
115+
last_child.tail = verb
116+
else:
117+
el.text = verb
118+
else:
119+
last_child = verb.xml()
120+
el.append(last_child)
123121

124122
return el

0 commit comments

Comments
 (0)
0