summaryrefslogtreecommitdiff
path: root/src/mailman/rest/preferences.py
blob: 8e13fd44302dc4378d68341f866f2575e6a3c5bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Copyright (C) 2011-2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Preferences."""

__all__ = [
    'ReadOnlyPreferences',
    'Preferences',
    ]


from lazr.config import as_boolean
from mailman.interfaces.member import DeliveryMode, DeliveryStatus
from mailman.rest.helpers import (
    GetterSetter, bad_request, etag, no_content, not_found, okay, path_to)
from mailman.rest.validator import (
    Validator, enum_validator, language_validator)


PREFERENCES = (
    'acknowledge_posts',
    'delivery_mode',
    'delivery_status',
    'hide_address',
    'preferred_language',
    'receive_list_copy',
    'receive_own_postings',
    )



class ReadOnlyPreferences:
    """.../<object>/preferences"""

    def __init__(self, parent, base_url):
        self._parent = parent
        self._base_url = base_url

    def on_get(self, request, response):
        resource = dict()
        for attr in PREFERENCES:
            # Handle this one specially.
            if attr == 'preferred_language':
                continue
            value = getattr(self._parent, attr, None)
            if value is not None:
                resource[attr] = value
        # Add the preferred language, if it's not missing.
        preferred_language = self._parent.preferred_language
        if preferred_language is not None:
            resource['preferred_language'] = preferred_language.code
        # Add the self link.
        resource['self_link'] = path_to(
            '{0}/preferences'.format(self._base_url),
            self.api_version)
        okay(response, etag(resource))



class Preferences(ReadOnlyPreferences):
    """Preferences which can be changed."""

    def patch_put(self, request, response, is_optional):
        if self._parent is None:
            not_found(response)
            return
        kws = dict(
            acknowledge_posts=GetterSetter(as_boolean),
            hide_address = GetterSetter(as_boolean),
            delivery_mode=GetterSetter(enum_validator(DeliveryMode)),
            delivery_status=GetterSetter(enum_validator(DeliveryStatus)),
            preferred_language=GetterSetter(language_validator),
            receive_list_copy=GetterSetter(as_boolean),
            receive_own_postings=GetterSetter(as_boolean),
            )
        if is_optional:
            # For a PUT, all attributes are optional.
            kws['_optional'] = kws.keys()
        try:
            Validator(**kws).update(self._parent, request)
        except ValueError as error:
            bad_request(response, str(error))
        else:
            no_content(response)

    def on_patch(self, request, response):
        """Patch the preferences."""
        self.patch_put(request, response, is_optional=True)

    def on_put(self, request, response):
        """Change all preferences."""
        self.patch_put(request, response, is_optional=False)

    def on_delete(self, request, response):
        """Delete all preferences."""
        for attr in PREFERENCES:
            if hasattr(self._parent, attr):
                setattr(self._parent, attr, None)
        no_content(response)