pyceo/ceod/transactions/members/RenewMemberTransaction.py

43 lines
1.2 KiB
Python

from typing import Union, List
from zope import component
from ..AbstractTransaction import AbstractTransaction
from ceo_common.errors import BadRequest
from ceo_common.interfaces import ILDAPService
class RenewMemberTransaction(AbstractTransaction):
"""Transaction to renew a user's terms or non-member terms."""
operations = [
'add_terms',
'add_non_member_terms',
]
def __init__(
self,
username: str,
terms: Union[List[str], None],
non_member_terms: Union[List[str], None],
):
super().__init__()
self.username = username
if (terms and non_member_terms) or not (terms or non_member_terms):
raise BadRequest('Must specify either terms or non-member terms')
self.terms = terms
self.non_member_terms = non_member_terms
self.ldap_srv = component.getUtility(ILDAPService)
def child_execute_iter(self):
user = self.ldap_srv.get_user(self.username)
if self.terms:
user.add_terms(self.terms)
yield 'add_terms'
elif self.non_member_terms:
user.add_non_member_terms(self.non_member_terms)
yield 'add_non_member_terms'
self.finish('OK')