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


Python Vote.all方法代码示例

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


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

示例1: prefetch_posts_list

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def prefetch_posts_list(posts):
  prefetch_refprops(posts, Post.user)
  posts_keys = [str(post.key()) for post in posts]

  # get user, if no user, all already_voted = no
  session = get_current_session()
  if session.has_key('user'):
    user = session['user']
    memcache_voted_keys = ["vp_" + post_key + "_" + str(user.key()) for post_key in posts_keys]
    memcache_voted = memcache.get_multi(memcache_voted_keys)
    memcache_to_add = {}
    for post in posts:
      vote_value = memcache_voted.get("vp_" + str(post.key()) + "_" +str(user.key()))
      if vote_value is not None:
        post.prefetched_already_voted = vote_value == 1
      else:
        vote = Vote.all().filter("user =", user).filter("post =", post).fetch(1)
        memcache_to_add["vp_" + str(post.key()) + "_" + str(user.key())] = len(vote)
        post.prefetched_already_voted = len(vote) == 1
    if memcache_to_add.keys():
      memcache.add_multi(memcache_to_add, 3600)
  else:
    for post in posts:
      post.prefetched_already_voted = False
  # now the sum_votes
  memcache_sum_votes_keys = ["p_" + post_key for post_key in posts_keys]
  memcache_sum_votes = memcache.get_multi(memcache_sum_votes_keys)
  memcache_to_add = {}
  for post in posts:
    sum_votes_value = memcache_sum_votes.get("p_" + str(post.key()))
    if sum_votes_value is not None:
      post.prefetched_sum_votes = sum_votes_value
    else:
      sum_votes = Vote.all().filter("post =", post).count()
      memcache_to_add["p_" + str(post.key())] = sum_votes
      post.prefetched_sum_votes = sum_votes
  if memcache_to_add.keys():
    memcache.add_multi(memcache_to_add, 3600)
  # finally we get all the comment count from memcache
  memcache_comment_count_keys = ["pc_" + post_key for post_key in posts_keys]
  memcache_comment_count = memcache.get_multi(memcache_comment_count_keys)
  memcache_to_add = {}
  for post in posts:
    comment_count = memcache_comment_count.get("pc_" + str(post.key()))
    if comment_count is not None:
      post.cached_comment_count = comment_count
    else:
      comment_count = post.comments.count() 
      memcache_to_add["pc_" + str(post.key())] = comment_count
      post.cached_comment_count = comment_count 
  if memcache_to_add.keys():
    memcache.add_multi(memcache_to_add, 3600)
开发者ID:DFectuoso,项目名称:Noticias-HAcker,代码行数:54,代码来源:prefetch.py

示例2: vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def vote(request, id):
    """
    Add a vote for a feedback message, but not twice from the same IP.
    """
    referer = request.META.get('HTTP_REFERER', '/feedback/')
    redirect = HttpResponseRedirect(referer)
    # Check if the selected feedback exists.
    feedback = Feedback.get_by_id(int(id))
    if feedback is None:
        logging.debug("Feedback '%s' not found." % id)
        return redirect
    # Check if this feedback was posted from the same IP.
    ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
    if feedback.ip == ip:
        logging.debug("Feedback '%s' was posted from the same IP." % id)
        return redirect
    # Check if this IP has already voted for this feedback.
    already = Vote.all().filter('feedback', feedback).filter('ip', ip).count()
    if already:
        logging.debug("Feedback '%s' was already voted %d times from this IP."
                      % (id, already))
        return redirect
    # Register this vote to prevent double voting.
    vote = Vote(feedback=feedback, ip=ip)
    vote.put()
    # Increase the points for this feedback.
    feedback.points += 1
    feedback.put()
    return redirect
开发者ID:jcrocholl,项目名称:minderbot,代码行数:31,代码来源:views.py

示例3: get_already_voted

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def get_already_voted(request):
    """
    Don't show vote buttons if posted or voted from the same IP.
    """
    ip = request.META.get('REMOTE_ADDR', '0.0.0.0')
    posted = [feedback.id()
              for feedback in Feedback.all(keys_only=True).filter('ip', ip)]
    voted = [vote.feedback_id()
             for vote in Vote.all().filter('ip', ip)]
    # logging.debug('posted=%s voted=%s' % (posted, voted))
    return set(posted + voted)
开发者ID:jcrocholl,项目名称:minderbot,代码行数:13,代码来源:views.py

示例4: prefetch_comment_list

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def prefetch_comment_list(comments):
  prefetch_refprops(comments, Comment.user, Comment.post)

  # call all the memcache information
  # starting by the already_voted area
  comment_keys = [str(comment.key()) for comment in comments]
  session = get_current_session()
  if session.has_key('user'):
    user = session['user']
    memcache_voted_keys = ["cp_" + comment_key + "_" + str(user.key()) for comment_key in comment_keys]
    memcache_voted = memcache.get_multi(memcache_voted_keys)
    memcache_to_add = {}
    for comment in comments:
      vote_value = memcache_voted.get("cp_" + str(comment.key()) + "_" +str(user.key()))
      if vote_value is not None:
        comment.prefetched_already_voted = vote_value == 1
      else:
        vote = Vote.all().filter("user =", user).filter("comment =", comment).fetch(1)
        memcache_to_add["cp_" + str(comment.key()) + "_" + str(user.key())] = len(vote)
        comment.prefetched_already_voted = len(vote) == 1
    if memcache_to_add.keys():
      memcache.add_multi(memcache_to_add, 3600)
  else:
    for comment in comments:
      comment.prefetched_already_voted = False
  # now the sum_votes
  memcache_sum_votes_keys = ["c_" + comment_key for comment_key in comment_keys]
  memcache_sum_votes = memcache.get_multi(memcache_sum_votes_keys)
  memcache_to_add = {}
  for comment in comments:
    sum_votes_value = memcache_sum_votes.get("c_" + str(comment.key()))
    if sum_votes_value is not None:
      comment.prefetched_sum_votes = sum_votes_value
    else:
      sum_votes = Vote.all().filter("comment =", comment).count()
      memcache_to_add["c_" + str(comment.key())] = sum_votes
      comment.prefetched_sum_votes =sum_votes
  if memcache_to_add.keys():
    memcache.add_multi(memcache_to_add, 3600)
开发者ID:DFectuoso,项目名称:Noticias-HAcker,代码行数:41,代码来源:prefetch.py

示例5: post

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
    def post(self):
        method = self.request.get("method", None)
        number = self.request.get("number", None)
        cursor = self.request.get("cursor", None)
        refresh = self.request.get("refresh", None)
        random_text = self.request.get("random_text", "")

        try:
            number = int(number)
        except:
            number = None

        if random_text == "random":
            showed_random = True
        else:
            showed_random = False

        if method and number and method in OPTIONS:
            key_name = "%s-%s-%s" % (method, number, showed_random)
            stat = VoteStat.get_or_insert(key_name)

            q = Vote.all()
            q.filter("method =", method)
            q.filter("number =", number)
            q.filter("showed_random =", showed_random)
            if cursor:
                q.with_cursor(cursor)

            count = q.count()
            next_cursor = q.cursor() if count == 1000 else None

            if refresh or not stat.count:
                stat.count = 0
            stat.count = stat.count + count
            stat.method = method
            stat.number = number
            stat.showed_random = showed_random
            stat.generated = datetime.now()
            stat.put()

            if next_cursor:
                params = {"number": number, "method": method, "cursor": next_cursor, "random_text": random_text}
                taskqueue.add(url="/tasks/tally-votes", params=params)
开发者ID:alecperkins,项目名称:nfrom1to10,代码行数:45,代码来源:tasks.py

示例6: vote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def vote(netid, talk_id, direction):
	app.logger.info(dir(netid))
	app.logger.info(netid)
	votes = Vote.all()
	votes.filter('talk_id =', talk_id)
	votes.filter('netid =', netid)
	
	votes_res = votes.fetch(1)

	netid = str(netid)

	if len(votes.fetch(1)) < 1 or (votes_res[0].direction != 'up' and votes_res[0].direction != 'down'):
		# User has not voted before
		new_vote = Vote(direction=direction, netid=str(netid), talk_id=talk_id)
		talk = Talk.get_by_id(talk_id)
		
		if direction == 'up':
			talk.votes += 1
		elif direction == 'down':
			talk.votes -= 1

		talk.put()
		new_vote.put()
		app.logger.info("Did a vote")
		return redirect(url_for('index'))
	elif votes_res[0].direction != direction:
		# The user has voted and is changing
		vote = votes_res[0]
		vote.direction = direction
		talk = Talk.get_by_id(talk_id)
		if direction == 'up':
			talk.votes += 2
		elif direction == 'down':
			talk.votes -= 2
		talk.put()
		vote.put()
		app.logger.info("Did a exist vote")
		return redirect(url_for('index'))
	else:
		# User is not allowed to vote
		return redirect(url_for('index'))
开发者ID:hashbrowncipher,项目名称:cwru-hactalks,代码行数:43,代码来源:cwru_hactalks.py

示例7: index

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def index(netid):
	talks = Talk.all()
	all_votes = Vote.all()
	votes = all_votes.filter('netid =', netid)
	return render_template('index.html', talks=talks, votes=votes, ticket=request.args['ticket'], netid=netid)
开发者ID:hashbrowncipher,项目名称:cwru-hactalks,代码行数:7,代码来源:cwru_hactalks.py

示例8: get

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
    def get(self):

        skeleton_key = self.request.get("skeleton", None)

        if settings.THROTTLE_API and skeleton_key != settings.SKELETON_KEY:
            ip = str(self.request.remote_addr)
            throttle = memcache.get(ip)
            if throttle:
                throttle = throttle - datetime.now()
                jsonResponse(self, {
                    'status': 'throttled',
                    'remaining': throttle.seconds
                })
                return
            else:
                throttle = datetime.now() + timedelta(seconds=settings.THROTTLE_LIFE)
                memcache.set(ip, throttle, settings.THROTTLE_LIFE)

        method          = self.request.get("method", None)
        number          = self.request.get("number", None)
        random_text     = self.request.get("random_text", None)

        cursor = self.request.get("cursor", None)

        showed_random = None
        if random_text:
            if random_text == 'visible':
                showed_random = True
            elif random_text == 'hidden':
                showed_random = False
            else:
                self.error(400)
                return

        q = Vote.all()

        if method:
            if method in OPTIONS:
                q.filter("method =", method)
            else:
                self.error(400)
                return
        
        if number is not None:
            try:
                number = int(number)
            except:
                self.error(400)
                return
            else:
                q.filter("number =", number)
        
        if showed_random is not None:
            q.filter("showed_random =", showed_random)

        if cursor:
            try:
                q.with_cursor(cursor)
            except:
                self.error(400)
                return
        
        data = None
        cached_data = None
        LIMIT = 1000

        key = "%s-%s-%s-%s-%s" % (settings.VERSION, method, number, random_text, cursor)

        if settings.CACHE:
            cached_data = memcache.get(key)
        
        if not cached_data:
            data = q.fetch(LIMIT)
            next_cursor = q.cursor() if len(data) == LIMIT else None

            result = {
                "objects"       : [vote.toJSON(full=(skeleton_key is not None)) for vote in data],
                "next_cursor"   : next_cursor
            }
            if settings.CACHE and not skeleton_key:
                memcache.set(key, result, settings.CACHE_LIFE)
        else:
            result = cached_data

        jsonResponse(self, result)
开发者ID:alecperkins,项目名称:nfrom1to10,代码行数:87,代码来源:handle_data.py

示例9: downvote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def downvote(username, tweet):
	u = Vote.all().filter('parent =', tweet).get()
	if not u and tweet.votes > 0:
		Vote(parent=tweet, user=username, down=True)
		tweet.upvotes -= 1
	return tweet.upvotes
开发者ID:daveyshindig,项目名称:TweetMoar,代码行数:8,代码来源:tweetmoar.py

示例10: upvote

# 需要导入模块: from models import Vote [as 别名]
# 或者: from models.Vote import all [as 别名]
def upvote(username, tweet):
	u = Vote.all().filter('parent =', tweet).get()
	if not u:
		Vote(parent=tweet, user=username)
		tweet.upvotes += 1
	return tweet.upvotes
开发者ID:daveyshindig,项目名称:TweetMoar,代码行数:8,代码来源:tweetmoar.py


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