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> """)
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> """)
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
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> """)
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)
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
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
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)
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)
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