# 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 Gallery(models.Model):
#     """
#     The Gallery model will contain info about our media gallery
#     """
# 
#     name = models.CharField(max_length=100,blank=True,null=True)
#     short_code = models.CharField(max_length=100,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     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)
#     elements = models.ManyToManyField(Element, through="GalleryElement")
#     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 = "Galleries"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# Path: media_explorer/forms.py
# class MediaFormField(forms.Field):
#     def __init__(self, name=None, required=False, widget=MediaWidget, label=None, initial=None, help_text="", max_length=None, *args, **kwargs):
#         super(MediaFormField, self).__init__(required=required, widget=MediaWidget, label=label, initial=initial, help_text=help_text, *args, **kwargs)
# 
#     def clean(self, value):
#         if value:
#             try:
#                 data = json.loads(value)
#             except Exception as e:
#                 raise ValidationError("JSON parsing error: " + str(e))
#             return value
#         return None
# 
# class RichTextFormField(forms.Field):
#     def __init__(self, name=None, required=False, widget=RichTextWidget, label=None, initial=None, help_text="", max_length=None, *args, **kwargs):
#         super(RichTextFormField, self).__init__(required=required, widget=RichTextWidget, label=label, initial=initial, help_text=help_text, *args, **kwargs)
# 
#     def clean(self, value):
#         if value:
#             return value
#         return None


import json
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from media_explorer.models import Element, Gallery
from media_explorer.forms import MediaFormField, RichTextFormField
from django.db.models import signals, FileField
from django.forms import forms
from django.template.defaultfilters import filesizeformat



def parse_media(string_or_obj):
    """Takes a JSON string, converts it into a Media object."""
    data = {}
    kwargs = {}
    kwargs["id"] = None
    kwargs["type"] = None
    kwargs["caption"] = None
    kwargs["credit"] = None
    try:
        if type(string_or_obj) is dict:
            data = string_or_obj

# 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 Gallery(models.Model):
#     """
#     The Gallery model will contain info about our media gallery
#     """
# 
#     name = models.CharField(max_length=100,blank=True,null=True)
#     short_code = models.CharField(max_length=100,blank=True,null=True)
#     description = models.TextField(blank=True,null=True)
#     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)
#     elements = models.ManyToManyField(Element, through="GalleryElement")
#     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 = "Galleries"
# 
#     def __unicode__(self):
#         return u"%s" % (self.name)
# 
# Path: media_explorer/forms.py
# class MediaFormField(forms.Field):
#     def __init__(self, name=None, required=False, widget=MediaWidget, label=None, initial=None, help_text="", max_length=None, *args, **kwargs):
#         super(MediaFormField, self).__init__(required=required, widget=MediaWidget, label=label, initial=initial, help_text=help_text, *args, **kwargs)
# 
#     def clean(self, value):
#         if value:
#             try:
#                 data = json.loads(value)
#             except Exception as e:
#                 raise ValidationError("JSON parsing error: " + str(e))
#             return value
#         return None
# 
# class RichTextFormField(forms.Field):
#     def __init__(self, name=None, required=False, widget=RichTextWidget, label=None, initial=None, help_text="", max_length=None, *args, **kwargs):
#         super(RichTextFormField, self).__init__(required=required, widget=RichTextWidget, label=label, initial=initial, help_text=help_text, *args, **kwargs)
# 
#     def clean(self, value):
#         if value:
#             return value
#         return None


# Path: media_explorer/fields.py
import json
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from media_explorer.models import Element, Gallery
from media_explorer.forms import MediaFormField, RichTextFormField
from django.db.models import signals, FileField
from django.forms import forms
from django.template.defaultfilters import filesizeformat




def parse_media(string_or_obj):
    """Takes a JSON string, converts it into a Media object."""
    data = {}
    kwargs = {}
    kwargs["id"] = None
    kwargs["type"] = None
    kwargs["caption"] = None
    kwargs["credit"] = None
    try:
        if type(string_or_obj) is dict:
            data = string_or_obj

        elif type(string_or_obj) is Element: