def __check_post_params(self, request_data): comments_enabled = request_data.get('comments_enabled') if comments_enabled is not None: check_boolean_field(comments_enabled) text = request_data.get('text') if text is not None: check_field_length(text, MIN_POST_TEXT_LENGTH) location_uid = request_data.get('location_uid') location = Location.get_active(uid=location_uid) if bool(location_uid) != bool(location): raise ResourceNotFound('`location_uid` not found') request_blobs = request_data.get('blobs') if not isinstance(request_blobs, list): raise BadRequest('`blobs` must be an array') post_slides = [] for blob_uid in request_blobs: try: blob = Blob.get_active(uid=blob_uid) post_slides.append(blob) except : raise ResourceNotFound('Blob `{}` not found'.format(blob_uid)) return dict( post_slides=post_slides, comments_enabled=comments_enabled, location=location, text=text)
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 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 put(self, collection_uid, post_uid): """Add a post to a collection""" collection = Collection.get_not_deleted(uid=collection_uid) if collection is None: raise ResourceNotFound('Collection not found') post = Post.get_not_deleted(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') post.update(collection_id=collection.id) return api_success_response()
def __check_user_params(self, request_data): username = request_data.get('username') if username is not None: check_username_field(username) password = request_data.get('password') if password is not None: check_password_field(password) bio = request_data.get('bio') if bio is not None: check_bio_field(bio) phone = request_data.get('phone') if phone is not None: check_phone_field(phone) email = request_data.get('email') if email is not None: check_email_field(email) profile_photo_uid = request_data.get('profile_photo_uid') profile_photo = Blob.get_active(uid=profile_photo_uid) if profile_photo is None: raise ResourceNotFound('Profile photo not found') return dict(username=username, password=password, bio=bio, email=email, phone=phone, profile_photo=profile_photo)
def delete(self, collection_uid, post_uid): """Remove a post from a collection""" collection = Collection.get_not_deleted(uid=collection_uid) if collection is None: raise ResourceNotFound('Collection not found') post = Post.get_not_deleted(uid=post_uid) if post is None: raise ResourceNotFound('Post not found') if post.collection_id != collection.id: raise ResourceNotFound('Post not found') post.update(collection_id=None) return api_deleted_response()
def get(self, hash_tag): """Retrieve posts about an hashtag""" request_args = get_current_request_args() scope = request_args.get('scope') or DEFAULT_HASH_TAG_FETCH_SCOPE if scope not in HASH_TAG_RETRIEVAL_SCOPES: raise BadRequest( '`scope` must be one of {}'.format(HASH_TAG_RETRIEVAL_SCOPES)) hash_tag = HashTag.get_not_deleted(hash_tag=hash_tag) if hash_tag is None: raise ResourceNotFound('Hash tag not found') hash_tag_details = { 'meta': lambda x: { 'data': None, 'meta': None }, 'posts': lambda y: { 'data': None, 'meta': None }, 'followers': lambda z: { 'data': None, 'meta': None } } scoped_details = hash_tag_details[scope]() return api_success_response(**scoped_details)
def delete(self, username): user = User.get_active(username=username) if user is None: raise ResourceNotFound('User not found') user.delete() return api_deleted_response()
def delete(self, collection_uid): """Delete a collection""" collection = Collection.get_active(collection_uid) if collection is None: raise ResourceNotFound('Collection not found') collection.delete() return api_deleted_response()
def delete(self, conversation_uid): """Delete a conversation""" conversation = Conversation.get_not_deleted(uid=conversation_uid) if conversation is None: raise ResourceNotFound('Conversation not found') conversation.delete() return api_deleted_response()
def patch(self, conversation_uid): """Update a conversation""" request_data = get_current_request_data() params = self.__validate_conversation_edit_params(request_data) conversation = Conversation.get_not_deleted(uid=conversation_uid) if conversation is None: raise ResourceNotFound('Conversation not found') self.edit_conversation(conversation, params)
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 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 patch(self, location_uid): request_data = get_current_request_data() location = Location.get_active(uid=location_uid) if location is None: raise ResourceNotFound('Location not found') params = self.__validate_location_update_params(request_data) location = self.edit_location(location, params) return location.as_json()
def patch(self, collection_uid): request_data = get_current_request_data() collection = Collection.get_active(collection_uid) if collection is None: raise ResourceNotFound('Collection not found') params = self.__validate_collection_update_params(request_data) collection = self.edit_collection(collection, params) return api_success_response(collection.as_json())
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 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 patch(self, username): request_data = get_current_request_data() params = self.__validate_user_update_params(request_data) user = User.get_active(username=username) if user is None: raise ResourceNotFound('User not found') user = self.edit_existing_user(user, params) return api_success_response(user.as_json())
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 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 __check_story_params(self, request_data): replies_enabled = request_data.get('replies_enabled') if replies_enabled is not None: check_boolean_field(replies_enabled) text = request_data.get('text') if text is not None: check_field_length(text, MIN_STORY_TEXT_LENGTH) blob_uid = request_data.get('blob_uid') blob = Blob.get_active(uid=blob_uid) if blob is None: raise ResourceNotFound('Blob not found') return dict(replies_enabled=replies_enabled, blob=blob, text=text)
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 __check_conversation_creation_params(self, params): users = params.get('users') users_ = None if users is not None: users_ = [] for username in users: try: user = User.get_not_deleted(username=username) except: raise ResourceNotFound( 'User `{}` not found'.format(username)) users_.append(user) return dict(users=users_)
def get(self, location_uid): location = Location.get_active(uid=location_uid) if location is None: raise ResourceNotFound('Location not found') return location.as_json()
def get(self, username): user = User.get_active(username=username) if user is None: raise ResourceNotFound('User not found') return api_success_response(user.as_json())