def get(self, blog_id): # get blog entry entry = BlogPost.get_by_id(int(blog_id)) # get user id uid = self.user and self.user.key().id() # Check if this user is allowed to like this post if entry.valid_author(self.user): error = "Authors aren't permitted to like their own posts" comments = Comment.get_comments(entry.comments) self.render('permalink.html', entry=entry, comments=comments, error=error) elif entry.user_already_liked(uid): error = "Users are only permitted to like a post once" comments = Comment.get_comments(entry.comments) self.render('permalink.html', entry=entry, comments=comments, error=error) else: # like the post entry.add_like(uid) entry.put() self.redirect('/blog/%d' % int(blog_id))
def topic_details(topic_id): topic = Topic.get_by_id(topic_id=topic_id) # get current user session_token = request.cookies.get("session_token") user = User.get_by_session_token(session_token=session_token) # START test background tasks TODO: delete this code if os.getenv('REDIS_URL'): from tasks import get_random_num get_random_num() # END test background tasks csrf_token = None if user: csrf_token = set_csrf_token(username=user.username) # get comments comments = Comment.get_comments(topic_id=topic_id) return render_template("topic/topic_details.html", topic=topic, user=user, csrf_token=csrf_token, comments=comments)
def blog_show(blog_id): blog = Blog.get(blog_id) user = get_user() if not user: is_join = False else: is_join = blog.get_is_join(user.id) comment_list = Comment.get_comments(blog_id) return header_render('blog_show.html', blog=blog, comment_list = comment_list, is_join = is_join)
def post_owner_wrapper(self): # Get blog id blog_id = self.request.get('blog_id') key = db.Key.from_path('BlogPost', int(blog_id)) blog_post = db.get(key) # Check for author of post if blog_post.valid_author(self.user): return function(self) else: # Invalid user msg = "Users can only edit or delete their own posts" comments = Comment.get_comments(blog_post.comments) self.render('permalink.html', entry=blog_post, comments=comments, error=msg)
def topic_details(topic_id): topic = Topic.get_by_id(topic_id=topic_id) # get current user session_token = request.cookies.get("session_token") user = User.get_by_session_token(session_token=session_token) csrf_token = None if user: csrf_token = set_csrf_token(username=user.username) # get comments comments = Comment.get_comments(topic_id=topic_id) return render_template("topic/topic_details.html", topic=topic, user=user, csrf_token=csrf_token, comments=comments)
def cmt_owner_wrapper(self, blog_id): # Get post from id key = db.Key.from_path('BlogPost', int(blog_id)) blog_post = db.get(key) # Get comment from page request cid = self.request.get('cid') key = db.Key.from_path('Comment', int(cid)) cmt = db.get(key) # Check user if cmt.valid_author(self.user): return function(self, blog_id) else: # Invalid user msg = "Users can only edit or delete comments they have made." comments = Comment.get_comments(blog_post.comments) self.render('permalink.html', entry=blog_post, comments=comments, error=msg)
def topic_details(topic_id): topic = Topic.get_by_id(topic_id=topic_id) # get current user session_token = request.cookies.get("session_token") user = User.get_by_session_token(session_token=session_token) csrf_token = None if user: csrf_token = str(uuid.uuid4()) # create CSRF token redis.set(name=user.username, value=csrf_token ) # store CSRF token into Redis for that specific user # get comments comments = Comment.get_comments(topic_id=topic_id) return render_template("topic/topic_details.html", topic=topic, user=user, csrf_token=csrf_token, comments=comments)
def get_memorial(url=None, user_id=None, create_if_none=True, init=False): from models.media import Media from models.comment import Comment memorial = None if url: # TODO - figure out how to condense into just a single db query. Either we have to build the Memorial obj from the tuple returned from exec, or research more on case sensitive searching using SQLAlchemy # https://bitbucket.org/mitsuhiko/flask/src/tip/examples/minitwit/minitwit.py --> db_query() memorial = db.session.execute('SELECT id FROM memorials WHERE BINARY memorials.url = :url and memorials.status < 5 LIMIT 1', { 'url': url } ).fetchone() if memorial: memorial = Memorial.query.filter_by(id=memorial[0]).first() memorial.media = Media.get_media(memorial.id, init=init, validate=False) memorial.comments = Comment.get_comments(memorial.id) else: if user_id > 0: memorial = Memorial.query.filter(Memorial.user_id == user_id, Memorial.status == 1).first() if memorial: memorial.media = Media.get_media(memorial.id, init=True, validate=False) elif create_if_none: memorial = Memorial(user_id, status=1) memorial.save_new_memorial() else: from lib import sessions memorial = Memorial(user_id, status=1) if sessions.save_photo('has'): memorial.media = Media(media_type=Media.media_types['photo'], filename=sessions.save_photo('get')) if memorial: # Need ordered to ensure validation goes through date checking in order from collections import OrderedDict memorial.errors = OrderedDict([ ('name', ''), ('dob', ''), ('dod', ''), ('cause', ''), ('epitaph', ''), ('funeral_type', ''), ('funeral_date', ''), ('funeral_loc', ''), ('media', '') ]) memorial.media = ( memorial.media if hasattr(memorial, 'media') and memorial.media else None ) #media = db.relationship('Media', backref = 'memorial', lazy = 'dynamic') return memorial
def get(self, blog_id): entry = BlogPost.get_by_id(int(blog_id)) comments = Comment.get_comments(entry.comments) self.render('permalink.html', entry=entry, comments=comments)