from django.test import TestCase
from django.core.files.images import ImageFile
import datetime
from models import Photo
from imagr_users.models import ImagrUser
from models import Album
from admin import FileSizeListFilter
from imagr_images import admin


#test file size save option
def create_photo(path, owner, title, access, description):
    with open('imagr_images/test_data/'+path, 'rb') as f:
        image = ImageFile(f)
        photo = Photo(
            image=image,
            owner=owner,
            title=title,
            access=access,
            description=description
        )
        photo.save()
    return photo

class PhotoModelTests(TestCase):
    def test_create_photo(self):
        title = u"Cat in Tent"
        owner = ImagrUser.objects.create(username=u'John Doe')
        description = "a cat in a tent"
        access = 1
        photo = create_photo(
            'cat_in_tent.jpg',
            owner,
            title,
            access,
            description
        )

        # have to make time naive to compare to another naive time
        naive_dt_uploaded = photo.date_uploaded.replace(tzinfo=None)
        naive_dt_modified = photo.date_modified.replace(tzinfo=None)
        self.assertTrue(naive_dt_uploaded < datetime.datetime.utcnow())
        photo.save()
        self.assertTrue(
            naive_dt_uploaded < naive_dt_modified < datetime.datetime.utcnow()
        )

        photo = Photo.objects.get(title=u"Cat in Tent")
        print photo.id
        self.assertEquals(photo.owner, owner)
        self.assertEquals(photo.title, title)
        self.assertEquals(photo.description, description)
        self.assertEquals(photo.access, 1)
        self.assertEquals(photo.image.height, 2448)
        self.assertEquals(photo.image.width, 3264)
        self.assertEquals(photo.size, 1541649)


        request1 = "<1MB"
        request10 = "<10MB"
        request100 = "<100MB"
        requestBig = ">100MB"
        filter = FileSizeListFilter(None, { 'size': request10}, Photo, admin.PhotoAdmin)
        cat_photosList = Photo.objects.all()
        result1 = filter.queryset(None, cat_photosList)
        self.assertEquals(result1[0], cat_photosList[0])

        filter2 = FileSizeListFilter(None, {'size': requestBig}, Photo, admin.PhotoAdmin)
        result2 = filter2.queryset(None, cat_photosList)
        self.assertNotEqual(result2, cat_photosList)



class AlbumModelTests(TestCase):
    def test_create_album(self):
        album_title = u"Cats"
        owner = ImagrUser.objects.create(username=u'John Doe')
        album_access = 1
        album_description = u"Photos of cats"

        photo1 = create_photo(
            'cat_in_tent.jpg',
            owner,
            'Cat in tent',
            1,
            'photo of cat in tent'
        )
        photo2 = create_photo(
            'cat_in_drawer.jpg',
            owner,
            'Cat in a drawer',
            1,
            'photo of a cat in a drawer'
        )
        cover_photo = create_photo(
            'cat_yawning.jpg',
            owner,
            'Cat yawning',
            1,
            'Photo of cat yawning'
        )
        album = Album(
            title=album_title,
            owner=owner,
            description=album_description,
            access=album_access,
            cover_photo=cover_photo
        )
        album.save()
        # add the photo
        album.photos.add(photo1)
        album.photos.add(photo2)
        album.save()
        # check dates
        naive_dt_created = album.date_created.replace(tzinfo=None)
        naive_dt_modified = album.date_modified.replace(tzinfo=None)
        self.assertTrue(naive_dt_created < datetime.datetime.utcnow())
        album.save()
        self.assertTrue(
            naive_dt_created < naive_dt_modified < datetime.datetime.utcnow()
        )
        # check that photos are in the album
        self.assertTrue(Photo.objects.get(id=1) in album.photos.all())
        self.assertTrue(Photo.objects.get(id=2) in album.photos.all())
        self.assertEquals(album.owner, owner)
        self.assertEquals(album.title, album_title)
        self.assertEquals(album.description, album_description)
        self.assertEquals(album.access, 1)
