def get_documents_sections(cls,doc_id,limit=None): session = DbSessionFactory.create_session() # query = session.query(Group) docs = [] for d, g, s in session.query(Document, Group, Section).\ filter(Document.doc_id == Group.doc_id,\ Section.sec_id == Group.sec_id,\ Document.doc_id == doc_id).all(): print("group_id: {} doc_name: {} text: {}".format(g.group_id, d.doc_name, s.sec_text)) u = Doc_Group_Sec() u.add_group_id(g.group_id) u.add_doc_name(d.doc_name) u.add_doc_id(d.doc_id) u.add_sec_text(s.sec_text) u.add_sec_id(s.sec_id) u.add_order(g.order) docs.append(u.to_dict()) # docs = session.query(Document, Group, Section).\ # filter(Document.doc_id == Group.doc_id,\ # Section.sec_id == Group.sec_id, \ # Document.doc_id == doc_id).all() # if limit: # groups = query[:limit] # else: # groups = query.all() session.close() return docs
def section_by_id(cls, sec_id): session = DbSessionFactory.create_session() section = session.query(Section).filter( Section.sec_id == sec_id).first() session.close() return section
def doc_by_name(cls, doc_name): session = DbSessionFactory.create_session() document = session.query(Document).filter( Document.doc_name == doc_name).first() session.close() return document
def delete_group(cls, group_id): session = DbSessionFactory.create_session() db_group = session.query(Group).filter(Group.group_id == group_id).first() if not db_group: return session.delete(group_id) session.commit()
def update_document(cls, doc_data): session = DbSessionFactory.create_session() db_doc = session.query(Document).filter( Document.doc_id == doc_data.doc_id).first() db_doc.doc_name = doc_data.doc_name session.commit() return db_doc
def delete_section(cls, sec_id): session = DbSessionFactory.create_session() db_section = session.query(Section).filter( Section.sec_id == sec_id).first() if not db_section: return session.delete(sec_id) session.commit()
def delete_document(cls, doc_id): session = DbSessionFactory.create_session() db_doc = session.query(Document).filter( Document.doc_id == doc_id).first() if not db_doc: return session.delete(doc_id) session.commit()
def all_sections(cls, limit=None): session = DbSessionFactory.create_session() query = session.query(Section) if limit: sections = query[:limit] else: sections = query.all() session.close() return sections
def all_groups(cls,limit=None): session = DbSessionFactory.create_session() query = session.query(Group) if limit: groups = query[:limit] else: groups = query.all() session.close() return groups
def update_group(cls, group_data): try: session = DbSessionFactory.create_session() db_group = session.query(Group).filter(Group.group_id == group_data.group_id).first() db_group.doc_id = group_data.doc_id db_group.sec_id = group_data.sec_id session.commit() return db_group except Exception as e: print(e)
def all_documents(cls, limit=None): session = DbSessionFactory.create_session() query = session.query(Document). \ order_by(Document.doc_name) if limit: documents = query[:limit] else: documents = query.all() session.close() return documents
def update_section(cls, section_data): try: session = DbSessionFactory.create_session() db_section = session.query(Section).filter( Section.sec_id == section_data.sec_id).first() db_section.sec_text = section_data.sec_text db_section.sec_date_in = parse(section_data.sec_date_in) session.commit() return db_section except Exception as e: print(e)
def add_document(cls, doc): try: session = DbSessionFactory.create_session() db_doc = Document() db_doc.doc_id = doc.doc_id db_doc.doc_name = doc.doc_name session.add(db_doc) session.commit() return db_doc except Exception as e: print(e)
def add_section(cls, section): try: session = DbSessionFactory.create_session() db_section = Section() # db_section.sec_id = section.sec_id db_section.sec_text = section.sec_text db_section.sec_date_in = parse(section.sec_date_in) session.add(db_section) session.commit() return db_section except Exception as e: print(e)
def add_group(cls,group): try: session = DbSessionFactory.create_session() db_group = Group() # db_section.sec_id = section.sec_id db_group.doc_id = group.doc_id db_group.sec_id = group.sec_id db_group.order = 1 session.add(db_group) session.commit() return db_group except Exception as e: print (e)
def create_doc_parent(cls, doc_parent): try: session = DbSessionFactory.create_session() db_doc_parent = DocumentParent() # db_doc_parent.doc_parent_id = doc_parent['doc_parent_id'] db_doc_parent.doc_id = doc_parent['doc_id'] db_doc_parent.parent_id = doc_parent['parent_id'] db_doc_parent.relationship = doc_parent['relationship'] session.add(db_doc_parent) session.commit() return db_doc_parent except Exception as e: print(e)
def doc_parent_by_doc_id(cls, doc_id): session = DbSessionFactory.create_session() # section = session.query(Section).filter(Section.sec_id == sec_id).first() u = 0 docs = [] # for dp, d1a, d2a in session.query(DocumentParent, d1, d2).\ # filter(DocumentParent.doc_id == d1.doc_id,\ # DocumentParent.parent_id == d2.doc_id).all(): # # d2.doc_id == doc_id).all(): # for dp, d1a, d2a in session.query(DocumentParent, d1, d2). \ # filter(DocumentParent.doc_id == d1.doc_id,\ # DocumentParent.parent_id == d2.doc_id).all(): q = ( session.query(DocumentParent, Document, Document).filter( DocumentParent.doc_id == Document.doc_id).filter( DocumentParent.parent_id == Document.doc_id) # .filter(User.email == 'someemail') .all()) u = u + 1 # print("relationship: {} doc_name: {} doc_name: {}".format(dp.relationship, d1a.doc_name)) print("relationship: {} doc_name: {} doc_name: {}".format( DocumentParent.relationship, d1.doc_name, d2.doc_name)) # # u = Doc_Group_Sec() # # u.add_group_id(g.group_id) # u.add_doc_name(d.doc_name) # u.add_sec_text(s.sec_text) # u.add_sec_id(s.sec_id) # # docs.append(u.to_dict()) session.close() return u # docs
def group_by_id(cls, group_id): session = DbSessionFactory.create_session() group = session.query(Group).filter(Group.group_id == group_id).first() session.close() return group
def section_by_doc(cls, doc_id): session = DbSessionFactory.create_session() section = session.query(Group).filter(Group.doc_id == doc_id).all() session.close() return section