Compatibility fixes.
[django-cas-provider.git] / cas_provider / attribute_formatters.py
1 from __future__ import unicode_literals
2
3 from lxml import etree
4 import collections
5
6 CAS_URI = 'http://www.yale.edu/tp/cas'
7 NSMAP = {'cas': CAS_URI}
8 CAS = '{%s}' % CAS_URI
9
10
11 try:
12     basestring
13 except NameError:
14     basestring = (str, bytes)
15
16
17 def jasig(auth_success, attrs):
18     attributes = etree.SubElement(auth_success, CAS + 'attributes')
19     style = etree.SubElement(attributes, CAS + 'attraStyle')
20     style.text = 'Jasig'
21     for name, value in sorted(attrs.items()):
22         if isinstance(value, collections.Iterable) and not isinstance(value, basestring):
23             for e in value:
24                 element = etree.SubElement(attributes, CAS + name)
25                 element.text = e
26         else:
27             element = etree.SubElement(attributes, CAS + name)
28             element.text = value
29
30
31 def ruby_cas(auth_success, attrs):
32     style = etree.SubElement(auth_success, CAS + 'attraStyle')
33     style.text = 'RubyCAS'
34     for name, value in sorted(attrs.items()):
35         if isinstance(value, collections.Iterable) and not isinstance(value, basestring):
36             for e in value:
37                 element = etree.SubElement(auth_success, CAS + name)
38                 element.text = e
39         else:
40             element = etree.SubElement(auth_success, CAS + name)
41             element.text = value
42
43
44 def name_value(auth_success, attrs):
45     etree.SubElement(auth_success, CAS + 'attribute', name='attraStyle', value='Name-Value')
46     for name, value in sorted(attrs.items()):
47         if isinstance(value, collections.Iterable) and not isinstance(value, basestring):
48             for e in value:
49                 etree.SubElement(auth_success, CAS + 'attribute', name=name, value=e)
50         else:
51             etree.SubElement(auth_success, CAS + 'attribute', name=name, value=value)