当前位置: 首页>>代码示例>>Python>>正文


Python User.get_or_insert方法代码示例

本文整理汇总了Python中models.User.User.get_or_insert方法的典型用法代码示例。如果您正苦于以下问题:Python User.get_or_insert方法的具体用法?Python User.get_or_insert怎么用?Python User.get_or_insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.User.User的用法示例。


在下文中一共展示了User.get_or_insert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get

# 需要导入模块: from models.User import User [as 别名]
# 或者: from models.User.User import get_or_insert [as 别名]
    def get(self):
        # doesn't make sense if the user is already logged in
        session = get_current_session()
        if session.is_active():
            return self.redirect('/tracker?err=You%20are%20already%20logged%20in.')

        # get a unique ID for the anonymous user
        str_id = str(db.allocate_ids(db.Key.from_path('User', 1), 1)[0])
        hashed_id = User.make_key_name(str_id, anon=True)
        user = User.get_or_insert(key_name=hashed_id, display_name='anonymous', email='')

        # start a session for the user
        session['my_dname'] = user.display_name
        session['my_id'] = hashed_id
        session['my_last_seen'] = int(time.mktime(user.last_seen.timetuple()))
        session['my_email'] = user.email

        self.redirect('/tracker')
开发者ID:dound,项目名称:CraigNotes,代码行数:20,代码来源:AnonLogin.py

示例2: post

# 需要导入模块: from models.User import User [as 别名]
# 或者: from models.User.User import get_or_insert [as 别名]
    def post(self):
        token = self.request.get('token')
        url = 'https://rpxnow.com/api/v2/auth_info'
        args = {
            'format': 'json',
            'apiKey': '78383d7d2030d84d6df056d5cd3fb962c88f18a4',
            'token': token
        }
        r = urlfetch.fetch(url=url,
                           payload=urllib.urlencode(args),
                           method=urlfetch.POST,
                           headers={'Content-Type':'application/x-www-form-urlencoded'})
        json = simplejson.loads(r.content)

        # close any active session the user has since he is trying to login
        session = get_current_session()
        old_uid = old_feed_infos = None
        if session.is_active():
            old_uid = session.get('my_id', 'x')
            if old_uid[0] == 'Z':
                old_feed_infos = get_feed_infos(None)
            session.terminate()

        redir_to = self.request.get('redir_to')
        if json['stat'] == 'ok':
            # extract some useful fields
            info = json['profile']
            oid = info['identifier']
            email = info.get('email', '')
            try:
                display_name = info['displayName']
            except KeyError:
                display_name = email.partition('@')[0]
            if not display_name:
                display_name = 'Unknown'

            # get the user's account (creating one if they did not previously have one)
            hashed_id = User.make_key_name(oid)
            user = User.get_or_insert(key_name=hashed_id, display_name=display_name, email=email)

            # start a session for the user
            session['my_dname'] = user.display_name
            session['my_id'] = hashed_id
            session['my_last_seen'] = int(time.mktime(user.last_seen.timetuple()))
            session['my_email'] = user.email

            # if the user was logged in anonymously, merge their info into this account
            more = ''
            if old_uid and old_uid[0] == 'Z':
                # 1) Add User.feeds and feed_names into this User
                old_feed_infos = [(of_name, of_feed.key().name()) for of_name, of_feed in old_feed_infos]
                new_feed_infos = zip(user.feed_names, user.feeds)
                changed = False
                for ofi in old_feed_infos:
                    if ofi not in new_feed_infos:
                        new_feed_infos.append(ofi)
                        changed = True
                if changed:
                    tfn, tf = zip(*new_feed_infos)
                    user.feed_names, user.feeds = list(tfn), list(tf)
                    user.put()
                    # clear memcache for this user since the value is stale if present
                    memcache.delete("user-feeds:%s" % hashed_id)

                # 2) Get the UserCmt for old_uid and move them to the logged in user account
                if old_feed_infos:
                    start_key, end_key = db.Key.from_path('UserCmt', old_uid), db.Key.from_path('UserCmt', old_uid+'\ufffd')
                    old_cmts = UserCmt.all().filter('__key__ >', start_key).filter('__key__ <', end_key).fetch(250)
                    new_cmts = [UserCmt(key_name='%s%s'%(hashed_id,cmt.cid), feeds=cmt.feeds, rating=cmt.rating, cmt=cmt.cmt, dt_hidden=cmt.dt_hidden) for cmt in old_cmts]
                    db.put(new_cmts)
                    db.delete(old_cmts)
                    if len(new_cmts) == 250:
                        more = '?info=We%20merged%20250%20comments%20so%20far%20and%20we%20will%20finish%20the%20rest%20soon.'
                        logging.error('Need to finish converting anonymous comments by uid %s to %s' % (old_uid, hashed_id))
                        # TODO: do this on the task queue instead of just raising an error

            if not redir_to or more:
                self.redirect('/tracker' + more)
            else:
                self.redirect(redir_to.replace('@[email protected]', '&'))
        else:
            msg = json['err']['msg']
            if not redir_to:
                qp = dict(login_error=msg)
            else:
                qp = dict(login_error=msg, redir_to=redir_to)
            self.redirect('/?' + urllib.urlencode(qp))
开发者ID:dound,项目名称:CraigNotes,代码行数:89,代码来源:RPX.py


注:本文中的models.User.User.get_or_insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。