Ejemplo n.º 1
0
def write_to_db(nchapter, title, book_id=1):
    session = sa_conn.Session()
    chapter = session.query(ChapterSA).\
      filter(ChapterSA.chapter_n==nchapter).\
      filter(ChapterSA.book_id==book_id).\
      first()
    doCommit = False
    if chapter is None:
        chapter = ChapterSA()
        chapter.book_id = book_id
        session.add(chapter)
        doCommit = True
    if chapter.title != title:
        chapter.title = title
        doCommit = True
    if chapter.chapter_n != nchapter:
        chapter.chapter_n = nchapter
        doCommit = True
    slug = slugify(title)
    if chapter.slug != slug:
        chapter.slug = slug
        doCommit = True
    if doCommit:
        print('Committing', chapter)
        session.commit()
    session.close()
    return doCommit
def fetch_all_chapters():  # session
    session = sa_conn.Session()
    chapters = session.query(gmodels.ChapterSA). \
      filter(gmodels.ChapterSA.book_id == 1). \
      order_by(gmodels.ChapterSA.chapter_n). \
      all()
    session.close()
    return chapters
def read_chapters_from_db():
    session = sa_conn.Session()
    pt = PrettyTable()
    pt.field_names = ['Chap N', 'slug', 'Title']
    book = session.query(BookSA).filter(BookSA.slug == slug).first()
    if not book:
        print('slug', slug, 'is not in db.')
        return
    for chapter in book.chapters:
        # print (chapter.chapter_n, '||', chapter.title, '||', chapter.slug)
        pt.add_row([chapter.chapter_n, chapter.title, chapter.slug])
    print(pt)
Ejemplo n.º 4
0
def updatedb(triples):
    session = sa_conn.Session()
    for number_n_title_n_slug in triples:
        nchapter = number_n_title_n_slug.n_chapter
        title = number_n_title_n_slug.title
        slug = number_n_title_n_slug.slug
        chapter = fetch_if_exists(nchapter, session)
        if chapter is None:
            bool_result = insert_chapter(nchapter, title, slug, session)
        else:
            bool_result = update_chapter(chapter, title, slug, session)
        print('ins or upt boolean result', bool_result)
    session.close()
Ejemplo n.º 5
0
def delete_chapter_above_n(seq, book_id=1):
    session = sa_conn.Session()
    chapters = session.query(ChapterSA).\
      filter(ChapterSA.chapter_n > seq).\
      filter(ChapterSA.book_id == book_id).\
      all()
    doCommit = False
    for chapter in chapters:
        print('Deleting former chapter', chapter)
        session.delete(chapter)
        doCommit = True
    if doCommit:
        session.commit()
    session.close()
Ejemplo n.º 6
0
def process(save_to_db=False):
    print('In updatedb()')
    session = sa_conn.Session()
    book = session.query(
        gmodels.BookSA).filter(gmodels.BookSA.slug == slug).first()
    if book is None:
        print(slug, 'not found')
        return
    measures_per_day = []
    for measure in book.measuresondates:
        print(measure.measuredate)
        print(measure.n_words)
        measures_per_day.append(measure)
        print(measure.json_chapters_words)
    calc_days_yet(measures_per_day[-2], measures_per_day[-1])
    session.close()