Skip to content

Commit

Permalink
Issue samluescher#25: Filter on the current site.
Browse files Browse the repository at this point in the history
Making sure that the image plugin use the site filter so that only
images that are added to the current site can be used.
  • Loading branch information
erlenddalen committed May 27, 2013
1 parent 5c55534 commit 8e8ec15
Show file tree
Hide file tree
Showing 41 changed files with 9,141 additions and 4 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
9 changes: 9 additions & 0 deletions media_tree/contrib/cms_plugins/cms_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _

class MediaTreeApphook(CMSApp):
name = _("Media Tree")
urls = ["media_tree.urls"]

apphook_pool.register(MediaTreeApphook)
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
9 changes: 9 additions & 0 deletions media_tree/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python
import os, sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "datakortet.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
7 changes: 7 additions & 0 deletions media_tree/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf import settings

class SessionPostMiddleware(object):

def process_request(self, request):
if not request.COOKIES.has_key(settings.SESSION_COOKIE_NAME) and request.POST.has_key(settings.SESSION_COOKIE_NAME):
request.COOKIES[settings.SESSION_COOKIE_NAME] = request.POST[settings.SESSION_COOKIE_NAME]
4 changes: 2 additions & 2 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
Loading

0 comments on commit 8e8ec15

Please sign in to comment.