Exemple #1
0
def edit_item(item):
    session = database_factory.get_session()
    item_db = session.query(Item).filter_by(id=item.id).one()
    item_db = item
    session.add(item_db)
    session.commit()
    session.close()
    return item
Exemple #2
0
def update_item(item):
    session = database_factory.get_session()
    get_item_by_id(item.id)
    db_item = item
    session.add(db_item)
    name = db_item.name
    session.commit()
    session.close()
    return name
Exemple #3
0
def get_complete_data():
    session = database_factory.get_session()
    category_list = session.query(Category).all()
    category_response = []
    for category in category_list:
        category.items = get_items_by_category(category_name=category.name)\
            .all()
        category_response.append(category)
    session.close()
    return category_response
Exemple #4
0
def insert_database_objects():
    session = database_factory.get_session()
    if session.query(Category).count() == 0:

        def insert_items():
            with open('setup_db.json') as data_file:
                json_categories = json.load(data_file)
                for json_category in json_categories['categories']:
                    category = Category()
                    category.name = json_category['name']
                    session.add(category)
                    session.commit()
                    for json_item in json_category['items']:
                        item = Item()
                        item.name = json_item['name']
                        item.description = json_item['description']
                        item.date = datetime.datetime.utcnow()
                        item.category = category
                        session.add(item)
                        session.commit()
            session.close()

        insert_items()
Exemple #5
0
def get_items_by_category(category_name):
    session = database_factory.get_session()
    category = session.query(Category).filter_by(name=category_name).one()
    item_list = session.query(Item).filter_by(category_id=category.id)
    session.close()
    return item_list
Exemple #6
0
def get_latest_items():
    session = database_factory.get_session()
    item_list = session.query(Item).order_by(Item.date).limit(8)
    session.close()
    return item_list
Exemple #7
0
def get_all_items():
    session = database_factory.get_session()
    item_list = session.query(Item).all()
    session.close()
    return item_list
Exemple #8
0
def remove_item_by_id(item_id):
    session = database_factory.get_session()
    db_item = get_item_by_id(item_id)
    session.delete(db_item)
    session.commit()
    session.close()
Exemple #9
0
def get_category_by_name(category_name):
    session = database_factory.get_session()
    category = session.query(Category).filter_by(name=category_name).first()
    session.close()
    return category
Exemple #10
0
def get_all_categories():
    session = database_factory.get_session()
    category_list = session.query(Category).all()
    session.close()
    return category_list
Exemple #11
0
def get_user_by_id(user_id):
    session = database_factory.get_session()
    user = session.query(User).filter_by(id=user_id).first()
    session.close()
    return user
Exemple #12
0
def get_user_by_email(email):
    session = database_factory.get_session()
    user = session.query(User).filter_by(email=email).first()
    session.close()
    return user
Exemple #13
0
def create_user(user):
    session = database_factory.get_session()
    session.add(user)
    session.commit()
    session.close()
Exemple #14
0
def get_item_by_id(item_id):
    session = database_factory.get_session()
    item = session.query(Item).filter_by(id=item_id).one()
    session.close()
    return item
Exemple #15
0
def get_item_by_name(item_name):
    session = database_factory.get_session()
    item = session.query(Item).filter_by(name=item_name).one()
    session.close()
    return item