# Path: smartsnippets/cms_plugins.py
# def variables_media(media, variables=None):
#     variables = variables or []
#     media.add_js([static('admin/js/SmartSnippetLib.js')])
#     media.add_js(
#         itertools.chain(*[var.js for var in variables])
#     )
#     media.add_css(
#         {'all': itertools.chain(*[var.css for var in variables])})
# 
# Path: smartsnippets/models.py
# class Variable(models.Model):
#     snippet_variable = models.ForeignKey(SmartSnippetVariable,
#                                          related_name='variables')
#     value = models.TextField()
#     snippet = models.ForeignKey(SmartSnippetPointer, related_name='variables')
# 
#     class Meta:
#         unique_together = (('snippet_variable', 'snippet'))
# 
#     @property
#     def formatted_value(self):
#         from widgets_pool import widget_pool
#         widget_instance = widget_pool.get_widget(self.widget)(self)
#         return widget_instance.formatted_value
# 
#     @property
#     def name(self):
#         return self.snippet_variable.name
# 
#     @property
#     def widget(self):
#         return self.snippet_variable.widget
# 
#     @property
#     def templates(self):
#         return self.snippet_variable.resources_dict.get('html', set())
# 
#     @property
#     def js(self):
#         return self.snippet_variable.resources_dict.get('js', set())
# 
#     @property
#     def css(self):
#         return self.snippet_variable.resources_dict.get('css', set())
# 
# Path: smartsnippets_inherit/models.py
# class InheritPageContent(CMSPlugin):
#     # from which page
#     from_page = models.ForeignKey(Page)
#     # from which section
#     from_placeholder = models.CharField(max_length=255, db_index=True)
# 
#     class Meta:
#         db_table = 'cmsplugin_inheritpagecontent'
# 
#     def get_placeholder(self):
#         try:
#             return self.from_page.placeholders.get(slot=self.from_placeholder)
#         except Placeholder.DoesNotExist:
#             return None


from django import forms
from django.forms.models import ModelForm
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from cms.models import Page
from smartsnippets.cms_plugins import variables_media
from smartsnippets.models import Variable
from smartsnippets_inherit.models import InheritPageContent


class InheritPageForm(ModelForm):
    from_page = forms.ModelChoiceField(
        label=_("page"), queryset=Page.objects.drafts())
    site = None
    current_page = None

    class Meta:
        model = InheritPageContent
        fields = ('from_page', 'from_placeholder')

    @property
    def media(self):
        _media = super(InheritPageForm, self).media
        variables_media(_media, self._cache_variables)
        return _media

    def __init__(self, *args, **kwargs):
        self._cache_variables = []
        super(InheritPageForm, self).__init__(*args, **kwargs)
        if self.current_page is not None:
            self.site = self.current_page.site
        if self.site is not None and 'from_page' in self.fields:
            site_pages = Page.objects.drafts().on_site(self.site)
            self.fields['from_page'].queryset = site_pages
        if self.instance.pk:
            phd = self.instance.get_placeholder()

# Path: smartsnippets/cms_plugins.py
# def variables_media(media, variables=None):
#     variables = variables or []
#     media.add_js([static('admin/js/SmartSnippetLib.js')])
#     media.add_js(
#         itertools.chain(*[var.js for var in variables])
#     )
#     media.add_css(
#         {'all': itertools.chain(*[var.css for var in variables])})
# 
# Path: smartsnippets/models.py
# class Variable(models.Model):
#     snippet_variable = models.ForeignKey(SmartSnippetVariable,
#                                          related_name='variables')
#     value = models.TextField()
#     snippet = models.ForeignKey(SmartSnippetPointer, related_name='variables')
# 
#     class Meta:
#         unique_together = (('snippet_variable', 'snippet'))
# 
#     @property
#     def formatted_value(self):
#         from widgets_pool import widget_pool
#         widget_instance = widget_pool.get_widget(self.widget)(self)
#         return widget_instance.formatted_value
# 
#     @property
#     def name(self):
#         return self.snippet_variable.name
# 
#     @property
#     def widget(self):
#         return self.snippet_variable.widget
# 
#     @property
#     def templates(self):
#         return self.snippet_variable.resources_dict.get('html', set())
# 
#     @property
#     def js(self):
#         return self.snippet_variable.resources_dict.get('js', set())
# 
#     @property
#     def css(self):
#         return self.snippet_variable.resources_dict.get('css', set())
# 
# Path: smartsnippets_inherit/models.py
# class InheritPageContent(CMSPlugin):
#     # from which page
#     from_page = models.ForeignKey(Page)
#     # from which section
#     from_placeholder = models.CharField(max_length=255, db_index=True)
# 
#     class Meta:
#         db_table = 'cmsplugin_inheritpagecontent'
# 
#     def get_placeholder(self):
#         try:
#             return self.from_page.placeholders.get(slot=self.from_placeholder)
#         except Placeholder.DoesNotExist:
#             return None


# Path: smartsnippets_inherit/forms.py
from django import forms
from django.forms.models import ModelForm
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from cms.models import Page
from smartsnippets.cms_plugins import variables_media
from smartsnippets.models import Variable
from smartsnippets_inherit.models import InheritPageContent



class InheritPageForm(ModelForm):
    from_page = forms.ModelChoiceField(
        label=_("page"), queryset=Page.objects.drafts())
    site = None
    current_page = None

    class Meta:
        model = InheritPageContent
        fields = ('from_page', 'from_placeholder')

    @property
    def media(self):
        _media = super(InheritPageForm, self).media
        variables_media(_media, self._cache_variables)
        return _media

    def __init__(self, *args, **kwargs):
        self._cache_variables = []
        super(InheritPageForm, self).__init__(*args, **kwargs)
        if self.current_page is not None:
            self.site = self.current_page.site
        if self.site is not None and 'from_page' in self.fields:
            site_pages = Page.objects.drafts().on_site(self.site)
            self.fields['from_page'].queryset = site_pages
        if self.instance.pk:
            phd = self.instance.get_placeholder()

            self._cache_variables = Variable.objects.filter(