# Path: media_explorer/fields.py
# class MediaField(models.TextField):
#     """The Django MediaField."""
# 
#     description = _("A Media Explorer custom model field")
# 
#     def __init__(self, id=None, type=None, \
#             credit=None, caption=None, *args, **kwargs):
#         self.id = id
#         self.type = type
#         self.caption = caption
#         self.credit = credit
# 
#         kwargs['null'] = True
#         kwargs['blank'] = True
# 
#         super(MediaField, self).__init__(*args, **kwargs)
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(MediaField, self).deconstruct()
#         del kwargs["null"]
#         del kwargs["blank"]
#         return name, path, args, kwargs
# 
#     def db_type(self, connection):
#         return "longtext"
# 
#     def from_db_value(self, value, expression, connection, context):
#         if value is None:
#             return value
#         return parse_media(value)
# 
#     def do_validation(self, media):
#         if self.type and media.type and self.type != media.type:
#             raise ValidationError("Invalid media type selected for this MediaField instance. It expected a '%s' but got a '%s' instead." % (self.type, media.type))
# 
#         #Override id/credit/caption
#         if self.id : media.id = self.id
#         if self.type: media.type = self.type
#         if self.caption : media.caption = self.caption
#         if self.credit: media.credit = self.credit
# 
#         #Validate that the image/video is in the system
#         if media.type in ["image","video"] and \
#                 not Element.objects.filter(id=media.id,type=media.type).exists():
#             raise ValidationError("Invalid %s selected. The %s was not found." % (media.type, media.type))
# 
#         #Validate that the gallery is in the system
#         if media.type == "gallery" and \
#                 not Gallery.objects.filter(id=media.id).exists():
#             raise ValidationError("Invalid %s selected. The %s was not found." % (media.type, media.type))
# 
#         return media
# 
#     def to_python(self, value):
#         if isinstance(value, Media):
#             return value
# 
#         if value is None:
#             return value
# 
#         return self.do_validation(parse_media(value))
#     
#     def get_prep_value(self, value):
#         value_dict = {}
# 
#         try:
#             value_dict["id"] = value.id
#             value_dict["type"] = value.type
#             value_dict["caption"] = value.caption
#             value_dict["credit"] = value.credit
#         except:
#             pass
# 
#         if type(value) is Element:
#             value_dict["id"] = value.id
#             value_dict["type"] = value.type
#             value_dict["caption"] = None
#             value_dict["credit"] = None
#             if value.description:
#                 value_dict["caption"] = value.description
#             if value.credit:
#                 value_dict["credit"] = value.credit
# 
#         if value_dict:
#             self.do_validation(parse_media(value_dict))
#             return str(json.dumps(value_dict))
# 
#         if value: return str(value)
#         return value
# 
#     def formfield(self, **kwargs):
#         defaults = {}
#         defaults["form_class"] = MediaFormField
#         defaults.update(kwargs)
#         return super(MediaField, self).formfield(**defaults)
# 
# class RichTextField(models.TextField):
#     """The Django RichTextField."""
# 
#     description = _("A RichText Explorer custom model field")
# 
#     def __init__(self, *args, **kwargs):
#         kwargs['null'] = True
#         kwargs['blank'] = True
#         super(RichTextField, self).__init__(*args, **kwargs)
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(RichTextField, self).deconstruct()
#         del kwargs["null"]
#         del kwargs["blank"]
#         return name, path, args, kwargs
# 
#     def db_type(self, connection):
#         return "longtext"
# 
#     def from_db_value(self, value, expression, connection, context):
#         if value is None:
#             return value
#         return parse_richtext(value)
# 
#     def do_validation(self, richtext):
#         return richtext
# 
#     def to_python(self, value):
#         if isinstance(value, RichText):
#             return value
# 
#         if value is None:
#             return value
# 
#         return self.do_validation(parse_richtext(value))
#     
# 
#     def get_prep_value(self, value):
#         if value: return str(value)
#         return value
# 
#     def formfield(self, **kwargs):
#         defaults = {}
#         defaults["form_class"] = RichTextFormField
#         defaults.update(kwargs)
#         return super(RichTextField, self).formfield(**defaults)


import os, datetime
from media_explorer.fields import MediaField, RichTextField
from django.db import models
from __future__ import unicode_literals


#from django.db import connection



class Example(models.Model):
    """
    Example model that uses MediaField and RichTextField
    """

    name = models.CharField(null=True,blank=True)
    lead_media = MediaField()
    image = MediaField(type="image")

# Path: media_explorer/fields.py
# class MediaField(models.TextField):
#     """The Django MediaField."""
# 
#     description = _("A Media Explorer custom model field")
# 
#     def __init__(self, id=None, type=None, \
#             credit=None, caption=None, *args, **kwargs):
#         self.id = id
#         self.type = type
#         self.caption = caption
#         self.credit = credit
# 
#         kwargs['null'] = True
#         kwargs['blank'] = True
# 
#         super(MediaField, self).__init__(*args, **kwargs)
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(MediaField, self).deconstruct()
#         del kwargs["null"]
#         del kwargs["blank"]
#         return name, path, args, kwargs
# 
#     def db_type(self, connection):
#         return "longtext"
# 
#     def from_db_value(self, value, expression, connection, context):
#         if value is None:
#             return value
#         return parse_media(value)
# 
#     def do_validation(self, media):
#         if self.type and media.type and self.type != media.type:
#             raise ValidationError("Invalid media type selected for this MediaField instance. It expected a '%s' but got a '%s' instead." % (self.type, media.type))
# 
#         #Override id/credit/caption
#         if self.id : media.id = self.id
#         if self.type: media.type = self.type
#         if self.caption : media.caption = self.caption
#         if self.credit: media.credit = self.credit
# 
#         #Validate that the image/video is in the system
#         if media.type in ["image","video"] and \
#                 not Element.objects.filter(id=media.id,type=media.type).exists():
#             raise ValidationError("Invalid %s selected. The %s was not found." % (media.type, media.type))
# 
#         #Validate that the gallery is in the system
#         if media.type == "gallery" and \
#                 not Gallery.objects.filter(id=media.id).exists():
#             raise ValidationError("Invalid %s selected. The %s was not found." % (media.type, media.type))
# 
#         return media
# 
#     def to_python(self, value):
#         if isinstance(value, Media):
#             return value
# 
#         if value is None:
#             return value
# 
#         return self.do_validation(parse_media(value))
#     
#     def get_prep_value(self, value):
#         value_dict = {}
# 
#         try:
#             value_dict["id"] = value.id
#             value_dict["type"] = value.type
#             value_dict["caption"] = value.caption
#             value_dict["credit"] = value.credit
#         except:
#             pass
# 
#         if type(value) is Element:
#             value_dict["id"] = value.id
#             value_dict["type"] = value.type
#             value_dict["caption"] = None
#             value_dict["credit"] = None
#             if value.description:
#                 value_dict["caption"] = value.description
#             if value.credit:
#                 value_dict["credit"] = value.credit
# 
#         if value_dict:
#             self.do_validation(parse_media(value_dict))
#             return str(json.dumps(value_dict))
# 
#         if value: return str(value)
#         return value
# 
#     def formfield(self, **kwargs):
#         defaults = {}
#         defaults["form_class"] = MediaFormField
#         defaults.update(kwargs)
#         return super(MediaField, self).formfield(**defaults)
# 
# class RichTextField(models.TextField):
#     """The Django RichTextField."""
# 
#     description = _("A RichText Explorer custom model field")
# 
#     def __init__(self, *args, **kwargs):
#         kwargs['null'] = True
#         kwargs['blank'] = True
#         super(RichTextField, self).__init__(*args, **kwargs)
# 
#     def deconstruct(self):
#         name, path, args, kwargs = super(RichTextField, self).deconstruct()
#         del kwargs["null"]
#         del kwargs["blank"]
#         return name, path, args, kwargs
# 
#     def db_type(self, connection):
#         return "longtext"
# 
#     def from_db_value(self, value, expression, connection, context):
#         if value is None:
#             return value
#         return parse_richtext(value)
# 
#     def do_validation(self, richtext):
#         return richtext
# 
#     def to_python(self, value):
#         if isinstance(value, RichText):
#             return value
# 
#         if value is None:
#             return value
# 
#         return self.do_validation(parse_richtext(value))
#     
# 
#     def get_prep_value(self, value):
#         if value: return str(value)
#         return value
# 
#     def formfield(self, **kwargs):
#         defaults = {}
#         defaults["form_class"] = RichTextFormField
#         defaults.update(kwargs)
#         return super(RichTextField, self).formfield(**defaults)


# Path: media_explorer/tests/customfields/models.py
import os, datetime
from media_explorer.fields import MediaField, RichTextField
from django.db import models

from __future__ import unicode_literals


#from django.db import connection



class Example(models.Model):
    """
    Example model that uses MediaField and RichTextField
    """

    name = models.CharField(null=True,blank=True)
    lead_media = MediaField()
    image = MediaField(type="image")

    video = MediaField(type="video")