def get(self, story_uid=None): """Get a story or a list of a user's stories""" request_args = get_current_request_args() user_uid = request_args.get('user_uid') if user_uid is not None: user = User.get_active(uid=user_uid) if user is None: raise ResourceNotFound('User not found') else: user = get_current_user() if story_uid is not None: story = Story.get_active(uid=story_uid, has_expired=False) if story is None: raise ResourceNotFound('Story not found') return api_success_response(data=story.as_json()) todays_stories = Story.prepare_get_active(user_id=user.id).filter_by( has_expired=False).all() return api_success_response( [story.as_json() for story in todays_stories])
def get(self, post_uid=None): """Retrieve a post or a list of posts""" request_args = get_current_request_args() user_uid = request_args.get('user_uid') if user_uid is not None: user = User.get_active(uid=user_uid) if user is None: raise ResourceNotFound('User not found') else: user = get_current_user() if post_uid is not None: post = Post.get_active(uid=post_uid, user_id=user.id) if post is None: raise ResourceNotFound('Post not found') return api_success_response(data=post.as_json()) pagination = Post.get_active(user_id=user.id).paginate() return api_success_response( [item.as_json() for item in pagination.items], meta=pagination.meta )
def after_every_request(response): """Do necessary operations after every request.""" # Update API activity log: Save response payload from app.models import APILog try: response_data = response.response[0] except IndexError: response_data = None user = get_current_user() _update_api_log(api_log_id=get_current_api_log().id, request_method=request.method, request_url=request.url, user_id=getattr(user, 'id', None), response_data=response_data, request_headers=get_current_request_headers()) if user: # and g.app.ownership != 'Proprietary': users_today_requests = APILog.query.filter_by( created_by_id=g.user.id).count() if users_today_requests > g.user.pricing.toll_free_daily_requests: g.request_cost += g.user.pricing.request_cost user.record_request_cost(g.request_cost) return response
def get(self, collection_uid=None): """Get a list of a user's collections, or """ request_args = get_current_request_args() user_uid = request_args.get('user_uid') if user_uid is not None: user = User.get_not_deleted(uid=user_uid) if user is None: raise ResourceNotFound('User not found') else: user = get_current_user() if collection_uid is not None: collection = Collection.get_not_deleted(uid=collection_uid, user_id=user.id) if collection is None: raise ResourceNotFound('Collection not found') return api_success_response(collection.as_json()) pagination = user.collections.paginate() return api_success_response( data=[collection.as_json() for collection in pagination.items()], meta=pagination.meta)
def delete(self, user_uid): """Unfollow a user""" user = get_current_user() to_unfollow = User.get_active(uid=user_uid) if to_unfollow is None: raise ResourceNotFound('User not found') user.followed.remove(to_unfollow) return api_deleted_response()
def get(self): """Get a list of users being followed""" user = get_current_user() pagination = user.followed.filter( followers.c.followed_id == user.id).paginate() return api_success_response( data=[user.as_json() for user in pagination], meta=pagination.meta )
def delete(self, hash_tag): """Unfollow a hash tag""" user = get_current_user() to_unfollow = HashTag.get_active(entity=hash_tag) if to_unfollow is None: raise ResourceNotFound('Hash tag not found') user.hash_tags_followed.remove(to_unfollow) return api_deleted_response()
def delete(self, post_uid): """Delete a post""" post = Post.get_active(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') if post.user != get_current_user(): raise UnauthorizedError() post.delete() return api_deleted_response()
def get(self): current_user = get_current_user() pagination = Notification.query.filter( user_id=current_user.id).paginate() data = [] for item in pagination.items: item.mark_as_read() data.append(item.as_json()) return api_success_response(data=data, meta=pagination.meta)
def delete(self, story_uid): """Delete a slide from a user's story""" story = Story.get_active(uid=story_uid) if story is None: raise ResourceNotFound('Story not found') if story.user != get_current_user(): raise UnauthorizedError() story.delete() return api_deleted_response()
def put(self, hash_tag): """Follow a hash tag""" user = get_current_user() to_follow = HashTag.get_active(entity=hash_tag) if to_follow is None: raise ResourceNotFound('Hash tag not found') if user.is_following(to_follow): raise ResourceConflict('User already follows them.') user.hash_tags_followed.append(to_follow) return api_created_response()
def put(self, user_uid): """Follow a user""" user = get_current_user() to_follow = User.get_active(uid=user_uid) if to_follow is None: raise ResourceNotFound('User not found') if user.is_following(to_follow): raise ResourceConflict('User already follows them.') user.followed.append(to_follow) return api_created_response()
def get(self): """Get the stories for a user's timeline""" user = get_current_user() followed = Story.query.join( followers, (followers.c.followed_id == Story.user_id)).filter( followers.c.follower_id == user.id) own = Story.query.filter_by(user_id=user.id) pagination = followed.union(own).paginate() return api_success_response( data=[item.as_json() for item in pagination.items], meta=pagination.meta)
def patch(self, post_uid): """Edit a post""" request_data = get_current_request_data() post = Post.get_active(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') if post.user != get_current_user(): raise UnauthorizedError() params = self.__validate_post_edit_params(request_data) self.edit_post(post, params) return api_success_response(post.as_json())
def create_post(params): text = params['text'] extracted_hash_tags = extract_hash_tags_for_text(text) hash_tag_orms = map( lambda x: HashTagsView.create_hash_tag(x), extracted_hash_tags ) post = Post( user_id=get_current_user().id, **params ) post.save() map( lambda x: HashTagsView.add_post_to_hash_tag(post, x), hash_tag_orms ) return post
def get(self): """Get all the posts for a timeline""" user = get_current_user() followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id) ).filter( followers.c.follower_id == user.id ) own = Post.query.filter_by( user_id=user.id ).order_by( Post.created_at.desc() ) pagination = followed.union(own).paginate() return api_success_response( data=[item.as_json() for item in pagination.items], meta=pagination.meta )
def get(self): """Get the conversations a user is participating in""" user = get_current_user() return api_success_response( [conversation.as_json() for conversation in user.conversations])
def create_new_story(params): story = Story(user_id=get_current_user().id, **params) story.save() return story