# Path: media_tree/media_types.py
# FOLDER = 100
# FILE = 200
# ARCHIVE = 210
# AUDIO = 220
# DOCUMENT = 230
# IMAGE = 240
# SUPPORTED_IMAGE = 241
# VECTOR_IMAGE = 242
# TEXT = 250
# VIDEO = 260
# 
# Path: media_tree/contrib/cms_plugins/settings.py
# MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_CHOICES = (
#     (PluginLink.LINK_PAGE, _('Link to page')),
#     (PluginLink.LINK_URL, _('Link to web address')),
#     (PluginLink.LINK_IMAGE_DETAIL, _('Link to image detail')),
#     (PluginLink.LINK_URL_REVERSE, _('Link to URL pattern')),
# )
# MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_DEFAULT = PluginLink.LINK_IMAGE_DETAIL  \
#     if getattr(settings, 'CMS_APPLICATIONS_URLS', {}).has_key('media_tree.urls') else None
# 
# Path: media_tree/fields.py
# class FileNodeForeignKey(models.ForeignKey):
#     """
#     A model field for selecting a ``FileNode`` object.
# 
#     Its constructor takes the following arguments that are relevant when selecting ``FileNode`` objects:
# 
#     :param allowed_node_types: A list of node types that are allowed and will validate, e.g. ``(FileNode.FILE,)`` if the user should only be able to select files, but not folders
#     :param allowed_media_types: A list of media types that are allowed and will validate, e.g. ``(media_types.DOCUMENT,)``
#     :param allowed_extensions: A list of file extensions that are allowed and will validate, e.g. ``("jpg", "jpeg")``
# 
#     Since this class is a subclass of ``models.ForeignKey``, you can also pass it that class'
#     parameters, such as ``limit_choices_to`` if you would like to restrict the objects that will
#     be available for selection.
#     """
# 
#     def __init__(self, allowed_node_types=None, allowed_media_types=None, allowed_extensions=None, level_indicator=LEVEL_INDICATOR, *args, **kwargs):
#         self.allowed_node_types = allowed_node_types
#         self.allowed_media_types = allowed_media_types
#         self.allowed_extensions = allowed_extensions
#         self.level_indicator = level_indicator
#         kwargs['to'] = 'media_tree.FileNode'
#         super(FileNodeForeignKey, self).__init__(*args, **kwargs)
# 
#     def formfield(self, **kwargs):
#         defaults = {
#             'form_class': FileNodeChoiceField,
#             'rel': self.rel,
#             'allowed_node_types': self.allowed_node_types,
#             'allowed_media_types': self.allowed_media_types,
#             'allowed_extensions': self.allowed_extensions,
#             'empty_label': '',
#         }
# 
#         defaults.update(kwargs)
#         field = super(FileNodeForeignKey, self).formfield(**defaults)
# 
#         # If the widget is a ForeignKeyRawIdWidget, overwrite it with
#         # FileNodeForeignKeyRawIdWidget. This is done here since the
#         # widget's contructor parameters are coming from the ModelAdmin,
#         # which we have no direct access to here.
#         if isinstance(field.widget, ForeignKeyRawIdWidget) and not  \
#             isinstance(field.widget, FileNodeForeignKeyRawIdWidget):
#             field.widget = FileNodeForeignKeyRawIdWidget(field.widget.rel,
#                 field.widget.admin_site, using=field.widget.db)
#         return field
# 
#     def south_field_triple(self):
#         "Returns a suitable description of this field for South."
#         from south.modelsinspector import introspector
#         field_class = "django.db.models.fields.related.ForeignKey"
#         args, kwargs = introspector(self)
#         return (field_class, args, kwargs)
# 
# class DimensionField(models.CharField):
#     """
#     CharField for specifying image dimensions, i.e. width or height. Currently,
#     this needs to be an integer > 0, but since it is a CharField, it might also
#     contain units such as "px" or "%" in the future.
#     """
#     def __init__(self, verbose_name=None, name=None, **kwargs):
#         if not 'max_length' in kwargs:
#             kwargs['max_length'] = 8
#         super(DimensionField, self).__init__(verbose_name, name, **kwargs)
# 
#     def formfield(self, **kwargs):
#         defaults = {'regex': '^[1-9][0-9]*$',
#                     'form_class': forms.RegexField}
#         defaults.update(kwargs)
#         return models.Field.formfield(self, **defaults)
# 
#     def south_field_triple(self):
#         "Returns a suitable description of this field for South."
#         from south.modelsinspector import introspector
#         field_class = "django.db.models.fields.CharField"
#         args, kwargs = introspector(self)
#         return (field_class, args, kwargs)


from media_tree import media_types
from media_tree.contrib.cms_plugins import settings as plugins_settings
from media_tree.fields import FileNodeForeignKey, DimensionField
from cms.models import CMSPlugin, Page
from django.db import models
from django.utils.translation import ugettext_lazy as _


class MediaTreeImage(CMSPlugin):
    node = FileNodeForeignKey(allowed_media_types=(media_types.SUPPORTED_IMAGE,), verbose_name=_('file'))
    link_type = models.CharField(_('link type'), max_length=1, blank=True, null=True, default=plugins_settings.MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_DEFAULT, choices=plugins_settings.MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_CHOICES, help_text=_('Makes the image a clickable link.'))
    link_page = models.ForeignKey(Page, verbose_name=_("page"), null=True, blank=True, help_text=_('For link to page. Select any page from the list.'))
    link_url = models.CharField(_("web address"), max_length=255, blank=True, null=True, help_text=_('For link to web address. Example: Enter "http://www.domain.com" to create an absolute link to an external site, or enter a relative URL like "/about/contact".'))
    link_target = models.CharField(_("link target"), max_length=64, blank=True, null=True, choices=(('', _('same window')), ('_blank', _('new window'))))
    width = DimensionField(_('max. width'), null=True, blank=True, help_text=_('You can leave this empty to use an automatically determined image width.'))

# Path: media_tree/media_types.py
# FOLDER = 100
# FILE = 200
# ARCHIVE = 210
# AUDIO = 220
# DOCUMENT = 230
# IMAGE = 240
# SUPPORTED_IMAGE = 241
# VECTOR_IMAGE = 242
# TEXT = 250
# VIDEO = 260
# 
# Path: media_tree/contrib/cms_plugins/settings.py
# MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_CHOICES = (
#     (PluginLink.LINK_PAGE, _('Link to page')),
#     (PluginLink.LINK_URL, _('Link to web address')),
#     (PluginLink.LINK_IMAGE_DETAIL, _('Link to image detail')),
#     (PluginLink.LINK_URL_REVERSE, _('Link to URL pattern')),
# )
# MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_DEFAULT = PluginLink.LINK_IMAGE_DETAIL  \
#     if getattr(settings, 'CMS_APPLICATIONS_URLS', {}).has_key('media_tree.urls') else None
# 
# Path: media_tree/fields.py
# class FileNodeForeignKey(models.ForeignKey):
#     """
#     A model field for selecting a ``FileNode`` object.
# 
#     Its constructor takes the following arguments that are relevant when selecting ``FileNode`` objects:
# 
#     :param allowed_node_types: A list of node types that are allowed and will validate, e.g. ``(FileNode.FILE,)`` if the user should only be able to select files, but not folders
#     :param allowed_media_types: A list of media types that are allowed and will validate, e.g. ``(media_types.DOCUMENT,)``
#     :param allowed_extensions: A list of file extensions that are allowed and will validate, e.g. ``("jpg", "jpeg")``
# 
#     Since this class is a subclass of ``models.ForeignKey``, you can also pass it that class'
#     parameters, such as ``limit_choices_to`` if you would like to restrict the objects that will
#     be available for selection.
#     """
# 
#     def __init__(self, allowed_node_types=None, allowed_media_types=None, allowed_extensions=None, level_indicator=LEVEL_INDICATOR, *args, **kwargs):
#         self.allowed_node_types = allowed_node_types
#         self.allowed_media_types = allowed_media_types
#         self.allowed_extensions = allowed_extensions
#         self.level_indicator = level_indicator
#         kwargs['to'] = 'media_tree.FileNode'
#         super(FileNodeForeignKey, self).__init__(*args, **kwargs)
# 
#     def formfield(self, **kwargs):
#         defaults = {
#             'form_class': FileNodeChoiceField,
#             'rel': self.rel,
#             'allowed_node_types': self.allowed_node_types,
#             'allowed_media_types': self.allowed_media_types,
#             'allowed_extensions': self.allowed_extensions,
#             'empty_label': '',
#         }
# 
#         defaults.update(kwargs)
#         field = super(FileNodeForeignKey, self).formfield(**defaults)
# 
#         # If the widget is a ForeignKeyRawIdWidget, overwrite it with
#         # FileNodeForeignKeyRawIdWidget. This is done here since the
#         # widget's contructor parameters are coming from the ModelAdmin,
#         # which we have no direct access to here.
#         if isinstance(field.widget, ForeignKeyRawIdWidget) and not  \
#             isinstance(field.widget, FileNodeForeignKeyRawIdWidget):
#             field.widget = FileNodeForeignKeyRawIdWidget(field.widget.rel,
#                 field.widget.admin_site, using=field.widget.db)
#         return field
# 
#     def south_field_triple(self):
#         "Returns a suitable description of this field for South."
#         from south.modelsinspector import introspector
#         field_class = "django.db.models.fields.related.ForeignKey"
#         args, kwargs = introspector(self)
#         return (field_class, args, kwargs)
# 
# class DimensionField(models.CharField):
#     """
#     CharField for specifying image dimensions, i.e. width or height. Currently,
#     this needs to be an integer > 0, but since it is a CharField, it might also
#     contain units such as "px" or "%" in the future.
#     """
#     def __init__(self, verbose_name=None, name=None, **kwargs):
#         if not 'max_length' in kwargs:
#             kwargs['max_length'] = 8
#         super(DimensionField, self).__init__(verbose_name, name, **kwargs)
# 
#     def formfield(self, **kwargs):
#         defaults = {'regex': '^[1-9][0-9]*$',
#                     'form_class': forms.RegexField}
#         defaults.update(kwargs)
#         return models.Field.formfield(self, **defaults)
# 
#     def south_field_triple(self):
#         "Returns a suitable description of this field for South."
#         from south.modelsinspector import introspector
#         field_class = "django.db.models.fields.CharField"
#         args, kwargs = introspector(self)
#         return (field_class, args, kwargs)


# Path: media_tree/contrib/cms_plugins/media_tree_image/models.py
from media_tree import media_types
from media_tree.contrib.cms_plugins import settings as plugins_settings
from media_tree.fields import FileNodeForeignKey, DimensionField
from cms.models import CMSPlugin, Page
from django.db import models
from django.utils.translation import ugettext_lazy as _



class MediaTreeImage(CMSPlugin):
    node = FileNodeForeignKey(allowed_media_types=(media_types.SUPPORTED_IMAGE,), verbose_name=_('file'))
    link_type = models.CharField(_('link type'), max_length=1, blank=True, null=True, default=plugins_settings.MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_DEFAULT, choices=plugins_settings.MEDIA_TREE_CMS_PLUGIN_LINK_TYPE_CHOICES, help_text=_('Makes the image a clickable link.'))
    link_page = models.ForeignKey(Page, verbose_name=_("page"), null=True, blank=True, help_text=_('For link to page. Select any page from the list.'))
    link_url = models.CharField(_("web address"), max_length=255, blank=True, null=True, help_text=_('For link to web address. Example: Enter "http://www.domain.com" to create an absolute link to an external site, or enter a relative URL like "/about/contact".'))
    link_target = models.CharField(_("link target"), max_length=64, blank=True, null=True, choices=(('', _('same window')), ('_blank', _('new window'))))
    width = DimensionField(_('max. width'), null=True, blank=True, help_text=_('You can leave this empty to use an automatically determined image width.'))

    height = DimensionField(_('max. height'), null=True, blank=True, help_text=_('You can leave this empty to use an automatically determined image height.'))