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