Example #1
0
 def add_post(self, request: foundation.Request) -> foundation.Response:
     """ Add a new Blog post. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1>Add new post</h1>
             <form action="/add" method="post">
                 <fieldset>
                     <legend>Add Post</legend>
                     <div class="clarfix">
                         <label for="title">Title</label>
                         <div class="input">
                             <input type="text" id="title" name="title"></input>
                         </div>
                     </div>
                     <div class="clarfix">
                         <label for="content">Content</label>
                         <div class="input">
                             <textarea id="content" name="content" class="xlarge" rows="3"></textarea>
                         </div>
                     </div>
                     <div class="actions">
                         <input class="btn primary" type="submit" name="submit" value="Add" />
                     </div>
                 </fieldset>
             </form>
         </body>
     </html>
     """)
Example #2
0
 def add_post(self, request: foundation.Request) -> foundation.Response:
     """ Add a new Blog post. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1>Add new post</h1>
             <form action="/add" method="post">
                 <fieldset>
                     <legend>Add Post</legend>
                     <div class="clarfix">
                         <label for="title">Title</label>
                         <div class="input">
                             <input type="text" id="title" name="title"></input>
                         </div>
                     </div>
                     <div class="clarfix">
                         <label for="content">Content</label>
                         <div class="input">
                             <textarea id="content" name="content" class="xlarge" rows="3"></textarea>
                         </div>
                     </div>
                     <div class="actions">
                         <input class="btn primary" type="submit" name="submit" value="Add" />
                     </div>
                 </fieldset>
             </form>
         </body>
     </html>
     """)
Example #3
0
 def list_posts(self, request: foundation.Request) -> foundation.Response:
     """ List summaries for posts added more recently. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1>List of posts</h1>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
         </body>
     </html>
     """)
Example #4
0
 def list_posts(self, request: foundation.Request) -> foundation.Response:
     """ List summaries for posts added more recently. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1>List of posts</h1>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
             <div>
                 <h2><a href="/post/1">Post title</a></h2>
                 <p>
                     Post summary (or the initial segment of post
                     content).
                 </p>
             </div>
         </body>
     </html>
     """)
Example #5
0
    def get_session_info(self, request: foundation.Request) -> (str, str):
        if hasattr(request, '_session_id'):
            id = request._session_id
            hash = request._session_hash
        else:
            cn = self.cookie_name
            if cn in request.cookies and \
                    request.cookies[cn][8:] == self.make_hash(
                        request.cookies[cn][:8]):
                id = request.cookies[self.cookie_name][:8]
                hash = request.cookies[self.cookie_name][8:]
            else:
                id = self.generate_id()
                hash = self.make_hash(id)

            request._session_id = id
            request._session_hash = hash

        return id, hash
Example #6
0
 def show_post(self, request: foundation.Request) -> foundation.Response:
     """ Show a post. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1><a href="/post/1">Post title</a></h1>
             <p>
                 Post content (full).
             </p>
         </body>
     </html>
     """)
Example #7
0
 def show_post(self, request: foundation.Request) -> foundation.Response:
     """ Show a post. """
     return request.response_factory(text="""
     <html>
         <body>
             <h1><a href="/post/1">Post title</a></h1>
             <p>
                 Post content (full).
             </p>
         </body>
     </html>
     """)
Example #8
0
    def compose_post(self, request: foundation.Request) -> foundation.Response:
        """ Present a form on the client browser used to compose a new Post.

        If the request method is ``POST`` this Web request handler attempt to
        process and persist the Blog post.

        :param request: The
            :class:`Web request <aurora.webapp.foundation.Request>` object.
        :return: A :class:`Web response <aurora.webapp.foundation.Response>`
            object.
        """
        if request.method == 'POST':
            # process form submission
            # TODO: need to implement form validation here.
            post = models.Post(
                title=request.POST['title'],
                content=request.POST['content'],
                author=self.get_profile_id(request),
                created=datetime.datetime.utcnow(),
                modified=datetime.datetime.utcnow(),
                published=datetime.datetime.utcnow(),
            )

            orm_session = orm.sessionmaker(bind=self.get_engine())()
            orm_session.add(post)
            orm_session.commit()

            # redirect to the post page
            resp = request.response_factory()
            resp.status_int = 302
            resp.location = self.url_for(_handler=self.show_post,
                                         id=str(post.id))

            return resp
        else:
            return self.render2response(request,
                                        'blog/form.html',
                                        blog=self,
                                        url_for=self.url_for)
Example #9
0
    def handler(self, request: foundation.Request) -> foundation.Response:
        """ Handle Web requests by serving static assets.
        """

        # resolve absolute file path name
        file_name = self._resolve_filename(request.GET['filename'])

        response = request.response_factory()

        # resolve file content type
        response.content_type, _ = mimetypes.guess_type(file_name)
        if not response.content_type:
            response.content_type = 'application/octet-stream'

        file = open(file_name, 'rb')
        fs = os.fstat(file.fileno())
        response.content_length = str(fs[6])
        response.last_modified = email_utils.formatdate(fs.st_mtime,
                                                        usegmt=True)
        response.body_file = file

        return response
Example #10
0
    def handler(self, request: foundation.Request) -> foundation.Response:
        """ Handle Web requests by serving static assets.
        """

        # resolve absolute file path name
        file_name = self._resolve_filename(request.GET['filename'])

        response = request.response_factory()

        # resolve file content type
        response.content_type, _ = mimetypes.guess_type(file_name)
        if not response.content_type:
            response.content_type = 'application/octet-stream'

        file = open(file_name, 'rb')
        fs = os.fstat(file.fileno())
        response.content_length = str(fs[6])
        response.last_modified = email_utils.formatdate(
            fs.st_mtime, usegmt=True
        )
        response.body_file = file

        return response
Example #11
0
    def compose_post(self, request: foundation.Request) -> foundation.Response:
        """ Present a form on the client browser used to compose a new Post.

        If the request method is ``POST`` this Web request handler attempt to
        process and persist the Blog post.

        :param request: The
            :class:`Web request <aurora.webapp.foundation.Request>` object.
        :return: A :class:`Web response <aurora.webapp.foundation.Response>`
            object.
        """
        if request.method == 'POST':
            # process form submission
            # TODO: need to implement form validation here.
            post = models.Post(
                title=request.POST['title'],
                content=request.POST['content'],
                author=self.get_profile_id(request),
                created = datetime.datetime.utcnow(),
                modified = datetime.datetime.utcnow(),
                published=datetime.datetime.utcnow(),
            )

            orm_session = orm.sessionmaker(bind=self.get_engine())()
            orm_session.add(post)
            orm_session.commit()

            # redirect to the post page
            resp = request.response_factory()
            resp.status_int = 302
            resp.location = self.url_for(_handler=self.show_post,
                id=str(post.id))

            return resp
        else:
            return self.render2response(request, 'blog/form.html', blog=self,
                url_for=self.url_for)
Example #12
0
    def add_post(self, request: foundation.Request) -> foundation.Response:
        """ Add a new Blog post. """
        if request.method == 'POST':
            # process form submission
            # TODO: need to implement form validation here.
            post = models.Post(
                title=request.POST['title'],
                content=request.POST['content'],
                author='',
                date=datetime.datetime.utcnow(),
            )

            orm_session = orm.sessionmaker(bind=self.db.get_engine())()
            orm_session.add(post)
            orm_session.commit()

            # redirect to the post page
            resp = request.response_factory()
            resp.status_int = 302
            resp.location = request.application_url

            return resp
        else:
            return self.views.render2response(request, 'form.html', blog=self)
Example #13
0
    def render2response(self, request: foundation.Request, template_name: str,
                        **context) -> foundation.Response:
        """ Render a template into a :class:`~aurora.webapp.foundation.Response` object with context.

        Template file names must have two extensions. The first extension is
        used to identify the content type of the output and the second
        extension is used to identify the engine capable of handling
        correctly the syntax used in the template.

        :param request: The request object used to build the response.
        :param template_name: The relative template name string without the
            last extension.
        :param context: The context mapping.
        :return: The rendered :class:`~aurora.webapp.foundation.Response`
            object.
        """
        response = request.response_factory(
            text=self.render(template_name, request=request, **context))

        response.content_type, _ = mimetypes.guess_type(template_name)
        if not response.content_type:
            response.content_type = self.DEFAULT_MIME_TYPE

        return response
Example #14
0
    def add_post(self, request: foundation.Request) -> foundation.Response:
        """ Add a new Blog post. """
        if request.method == 'POST':
            # process form submission
            # TODO: need to implement form validation here.
            post = models.Post(
                title=request.POST['title'],
                content=request.POST['content'],
                author='',
                date=datetime.datetime.utcnow(),
            )

            orm_session = orm.sessionmaker(bind=self.db.get_engine())()
            orm_session.add(post)
            orm_session.commit()

            # redirect to the post page
            resp = request.response_factory()
            resp.status_int = 302
            resp.location = request.application_url

            return resp
        else:
            return self.views.render2response(request, 'form.html', blog=self)
Example #15
0
    def render2response(self, request: foundation.Request, template_name: str,
                        **context) -> foundation.Response:
        """ Render a template into a :class:`~aurora.webapp.foundation.Response` object with context.

        Template file names must have two extensions. The first extension is
        used to identify the content type of the output and the second
        extension is used to identify the engine capable of handling
        correctly the syntax used in the template.

        :param request: The request object used to build the response.
        :param template_name: The relative template name string without the
            last extension.
        :param context: The context mapping.
        :return: The rendered :class:`~aurora.webapp.foundation.Response`
            object.
        """
        response = request.response_factory(
            text=self.render(template_name, request=request, **context))

        response.content_type, _ = mimetypes.guess_type(template_name)
        if not response.content_type:
            response.content_type = self.DEFAULT_MIME_TYPE

        return response