def test_created(self): location = "http://localhost/abc" r = http.created(location, [("Content-Type", "text/plain")], location) assert r.status.startswith("201") assert r.headers["Content-Type"] == "text/plain" assert r.headers["Location"] == location assert r.body == location
def create(self, request): """Create a new user.""" try: validator = Validator(email=unicode, display_name=unicode, password=unicode, _optional=('display_name', 'password')) arguments = validator(request) except ValueError as error: return http.bad_request([], str(error)) # We can't pass the 'password' argument to the user creation method, # so strip that out (if it exists), then create the user, adding the # password after the fact if successful. password = arguments.pop('password', None) try: user = getUtility(IUserManager).create_user(**arguments) except ExistingAddressError as error: return http.bad_request([], b'Address already exists {0}'.format( error.email)) if password is None: # This will have to be reset since it cannot be retrieved. password = generate(int(config.passwords.password_length)) scheme = lookup(config.passwords.password_scheme.upper()) user.password = make_secret(password, scheme) location = path_to('users/{0}'.format(user.user_id.int)) return http.created(location, [], None)
def test_created(self): location = 'http://localhost/abc' r = http.created(location, [('Content-Type', 'text/plain')], location) assert r.status.startswith('201') assert r.headers['Content-Type'] == 'text/plain' assert r.headers['Location'] == location assert r.body == location
def create(self, request): """Create a new member.""" service = getUtility(ISubscriptionService) try: validator = Validator( list_id=unicode, subscriber=subscriber_validator, display_name=unicode, delivery_mode=enum_validator(DeliveryMode), role=enum_validator(MemberRole), _optional=('delivery_mode', 'display_name', 'role')) member = service.join(**validator(request)) except AlreadySubscribedError: return http.conflict([], b'Member already subscribed') except NoSuchListError: return http.bad_request([], b'No such list') except InvalidEmailAddressError: return http.bad_request([], b'Invalid email address') except ValueError as error: return http.bad_request([], str(error)) # The member_id are UUIDs. We need to use the integer equivalent in # the URL. member_id = member.member_id.int location = path_to('members/{0}'.format(member_id)) # Include no extra headers or body. return http.created(location, [], None)
def create(self, request): session = create_session_from_request( request, hashlib.md5(''.join(map(str, [time(), random()]))).hexdigest() ) return http.created('/session/%s/' % session.key().name(), [], '')
def create(self, request): """Create a new mailing list.""" try: validator = Validator(fqdn_listname=unicode) mlist = create_list(**validator(request)) except ListAlreadyExistsError: return http.bad_request([], b'Mailing list exists') except BadDomainSpecificationError as error: return http.bad_request([], b'Domain does not exist {0}'.format( error.domain)) except ValueError as error: return http.bad_request([], str(error)) # wsgiref wants headers to be bytes, not unicodes. location = path_to('lists/{0}'.format(mlist.fqdn_listname)) # Include no extra headers or body. return http.created(location, [], None)
def create(self, request): """Create a new domain.""" domain_manager = getUtility(IDomainManager) try: validator = Validator(mail_host=unicode, description=unicode, base_url=unicode, contact_address=unicode, _optional=('description', 'base_url', 'contact_address')) domain = domain_manager.add(**validator(request)) except BadDomainSpecificationError: return http.bad_request([], b'Domain exists') except ValueError as error: return http.bad_request([], str(error)) location = path_to('domains/{0}'.format(domain.mail_host)) # Include no extra headers or body. return http.created(location, [], None)
def create(self, request): """POST to /addresses Add a new address to the user record. """ if self._user is None: return http.not_found() user_manager = getUtility(IUserManager) validator = Validator(email=unicode, display_name=unicode, _optional=('display_name',)) try: address = user_manager.create_address(**validator(request)) except ValueError as error: return http.bad_request([], str(error)) except InvalidEmailAddressError: return http.bad_request([], b'Invalid email address') except ExistingAddressError: return http.bad_request([], b'Address already exists') else: # Link the address to the current user and return it. address.user = self._user location = path_to('addresses/{0}'.format(address.email)) return http.created(location, [], None)