def post(self, user): # grab the form form = CommentForm(self.request.params) if not form.validate(): form.csrf_token.data = self.generate_csrf() self.r(form) return # get the comment try: comment = Key(urlsafe=form.key.data).get() except: # invalid key comment = None if comment is None: self.redirect('/') return if comment.author != user: self.redirect('/') return # better be a post here or else! post = comment.key.parent().get() if post is None: self.redirect('/') return # update the comment try: comment.content = form.comment.data comment.put() self.redirect('/post/view?key=%s' % post.key.urlsafe()) return except: # let's give them another chance form.csrf_token.data = self.generate_csrf() self.r(form, flashes=flash()) return
def post(self, user): # grab the form form = PostEditForm(self.request.params) # validate csrf if not self.validate_csrf(form.csrf_token.data): form.csrf_token.data = self.generate_csrf() self.r(form, flashes=flash('Please submit the form again.')) return # validate form if not form.validate(): self.r(form) return # get the post please post = Key(urlsafe=form.key.data).get() if post is None: self.r(flashes=flash('Post does not exist')) return # check if the user is the owner or not if post.author != user: self.redirect('/') return try: t = form.title.data post.title = t post.title_lower = lower(t) post.subject = form.subject.data post.content = form.content.data post.put() self.redirect('/author/%s' % post.author) return except Exception as e: form.csrf_token.data = self.generate_csrf() self.r(form) return