# -*- coding: utf-8 -*-

import tempfile, tg, os, shutil, cgi, base64, Image
from sqlalchemy.schema import Column
from sqlalchemy.types import Unicode, Integer
from sqla_base import setup_database, clear_database, DeclarativeBase, DBSession, Thing
from tgext.datahelpers.fields import Attachment, AttachedFile, AttachedImage

def setup():
    tg.config['attachments_path'] = '/tmp/datahelpers'
    try:
        os.mkdir(tg.config['attachments_path'])
    except:
        pass
    setup_database()

def teardown():
    shutil.rmtree(tg.config['attachments_path'], ignore_errors=True)

class Document(DeclarativeBase):
    __tablename__ = 'docu'

    uid = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(Unicode(16), unique=True)
    content = Column(Attachment)
    photo = Column(Attachment(AttachedImage))

class TestSQLAAttachments(object):
    def __init__(self):
        self.file_content = 'this is the file content'
        self.fake_file = tempfile.NamedTemporaryFile()
        self.fake_file.write(self.file_content)
        self.fake_file.flush()

    def setup(self):
        clear_database()

    def test_create_fromfile(self):
        doc = Document(name=u'Foo')
        doc.content = open(self.fake_file.name)
        DBSession.add(doc)
        DBSession.flush()
        DBSession.commit()

        d = DBSession.query(Document).filter_by(name=u'Foo').first()
        assert d.content.file.read() == self.file_content
        assert d.content.filename == os.path.basename(self.fake_file.name)

    def test_create_fromfield(self):
        field = cgi.FieldStorage()
        field.filename = 'test.jpg'
        field.file = open(self.fake_file.name)

        doc = Document(name=u'Foo', content=field)
        DBSession.add(doc)
        DBSession.flush()
        DBSession.commit()

        d = DBSession.query(Document).filter_by(name=u'Foo').first()
        assert d.content.file.read() == self.file_content
        assert d.content.filename == os.path.basename('test.jpg')
        assert d.content.url.startswith('/attachments')
        assert d.content.url.endswith('test.jpg')

class TestSQLAImageAttachments(object):
    def __init__(self):
        self.file_content = '''R0lGODlhEQAUAPcAAC4uLjAwMDIyMjMzMjQ0NDU1NDY2Njk2Mzg4ODo6Oj49Ozw8PD4+PkE+OEA/PkhAN0tCNk5JPFFGNV1KMFhNNVFHOFJJPVVLPVhOPXZfKXVcK2ZQNGZXNGtZMnNc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NBBjaIqDhhI+cGgrNmyJXooGrShBJBKcIpwiFCibl0TehDdsWBCiBxLZuIwolMGiwcTJ4gUenThBAokSVRgGFJnzhYQJ1KsyRkkhWfPK87McWPEM4sRhgItCsGitQ5PmtxYUdK6BY4rf/zwYRNmUihRpyQdaUHchQsoX/Y4amTnUqZPoG7YMO7ihfUcYNC0eRMJExMqMKweW59BfkYMEk2ykHAio3x5GvDjy58Pv4b9+/jz2w8IADs='''
        self.file_content = base64.b64decode(self.file_content)
        self.fake_file = tempfile.NamedTemporaryFile()
        self.fake_file.write(self.file_content)
        self.fake_file.flush()

    def setup(self):
        clear_database()

    def test_create_fromfile(self):
        doc = Document(name=u'Foo')
        doc.photo = open(self.fake_file.name)
        DBSession.add(doc)
        DBSession.flush()
        DBSession.commit()

        d = DBSession.query(Document).filter_by(name=u'Foo').first()
        assert d.photo.file.read() == self.file_content
        assert d.photo.filename == os.path.basename(self.fake_file.name)

    def test_create_fromfield(self):
        field = cgi.FieldStorage()
        field.filename = u'àèìòù.gif'
        field.file = open(self.fake_file.name)

        doc = Document(name=u'Foo', photo=field)
        DBSession.add(doc)
        DBSession.flush()
        DBSession.commit()

        d = DBSession.query(Document).filter_by(name=u'Foo').first()
        assert d.photo.file.read() == self.file_content
        assert d.photo.filename == field.filename
        assert d.photo.url.startswith('/attachments/')
        assert d.photo.url.endswith(field.filename)

    def test_thumbnail(self):
        field = cgi.FieldStorage()
        field.filename = 'test.gif'
        field.file = open(self.fake_file.name)

        doc = Document(name=u'Foo', photo=field)
        DBSession.add(doc)
        DBSession.flush()
        DBSession.commit()

        d = DBSession.query(Document).filter_by(name=u'Foo').first()
        assert os.path.exists(d.photo.thumb_local_path)

        thumb = Image.open(d.photo.thumb_local_path)
        thumb.verify()
        assert thumb.format.upper() == d.photo.thumbnail_format.upper()
