Esempio n. 1
0
    def get_num_votes(self, user):
        if user.num_votes is None:
            q = Vote.all()
            q.filter("user_fb_id =", user.fb_uid)
            user.num_votes = q.count()
            user.put()

        return user.num_votes
Esempio n. 2
0
  def get(self):
    vote_for = int(self.request.get('which_photo'))
    fb_uid = str(self.request.get('fb_uid'))
    user = CacheController.get_user_by_fb_id(fb_uid)

    if not user:
      self.write_error("Can't add vote: User with fb_uid %s is not logged in." % fb_uid)
      return

    # Since the post is taken from the cache, it might not be the most updated version
    # but that's ok, as it is only used as 'parent'
    choosie_post = CacheController.get_model(self.request.get('post_key'))

    vote = Vote(parent=choosie_post,
                user_fb_id=fb_uid,
                vote_for=int(vote_for))

    prev_vote = vote.prev_vote_for_user_for_post()
    #if the user voted to the same post but for different item, updating the vote
    if (prev_vote is not None and prev_vote.vote_for != vote_for):
      prev_vote.vote_for = vote_for
      prev_vote.put()
      choosie_post.add_vote_to_post(vote, False)
      self.response.write('Vote changed to photo number %d.' % vote_for)
    #if voted to same pic - error
    elif(prev_vote != None):
       self.write_error("already voted!")
    else:
      vote.put()
      ChoosiePost.add_vote_to_post(choosie_post, vote, True)
      # Make sure the ChoosiePost is invalidated in cache, so that next time it is asked
      # for, the updated one is retreived.
      Vote.invalidate_votes(self.request.get('post_key'))
      self.response.write('A new vote issued for photo number %d.' % vote_for)
 def get_serialized_votes(self):
     # updated_post.votes is a StringListProperty. Each vote_str is inflated to a dictionary
     # that looks like:
     # {"vote_for": 1,
     #  "user": {"fb_uid": "152343",
     #           ...}
     # }
     logging.info('For question [%s], returning %d votes.', self.question,
                  len(self.votes))
     return [
         Vote.from_string_for_choosie_post(vote_str)
         for vote_str in self.votes
     ]
  def try_parse_vote(choosie_comment, post_type):
    # If comment is something like: 'A is nicer' or 'B: because it is better' create a Vote object too.
    words = re.findall(r"[\w']+", choosie_comment.text)
    if len(words) > 0:
      vote = ScrapeCommentsHandler.vote_from_comment(words, post_type)

      if vote:
        return Vote(user_fb_id=choosie_comment.user_fb_id,
                    created_at=choosie_comment.created_at,
                    vote_for=vote,
                    is_scraped=choosie_comment.is_scraped,
                    scraped_user_details=choosie_comment.scraped_user_details)
    else:
      return None
    def add_vote_to_post_internal(self, new_vote):
        logging.info("Adding vote.")
        # Before adding a new vote, remove any existing votes by the same user.
        for existing_vote_str in self.votes:
            # updated_post.votes is a StringListProperty. To make the comparison,
            # each vote is 'inflated' to a dictionary, and its user ID is compared to the new_vote's.
            existing_vote_dict = Vote.from_string_for_choosie_post(
                existing_vote_str, keep_shallow=True)
            if existing_vote_dict["user"]["fb_uid"] == new_vote.user_fb_id:
                self.votes.remove(existing_vote_str)
                break

        # updated_post.votes is a StringListProperty. We add the new vote as a string.
        self.votes.append(new_vote.to_string_for_choosie_post())
Esempio n. 6
0
 def try_parse_vote(choosie_comment):
     # If comment is something like: 'A is nicer' or 'B: because it is better' create a Vote object too.
     words = re.findall(r"[\w']+", choosie_comment.text)
     if len(words) > 0:
         first_word = words[0].upper()
         logging.info(first_word)
         # Any of those as first word of the comment will be considered as a vote
         user_string_to_photo_number = {'A': 1, 'B': 2, '1': 1, '2': 2}
         if first_word in user_string_to_photo_number:
             return Vote(
                 user_fb_id=choosie_comment.user_fb_id,
                 created_at=choosie_comment.created_at,
                 vote_for=user_string_to_photo_number[first_word],
                 is_scraped=choosie_comment.is_scraped,
                 scraped_user_details=choosie_comment.scraped_user_details)
     else:
         return None
 def get_cached_votes(self):
     return Vote.get_votes_for_post(str(self.key()))