Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added hook for adding extra permission checks for published widgets

  • Loading branch information...
commit 16d1db9b67141c1b47bb275be9f26a0dd7e4da88 1 parent 1fced3c
@osiloke authored
Showing with 70 additions and 37 deletions.
  1. +12 −7 widget/models.py
  2. +33 −18 widget/tests.py
  3. +25 −12 widget/utilities.py
View
19 widget/models.py
@@ -17,6 +17,7 @@
from django.db.models import Q
from mezzanine.utils.timezone import now
+
class WidgetOption(object):
"""
Definition of a widget option
@@ -79,11 +80,14 @@ def published(self, for_user=None):
For non-staff/permissionless users, return items with a published status and
whose publish and expiry dates fall before and after the
current date when specified.
+ :param for_user:
"""
from mezzanine.core.models import CONTENT_STATUS_PUBLISHED
+ from widget.utilities import widget_extra_permission
+ #This allows a callback for extra user validation, eg. check if a user passes a test (has a subscription)
if for_user is not None and bool(for_user.is_staff
- or for_user.has_perm("widget.change_widget")):
+ or bool(for_user.has_perm("widget.change_widget") and widget_extra_permission(for_user))):
return self.all()
return self.filter(
Q(publish_date__lte=now()) | Q(publish_date__isnull=True),
@@ -98,17 +102,18 @@ def published_for_page_or_pageless(self, page, slot=None, for_user=None):
For non-staff/permissionless users, return items with that are published and
belong to a certain page or page less
"""
+
if slot:
- orslot = Q(widgetslot=slot, page_less=True)
+ return self.published(for_user).filter(widgetslot=slot).filter(
+ Q(page=page, page_less=False) | Q(page_less=True))
else:
- orslot = Q(page_less=True)
-
- return self.published(for_user).filter(Q(page=page, page_less=False) | orslot)
+ return self.published(for_user).filter(
+ Q(page=page, page_less=False) | Q(page_less=True))
class Widget(Orderable, Ownable, SiteRelated):
- display_title = models.CharField(default=None, verbose_name="Title", max_length=255, \
- null=False)
+ display_title = models.CharField(
+ default=None, verbose_name="Title", max_length=255, null=False)
widget_class = PageWidgetClass(default="", verbose_name="Widget Type")
active = models.BooleanField(default=True)
widget_file_title = models.CharField(max_length=255, editable=False)
View
51 widget/tests.py
@@ -9,7 +9,7 @@
from django.template import RequestContext, Context
from django.test import TestCase
-from mezzanine.core.models import CONTENT_STATUS_DRAFT
+from mezzanine.core.models import CONTENT_STATUS_DRAFT, CONTENT_STATUS_PUBLISHED
from mezzanine.pages.models import RichTextPage
from widget import widget_pool
@@ -31,7 +31,7 @@ def setUp(self):
"""
self._username = "test"
self._password = "test"
- self._user = User.objects.create_superuser(username=self._username, \
+ self._user = User.objects.create(username=self._username, \
password=self._password, email="example@example.com")
#register our test widget
@@ -47,38 +47,47 @@ def setUp(self):
self.slot = "slot"
#create test widget
- self.test_widget = Widget()
- self.test_widget.widget_class = 'TestWidget'
- self.test_widget.page = self.page
- self.test_widget.user_id = 1
- self.test_widget.widgetslot = self.slot
- self.test_widget.save()
-
- def test_widget_creation(self):
- widget_class = get_widget(self.test_widget.widget_class)
+ self.test_widgets = []
+ self.widget_count = 3
+ self.create_widgets()
+
+ def create_widgets(self):
+ for i in range(0, self.widget_count):
+ test_widget = Widget()
+ test_widget.widget_class = "TestWidget"
+ test_widget.page = self.page
+ test_widget.user_id = 1
+ test_widget.widgetslot = self.slot
+ test_widget.status=CONTENT_STATUS_DRAFT
+ test_widget.save()
+ self.test_widgets.append(test_widget)
+ self.test_widgets[self.widget_count-1].status=CONTENT_STATUS_PUBLISHED
+ self.test_widgets[self.widget_count-1].save()
+
+ def test_widget_class(self):
+ widget_class = get_widget(self.test_widgets[0].widget_class)
self.assertEqual(widget_class.Meta.name, "Test")
def test_widget_options_form_creation(self):
-
form = WidgetOptionsForm("TestWidget")
field_label = [f for f in form.fields]
- expected = ['option_First',\
+ expected = ['option_First',
'option_Second', 'option_Third']
self.assertListEqual(field_label, expected)
def test_widget_options_form_saving(self):
"""
- This will create a new test widget with the assigned options
+ This will modify a test widget with the assigned options
"""
form = WidgetOptionsForm("TestWidget",
data={"option_First": "Option 1", "option_Second": "Option 2", "option_Third": "Option 3"}
)
self.assertEqual(form.is_valid(), True)
- form.save(self.test_widget)
+ form.save(self.test_widgets[0])
- self.assertListEqual([o.value for o in self.test_widget.options.all()], ['Option 1', 'Option 2', 'Option 3'])
+ self.assertListEqual([o.value for o in self.test_widgets[0].options.all()], ['Option 1', 'Option 2', 'Option 3'])
def test_widget_renderer_for_page(self):
@@ -87,7 +96,7 @@ def test_widget_renderer_for_page(self):
context["page"] = self.page
#mock request
- request = type('request', (object,), {'user':self._user})
+ request = type('request', (object,), {'user': self._user})
context["request"] = request
#render widget, there should be only one widget i.e test_widget
@@ -95,7 +104,13 @@ def test_widget_renderer_for_page(self):
widgets = render_widgets_for_slot(self.slot, Context(context))
- self.assertEqual(widgets[0]['widget'], self.test_widget)
+ #only one widget item is published
+ self.assertEqual(len(widgets), 1)
+
+ self.assertEqual(widgets[0]['widget'].status, CONTENT_STATUS_PUBLISHED)
+
+ self.assertEqual(widgets[0]['widget'].display_title, self.test_widgets[self.widget_count-1].display_title)
+
#check if rendered content is correct
self.assertEqual(widgets[0]['content'], "Test Widget Rendered")
View
37 widget/utilities.py
@@ -1,20 +1,21 @@
from copy import copy
from exceptions import Exception
-from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseNotFound
-from django.template import Template
-from widget.forms import ModelFormForWidget
-from widget.models import WidgetModel
-
+import os
+import errno
-__author__ = 'osilocks'
-
-from django.db.models.related import RelatedObject
-from django.http import HttpResponseRedirect, Http404
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
+from django.template import Template
+from mezzanine.utils.importing import import_dotted_path
+from django.utils.translation import ugettext_lazy as _
+from django.http import Http404
from django.utils.functional import Promise
from django.utils.encoding import force_unicode
from django.db.models import get_model
-from django.core.exceptions import ObjectDoesNotExist
-import os, errno
+from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
+
+from widget.forms import ModelFormForWidget
+from widget.models import WidgetModel
+
try:
from simplejson import JSONEncoder
@@ -280,4 +281,16 @@ def get_widget_model_queryset(widget, widget_class):
return model_queryset
except Exception:
raise
- return None
+ return None
+
+def widget_extra_permission(user):
+ from mezzanine.conf import settings
+ try:
+ perm = import_dotted_path(settings.WIDGET_PERMISSION)
+ return perm(user)
+ except AttributeError:
+ return False
+ except ImportError:
+ raise ImproperlyConfigured(_("Could not import the value of "
+ "settings.WIDGET_PERMISSION: %s"
+ % settings.WIDGET_PERMISSION))
Please sign in to comment.
Something went wrong with that request. Please try again.