SHARE
TWEET

Untitled

a guest Aug 1st, 2016 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2.  
  3. import webapp2
  4. import jinja2
  5.  
  6. from google.appengine.ext import db
  7.  
  8. template_dir = os.path.join(os.path.dirname(__file__), 'templates')
  9. jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
  10.                                 autoescape = True)
  11.  
  12. def render_str(template, **params):
  13.     t = jinja_env.get_template(template)
  14.     return t.render(params)
  15.  
  16. class Handler (webapp2.RequestHandler):
  17.  
  18.     def write(self, *a, **kw):
  19.         """
  20.        calls response.out.write, no need to write out every time
  21.        """
  22.         self.response.out.write(*a, **kw)
  23.  
  24.     def render_str(self, template, **params):
  25.         """
  26.        Takes a template name and returns a string of that rendered template
  27.        """
  28.         t = jinja_env.get_template(template)
  29.         return t.render(params)
  30.  
  31.     def render(self, template, **kw):
  32.         """
  33.        Calls write on the string from render_str
  34.        """
  35.         self.write(self.render_str(template, **kw))
  36.  
  37. class Post(db.Model):
  38.     subject = db.StringProperty(required=True) #particular type
  39.     content = db.TextProperty(required=True)
  40.     created = db.DateTimeProperty(auto_now_add=True)
  41.     last_modified = db.DateTimeProperty(auto_now=True)
  42.  
  43.     def render(self):
  44.         print ("[!] render in Post")
  45.         self._render_text = self.content.replace('\n', '<br>')
  46.         return render_str("post.html", p = self)
  47.  
  48. class NewPost(Handler):
  49.     def get(self):
  50.         #render form page
  51.         self.render("newpost.html")
  52.  
  53.     def post(self):
  54.         subject = self.request.get("subject")
  55.         content = self.request.get("content")
  56.  
  57.         if subject and content:
  58.             p = Post(subject=subject, content=content)
  59.             #add to the DB
  60.             p.put()
  61.  
  62.             self.redirect('/blog/%s' % str(p.key().id()))
  63.         else:
  64.             print ("[!] Error")
  65.             print (subject)
  66.             print(content)
  67.  
  68. class PostPage(Handler):
  69.     def get(self, post_id):
  70.         key = db.Key.from_path('Post', int(post_id) )
  71.         post = db.get(key)
  72.  
  73.         if not post:
  74.             self.Error(404)
  75.  
  76.         self.render("permalink.html", post = post)
  77.  
  78. class BlogFront(Handler):
  79.     def get(self):
  80.         posts = db.GqlQuery("select * from Post order by created desc limit 10")
  81.         self.render('front.html', posts = posts)
  82.  
  83. app = webapp2.WSGIApplication([
  84.                                 ("/blog/newpost", NewPost),
  85.                                 ('/blog/([0-9]+)', PostPage),
  86.                                 ('/blog/?', BlogFront),
  87.                             ], debug=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top