-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
131 lines (115 loc) · 4.89 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""A barebones Tornado application that uses Facebook for login.
Assumes a database with a schema as specified in schema.sql. We store a
local copy of basic user data so we don't need to make a round-trip to
the Facebook API on every request once a user has logged in.
"""
import facebook
#import tornado.database
import tornado.httpserver
import tornado.options
import tornado.web
from tornado.options import define, options
#shinriyo
from models import dbsession, engine, metadata
from sqlalchemy import Table, Column, String, Integer, MetaData, select, func
from sqlalchemy.sql import table, column
import os
FB_ID = os.environ.get('FB_ID', "default")
FB_SECRET = os.environ.get('FB_SECRET', "default")
define("port", default=8888, help="run on the given port", type=int)
define("facebook_app_id", default=FB_ID, help="Facebook Application ID")
define("facebook_app_secret",
default=FB_SECRET,
help="Facebook Application Secret")
#define("mysql_host", help="MySQL database host")
#define("mysql_database", help="MySQL database database")
#define("mysql_user", help="MySQL database user")
#define("mysql_password", help="MySQL database password")
""" prepare
try:
unicode # python2
except: # python3
pass
"""
class BaseHandler(tornado.web.RequestHandler):
"""Implements authentication via the Facebook JavaScript SDK cookie."""
def get_current_user(self):
cookies = dict((n, self.cookies[n].value) for n in self.cookies.keys())
cookie = facebook.get_user_from_cookie(
cookies, options.facebook_app_id, options.facebook_app_secret)
if not cookie:
return None
#user = self.db.get(
# "SELECT * FROM users WHERE id = %s", cookie["uid"])
#shinriyo
#user = engine.execute("SELECT * FROM users WHERE id = %s", cookie["uid"])
#sql="SELECT * FROM users WHERE id = %s"
#user = self.db.engine.execute(sql, (cookie["uid"]))
users = Table('users', metadata, autoload=True)
s = users.select(users.c.id == cookie["uid"])
rs = s.execute()
user = rs.fetchone()
if user is None:
#if not user:
# TODO: Make this fetch async rather than blocking
graph = facebook.GraphAPI(cookie["access_token"])
profile = graph.get_object("me")
#self.db.execute(
# "REPLACE INTO users (id, name, profile_url, access_token) "
# "VALUES (%s,%s,%s,%s)", profile["id"], profile["name"],
# profile["link"], cookie["access_token"])
#user = self.db.get(
# "SELECT * FROM users WHERE id = %s", profile["id"])
sql = "REPLACE INTO users (id, name, profile_url, access_token) VALUES (?,?,?,?)"
self.db.execute(sql,
(profile["id"], profile["name"], profile["link"], cookie["access_token"]))
users = Table('users', metadata, autoload=True)
s = users.select(users.c.id == profile["id"])
rs = s.execute()
user = rs.fetchone()
#elif user.access_token != cookie["access_token"]:
elif user["access_token"] != cookie["access_token"]:
#self.db.execute(
# "UPDATE users SET access_token = %s WHERE id = %s",
# cookie["access_token"], user.id)
#shinriyo
#self.db.execute("UPDATE users SET access_token = %s WHERE id = %s" % cookie["access_token"], user.id)
sql = "UPDATE users SET access_token = ? WHERE id = ?"
self.db.execute(sql, (cookie["access_token"], user.id))
return user
@property
def db(self):
if not hasattr(BaseHandler, "_db"):
#BaseHandler._db = tornado.database.Connection(
# host=options.mysql_host, database=options.mysql_database,
# user=options.mysql_user, password=options.mysql_password)
BaseHandler._db = engine.connect()
return BaseHandler._db
class MainHandler(BaseHandler):
def get(self):
self.render("example.html", options=options)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(tornado.web.Application([
(r"/", MainHandler),
]))
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()