~abompard/mailman/pgsql_upgrade

« back to all changes in this revision

Viewing changes to src/mailman/rest/addresses.py

  • Committer: Barry Warsaw
  • Date: 2014-04-15 03:12:01 UTC
  • mfrom: (7240.1.1 floaddr)
  • Revision ID: barry@list.org-20140415031201-td1fuuyvptco2pqy
 * Addresses can be added to existing users, including display names, via the
   REST API.  [Florian Fuchs]

Also, email addresses are validated when they are added via the IUserManager
interface.  They are still also validated when subscribing to a mailing list,
but that is redundant.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from restish import http, resource
32
32
from zope.component import getUtility
33
33
 
 
34
from mailman.interfaces.address import (
 
35
    ExistingAddressError, InvalidEmailAddressError)
 
36
from mailman.interfaces.usermanager import IUserManager
34
37
from mailman.rest.helpers import CollectionMixin, etag, no_content, path_to
35
38
from mailman.rest.members import MemberCollection
36
39
from mailman.rest.preferences import Preferences
37
 
from mailman.interfaces.usermanager import IUserManager
 
40
from mailman.rest.validator import Validator
38
41
from mailman.utilities.datetime import now
39
42
 
40
43
 
174
177
        resource = self._make_collection(request)
175
178
        return http.ok([], etag(resource))
176
179
 
 
180
    @resource.POST()
 
181
    def create(self, request):
 
182
        """POST to /addresses
 
183
 
 
184
        Add a new address to the user record.
 
185
        """
 
186
        if self._user is None:
 
187
            return http.not_found()
 
188
        user_manager = getUtility(IUserManager)
 
189
        validator = Validator(email=unicode,
 
190
                              display_name=unicode,
 
191
                              _optional=('display_name',))
 
192
        try:
 
193
            address = user_manager.create_address(**validator(request))
 
194
        except ValueError as error:
 
195
            return http.bad_request([], str(error))
 
196
        except InvalidEmailAddressError:
 
197
            return http.bad_request([], b'Invalid email address')
 
198
        except ExistingAddressError:
 
199
            return http.bad_request([], b'Address already exists')
 
200
        else:
 
201
            # Link the address to the current user and return it.
 
202
            address.user = self._user
 
203
            location = path_to('addresses/{0}'.format(address.email))
 
204
            return http.created(location, [], None)
 
205
 
177
206
 
178
207
 
179
208
def membership_key(member):