1# Copyright (C) 2011-2020 by the Free Software Foundation, Inc. 2# 3# This file is part of GNU Mailman. 4# 5# GNU Mailman is free software: you can redistribute it and/or modify it under 6# the terms of the GNU General Public License as published by the Free 7# Software Foundation, either version 3 of the License, or (at your option) 8# any later version. 9# 10# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT 11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13# more details. 14# 15# You should have received a copy of the GNU General Public License along with 16# GNU Mailman. If not, see <https://www.gnu.org/licenses/>. 17 18"""Preferences.""" 19 20from lazr.config import as_boolean 21from mailman.interfaces.member import DeliveryMode, DeliveryStatus 22from mailman.rest.helpers import ( 23 GetterSetter, bad_request, etag, no_content, not_found, okay) 24from mailman.rest.validator import ( 25 Validator, enum_validator, language_validator) 26from public import public 27 28 29PREFERENCES = ( 30 'acknowledge_posts', 31 'delivery_mode', 32 'delivery_status', 33 'hide_address', 34 'preferred_language', 35 'receive_list_copy', 36 'receive_own_postings', 37 ) 38 39 40@public 41class ReadOnlyPreferences: 42 """.../<object>/preferences""" 43 44 def __init__(self, parent, base_url): 45 self._parent = parent 46 self._base_url = base_url 47 48 def on_get(self, request, response): 49 resource = dict() 50 for attr in PREFERENCES: 51 # Handle this one specially. 52 if attr == 'preferred_language': 53 continue 54 value = getattr(self._parent, attr, None) 55 if value is not None: 56 resource[attr] = value 57 # Add the preferred language, if it's not missing. 58 preferred_language = self._parent.preferred_language 59 if preferred_language is not None: 60 resource['preferred_language'] = preferred_language.code 61 # Add the self link. 62 resource['self_link'] = self.api.path_to( 63 '{}/preferences'.format(self._base_url)) 64 okay(response, etag(resource)) 65 66 67@public 68class Preferences(ReadOnlyPreferences): 69 """Preferences which can be changed.""" 70 71 def patch_put(self, request, response, is_optional): 72 if self._parent is None: 73 not_found(response) 74 return 75 kws = dict( 76 acknowledge_posts=GetterSetter(as_boolean), 77 hide_address=GetterSetter(as_boolean), 78 delivery_mode=GetterSetter(enum_validator(DeliveryMode)), 79 delivery_status=GetterSetter(enum_validator(DeliveryStatus)), 80 preferred_language=GetterSetter(language_validator), 81 receive_list_copy=GetterSetter(as_boolean), 82 receive_own_postings=GetterSetter(as_boolean), 83 ) 84 if is_optional: 85 # For a PUT, all attributes are optional. 86 kws['_optional'] = kws.keys() 87 try: 88 Validator(**kws).update(self._parent, request) 89 except ValueError as error: 90 bad_request(response, str(error)) 91 else: 92 no_content(response) 93 94 def on_patch(self, request, response): 95 """Patch the preferences.""" 96 self.patch_put(request, response, is_optional=True) 97 98 def on_put(self, request, response): 99 """Change all preferences.""" 100 self.patch_put(request, response, is_optional=False) 101 102 def on_delete(self, request, response): 103 """Delete all preferences.""" 104 for attr in PREFERENCES: 105 if hasattr(self._parent, attr): 106 setattr(self._parent, attr, None) 107 no_content(response) 108