def show(parameters): post = Post.find(Post.cxn, "posts", parameters["id"]) template = open('./templates/posts/show.html').read() comment_template = open('./templates/comments/show.html').read() show_post_script_tag = '<script src="/show_post.js"></script>' comments = post.comments(globals()) if comments: rendered_comments = "<h3>Comments</h3>" + "".join([TemplateEngine(comment_template, comment.attributes).render_partial() for comment in comments]) else: rendered_comments = '<p id="no_comments">No comments yet.</p>' new_comment_link_html = '<a id="new_comment_link" href="#">Make a new comment!</a>' parameters.update({"rendered_comments": rendered_comments, "new_comment_link": new_comment_link_html, "show_post_script_tag": show_post_script_tag}) return TemplateEngine(template, definitions(post, parameters)).render()
class UserModel(Model): def __init__(self, db, collection, obj): super(UserModel, self).__init__(db, collection, obj) self.username = obj['username'] self.password = obj['password'] self.voted = obj['voted'] self.posts = Post() self.comments = Comment() # Change password with authentication--username auth to be done # in middleware def change_password(self, oldpass, newpass): if oldpass == self.password: self.password = newpass self.collection.update({'_id': self.get_id()}, password=newpass) return True return False # Vote a post up, cannot do this more than once for any given post def vote_up(self, post_id): if not post_id in self.voted: p = self.posts.find_one(_id=post_id) p.vote_up() self.voted.append(post_id) self.collection.update({'_id': self.get_id()}, voted=self.voted) # Adds a post under the users page def add_post(self, **kwargs): return self.posts.insert(user=self.username, **kwargs) # Get blog posts made by this user, and with other arguments def get_posts(self, **kwargs): return self.posts.find(user=self.username, **kwargs) # Get comments made by the user, with other parameters def get_comments(self, **kwargs): return self.comments.find(user=self.username, **kwargs)