Exemplo n.º 1
0
    def test_aspect(self):

        DataHelper.create_data_items(self, db)

        from cloud_on_film.files.picture import Picture

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.aspect == 4 ) \
            .first()

        self.assertEqual(4, file_test.aspect)
        self.assertEqual('random320x240.png', file_test.name)

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.width == 100 ) \
            .first()

        self.assertEqual(1, file_test.aspect)
        self.assertEqual('random100x100.png', file_test.name)

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.aspect == 10 ) \
            .first()

        self.assertEqual(10, file_test.aspect)
        self.assertEqual('random640x400.png', file_test.name)
Exemplo n.º 2
0
    def from_path( lib, path, user_id ):

        picture = None
        try:
            picture = Item.from_path( lib, path, user_id )
        except DBItemNotFoundException as e:
            # Item doesn't exist in DB, but it does on FS, so add it to the DB.
            img_stat = os.stat( e.absolute_path )
            current_app.logger.info(
                'creating entry for file: {}'.format( e.absolute_path ) )
            picture = Picture(
                name=e.filename,
                folder_id=e.folder.id,
                timestamp=datetime.fromtimestamp( img_stat[stat.ST_MTIME] ),
                size=img_stat[stat.ST_SIZE],
                added=datetime.now(),
                hash=Picture.hash_file( e.absolute_path, 1 ),
                hash_algo=1 )
            db.session.add( picture )
            db.session.commit()

        if 'width' not in picture.meta or 'height' not in picture.meta:
            with Image.open( picture.absolute_path ) as im:
                print( im.size )
                picture.meta['width'] = im.size[0]
                picture.meta['height'] = im.size[1]
            db.session.commit()

            current_app.logger.info( 'found new image with size: {}x{}'.format(
                picture.width, picture.height
            ) )

        return picture
Exemplo n.º 3
0
    def test_item_machine_path(self):

        DataHelper.create_data_items(self, db)

        file1 = Item.from_path(self.lib.id,
                               'subfolder2/subfolder3/random320x240.png',
                               self.user_id)

        self.assertEqual(file1.folder.machine_path, ['testing_library', 2, 3])
Exemplo n.º 4
0
    def test_nsfw(self):

        DataHelper.create_data_items(self, db)

        from cloud_on_film.files.picture import Picture

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.nsfw == 1 ) \
            .first()

        self.assertTrue(file_test.nsfw)
Exemplo n.º 5
0
    def test_rating(self):

        DataHelper.create_data_items(self, db)

        from cloud_on_film.files.picture import Picture

        files_test = Item.secure_query( self.user_id ) \
            .filter( Picture.rating > 1 ) \
            .all()

        self.assertEqual(1, len(files_test))
        self.assertEqual(4, files_test[0].rating)
        self.assertEqual('random100x100.png', files_test[0].name)

        files_test = Item.secure_query( self.user_id ) \
            .filter( Picture.rating == 1 ) \
            .all()

        self.assertEqual(1, len(files_test))
        self.assertEqual(1, files_test[0].rating)
        self.assertEqual('random500x500.png', files_test[0].name)
Exemplo n.º 6
0
    def test_query_width(self):

        DataHelper.create_data_items(self, db)

        from cloud_on_film.files.picture import Picture

        files_test = Item.secure_query( self.user_id ) \
            .filter( Picture.width == 100 ) \
            .all()

        self.assertEqual(1, len(files_test))
        self.assertEqual('random100x100.png', files_test[0].name)
        self.assertEqual(100, files_test[0].width)
Exemplo n.º 7
0
    def test_item_tags(self):

        DataHelper.create_data_items(self, db)

        from cloud_on_film.files.picture import Picture

        tag = Tag.from_path('IFDY/Test Tag 1/Sub Test Tag 3')

        files_test = Item.secure_query( self.user_id ) \
            .filter( Picture.width == 100 ) \
            .all()

        self.assertEqual(1, len(files_test))
        self.assertIn(tag, files_test[0].tags)
Exemplo n.º 8
0
    def test_import(self):

        # Perform the import.
        pics_json = None
        with open('testdata/test_import.json', 'r') as import_file:
            pics_json = json.loads(import_file.read())
        for pic_json in pics_json:
            pic_json['filename'] = os.path.join(self.nsfw_lib_path,
                                                pic_json['filename'])
            picture(pic_json)

        # Test the results.
        tag_testing_img = Tag.from_path('Testing Imports/Testing Image')

        from cloud_on_film.files.picture import Picture

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.width == 500 ) \
            .first()

        self.assertIn(tag_testing_img, file_test.tags)
        self.assertEqual(500, file_test.width)
        self.assertEqual(500, file_test.height)
        self.assertEqual(1, file_test.aspect)
        self.assertTrue(file_test.nsfw)
        self.assertEqual(3, file_test.rating)

        file_test = Item.secure_query( self.user_id ) \
            .filter( Picture.aspect == 10 ) \
            .first()

        self.assertNotIn(tag_testing_img, file_test.tags)
        self.assertEqual(640, file_test.width)
        self.assertEqual(400, file_test.height)
        self.assertEqual(10, file_test.aspect)
        self.assertTrue(file_test.nsfw)
        self.assertEqual(0, file_test.rating)
Exemplo n.º 9
0
    def search(self, user_id, _tree_start=None, _query=None):

        if not _query:
            _query = Item.secure_query(user_id)

        if not _tree_start:
            _tree_start = self.lexer.root

        if isinstance(_tree_start, SearchLexerParser.Group):
            child_filter_list = []
            for c in _tree_start.children:
                child_filter_list.append(self.search(user_id, c, _query))

            filter_out = None
            if isinstance(_tree_start, SearchLexerParser.Or):
                filter_out = db.or_(*child_filter_list)

            elif isinstance(_tree_start, SearchLexerParser.Not):
                filter_out = db.not_(*child_filter_list)

            else:
                # If it's not an or then it's an and.
                filter_out = db.and_(*child_filter_list)

            if _tree_start.parent:
                return filter_out
            else:
                return _query.filter(filter_out)

        elif isinstance(_tree_start, SearchLexerParser.Compare):

            # TODO: Get plugin model or something.
            from cloud_on_film.files.picture import Picture

            if SearchLexerParser.Op.in_ == _tree_start.op:
                # Special ops that use the attrib postfix.
                if not hasattr(Picture, _tree_start.children[1]):
                    raise SearchExecuteException(
                        'invalid attribute "{}" specified'.format(
                            _tree_start.children[1]))

            elif not hasattr(Picture, _tree_start.children[0]):
                raise SearchExecuteException(
                    'invalid attribute "{}" specified'.format(
                        _tree_start.children[0]))

            if SearchLexerParser.Op.eq == _tree_start.op:
                return (getattr(
                    Picture,
                    _tree_start.children[0]) == _tree_start.children[1])
            elif SearchLexerParser.Op.gt == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]) >
                        _tree_start.children[1])
            elif SearchLexerParser.Op.lt == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]) <
                        _tree_start.children[1])
            elif SearchLexerParser.Op.gte == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]) >=
                        _tree_start.children[1])
            elif SearchLexerParser.Op.lte == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]) <=
                        _tree_start.children[1])
            elif SearchLexerParser.Op.neq == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]) !=
                        _tree_start.children[1])
            elif SearchLexerParser.Op.like == _tree_start.op:
                return (getattr(Picture, _tree_start.children[0]).like(
                    _tree_start.children[1]))
            elif SearchLexerParser.Op.in_ == _tree_start.op:
                #if 'tags' == _tree_start.children[1]:
                #    _tree_start.children = (Tag.from_path(_tree_start.children[1] ), 'tag_ids')
                return getattr(
                    Picture,
                    _tree_start.children[1]).any(name=_tree_start.children[0])

        return _query