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(self): vote_for = int(self.request.get('which_photo')) fb_uid = str(self.request.get('fb_uid')) # 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) 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) # 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 notify_friends_async(self, choosie_post_key): try: choosie_post = CacheController.get_model(str(choosie_post_key)) logging.info("choosie post key = %s", str(choosie_post.key())) for user in User.all(): logging.info("%s: device %s, uid: %s", user.name(), user.device_id, user.fb_uid) users = User.all() top_list = self.top_users() recipients = [u for u in User.all() if ((ChoosieConfiguration.get_send_to_rest_setting() == False) and (u.fb_uid in top_list) or (ChoosieConfiguration.get_send_to_rest_setting() == True))] logging.info("First selection: %s", ", ".join([user.name() for user in recipients])) recipients = [u for u in recipients if u.device_id is not None and user.fb_uid != choosie_post.user_fb_id] result = NotifyHandler.send_notifiction(NotifyHandler.notify_type["new_post"], self.get_user().display_name(), str(choosie_post_key), recipients) ChoosieConfiguration.set_sent_to_rest_setting(not ChoosieConfiguration.get_send_to_rest_setting()) logging.info("result of notifying friends= " + result) except Exception, e: logging.error("Faled to notify friends on new post: %s" % e)
def get(self, key): logging.info(key) # This post is got from the cache. That's ok, because all of the actual # dynamic data (votes, comments) is retrieved from the datastore during # choosie_post.to_json(). choosie_post = CacheController.get_model(key) self.response.out.write(json.dumps(choosie_post.to_json()))
def scrape_comments_and_votes_from_facebook(choosie_post_key): choosie_post = CacheController.get_model(choosie_post_key) if not choosie_post: return (None, None, "[%s] is not a valid ChoosiePost key" % choosie_post_key) fb_post_id = choosie_post.fb_post_id if not fb_post_id: return (None, None, "[%s] doesn't have an associated Facebook post ID" % choosie_post_key) fb_access_token = choosie_post.get_user().fb_access_token json_comments = Utils.get_json_comments_from_fb_post(fb_post_id, fb_access_token) comments, votes = ScrapeCommentsHandler.parse_facebook_comments(json_comments) if choosie_post_key and (len(comments) > 0 or len(votes) > 0): choosie_post = CacheController.get_model(choosie_post_key) if choosie_post: choosie_post.add_scraped_comments_to_post(comments, votes) return comments, votes, None
def get_comments_for_post(post_key): comments = memcache.get(post_key, namespace=COMMENTS_NAMESPACE) if comments is not None: logging.info('Skipped a data store call for comments.') return comments else: logging.info('Retrieving comments for [%s] from data store.' % post_key) post = CacheController.get_model(post_key) comments = Comment.all().ancestor(post) memcache.set(post_key, comments, namespace=COMMENTS_NAMESPACE) return comments
def get_votes_for_post(post_key): votes = memcache.get(post_key, namespace=VOTES_NAMESPACE) if votes is not None: logging.info('Skipped a data store call for votes.') return votes else: logging.info('Retrieving votes for [%s] from data store.' % post_key) post = CacheController.get_model(post_key) votes = Vote.all().ancestor(post) memcache.set(post_key, votes, namespace=VOTES_NAMESPACE) return votes
def get(self): choosie_post = CacheController.get_model(self.request.get('post_key')) which_photo = int(self.request.get('which_photo')) result = { 0: choosie_post.photo, 1: choosie_post.photo1, 2: choosie_post.photo2 }[which_photo] self.response.headers['Content-Type'] = 'image/png' self.response.headers['Cache-Control'] = 'max-age=290304000, private' self.response.out.write(result)
def scrape_comments_and_votes_from_facebook(choosie_post_key): choosie_post = CacheController.get_model(choosie_post_key) if not choosie_post: return (None, None, "[%s] is not a valid ChoosiePost key" % choosie_post_key) fb_post_id = choosie_post.fb_post_id if not fb_post_id: return (None, None, "[%s] doesn't have an associated Facebook post ID" % choosie_post_key) fb_access_token = choosie_post.get_user().fb_access_token json_comments = Utils.get_json_comments_from_fb_post( fb_post_id, fb_access_token) comments, votes = ScrapeCommentsHandler.parse_facebook_comments( json_comments) if choosie_post_key and (len(comments) > 0 or len(votes) > 0): choosie_post = CacheController.get_model(choosie_post_key) if choosie_post: choosie_post.add_scraped_comments_to_post(comments, votes) return comments, votes, None
def scrape_comments_and_votes_from_facebook(choosie_post_key): choosie_post = CacheController.get_model(choosie_post_key) if not choosie_post: return (None, None, "[%s] is not a valid ChoosiePost key" % choosie_post_key) fb_post_id = choosie_post.fb_post_id if not fb_post_id: return (None, None, "[%s] doesn't have an associated Facebook post ID" % choosie_post_key) fb_access_token = choosie_post.get_user().fb_access_token json_comments, error = Utils.get_json_comments_from_fb_post(fb_post_id, fb_access_token) if error: return (None, None, "Couldn't scraped comments for post with ID [%s] (user FB UID = [%s]). Error: %s" % (fb_post_id, choosie_post.fb_post_id, error)) comments, votes = ScrapeCommentsHandler.parse_facebook_comments(json_comments, choosie_post.post_type_id) if choosie_post_key and (len(comments) > 0 or len(votes) > 0): choosie_post = CacheController.get_model(choosie_post_key) if choosie_post: choosie_post.add_scraped_comments_to_post(comments, votes) return comments, votes, None
def post(self): fb_uid = str(self.request.get('fb_uid')) # TODO: Make sure text is Unicode text = self.request.get('text') # 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')) comment = Comment(parent=choosie_post, user_fb_id=self.request.get('fb_uid'), text=text) comment.put() choosie_post.add_comment_to_post(comment) # Make sure the ChoosiePost's comments are invalidated in cache, so that next time # they are asked for, the updated are retreived. Comment.invalidate_comments(self.request.get('post_key')) self.response.write('Comment added.')
def publish_dillema_on_wall(self, choosie_post_key, domain): try: choosie_post = CacheController.get_model(str(choosie_post_key)) logging.info("publishing on wall") logging.info("publishing with access_token " + choosie_post.get_user().fb_access_token) graph = facebook.GraphAPI(choosie_post.get_user().fb_access_token) logging.info("type = " + str(choosie_post.post_type())) if choosie_post.post_type() == CHOOSIE_POST_TYPE_DILEMMA: pic = Utils.create_dillema_post_image(self) question = choosie_post.question + '\n(Start your comment with #1 or #2 to help me choose.)' else: pic = Utils.create_yesno_post_image(self) img1_blob_reader = blobstore.BlobReader(choosie_post.photo1_blob_key) question = choosie_post.question + '\n(Start your comment with #yes or #no to help me choose.)' picIO = StringIO(pic) response = graph.put_photo(picIO, question.encode('utf-8')) logging.info(str(response)) choosie_post.fb_post_id = response['post_id'] choosie_post.posted_to_fb = True choosie_post.put() except Exception, e: logging.error("Facebook publishing failed: %s" % e)
def notify_friends_async(self, choosie_post_key): try: choosie_post = CacheController.get_model(str(choosie_post_key)) logging.info("choosie post key = %s", str(choosie_post.key())) for user in User.all(): logging.info("%s: device %s, uid: %s", user.name(), user.device_id, user.fb_uid) users = User.all() top_list = self.top_users() recipients = [ u for u in User.all() if ((ChoosieConfiguration.get_send_to_rest_setting() == False) and (u.fb_uid in top_list) or (ChoosieConfiguration.get_send_to_rest_setting() == True)) ] logging.info("First selection: %s", ", ".join([user.name() for user in recipients])) recipients = [ u for u in recipients if u.device_id is not None and user.fb_uid != choosie_post.user_fb_id ] result = NotifyHandler.send_notifiction( NotifyHandler.notify_type["new_post"], self.get_user().display_name(), str(choosie_post_key), recipients) ChoosieConfiguration.set_sent_to_rest_setting( not ChoosieConfiguration.get_send_to_rest_setting()) logging.info("result of notifying friends= " + result) except Exception, e: logging.error("Faled to notify friends on new post: %s" % e)
def publish_dillema_on_wall(self, choosie_post_key, domain): try: choosie_post = CacheController.get_model(str(choosie_post_key)) logging.info("publishing on wall") logging.info("publishing with access_token " + choosie_post.get_user().fb_access_token) graph = facebook.GraphAPI(choosie_post.get_user().fb_access_token) logging.info("type = " + str(choosie_post.post_type())) if choosie_post.post_type() == CHOOSIE_POST_TYPE_DILEMMA: pic = Utils.create_dillema_post_image(self) question = choosie_post.question + '\n(Start your comment with #1 or #2 to help me choose.)' else: pic = Utils.create_yesno_post_image(self) img1_blob_reader = blobstore.BlobReader( choosie_post.photo1_blob_key) question = choosie_post.question + '\n(Start your comment with #yes or #no to help me choose.)' picIO = StringIO(pic) response = graph.put_photo(picIO, question.encode('utf-8')) logging.info(str(response)) choosie_post.fb_post_id = response['post_id'] choosie_post.posted_to_fb = True choosie_post.put() except Exception, e: logging.error("Facebook publishing failed: %s" % e)