Skip to content

Commit

Permalink
Issue samluescher#25: Filter on the current site
Browse files Browse the repository at this point in the history
  • Loading branch information
erlenddalen committed May 27, 2013
1 parent ee83f05 commit 7a40e8d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
6 changes: 5 additions & 1 deletion media_tree/admin/filenode_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# TODO: Ordering of tree by column (within parent) should be possible
# TODO: Refactor SWFUpload stuff as extension. This would require signals calls
# to be called in the FileNodeAdmin view methods.

from django.contrib.sites.models import Site

from media_tree.fields import FileNodeChoiceField
from media_tree.models import FileNode
Expand Down Expand Up @@ -132,6 +132,10 @@ def __init__(self, *args, **kwargs):
# http://stackoverflow.com/questions/1618728/disable-link-to-edit-object-in-djangos-admin-display-list-only
self.list_display_links = (None, )

def queryset(self, request):
qs = super(FileNodeAdmin, self).queryset(request)
return qs.filter(site=Site.objects.get_current())

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'parent' and issubclass(db_field.rel.to, FileNode):
# overriding formfield_for_dbfield, thus bypassign both Django's and mptt's
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.sites.models import Site
from media_tree.contrib.cms_plugins.media_tree_image.models import MediaTreeImage
from media_tree.contrib.cms_plugins.forms import MediaTreePluginFormBase
from media_tree.contrib.views.detail.image import ImageNodeDetailMixin
Expand All @@ -9,9 +10,15 @@
from django.utils.translation import ugettext_lazy as _

# TODO: Solve image_detail with get_absolute_url()?
from media_tree.models import FileNode


class MediaTreeImagePluginForm(MediaTreePluginFormBase):

def __init__(self, *args, **kwargs):
super(MediaTreeImagePluginForm, self).__init__(*args, **kwargs)
self.fields['node'].queryset = FileNode.objects.filter(site=Site.objects.get_current())

class Meta:
model = MediaTreeImage

Expand Down
8 changes: 8 additions & 0 deletions media_tree/contrib/cms_plugins/media_tree_image/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.sites.models import Site
from media_tree import media_types
from media_tree.contrib.cms_plugins import settings as plugins_settings
from media_tree.fields import FileNodeForeignKey, DimensionField
Expand All @@ -6,6 +7,11 @@
from django.utils.translation import ugettext_lazy as _


class MediaTreeImageSiteManager(models.Manager):
def get_query_set(self):
return super(MediaTreeImageSiteManager, self).get_query_set().filter(node__site=Site.objects.get_current())


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.'))
Expand All @@ -16,5 +22,7 @@ class MediaTreeImage(CMSPlugin):
height = DimensionField(_('max. height'), null=True, blank=True, help_text=_('You can leave this empty to use an automatically determined image height.'))
render_template = models.CharField(_("template"), max_length=100, choices=None, blank=True, null=True, help_text=_('Template used to render the image.'))

objects = MediaTreeImageSiteManager()

def __unicode__(self):
return self.node.__unicode__()
2 changes: 2 additions & 0 deletions media_tree/contrib/views/detail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.sites.models import Site
from media_tree.models import FileNode
from media_tree.contrib.views.mixin_base import PluginMixin
from django.views.generic.detail import DetailView
Expand Down Expand Up @@ -75,6 +76,7 @@ def get_object(self, queryset=None):

def get_queryset(self, *args, **kwargs):
queryset = super(FileNodeDetailView, self).get_queryset(*args, **kwargs)
queryset = queryset.filter(node__site=Site.objects.get_current())
kwargs = {}
if self.filter_node_types:
kwargs['node_type__in'] = self.filter_node_types
Expand Down
2 changes: 1 addition & 1 deletion media_tree/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, allowed_node_types=None, allowed_media_types=None, allowed_ex
self.allowed_node_types = allowed_node_types
self.allowed_media_types = allowed_media_types
self.allowed_extensions = allowed_extensions
kwargs['level_indicator'] = level_indicator;
kwargs['level_indicator'] = level_indicator
if not kwargs.has_key('widget'):
kwargs['widget'] = self.widget

Expand Down
7 changes: 4 additions & 3 deletions media_tree/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def filter(self, *args, **kwargs):
"""
if 'path' in kwargs:
kwargs = self.get_filter_args_with_path(False, **kwargs)
return super(FileNodeManager, self).filter(*args, **kwargs)
return super(FileNodeManager, self).filter(site=Site.objects.get_current()).filter(*args, **kwargs)

def exclude(self, *args, **kwargs):
"""
Expand All @@ -98,7 +98,7 @@ def exclude(self, *args, **kwargs):
"""
if 'path' in kwargs:
kwargs = self.get_filter_args_with_path(False, **kwargs)
return super(FileNodeManager, self).exclude(*args, **kwargs)
return super(FileNodeManager, self).filter(site=Site.objects.get_current()).exclude(*args, **kwargs)

def get(self, *args, **kwargs):
"""
Expand Down Expand Up @@ -316,7 +316,8 @@ def get_node_path(self):
return nodes

def get_folder_tree(self):
return self._tree_manager.all().filter(node_type=FileNode.FOLDER)
return self._tree_manager.all().filter(node_type=FileNode.FOLDER,
site=Site.objects.get_current())

def get_default_file(self, media_types=None):
if self.node_type == FileNode.FOLDER:
Expand Down

0 comments on commit 7a40e8d

Please sign in to comment.