Exemplo n.º 1
0
    def put(self, request):

        ''' Create a new custom URL. If the url is already in use, error.'''

        url_key = ndb.Key('CustomURL', request.slug)
        custom_url = url_key.get()

        if custom_url != None:
            # Url is already in use.
            raise remote.ApplicationError('Custom URL already exists.')

        target = ndb.Key(urlsafe=self.decrypt(request.target))
        target_obj = target.get()
        if not target_obj:
            # Target object not found.
            raise remote.ApplicationError('put url requires a valid target')

        custom_url = CustomURL(key=url_key, slug=request.slug, target=target)
        custom_url.put()

        target_obj.customurl = url_key
        target_obj.put()

        return custom_url.to_message()