# Path: media_explorer/models.py
# class Element(models.Model):
#     """
#     The Element model will contain images and videos
#     NOTE: if type=video you can still have a thumbnail_image
#     """
# 
#     TYPE_CHOICES = (('image','Image'),('video','Video'))
# 
#     name = models.CharField(max_length=150,blank=True,null=True)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     original_file_name = models.CharField(max_length=150,blank=True,null=True)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     video_url = models.CharField(max_length=255,blank=True,null=True)
#     video_embed = models.TextField(blank=True,null=True)
#     manual_embed_code = models.BooleanField(_("Manually enter video embed code"), default=False)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     thumbnail_image_width = models.IntegerField(blank=True,null=True,default='0')
#     thumbnail_image_height = models.IntegerField(blank=True,null=True,default='0')
#     type = models.CharField(_("Type"), max_length=10, default="image",choices=TYPE_CHOICES)
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Elements"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
#     def save(self, *args, **kwargs):
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
#         super(Element, self).save(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
# 
#         super(Element, self).save(*args, **kwargs)
# 
#         save_again = False
# 
#         #Init thumbnail_image
#         if self.type == "image" \
#                 and self.image \
#                 and not self.thumbnail_image:
#             self.image_url = self.image.url
#             self.thumbnail_image = self.image
#             self.thumbnail_image_url = self.image.url
#             save_again = True
# 
#         if save_again:
#             super(Element, self).save(*args, **kwargs)
# 
# class ResizedImage(models.Model):
#     """
#     The ResizedImage is a resized image version of Element.image
#     """
# 
#     image = models.ForeignKey(Element)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     size = models.CharField(max_length=25,blank=True,null=True)
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name = "Resized image"
#         verbose_name_plural = "Resized images"
# 
#     def html_img(self):
#         try:
#             return "<a target='_blank' href='" + self.image_url + "'><img style='width:100px' src='" + self.image_url + "'></a>"
#         except:
#             pass
# 
#     html_img.allow_tags = True


import os
from django.test import TestCase, Client, override_settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from media_explorer.models import Element, ResizedImage

        #Check the DB to make sure element is present
        count1 = Element.objects.filter(type="image",name="test_image_upload_with_no_resize",image_url__icontains="Oxfam-Cambodia").count()
        self.assertEqual(count1, 1)

        #Check to make sure image was NOT resized
        count2 = ResizedImage.objects.filter(image__type="image",image__name="test_image_upload_with_no_resize",image__image_url__icontains="Oxfam-Cambodia").count()
        self.assertEqual(count2, 0)

    @override_settings(DME_RESIZE=True)
    def test_image_upload_with_resize(self):
        """
        Test image upload with resize
        Condition: User is logged in and authorized
        Condition: Post has an image
        Condition: Resize image
        Result: Success
        Result: Element count should equal 1
        Result: ResizedImage count should be more than 0
        """
        url = reverse("api-media-elements")
        c = Client()
        c.login(username="admin",password="password")

        test_path = os.path.dirname(os.path.abspath(__file__))
        with open(test_path + '/Oxfam-Cambodia.jpg') as fp:
            response = c.post(url, {'name':'test_image_upload_with_resize','image':fp},HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)

        #Check the DB to make sure element is present

# Path: media_explorer/models.py
# class Element(models.Model):
#     """
#     The Element model will contain images and videos
#     NOTE: if type=video you can still have a thumbnail_image
#     """
# 
#     TYPE_CHOICES = (('image','Image'),('video','Video'))
# 
#     name = models.CharField(max_length=150,blank=True,null=True)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     original_file_name = models.CharField(max_length=150,blank=True,null=True)
#     credit = models.CharField(max_length=255,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     video_url = models.CharField(max_length=255,blank=True,null=True)
#     video_embed = models.TextField(blank=True,null=True)
#     manual_embed_code = models.BooleanField(_("Manually enter video embed code"), default=False)
#     thumbnail_image = models.ImageField(blank=True,null=True,max_length=255,upload_to="images/")
#     thumbnail_image_url = models.CharField(max_length=255,blank=True,null=True)
#     thumbnail_image_width = models.IntegerField(blank=True,null=True,default='0')
#     thumbnail_image_height = models.IntegerField(blank=True,null=True,default='0')
#     type = models.CharField(_("Type"), max_length=10, default="image",choices=TYPE_CHOICES)
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name_plural = "Elements"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
#     def save(self, *args, **kwargs):
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
#         super(Element, self).save(*args, **kwargs)
# 
#     def save(self, *args, **kwargs):
# 
#         if not self.name:
#             if self.type == "image":
#                 self.name = self.file_name
#             elif self.type == "video":
#                 self.name = self.video_url
# 
#         super(Element, self).save(*args, **kwargs)
# 
#         save_again = False
# 
#         #Init thumbnail_image
#         if self.type == "image" \
#                 and self.image \
#                 and not self.thumbnail_image:
#             self.image_url = self.image.url
#             self.thumbnail_image = self.image
#             self.thumbnail_image_url = self.image.url
#             save_again = True
# 
#         if save_again:
#             super(Element, self).save(*args, **kwargs)
# 
# class ResizedImage(models.Model):
#     """
#     The ResizedImage is a resized image version of Element.image
#     """
# 
#     image = models.ForeignKey(Element)
#     file_name = models.CharField(max_length=150,blank=True,null=True)
#     size = models.CharField(max_length=25,blank=True,null=True)
#     image_url = models.CharField(max_length=255,blank=True,null=True)
#     image_width = models.IntegerField(blank=True,null=True,default='0')
#     image_height = models.IntegerField(blank=True,null=True,default='0')
#     created_at = models.DateTimeField(blank=True,null=True,auto_now_add=True)
#     updated_at = models.DateTimeField(blank=True,null=True,auto_now=True)
# 
#     class Meta:
#         verbose_name = "Resized image"
#         verbose_name_plural = "Resized images"
# 
#     def html_img(self):
#         try:
#             return "<a target='_blank' href='" + self.image_url + "'><img style='width:100px' src='" + self.image_url + "'></a>"
#         except:
#             pass
# 
#     html_img.allow_tags = True


# Path: media_explorer/tests/elements/tests.py
import os
from django.test import TestCase, Client, override_settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from media_explorer.models import Element, ResizedImage


        #Check the DB to make sure element is present
        count1 = Element.objects.filter(type="image",name="test_image_upload_with_no_resize",image_url__icontains="Oxfam-Cambodia").count()
        self.assertEqual(count1, 1)

        #Check to make sure image was NOT resized
        count2 = ResizedImage.objects.filter(image__type="image",image__name="test_image_upload_with_no_resize",image__image_url__icontains="Oxfam-Cambodia").count()
        self.assertEqual(count2, 0)

    @override_settings(DME_RESIZE=True)
    def test_image_upload_with_resize(self):
        """
        Test image upload with resize
        Condition: User is logged in and authorized
        Condition: Post has an image
        Condition: Resize image
        Result: Success
        Result: Element count should equal 1
        Result: ResizedImage count should be more than 0
        """
        url = reverse("api-media-elements")
        c = Client()
        c.login(username="admin",password="password")

        test_path = os.path.dirname(os.path.abspath(__file__))
        with open(test_path + '/Oxfam-Cambodia.jpg') as fp:
            response = c.post(url, {'name':'test_image_upload_with_resize','image':fp},HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEqual(response.status_code, 200)

        #Check the DB to make sure element is present

        count1 = Element.objects.filter(type="image",name="test_image_upload_with_resize",image_url__icontains="Oxfam-Cambodia").count()