Skip to content

Commit

Permalink
显示当前登录用户;修正explore页面显示用户信息的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
toolib committed May 20, 2019
1 parent 5c0bd83 commit e18235b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
12 changes: 7 additions & 5 deletions handlers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from PIL import Image
from pycket.session import SessionMixin

from utils.account import add_post, get_all_posts, get_post
from utils.account import add_post, get_all_posts, get_post, get_posts_for
from utils.photo import UploadImage


Expand All @@ -11,16 +11,17 @@ def get_current_user(self):
return self.session.get('tudo_user', None)


class IndexHandler(tornado.web.RequestHandler):
class IndexHandler(BaseHandler):
"""
首页,用户上传图片的展示
"""
@tornado.web.authenticated
def get(self):
posts = get_all_posts()
posts = get_posts_for(self.current_user)
self.render('index.html', posts=posts)


class ExploreHandler(tornado.web.RequestHandler):
class ExploreHandler(BaseHandler):
"""
最近上传的缩略图片页面
"""
Expand All @@ -35,10 +36,11 @@ class PostHanlder(BaseHandler):
"""
def get(self, post_id):
post = get_post(post_id)
user = post.user
if not post:
self.write('wrong id {}'.format(post_id))
else:
self.render('post.html', post=post)
self.render('post.html', post=post, user=user)


class UploadHandler(BaseHandler):
Expand Down
5 changes: 5 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
</head>

<body>

{% if current_user %}
<h2>当前登录用户: {{ current_user }} </h2>
<hr>
{% end %}
{% block body %} this is tornado{% end %}
</body>

Expand Down
4 changes: 3 additions & 1 deletion templates/explore.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
{% block body %}
explore page <br>
{% for p in posts %}
<img src="{{ static_url(p.thumb_url) }}" />
<a href="{{ '/post/{}'.format(p.id) }}">
<img src="{{ static_url(p.thumb_url) }}" />
</a>
{% end %}
{% end %}
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
{% block body %}
index <br>
{% for p in posts %}
<img src="{{ static_url(p.image_url) }}" /> <br>
<a href="{{ '/post/{}'.format(p.id) }}"><img src="{{ static_url(p.image_url) }}" /></a> <br>
{% end %}
{% end %}
2 changes: 1 addition & 1 deletion templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@


{% block body %}
post id {{ post.id }} upload by {{ post.user.name }}<br>
post id {{ post.id }} upload by {{ user.name }}<br>
<img src="{{ static_url(post.image_url) }}" />
{% end %}
9 changes: 7 additions & 2 deletions utils/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from models.auth import User, Post
from models.db import Session

db_session = Session()

def hashed(text):
return hashlib.md5(text.encode('utf8')).hexdigest()
Expand Down Expand Up @@ -34,8 +35,12 @@ def add_post(image_url, thumb_url, username):
return post_id

def get_all_posts():
session = Session()
posts = session.query(Post).all()
posts = db_session.query(Post).all()
return posts

def get_posts_for(username):
user = db_session.query(User).filter_by(name=username).first()
posts = db_session.query(Post).filter_by(user=user).all()
return posts

def get_post(post_id):
Expand Down

0 comments on commit e18235b

Please sign in to comment.