Skip to content

Commit

Permalink
PictureHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
dgnydn committed Apr 2, 2012
1 parent 05197e2 commit 8171a90
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
16 changes: 15 additions & 1 deletion app.py
Expand Up @@ -11,15 +11,29 @@
define("port",default=18888,type=int)

urls = [
# main handler
(r"/", handlers.MainHandler),

# auth
(r"/auth/register", handlers.RegisterHandler),
(r"/auth/login", handlers.LoginHandler),
(r"/auth/logout", handlers.LogoutHandler),
(r"/auth/twitter/?",handlers.TwitterHandler),

# api
(r"/api", handlers.ApiHandler),

# websocket backend
(r"/update",handlers.UpdateHandler),

# image server
(r"/p/(?P<picture>.*)", handlers.PictureHandler),
(r"/(?P<username>.*)", handlers.UserHandler),

# my profile page
(r"/profile", handlers.ProfileHandler),

# other users profile page
(r"/(?P<username>.*)", handlers.UserHandler)
]

settings = dict({
Expand Down
19 changes: 16 additions & 3 deletions handlers.py
Expand Up @@ -6,6 +6,7 @@
import tornado.auth
import tornado.websocket
import pymongo,gridfs
from helpers import *

class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
Expand Down Expand Up @@ -120,6 +121,11 @@ def post(self):
file_name = self.request.files["profile"][0]["filename"]
file_body = self.request.files["profile"][0]["body"]

file_type = type_of(file_body)
if not file_type or type_of(file_body) not in ["png","jpeg"]:
self.write("Profile image must be png or jpeg")
return

profile_image = "profile_images/{0}/{1}".format(user_name,file_name)
user = dict(
name= name,
Expand All @@ -129,7 +135,7 @@ def post(self):
profile = profile_image
)

self.fs.put(file_body, filename=profile_image)
self.fs.put(file_body, filename=profile_image,content_type=file_type)
self.db.users.save(user)
self.redirect("/auth/login")

Expand Down Expand Up @@ -171,7 +177,13 @@ def get(self,username):

class PictureHandler(BaseHandler):
def get(self,picture):
pass
profile = self.fs.get_last_version(picture)
if profile:
header = "image/%s" % profile.content_type
print profile.content_type
self.set_header("Content-Type",header)
self.write(profile.read())


class TwitterHandler(BaseHandler,tornado.auth.TwitterMixin):
@tornado.web.asynchronous
Expand Down Expand Up @@ -199,4 +211,5 @@ def _on_auth(self, user):
class ProfileHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("profile.html")
user = self.current_user
self.render("profile.html",user=user)
13 changes: 13 additions & 0 deletions helpers.py
@@ -0,0 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image
import StringIO

def type_of(content):
try:
i=Image.open(StringIO.StringIO(content))
return i.format.lower()
except IOError:
return False

20 changes: 7 additions & 13 deletions templates/profile.html
Expand Up @@ -19,7 +19,7 @@
<div class="container">
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="/{{ current_user['user_name'] }}">Profile</a></li>
<li><a href="/page/profile">Profile</a></li>
<li><a href="/auth/logout">Logout</a></li>
</ul>
<ul class="nav pull-right">
Expand All @@ -31,21 +31,15 @@

<div class="row-fluid">

<div class="span8 well">
<form method="post" action="/">
<textarea name="feed" class="input-xxlarge" placeholder="Karala Bakalım" rows="6"></textarea>
<input type="checkbox" name="twitter" value="true">Twitter
<br><input type="submit" class="btn">
</form>
<div class="span8">
User: {{ current_user["user_name"] }}<br>
Name: {{ current_user["name"] }}<br>
<img src="/p/{{ current_user['profile'] }}">
debug : {{ current_user }}
</div>

<div class="span3">
<div class="row-fluid">
<div class="span12" style="background-color:blue">sad</div>
</div>
<div class="row-fluid">
<div class="span12" style="background-color:yellow">sad</div>
</div>

</div>
</div>

Expand Down

0 comments on commit 8171a90

Please sign in to comment.