remove mailman transactions

This commit is contained in:
Max Erenberg 2021-08-03 20:11:13 +00:00
parent 96cb2bc808
commit 4a312378b7
6 changed files with 29 additions and 66 deletions

View File

@ -8,3 +8,11 @@ class GroupNotFoundError(Exception):
class BadRequest(Exception):
pass
class UserAlreadySubscribedError(Exception):
pass
class UserNotSubscribedError(Exception):
pass

View File

@ -1,7 +1,9 @@
from flask import Blueprint
from zope import component
from .utils import authz_restrict_to_staff, create_sync_response
from ceod.transactions.mailman import SubscribeMemberTransaction, UnsubscribeMemberTransaction
from .utils import authz_restrict_to_staff
from ceo_common.errors import UserAlreadySubscribedError, UserNotSubscribedError
from ceo_common.interfaces import IMailmanService
bp = Blueprint('mailman', __name__)
@ -9,12 +11,20 @@ bp = Blueprint('mailman', __name__)
@bp.route('/<mailing_list>/<username>', methods=['POST'])
@authz_restrict_to_staff
def subscribe(mailing_list, username):
txn = SubscribeMemberTransaction(username, mailing_list)
return create_sync_response(txn)
mailman_srv = component.getUtility(IMailmanService)
try:
mailman_srv.subscribe(username, mailing_list)
except UserAlreadySubscribedError as err:
return {'error': str(err)}, 409
return {'result': 'OK'}
@bp.route('/<mailing_list>/<username>', methods=['DELETE'])
@authz_restrict_to_staff
def unsubscribe(mailing_list, username):
txn = UnsubscribeMemberTransaction(username, mailing_list)
return create_sync_response(txn)
mailman_srv = component.getUtility(IMailmanService)
try:
mailman_srv.unsubscribe(username, mailing_list)
except UserNotSubscribedError as err:
return {'error': str(err)}, 404
return {'result': 'OK'}

View File

@ -3,6 +3,7 @@ from requests.auth import HTTPBasicAuth
from zope import component
from zope.interface import implementer
from ceo_common.errors import UserAlreadySubscribedError, UserNotSubscribedError
from ceo_common.interfaces import IMailmanService, IConfig
@ -32,6 +33,8 @@ class MailmanService:
},
auth=HTTPBasicAuth(self.api_username, self.api_password),
)
if resp.status_code == 409:
raise UserAlreadySubscribedError(resp.json()['description'])
resp.raise_for_status()
def unsubscribe(self, address: str, mailing_list: str):
@ -48,4 +51,6 @@ class MailmanService:
},
auth=HTTPBasicAuth(self.api_username, self.api_password),
)
if resp.status_code == 404:
raise UserNotSubscribedError('user is not subscribed')
resp.raise_for_status()

View File

@ -1,29 +0,0 @@
from ..AbstractTransaction import AbstractTransaction
from zope import component
from ceo_common.interfaces import IMailmanService
class SubscribeMemberTransaction(AbstractTransaction):
"""Transaction to subscribe a member to a mailing list."""
operations = [
'subscribe_to_mailing_list',
]
def __init__(self, address: str, mailing_list: str):
"""
:param address: a username or email address
:param mailing_list: the list to which the user will be subscribed
"""
super().__init__()
self.address = address
self.mailing_list = mailing_list
self.mailman_srv = component.getUtility(IMailmanService)
def child_execute_iter(self):
self.mailman_srv.subscribe(self.address, self.mailing_list)
yield 'subscribe_to_mailing_list'
self.finish('OK')

View File

@ -1,29 +0,0 @@
from ..AbstractTransaction import AbstractTransaction
from zope import component
from ceo_common.interfaces import IMailmanService
class UnsubscribeMemberTransaction(AbstractTransaction):
"""Transaction to unsubscribe a member from a mailing list."""
operations = [
'unsubscribe_from_mailing_list',
]
def __init__(self, address: str, mailing_list: str):
"""
:param address: a username or email address
:param mailing_list: the list from which the user will be unsubscribed
"""
super().__init__()
self.address = address
self.mailing_list = mailing_list
self.mailman_srv = component.getUtility(IMailmanService)
def child_execute_iter(self):
self.mailman_srv.unsubscribe(self.address, self.mailing_list)
yield 'unsubscribe_to_mailing_list'
self.finish('OK')

View File

@ -1,2 +0,0 @@
from .SubscribeMemberTransaction import SubscribeMemberTransaction
from .UnsubscribeMemberTransaction import UnsubscribeMemberTransaction