コード例 #1
0
ファイル: user.py プロジェクト: Zhinsta/Zhinsta
    def get(self, ukey):
        next_url = request.args.get('next_url', None)
        if next_url and 'instagram' not in next_url:
            next_url = signer.loads(next_url)
        api = InstagramAPI(access_token=request.access_token)

        user = gevent.spawn(wrap_errors(InstagramAPIError, api.user),
                            user_id=ukey)
        feeds = gevent.spawn(wrap_errors(InstagramAPIError,
                             api.user_recent_media),
                             user_id=ukey, with_next_url=next_url)
        if request.ukey:
            isfollows = spawn(isfollow, ukey, api)
        else:
            isfollows = spawn(lambda x: False, ukey)

        gevent.joinall([user, feeds, isfollows])
        user, feeds, isfollows = user.get(), feeds.get(), isfollows.get()
        errors = [e for e in (user, feeds, isfollows)
                  if isinstance(e, InstagramAPIError)]
        if errors:
            if any([e.error_type == 'APINotAllowedError' for e in errors]):
                return render('profile-noauth.html', ukey=ukey)
            if any([e.error_type == 'APINotFoundError' for e in errors]):
                return notfound(u'用户不存在')
            app.logger.error([str(e) for e in errors])
            return apierror(u'服务器暂时出问题了')

        next_url = feeds[1] if feeds else None
        next_url = signer.dumps(next_url) if next_url else next_url
        feeds = feeds[0] if feeds else []
        isme = False
        if request.ukey and ukey == request.ukey:
            isme = True
        return render(
            'profile.html',
            user=user,
            feeds=feeds,
            isme=isme,
            isfollow=isfollows,
            next_url=next_url
        )
コード例 #2
0
ファイル: user.py プロジェクト: Zhinsta/Zhinsta
 def get(self):
     if has_login():
         return redirect(url_for('view.show'))
     redirect_url = url_for('view.show')
     code = request.args.get('code', '')
     redirect_uri = INSTAGRAM_REDIRECT_URI
     if request.args.get('uri', ''):
         redirect_url = request.args.get('uri')
         redirect_uri += '?uri=' + redirect_url
     api = InstagramAPI(client_id=INSTAGRAM_CLIENT_ID,
                        client_secret=INSTAGRAM_CLIENT_SECRET,
                        redirect_uri=redirect_uri)
     try:
         access_token = api.exchange_code_for_access_token(code)
     except:
         return apierror()
     user = (UserModel.query
             .filter_by(ukey=access_token[1]['id']).first())
     if user:
         user.access_token = access_token[0]
         user.username = access_token[1]['username']
         user.pic = access_token[1]['profile_picture']
     else:
         user = UserModel(ukey=access_token[1]['id'],
                          username=access_token[1]['username'],
                          pic=access_token[1]['profile_picture'],
                          access_token=access_token[0])
         db.session.add(user)
         redirect_url = url_for('view.welcome')
     db.session.commit()
     admin = AdminModel.query.get(user.ukey)
     session.permanent = True
     session['ukey'] = user.ukey
     session['username'] = user.username
     session['access_token'] = user.access_token
     session['is_admin'] = True if admin else False
     return redirect(redirect_url)