示例#1
0
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 ="******"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)
示例#2
0
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 ="******"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)
示例#3
0
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 ="******"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)
示例#4
0
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.info("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.info("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.info("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
示例#5
0
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
示例#6
0
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)
示例#7
0
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)
示例#8
0
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 ="******"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)
示例#9
0
    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)
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'))
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)
示例#12
0
    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)
示例#13
0
    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)
示例#14
0
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
示例#15
0
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