def get_stories_by_user_id(user_id: int):
    """
    Gets all stories for the given user id.
    :param user_id: the primary key for the user to search on
    :return: a list of stories ordered by date last modified descending
    """
    return db_session.query(Story).filter_by(user_id=user_id).order_by(
        Story.date_last_modified.desc()).all()
def get_published_stories_by_category_id(category_id: int):
    """
    Gets all published stories for the given category_id.
    :param category_id: the primary key for the category to search on
    :return: a list of stories
    """
    return db_session.query(Story).filter_by(published=True).filter(
        Story.categories.any(Category.id == category_id)).all()
def get_user_by_email(email: str):
    """
    Gets the User object for the given email address.
    :param email: the email address to use in the lookup
    :return: the user object
    """
    try:
        return db_session.query(User).filter_by(email=email).one()
    except NoResultFound:
        return None
def get_user_info(user_id: int):
    """
    Gets user info by user id.
    :param user_id: the primary key of the user
    :return: the user object
    """
    try:
        return db_session.query(User).filter_by(id=user_id).one()
    except NoResultFound:
        return None
def get_upload_file_by_id(upload_file_id: int):
    """
    Gets an upload file by id
    :param upload_file_id: the primary key for the upload file to search for
    :return: the upload file or None
    """
    try:
        return db_session.query(UploadFile).filter_by(id=upload_file_id).one()
    except NoResultFound:
        return None
def get_category_by_label(category_label: str):
    """
    Gets a category by label
    :param category_id: the unique label for the category to search for
    :return: the category or None
    """
    try:
        return db_session.query(Category).filter_by(label=category_label).one()
    except NoResultFound:
        return None
def get_category_by_id(category_id: int):
    """
    Gets a category by id
    :param category_id: the primary key for the category to search for
    :return: the category or None
    """
    try:
        return db_session.query(Category).filter_by(id=category_id).one()
    except NoResultFound:
        return None
def get_story_by_id(story_id: int):
    """
    Gets a story by id
    :param story_id: the primary key for the story to search for
    :return: the story or None
    """
    try:
        return db_session.query(Story).filter_by(id=story_id).one()
    except NoResultFound:
        return None
def get_user_id_by_email(email: str):
    """
    Gets the user_id of the user for the given email address.
    :param email: the email address to use in the lookup
    :return: an integer representing the primary key of the user object retrieved
    """
    try:
        user = db_session.query(User).filter_by(email=email).one()
        return user.id
    except NoResultFound:
        return None
def get_categories_by_ids(category_ids: List):
    """
    Gets a list of categories by their ids
    :param category_ids: the list of category ids
    :return: a list of categories
    """
    try:
        return db_session.query(Category).filter(
            Category.id.in_(category_ids)).all()
    except NoResultFound:
        return None
def get_published_stories(count: int = None):
    """
    Gets all published stories.
    :param count: the number of stories to retrieve
    :return: a list of stories
    """
    query = db_session.query(Story).filter_by(published=True).order_by(
        Story.date_created.desc())
    if count:
        query = query.limit(count)
    return query.all()
def delete_story(story_id: int):
    """
    Permanently deletes the story for the given story_id
    :param story_id: the primary key of the story to delete
    """
    try:
        story = db_session.query(Story).filter_by(id=story_id).one()
        upload_file = story.upload_file
        story.categories = []
        db_session.delete(story)
        if upload_file:
            file_storage_service.delete_file(file=upload_file)
            db_session.delete(upload_file)
        db_session.commit()
    except Exception as exc:
        db_session.rollback()
        raise exc
def get_categories():
    """
    Gets all active categories.
    :return: a list of categories
    """
    return db_session.query(Category).order_by(Category.label.asc()).all()
def get_published_stories_count():
    """
    Gets the count of all published stories.
    :return: the number of published stories
    """
    return db_session.query(Story).filter_by(published=True).count()
示例#15
0
def delete_and_recreate_test_data():
    """
    Deletes and recreates Story Time test data.
    """
    try:
        # Delete all data
        for story in db_session.query(Story):
            story.categories = []
        num_rows_deleted = db_session.query(Story).delete()
        print('Deleted {} stories'.format(num_rows_deleted))
        num_rows_deleted = db_session.query(Category).delete()
        print('Deleted {} categories'.format(num_rows_deleted))
        num_rows_deleted = db_session.query(User).delete()
        print('Deleted {} users'.format(num_rows_deleted))
        db_session.commit()

        # Create test data
        # Users
        user_1 = story_time_service.create_user(
            User(name='gregdferrell',
                 email='*****@*****.**',
                 active=True))
        num_rows_created = db_session.query(User).count()
        print('Created {} users'.format(num_rows_created))

        # Categories
        cat_scary_id = story_time_service.create_category(
            Category(label='Scary', description='Eek, scary stories!'))
        cat_funny_id = story_time_service.create_category(
            Category(label='Funny', description='Funny stories :)'))
        cat_animal_id = story_time_service.create_category(
            Category(label='Animals', description='Stories about Animals'))
        cat_musical_id = story_time_service.create_category(
            Category(label='Musical', description='Musicals'))
        cat_nonfiction_id = story_time_service.create_category(
            Category(label='Nonfiction', description='True Stories'))
        cat_scary = story_time_service.get_category_by_id(
            category_id=cat_scary_id)
        cat_funny = story_time_service.get_category_by_id(
            category_id=cat_funny_id)
        cat_animal = story_time_service.get_category_by_id(
            category_id=cat_animal_id)
        cat_musical = story_time_service.get_category_by_id(
            category_id=cat_musical_id)
        cat_nonfiction = story_time_service.get_category_by_id(
            category_id=cat_nonfiction_id)
        num_rows_created = db_session.query(Category).count()
        print('Created {} categories'.format(num_rows_created))

        # Stories
        story_initial = Story(
            title='Story Time',
            description=
            'A story about children writing and sharing stories with each other ...',
            story_text='<REPLACE>',
            published=True,
            user_id=user_1,
            categories=[cat_funny, cat_animal, cat_musical])
        story_initial_id = story_time_service.create_story(story_initial)

        # Update story text for each story from lipsum generator (every second so we don't hit server too hard)
        url = 'https://lipsum.com/feed/json?what=paras&amount=5&start=yes'
        for story in db_session.query(Story).all():
            h = httplib2.Http()
            json_result = json.loads(str(h.request(url, 'GET')[1], 'utf-8'))
            story.story_text = json_result['feed']['lipsum']
            db_session.add(story)
            db_session.commit()
            time.sleep(1)

        num_rows_created = db_session.query(Story).count()
        print('Created {} stories'.format(num_rows_created))

        db_session.commit()
    except Exception as exc:
        print('Error creating test data:')
        print(exc)
        db_session.rollback()