Add per list member roster visibility option
[mailman.git] / src / mailman / rest / preferences.py
blobced517d3bfada37653aa728906782793b1344080
1 # Copyright (C) 2011-2019 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
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.
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.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Preferences."""
20 from lazr.config import as_boolean
21 from mailman.interfaces.member import DeliveryMode, DeliveryStatus
22 from mailman.rest.helpers import (
23 GetterSetter, bad_request, etag, no_content, not_found, okay)
24 from mailman.rest.validator import (
25 Validator, enum_validator, language_validator)
26 from public import public
29 PREFERENCES = (
30 'acknowledge_posts',
31 'delivery_mode',
32 'delivery_status',
33 'hide_address',
34 'preferred_language',
35 'receive_list_copy',
36 'receive_own_postings',
40 @public
41 class ReadOnlyPreferences:
42 """.../<object>/preferences"""
44 def __init__(self, parent, base_url):
45 self._parent = parent
46 self._base_url = base_url
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))
67 @public
68 class Preferences(ReadOnlyPreferences):
69 """Preferences which can be changed."""
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),
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)
94 def on_patch(self, request, response):
95 """Patch the preferences."""
96 self.patch_put(request, response, is_optional=True)
98 def on_put(self, request, response):
99 """Change all preferences."""
100 self.patch_put(request, response, is_optional=False)
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)