def apply_vote(user_id, post_id, vote): #if the post dne, return false post = Post.find_post(post_id) if post is None: return False userpost = db.session.query(UserPost).filter(UserPost.post_id==post_id,\ UserPost.user_id==user_id).first() # if the post has not been voted on by this user, we create an entry # and add it to the db # Standardize values if (vote > 1): vote = 1 elif (vote < -1): vote = -1 # Edit userpost table if userpost is None: userpost = UserPost(user_id, post_id, vote) db.session.add(userpost) difference = vote else: currentvote = userpost.get_vote() difference = vote - currentvote userpost.set_vote(vote) # Change value of votes on that post post = Post.find_post(post_id) post.increment_vote(difference) db.session.commit() return True
def post(self): logging.info('GetPostHandler...') request = simplejson.loads(self.request.body) talky_uid = int(request['talky_uid']) user = TalkyUser.get_by_id(talky_uid) if user == None: logging.info('user is None') logging.info(user.spot.name) if user.spot == None: logging.info('user.spot is None') query = UserPost.all() query.filter('spotName = ', user.spot.name) posts = query.fetch(limit = 10) response = { 'success':True, 'posts':[] } for post in posts: postData = { 'owner':post.owner.fb_uid, 'id':post.key().id(), 'date_time':serializeDateTime(post.dateTime), 'content':post.content, } response['posts'].append(postData) self.response.out.write(simplejson.dumps(response))
def to_json_fields_for_FE(self, current_user_id, user_img_tag=[]): user = User.find_user_by_id(self.poster_id) cur_user_vote = UserPost.get_vote_by_ids(current_user_id, self.post_id) cur_user_vote = cur_user_vote if cur_user_vote else 0 img_data = [] # retrieve image data from s3 if its an image if self.post_type.describe() == 1: key = self.get_s3_key() try: img_data = s3_client.get_object(\ Bucket='graffiti-post-images',\ Key=key)['Body'].read().decode('ascii') except: logger.error('Error retrieving image post: ' + key) return json.dumps( dict(postid=self.post_id, type=self.post_type.describe(), text=self.text, location=dict(longitude=self.longitude, latitude=self.latitude), created_at=self.created_at, poster=json.loads(user.to_json_fields_for_FE(user_img_tag)), num_votes=self.num_votes, current_user_vote=cur_user_vote, image=img_data))
def get(self): spotName = unicode(self.request.get('spot-name')) query = UserPost.all() query.filter('spotName = ', spotName) results = query.fetch(limit=1000) for result in results: result.delete() self.response.out.write('Done')
def post(self): logging.info('CreatePostHandler...') request = simplejson.loads(self.request.body) talky_uid = int(request['talky_uid']) user = TalkyUser.get_by_id(talky_uid) content = request['content'] post = UserPost(content=content, owner=user, spot=user.spot.key()) post.spotName = user.spot.name; key = post.put() response = { 'success':True, 'post_id':key.id() } self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.out.write(simplejson.dumps(response))
def post(self): request = simplejson.loads(self.request.body) talkyUId = request['talky_uid'] postId = request['post_id'] post = UserPost.get_by_id(postId) response = {} response['replies'] = [] for reply in post.replies: response['replies'].append(reply.toInterchangeable()) #response['replies'].append({'owner':reply.owner.fb_uid,'content':reply.content,'date_time':serializeDateTime(reply.dateTime)}) response['success'] = True self.response.out.write(simplejson.dumps(response))
def post(self): logging.info('SendReplyHandler...') request = simplejson.loads(self.request.body) userId = int(request['talky_uid']) postId = int(request['post_id']) content = request['content'] owner = TalkyUser.get_by_id(userId) post = UserPost.get_by_id(postId) postReply = PostReply(owner=owner, post=post, content=content) key = postReply.put() response = { 'success':True, 'reply_id':key.id() } self.response.headers['Content-Type'] = 'application/json; charset=utf-8' self.response.out.write(simplejson.dumps(response)) logging.info('DONE')