# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# !pip install sqlalchemy

# +
import sqlalchemy as db
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Text, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
engine = db.create_engine('sqlite:///clother.db')
connection = engine.connect()
Session = sessionmaker(bind=engine)


# -

class Category(Base):
    __tablename__ = 'category'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('category.id'), nullable=True)
    title = Column(Text, nullable=False)
    last_level = Column(Integer, default=1, nullable=False)

    subcategories = relationship('Category',
                                    backref=backref('parent', remote_side=[id]),
                                    lazy=True)


Base.metadata.create_all(engine)

# +
session = Session()

categories = json.load(open("./clother/static/categories.json", 'r'))
for entry in categories:
    category = Category(parent_id=entry['parent_id'], title=entry['title'])

    session.add(category)
    session.commit()

delete_query = Category.__table__.delete().where(Category.title.ilike("view all"))
session.execute(delete_query)
session.commit()

categories = session.query(Category).all()
for category in categories:
    if len(category.subcategories) > 0:
        category.last_level = False
        session.commit()

StarcoderdataJupyterScriptsDedupFiltered