def update(cls, idnum, instance): # TODO: not sure if check necessary if instance['tags'] is None: instance['tags'] = [] if instance['drtype'] is None: instance['drtype'] = '' try: with session_scope() as session: record = session.query(Dream).filter_by(id=idnum).one() old_tags = record.get_tags() old_drtype = record.get_drtype() record.date = instance['date'] record.title = instance['title'] record.recit = instance['recit'] session.add(record) except IntegrityError: logger.info("error update dream db") to_add_tags = list(set(instance['tags']).difference(old_tags)) to_rm_tags = list(set(old_tags).difference(instance['tags'])) cls._add_tags(idnum, to_add_tags) cls._rm_tags(idnum, to_rm_tags) if instance['drtype'] != old_drtype: cls._add_drtype(idnum, instance['drtype']) cls._rm_drtype(idnum, old_drtype)
def create(cls, instance): """ Arguments: - instance (dict): dictionnary containing row attributes and values - title (str): dream title - date (datetime): date of the dream - recit (str): the corpus of the dream - tags (list(str)): a list of tags - drtype (str): a dream type (normal, lucid, ...) """ record = Dream(title=instance['title'], recit=instance['recit'], date=instance['date']) idnum = None try: with session_scope() as session: session.add(record) session.flush() idnum = record.id except IntegrityError: logger.info("duplicate Dream") if idnum is not None: cls._add_tags(idnum, instance['tags']) cls._add_drtype(idnum, instance['drtype']) return idnum
def find(cls, labels): """ Arguments: - labels: list(str): a list of strings """ with session_scope() as session: obj = session.query(DreamType).filter(DreamType.label.in_(labels)) return obj
def create(cls, drtype): record = DreamType(label=drtype) try: with session_scope() as session: session.add(record) except IntegrityError: logger.info(f'duplicate DreamType: {drtype}')
def get_labels(cls): """ Return the list of defined labels """ labels = [] with session_scope() as session: for inst in session.query(DreamType): labels.append(inst.label) return labels
def create(cls, tag): """ Arguments: - tag (str) """ record = Tag(label=tag) try: with session_scope() as session: session.add(record) except IntegrityError: logger.info(f'duplicate Tag: {tag}')
def _rm_drtype(cls, idnum, drtype=None): if drtype is None: drtype = '' if drtype: try: with session_scope() as session: instance = session.query(Dream).filter(Dream.id == idnum).one() drtype_rec = session.query(DreamType).filter(DreamType.label == drtype).first() if drtype_rec in instance.drtype: instance.drtype.remove(drtype_rec) except: logger.error("remove drtype error")
def _add_drtype(cls, idnum, drtype=None): if drtype is None: drtype = '' if drtype: DreamTypeDAO.create(drtype) try: with session_scope() as session: instance = session.query(Dream).filter(Dream.id == idnum).one() drtype_rec = session.query(DreamType).filter(DreamType.label == drtype).first() if drtype_rec not in instance.drtype: instance.drtype.append(drtype_rec) except: logger.error("append drtype error")
def _rm_tags(cls, idnum, tags=None): """ remove tags to record whose id = idnum """ if tags is None: tags = [] if tags: try: with session_scope() as session: instance = session.query(Dream).filter(Dream.id==idnum).one() tags_rec = session.query(Tag).filter(Tag.label.in_(tags)) for elem in tags_rec: if elem in instance.tags: instance.tags.remove(elem) except: logger.error("remove tags error")
def get_tree(cls): tree = OrderedDict() with session_scope() as session: instances = session.query(Dream).order_by(Dream.date, Dream.created) nbrows = instances.count() dftree = pd.DataFrame(index = np.arange(0, nbrows), columns=['year', 'month', 'day', 'id', 'title']) for i, inst in enumerate(instances): year = inst.date.strftime('%Y') month = inst.date.strftime('%m') day = inst.date.strftime('%d') dftree.loc[i] = [year, month, day, inst.id, inst.title] dftree.apply(cls._poptree, args=(tree,), axis=1) return(tree)
def find_by_id(cls, idnum): """ Find a record by id Arguments: - idnum(int): identity field (primary key) of the table Return: obj: a query object """ logger.info(f'find by id: {idnum}') try: with session_scope() as session: obj = session.query(Dream).filter(Dream.id == idnum).one() record = object_as_dict(obj) # as a dict record['tags'] = obj.get_tags() record['drtype'] = obj.get_drtype() except: logger.info("find by id error") return record
def _add_tags(cls, idnum, tags=None): """ append tags to record whose id = idnum """ if tags is None: tags = [] if tags: for elem in tags: TagDAO.create(elem) try: with session_scope() as session: instance = session.query(Dream).filter(Dream.id==idnum).one() tags_rec = session.query(Tag).filter(Tag.label.in_(tags)) for elem in tags_rec: if elem not in instance.tags: instance.tags.append(elem) except: logger.error("append tags error")