import web
from config import setting
from datetime import datetime
import json

render = setting.render
db = setting.db

class add:
    def POST(self):
        """ add one """
        ret = {}
        data = web.input(_method="post")
        id = db.insert("piece",content=data["content"],addtime=datetime.now(),link=None)
        ret["code"] = 200
        ret["msg"] = {"id":id}
        return json.dumps(ret)

    def GET(self):
        """ get nothing """
        return "hello"

class index:
    def GET(self):
        piecesitr = db.select('piece',what="id,content",offset=0,limit=10)
        pieces=[]
        for piece in piecesitr:
            pieces.append(piece)
        jsdata = {"pieces":json.dumps(pieces)}
        data = {"jsdata":jsdata}
        return render.index(data)

class piece:
    def GET():
        return render.piece()

@@ -8,77 +8,163 @@
 web%0A
-from config import setting%0Afrom datetime import datetime%0Aimport json%0A
+import requests%0Afrom datetime import datetime%0Afrom config import setting%0Afrom model import user%0Afrom util import login%0Aimport json%0A%0Aconfig = setting.config
 %0Aren
@@ -252,24 +252,25 @@
 add one %22%22%22%0A
+%0A
         ret 
@@ -274,16 +274,47 @@
 et = %7B%7D%0A
+        if login.logged():%0A    
         
@@ -346,16 +346,20 @@
 %22post%22)%0A
+    
         
@@ -439,16 +439,20 @@
 k=None)%0A
+    
         
@@ -473,24 +473,28 @@
 200%0A        
+    
 ret%5B%22msg%22%5D =
@@ -516,77 +516,81 @@
     
-return json.dumps(ret)%0A%0A    def GET(self):%0A        %22%22%22 get nothing %22%22
+else:%0A            ret%5B%22code%22%5D = 403%0A            ret%5B%22msg%22%5D = %22access deny
 %22%0A  
@@ -606,15 +606,23 @@
 urn 
-%22hello%22
+json.dumps(ret)
 %0A%0Acl
@@ -643,32 +643,58 @@
  def GET(self):%0A
+        %22%22%22 app index %22%22%22%0A
         piecesit
@@ -914,16 +914,106 @@
 :jsdata%7D
+%0A%0A        cur_user = login.logged()%0A        if cur_user:%0A            data%5B%22user%22%5D=cur_user
 %0A       
@@ -1065,16 +1065,20 @@
 def GET(
+self
 ):%0A     
@@ -1102,8 +1102,1138 @@
 piece()%0A
+%0Aclass auth:%0A    def GET(self,name):%0A        input = web.input()%0A        if %22code%22 in input:%0A            data = %7B%0A                %22client_id%22:config%5B%22auth%22%5D%5B%22douban%22%5D%5B%22key%22%5D,%0A                %22client_secret%22:config%5B%22auth%22%5D%5B%22douban%22%5D%5B%22secret%22%5D,%0A                %22redirect_uri%22:config%5B%22auth%22%5D%5B%22douban%22%5D%5B%22callback%22%5D,%0A                %22grant_type%22:%22authorization_code%22,%0A                %22code%22:input%5B%22code%22%5D%0A            %7D%0A            res_access_token = requests.post(%22https://www.douban.com/service/auth2/token%22,data=data)%0A            res_json = res_access_token.json()%0A            if %22code%22 in res_json:%0A                return res_json%5B%22msg%22%5D%0A            access_token = res_json%5B%22access_token%22%5D%0A            res_user_info = requests.get(%22https://api.douban.com/v2/user/~me%22,headers=%7B%22Authorization%22:%22Bearer %22+access_token%7D)%0A            user_info = res_user_info.json()%0A            user_info%5B%22access_token%22%5D = access_token%0A            if not user.exist_douban_user(user_info):%0A                user.new_douban_user(user_info)%0A            user.login_douban_user(user_info)%0A            return %22logged%22%0A        else:%0A            return %22wrong%22

add auth controller