pyceo/ceod/model/utils.py

43 lines
1.3 KiB
Python

from typing import Dict, List, Union
from ceo_common.model import Term
def bytes_to_strings(data: Dict[str, List[bytes]]) -> Dict[str, List[str]]:
"""Convert the attribute values from bytes to strings"""
return {
key: [b.decode() for b in val]
for key, val in data.items()
}
def strings_to_bytes(data: Dict[str, List[str]]) -> Dict[str, List[bytes]]:
"""Convert the attribute values from strings to bytes"""
return {
key: [b.encode() for b in val]
for key, val in data.items()
}
def dn_to_uid(dn: str) -> str:
"""Extract the UID from an LDAP DN.
Examples:
dn_to_uid('uid=ctdalek,ou=People,dc=csclub,dc=uwaterloo,dc=ca')
-> 'ctdalek'
"""
return dn.split(',', 1)[0].split('=')[1]
def should_be_club_rep(terms: Union[None, List[str]],
non_member_terms: Union[None, List[str]]) -> bool:
"""Returns True iff a user's most recent term was a non-member term."""
if not non_member_terms:
# no non-member terms => was only ever a member
return False
if not terms:
# no member terms => was only ever a club rep
return True
# decide using the most recent term (member or non-member)
return max(map(Term, non_member_terms)) > max(map(Term, terms))