d61af0f7c548c5e2ef9dd2d17936ece574836d9a
[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 def jasig(auth_success, attrs):
10     attributes = etree.SubElement(auth_success, CAS + 'attributes')
11     style = etree.SubElement(attributes, CAS + 'attraStyle')
12     style.text = u'Jasig'
13     for name, value in attrs.items():
14         if isinstance(value, collections.Iterable):
15             for e in value:
16                 element = etree.SubElement(attributes, CAS + name)
17                 element.text = e
18         else:
19             element = etree.SubElement(attributes, CAS + name)
20             element.text = value
21
22
23 def ruby_cas(auth_success, attrs):
24     style = etree.SubElement(auth_success, CAS + 'attraStyle')
25     style.text = u'RubyCAS'
26     for name, value in attrs.items():
27         if isinstance(value, list):
28             for e in value:
29                 element = etree.SubElement(auth_success, CAS + name)
30                 element.text = e
31         else:
32             element = etree.SubElement(auth_success, CAS + name)
33             element.text = value
34
35
36 def name_value(auth_success, attrs):
37     etree.SubElement(auth_success, CAS + 'attribute', name=u'attraStyle', value=u'Name-Value')
38     for name, value in attrs.items():
39         if isinstance(value, list):
40             for e in value:
41                 etree.SubElement(auth_success, CAS + 'attribute', name=name, value=e)
42         else:
43             etree.SubElement(auth_success, CAS + 'attribute', name=name, value=value)