# Path: media_explorer/widgets.py
# class MediaWidget(forms.Widget):
#     template_name = 'admin/media_field.html'
# 
#     class Media:
#         if getattr(settings,"DME_INCLUDE_JQUERY",True):
#             js1 = (
#                 'js/vendor/jquery/jquery-1.11.2.min.js',
#             )
#         else:
#             js1 = ()
# 
#         js2 = (
#             'js/media_explorer/media_explorer.js',
#             'js/media_field_admin.js',
#         )
# 
#         js = js1 + js2
# 
#         css = {
#             'all': (
#                 'css/hide_media_fields.css',
#             )
#         }
# 
#     def render(self, name, value, attrs=None):
#         context = {"name":name,"value":value}
#         return mark_safe(render_to_string(self.template_name, context))
# 
# class RichTextWidget(CKEditorWidget):
# 
#     class Media:
#         if getattr(settings,"DME_INCLUDE_JQUERY",True):
#             js1 = (
#                 'js/vendor/jquery/jquery-1.11.2.min.js',
#             )
#         else:
#             js1 = ()
# 
#         js2 = (
#             'js/vendor/jQuery-Impromptu-6.1.0/jquery-impromptu.min.js',
#             'js/media_explorer/media_explorer.js',
#             'ckeditor/ckeditor/plugins/media_explorer/callback.js',
#         )
# 
#         js = js1 + js2
# 
#         css = {
#             'all': (
#                 'js/vendor/jQuery-Impromptu-6.1.0/jquery-impromptu.min.css',
#             )
#         }


import json
from django.core.exceptions import ValidationError
from django import forms
from media_explorer.widgets import MediaWidget, RichTextWidget

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):

# Path: media_explorer/widgets.py
# class MediaWidget(forms.Widget):
#     template_name = 'admin/media_field.html'
# 
#     class Media:
#         if getattr(settings,"DME_INCLUDE_JQUERY",True):
#             js1 = (
#                 'js/vendor/jquery/jquery-1.11.2.min.js',
#             )
#         else:
#             js1 = ()
# 
#         js2 = (
#             'js/media_explorer/media_explorer.js',
#             'js/media_field_admin.js',
#         )
# 
#         js = js1 + js2
# 
#         css = {
#             'all': (
#                 'css/hide_media_fields.css',
#             )
#         }
# 
#     def render(self, name, value, attrs=None):
#         context = {"name":name,"value":value}
#         return mark_safe(render_to_string(self.template_name, context))
# 
# class RichTextWidget(CKEditorWidget):
# 
#     class Media:
#         if getattr(settings,"DME_INCLUDE_JQUERY",True):
#             js1 = (
#                 'js/vendor/jquery/jquery-1.11.2.min.js',
#             )
#         else:
#             js1 = ()
# 
#         js2 = (
#             'js/vendor/jQuery-Impromptu-6.1.0/jquery-impromptu.min.js',
#             'js/media_explorer/media_explorer.js',
#             'ckeditor/ckeditor/plugins/media_explorer/callback.js',
#         )
# 
#         js = js1 + js2
# 
#         css = {
#             'all': (
#                 'js/vendor/jQuery-Impromptu-6.1.0/jquery-impromptu.min.css',
#             )
#         }


# Path: media_explorer/forms.py
import json
from django.core.exceptions import ValidationError
from django import forms
from media_explorer.widgets import MediaWidget, RichTextWidget


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)