Ejemplo n.º 1
0
    def query_find_by_hash(cls, hash_type, hash_value, tags, session,
                           distinct_name=True):
        if distinct_name:
            last_ids = session.query(cls.id_file,
                                     cls.name,
                                     func.max(cls.id).label('last_id')) \
                .group_by(cls.id_file, cls.name).subquery()

            query = session.query(cls) \
                .join((last_ids, and_(cls.id_file == last_ids.c.id_file,
                                      cls.name == last_ids.c.name,
                                      cls.id == last_ids.c.last_id)))
        else:
            query = session.query(cls)

        query = query.join(File, File.id == cls.id_file)

        query = query.filter(getattr(File, hash_type) == hash_value) \
            .order_by(cls.name)
        # Update the query with tags if user asked for it
        if tags is not None:
            query = query.join(File.tags)
            for tagid in tags:
                # check if tag exists
                Tag.find_by_id(tagid, session)
                query = query.filter(File.tags.any(Tag.id == tagid))

        return query
Ejemplo n.º 2
0
 def test_remove_tag_error(self):
     text = "whatever"
     t = Tag(text=text)
     m_session = MagicMock()
     m_session.query(Tag).filter().one.return_value = t
     with self.assertRaises(IrmaDatabaseError):
         self.file.remove_tag("id", m_session)
     self.assertEqual(len(self.file.tags), 0)
Ejemplo n.º 3
0
 def test_remove_tag(self):
     text = "whatever"
     t = Tag(text=text)
     m_session = MagicMock()
     m_session.query(Tag).filter().one.return_value = t
     self.assertEqual(len(self.file.tags), 0)
     self.file.add_tag("id", m_session)
     self.file.remove_tag("id", m_session)
     self.assertEqual(len(self.file.tags), 0)
Ejemplo n.º 4
0
 def test_add_tag_error(self):
     text = "whatever"
     t = Tag(text=text)
     m_session = MagicMock()
     m_session.query(Tag).filter().one.return_value = t
     self.file.add_tag("id", m_session)
     with self.assertRaises(IrmaDatabaseError):
         self.file.add_tag("id", m_session)
     self.assertCountEqual(self.file.tags, [t])
Ejemplo n.º 5
0
def list():
    """ Search for all tags in TAG table using query.
    :return:
        on success 'items' contains a list of all tags
        on error 'msg' gives reason message
    """
    session = db.session
    available_tags = Tag.query_find_all(session)

    return {
        'items': tag_schema.dump(available_tags, many=True).data,
    }
Ejemplo n.º 6
0
def new(text: hug.types.text = None):
    """ Add a new tag, text is passed as query parameter.
    :return:
        on success new tag object is returned
        on error 'msg' gives reason message
    """
    session = db.session

    if text is None or text == "":
        raise HTTPInvalidParam("text", "Text should not be empty")

    # Check if a tag with same tag already exists
    available_tags = Tag.query_find_all(session)
    if text in [t.text for t in available_tags]:
        raise HTTPInvalidParam("text", "Tag already exists")

    # create tag if not already present
    tag = Tag(text)
    session.add(tag)
    session.commit()

    # returns new tag
    return tag_schema.dump(tag).data
Ejemplo n.º 7
0
    def query_find_by_name(cls, name, tags, session):
        last_ids = session.query(cls.id_file,
                                 cls.name,
                                 func.max(cls.id).label('last_id')) \
            .group_by(cls.id_file, cls.name).subquery()

        query = session.query(cls) \
            .join((last_ids, and_(cls.id_file == last_ids.c.id_file,
                                  cls.name == last_ids.c.name,
                                  cls.id == last_ids.c.last_id))) \
            .join(File, File.id == cls.id_file) \
            .filter(cls.name.like("%{0}%".format(name))) \
            .order_by(cls.name)

        # Update the query with tags if user asked for it
        if tags is not None:
            query = query.join(File.tags)
            for tagid in tags:
                # check if tag exists
                Tag.find_by_id(tagid, session)
                query = query.filter(File.tags.any(Tag.id == tagid))

        return query
Ejemplo n.º 8
0
 def test___init__(self):
     text = "whatever"
     t = Tag(text=text)
     self.assertEqual(t.text, text)
Ejemplo n.º 9
0
 def test_query_find_all(self):
     m_session = MagicMock()
     Tag.query_find_all(m_session)
     m_session.query().all.assert_called_once()
Ejemplo n.º 10
0
 def test_to_json(self):
     text = "whatever"
     t = Tag(text=text)
     expected = {'text': text}
     self.assertEqual(t.to_json(), expected)