示例#1
0
 def remove(self, value, by='id'):
     self.session = generate_session()
     if by not in ['id', 'category_id']:
         raise ValueError('`by`の値は"id"or"category_id"のみです。')
     self.session.query(db.Memo).\
         filter(db.Memo.__dict__[by]==value).\
         delete()
     self.flush()
示例#2
0
 def some_eq_tagname(self, tagname):
     session = generate_session()
     memo_ids = [
         r[0] for r in session.query(db.Tag.memo_id).filter_by(
             name=tagname).all()
     ]
     filters = [db.Memo.id.in_(memo_ids)]
     return self._generate_responses(filters, session=session)
示例#3
0
 def save(self, memo, update=False):
     self.session = generate_session()
     if not update:
         m = db.Memo(id=memo.id, category_id=memo.category_id,
                     title=memo.title, caption=memo.caption,
                     created_at=memo.created_at)
         m.tags = [db.Tag(id=memo.id+str(i), name=name)
                   for i, name in enumerate(memo.tagnames)]
         self.session.add(m)
     else:
         m = self.session.query(db.Memo).filter_by(id=memo.id).first()
         m.id = memo.id
         m.title = memo.title
         m.caption = memo.caption
         m.tags = [db.Tag(id=memo.id+str(i), name=name)
                       for i, name in enumerate(memo.tagnames)]
     self.flush()
示例#4
0
    def _generate_responses(self, filters, session=None):
        session = generate_session() if not session else session
        q = self._prepare_query(session)
        q = q.filter(*filters) if filters else q
        q = self._complete_query(q)

        responses = []
        group = groupby(q.all(), key=lambda x: x[0])
        for k, g in group:
            tagnames = [t.name for _, t in g if t]
            memo = k
            response = {
                'id': memo.id,
                'category_id': memo.category_id,
                'title': memo.title,
                'caption': memo.caption,
                'tagnames': tagnames,
                'created_at': memo.created_at.strftime('%Y-%m-%d %H:%M:%S')
            }
            responses.append(response)
        return responses
示例#5
0
 def _generate_names(self, filters):
     session = generate_session()
     q = session.query(db.Tag.name).group_by(db.Tag.name).filter(*filters)
     q = self._complete_query(q)
     return [r[0] for r in q.all()]