예제 #1
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 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
예제 #3
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