Пример #1
0
    def post(self, uk):
        user_data = self.get_data()
        user_data['deviceid'] = "test-test-test"
        user = User()
        user.from_dict(user_data)
        user.generate_secret()
        user.generate_avatar_path()

        if user.save():
            self.set_status(201)
            self.set_header('Location', user.get_link())
        else:
            raise HTTPError(500, 'Save avatar user info error.')

        self.finish()
Пример #2
0
    def _on_auth(self, outer_user):
        if not outer_user:
            raise HTTPError(500, "Douban auth failed.")

        auth_id = "%s_%s" % (Auth.DOUBAN, outer_user['access_token']['douban_user_id'])
        auth = Auth.query.get(auth_id)
        user = User.query.get_by_userkey(self.get_secure_cookie('userkey', None)) or auth and auth.user
        self.clear_cookie('userkey')

        # create or update the user
        if user is None and auth is None:
            did = self.get_secure_cookie('uid', None)
            self.clear_cookie('uid')
            if not did:
                raise HTTPError(500, "Douban auth failed.")
            # user data
            user_data = {}
            user_data['userkey'] = auth_id
            user_data['name'] = outer_user['name']
            user_data['avatar'] = outer_user['avatar']
            user_data['brief'] = outer_user['brief']
            user_data['deviceid'] = did

            user = User()
            user.from_dict(user_data)

            user.generate_secret()
            if not user.save():
                raise HTTPError(500, 'Save auth user info error.')

        # auth data
        auth_data = {}
        auth_data['site_label'] = Auth.DOUBAN
        auth_data['access_token'] = outer_user['access_token']['key']
        auth_data['access_secret'] = outer_user['access_token']['secret']
        auth_data['expired'] = outer_user['expired']
        auth_data['user_id'] = user.id
        auth_data['site_user_id'] = auth_id

        # create or update the auth
        if auth is None:
            auth = Auth()

        auth.from_dict(auth_data)
        if not auth.save():
            raise HTTPError(500, "Failed auth with douban account.")

        # send to douban 
        #sns_data = {
        #        'token': auth.access_token,
        #        'secret': auth.access_secret,
        #        'label': auth.DOUBAN,
        #        'content': u"我正在使用乐帮,乐帮是一款基于LBS的帮助应用, 旨在让你在有困难时能更容易地得到帮助。请关注乐帮小站http://site.douban.com/135015/"
        #        }
        #http_client = httpclient.HTTPClient()
        #try:
        #    http_client.fetch(
        #            options.mquri,
        #            body="queue=snspost&value=%s" % self.json(sns_data),
        #            method='POST',
        #            )
        #except httpclient.HTTPError, e:
        #    pass

        self.render_json(auth.user.user2dict4auth() if auth.user.id>0 else {})
        self.finish()