# Path: flask_fs/mongo.py
# class FileField(BaseField):
#     '''
#     Store reference to files in a given storage.
#     '''
#     proxy_class = FileReference
# 
#     def __init__(self, fs=None, upload_to=None, basename=None, *args, **kwargs):
#         self.fs = fs
#         self.upload_to = upload_to
#         self.basename = basename
#         super(FileField, self).__init__(*args, **kwargs)
# 
#     def proxy(self, filename=None, instance=None, **kwargs):
#         return self.proxy_class(
#             fs=self.fs,
#             filename=filename,
#             upload_to=self.upload_to,
#             basename=self.basename,
#             instance=instance,
#             name=self.name,
#             **kwargs
#         )
# 
#     def to_python(self, value):
#         if not isinstance(value, self.proxy_class):
#             if isinstance(value, dict):
#                 value = self.proxy(**value)
#             elif isinstance(value, six.text_type):
#                 value = self.proxy(filename=value)
#         return value
# 
#     def __set__(self, instance, value):
#         if not isinstance(value, self.proxy_class):
#             value = self.proxy(filename=value, instance=instance)
#         return super(FileField, self).__set__(instance, value)
# 
#     def __get__(self, instance, owner):
#         if instance is None:
#             return self
# 
#         fileref = instance._data.get(self.name)
#         if not isinstance(fileref, self.proxy_class):
#             fileref = self.proxy(filename=fileref, instance=instance)
#             instance._data[self.name] = fileref
#         elif fileref._instance is None:
#             fileref._instance = instance
#         return fileref
# 
#     def to_mongo(self, value):
#         if not value:
#             return None
#         return value.to_mongo()
# 
# class ImageField(FileField):
#     '''
#     Store reference to images in a given Storage.
# 
#     Allow to automatically generate thumbnails or resized image.
#     Original image always stay untouched.
#     '''
#     proxy_class = ImageReference
# 
#     def __init__(self, max_size=None, thumbnails=None, optimize=None, *args, **kwargs):
#         self.max_size = max_size
#         self.thumbnail_sizes = thumbnails
#         self.optimize = optimize
#         super(ImageField, self).__init__(*args, **kwargs)
# 
#     def proxy(self, **kwargs):
#         return super(ImageField, self).proxy(max_size=self.max_size,
#                                              thumbnail_sizes=self.thumbnail_sizes,
#                                              optimize=self.optimize,
#                                              **kwargs)


import filecmp
import os
import flask_fs as fs
import pytest
from PIL import Image
from flask_fs.mongo import FileField, ImageField
from flask_mongoengine import MongoEngine
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division






db = MongoEngine()


class MongoEngineTestCase(object):
    @pytest.fixture(autouse=True)
    def storage(self, app, tmpdir):
        app.instance_path = str(tmpdir)
        storage = fs.Storage('test', fs.ALL)
        fs.init_app(app, storage)
        db.init_app(app)
        yield storage
        with app.test_request_context():
            db_name = app.config['MONGODB_DB']
            try:
                db.connection.client.drop_database(db_name)
            except TypeError:
                db.connection.drop_database(db_name)


class FileFieldTest(MongoEngineTestCase):
    def test_default_validate(self, storage):
        class Tester(db.Document):

# Path: flask_fs/mongo.py
# class FileField(BaseField):
#     '''
#     Store reference to files in a given storage.
#     '''
#     proxy_class = FileReference
# 
#     def __init__(self, fs=None, upload_to=None, basename=None, *args, **kwargs):
#         self.fs = fs
#         self.upload_to = upload_to
#         self.basename = basename
#         super(FileField, self).__init__(*args, **kwargs)
# 
#     def proxy(self, filename=None, instance=None, **kwargs):
#         return self.proxy_class(
#             fs=self.fs,
#             filename=filename,
#             upload_to=self.upload_to,
#             basename=self.basename,
#             instance=instance,
#             name=self.name,
#             **kwargs
#         )
# 
#     def to_python(self, value):
#         if not isinstance(value, self.proxy_class):
#             if isinstance(value, dict):
#                 value = self.proxy(**value)
#             elif isinstance(value, six.text_type):
#                 value = self.proxy(filename=value)
#         return value
# 
#     def __set__(self, instance, value):
#         if not isinstance(value, self.proxy_class):
#             value = self.proxy(filename=value, instance=instance)
#         return super(FileField, self).__set__(instance, value)
# 
#     def __get__(self, instance, owner):
#         if instance is None:
#             return self
# 
#         fileref = instance._data.get(self.name)
#         if not isinstance(fileref, self.proxy_class):
#             fileref = self.proxy(filename=fileref, instance=instance)
#             instance._data[self.name] = fileref
#         elif fileref._instance is None:
#             fileref._instance = instance
#         return fileref
# 
#     def to_mongo(self, value):
#         if not value:
#             return None
#         return value.to_mongo()
# 
# class ImageField(FileField):
#     '''
#     Store reference to images in a given Storage.
# 
#     Allow to automatically generate thumbnails or resized image.
#     Original image always stay untouched.
#     '''
#     proxy_class = ImageReference
# 
#     def __init__(self, max_size=None, thumbnails=None, optimize=None, *args, **kwargs):
#         self.max_size = max_size
#         self.thumbnail_sizes = thumbnails
#         self.optimize = optimize
#         super(ImageField, self).__init__(*args, **kwargs)
# 
#     def proxy(self, **kwargs):
#         return super(ImageField, self).proxy(max_size=self.max_size,
#                                              thumbnail_sizes=self.thumbnail_sizes,
#                                              optimize=self.optimize,
#                                              **kwargs)


# Path: tests/test_mongoengine.py
import filecmp
import os
import flask_fs as fs
import pytest
from PIL import Image
from flask_fs.mongo import FileField, ImageField
from flask_mongoengine import MongoEngine

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, division






db = MongoEngine()


class MongoEngineTestCase(object):
    @pytest.fixture(autouse=True)
    def storage(self, app, tmpdir):
        app.instance_path = str(tmpdir)
        storage = fs.Storage('test', fs.ALL)
        fs.init_app(app, storage)
        db.init_app(app)
        yield storage
        with app.test_request_context():
            db_name = app.config['MONGODB_DB']
            try:
                db.connection.client.drop_database(db_name)
            except TypeError:
                db.connection.drop_database(db_name)


class FileFieldTest(MongoEngineTestCase):
    def test_default_validate(self, storage):
        class Tester(db.Document):

            file = FileField(fs=storage)