Skip to content
Browse files

master_page seems to be a different master page, why, made asserts pr…

…ints instead
  • Loading branch information...
2 parents ad5cdeb + b03897e commit f8d9cb3c74d8bda09424b9ae307969d5857e0fcb @fivethreeo fivethreeo committed Dec 30, 2010
Showing with 136 additions and 75 deletions.
  1. +55 −47 cms/middleware/multilingual.py
  2. +2 −2 cms/models/managers.py
  3. +1 −0 cms/tests/__init__.py
  4. +40 −0 cms/tests/multilingual.py
  5. +15 −0 cms/tests/page.py
  6. +23 −26 cms/tests/permmod.py
View
102 cms/middleware/multilingual.py
@@ -1,11 +1,12 @@
-import re
-import urllib
-from django.middleware.locale import LocaleMiddleware
-from django.utils.cache import patch_vary_headers
-from django.utils import translation
-from django.conf import settings
from cms.utils.i18n import get_default_language
+from django.conf import settings
from django.core.urlresolvers import reverse
+from django.middleware.locale import LocaleMiddleware
+from django.utils import translation
+from django.utils.cache import patch_vary_headers
+import re
+import time
+import urllib
SUPPORTED = dict(settings.CMS_LANGUAGES)
@@ -18,11 +19,52 @@ def has_lang_prefix(path):
else:
return False
+def patch_response(content, pages_root, language):
+ # Customarily user pages are served from http://the.server.com/~username/
+ # When a user uses django-cms for his pages, the '~' of the url appears quoted in href links.
+ # We have to quote pages_root for the regular expression to match.
+ #
+ # The used regex is quite complex. The exact pattern depends on the used settings.
+ # The regex extracts the path of the url without the leading page root, but only matches urls
+ # that don't already contain a language string or aren't considered multilingual.
+ #
+ # Here is an annotated example pattern (_r_ is a shorthand for the value of pages_root):
+ # pattern: <a([^>]+)href=("|\')(?=_r_)(?!(/fr/|/de/|/en/|/pt-br/|/media/|/media/admin/))(_r_(.*?))("|\')(.*?)>
+ # |-\1--| |-\2-| |---------------------\3---------------------| | |-\5--|||-\6-||-\7-|
+ # |---\4---|
+ # input (_r_=/): <a href="/admin/password_change/" class="foo">
+ # matched groups: (u' ', None, u'/admin/password_change/', u'admin/password_change/', u' class="foo"')
+ #
+ # Notice that (?=...) and (?!=...) do not consume input or produce a group in the match object.
+ # If the regex matches, the extracted path we want is stored in the fourth group (\4).
+ quoted_root = urllib.quote(pages_root)
+ HREF_URL_FIX_RE = re.compile(ur'<a([^>]+)href=("|\')(?=%s)(?!(%s|%s|%s))(%s(.*?))("|\')(.*?)>' % (
+ quoted_root,
+ "|".join(map(lambda l: quoted_root + l[0] + "/" , settings.CMS_LANGUAGES)),
+ settings.MEDIA_URL,
+ settings.ADMIN_MEDIA_PREFIX,
+ quoted_root
+ ))
+
+ # Unlike in href links, the '~' (see above) the '~' in form actions appears unquoted.
+ #
+ # For understanding this regex, please read the documentation for HREF_URL_FIX_RE above.
+ FORM_URL_FIX_RE = re.compile(ur'<form([^>]+)action=("|\')(?=%s)(?!(%s|%s|%s))(%s(.*?))("|\')(.*?)>' % (
+ pages_root,
+ "|".join(map(lambda l: pages_root + l[0] + "/" , settings.CMS_LANGUAGES)),
+ settings.MEDIA_URL,
+ settings.ADMIN_MEDIA_PREFIX,
+ pages_root
+ ))
+
+ content = HREF_URL_FIX_RE.sub(ur'<a\1href=\2/%s%s\5\6\7>' % (language, pages_root), content)
+ content = FORM_URL_FIX_RE.sub(ur'<form\1action=\2%s%s/\5\6\7>' % (pages_root, language), content).encode("utf8")
+ return content
+
class MultilingualURLMiddleware:
def get_language_from_request (self,request):
changed = False
prefix = has_lang_prefix(request.path_info)
- pages_root = urllib.unquote(reverse("pages-root"))
if prefix:
request.path = "/" + "/".join(request.path.split("/")[2:])
request.path_info = "/" + "/".join(request.path_info.split("/")[2:])
@@ -61,56 +103,22 @@ def process_response(self, request, response):
# note: pages_root is assumed to end in '/'.
# testing this and throwing an exception otherwise, would probably be a good idea
- pages_root = urllib.unquote(reverse("pages-root"))
-
+
if not path.startswith(settings.MEDIA_URL) and \
not path.startswith(settings.ADMIN_MEDIA_PREFIX) and \
response.status_code == 200 and \
response._headers['content-type'][1].split(';')[0] == "text/html":
+ pages_root = urllib.unquote(reverse("pages-root"))
try:
decoded_response = response.content.decode('utf-8')
except UnicodeDecodeError:
decoded_response = response.content
- # Customarily user pages are served from http://the.server.com/~username/
- # When a user uses django-cms for his pages, the '~' of the url appears quoted in href links.
- # We have to quote pages_root for the regular expression to match.
- #
- # The used regex is quite complex. The exact pattern depends on the used settings.
- # The regex extracts the path of the url without the leading page root, but only matches urls
- # that don't already contain a language string or aren't considered multilingual.
- #
- # Here is an annotated example pattern (_r_ is a shorthand for the value of pages_root):
- # pattern: <a([^>]+)href="(?=_r_)(?!(/fr/|/de/|/en/|/pt-br/|/media/|/media/admin/))(_r_([^"]*))"([^>]*)>
- # |-\1--| |---------------------\2---------------------| | |-\4--|| |-\5--|
- # |----\3----|
- # input (_r_=/): <a href="/admin/password_change/" class="foo">
- # matched groups: (u' ', None, u'/admin/password_change/', u'admin/password_change/', u' class="foo"')
- #
- # Notice that (?=...) and (?!=...) do not consume input or produce a group in the match object.
- # If the regex matches, the extracted path we want is stored in the fourth group (\4).
- HREF_URL_FIX_RE = re.compile(ur'<a([^>]+)href="(?=%s)(?!(%s|%s|%s))(%s([^"]*))"([^>]*)>' % (
- urllib.quote(pages_root),
- "|".join(map(lambda l: urllib.quote(pages_root) + l[0] + "/" , settings.CMS_LANGUAGES)),
- settings.MEDIA_URL,
- settings.ADMIN_MEDIA_PREFIX,
- urllib.quote(pages_root)
- ))
-
- # Unlike in href links, the '~' (see above) the '~' in form actions appears unquoted.
- #
- # For understanding this regex, please read the documentation for HREF_URL_FIX_RE above.
- FORM_URL_FIX_RE = re.compile(ur'<form([^>]+)action="(?=%s)(?!(%s|%s|%s))(%s([^"]*))"([^>]*)>' % (
+ response.content = patch_response(
+ decoded_response,
pages_root,
- "|".join(map(lambda l: pages_root + l[0] + "/" , settings.CMS_LANGUAGES)),
- settings.MEDIA_URL,
- settings.ADMIN_MEDIA_PREFIX,
- pages_root
- ))
-
- # Documentation comments for HREF_URL_FIX_RE above explain each match group (\1, \4, \5) represents.
- decoded_response = HREF_URL_FIX_RE.sub(ur'<a\1href="/%s%s\4"\5>' % (request.LANGUAGE_CODE, pages_root), decoded_response)
- response.content = FORM_URL_FIX_RE.sub(ur'<form\1action="%s%s/\4"\5>' % (pages_root, request.LANGUAGE_CODE), decoded_response).encode("utf8")
+ request.LANGUAGE_CODE
+ )
if (response.status_code == 301 or response.status_code == 302 ):
location = response['Location']
View
4 cms/models/managers.py
@@ -459,7 +459,7 @@ def __get_id_list(self, user, site, attr):
page_id_allow_list = []
desc_list = []
for permission in qs:
- desc_list.append((permission.page.pk, permission.grant_on, permission.page.get_descendants().values_list('id', flat=True)))
+ desc_list.append((permission.page.get_descendants(include_self=True), permission.page.pk, 'grant %i' % permission.grant_on))
is_allowed = getattr(permission, attr)
if is_allowed:
@@ -472,7 +472,7 @@ def __get_id_list(self, user, site, attr):
elif permission.grant_on & MASK_DESCENDANTS:
page_id_allow_list.extend(permission.page.get_descendants().values_list('id', flat=True))
- assert False, str(desc_list) # no pages with permissions has descendants???
+ print 'descendants for pages in permissions for (' + str(user) + ') : ' + str(desc_list) # no pages with permissions has descendants???
# store value in cache
#set_permission_cache(user, attr, page_id_allow_list)
View
1 cms/tests/__init__.py
@@ -14,3 +14,4 @@
from cms.tests.site import SiteTestCase
from cms.tests.urlutils import UrlutilsTestCase
from cms.tests.publisher import PublisherTestCase
+from cms.tests.multilingual import MultilingualTestCase
View
40 cms/tests/multilingual.py
@@ -0,0 +1,40 @@
+from cms.middleware.multilingual import patch_response
+from django.core.urlresolvers import reverse
+from django.test.testcases import TestCase
+import urllib
+
+
+class MultilingualTestCase(TestCase):
+ def test_01_multilingual_url_middleware(self):
+ """
+ Test the Multilingual URL Middleware correctly handles the various ways
+ one can write URLs in HTML.
+ """
+ # stuff we need
+ pages_root = urllib.unquote(reverse('pages-root'))
+ language = "en"
+ # single quoted a tag
+ content = "<a href='/url/'>"
+ output = patch_response(content, pages_root, language)
+ expected = "<a href='/en/url/'>"
+ self.assertEqual(output, expected)
+ # double quoted a tag
+ content = '<a href="/url/">'
+ output = patch_response(content, pages_root, language)
+ expected = '<a href="/en/url/">'
+ self.assertEqual(output, expected)
+ # single quoted a tag with a class and rel attribute
+ content = "<a rel='rel' href='/url/' class='cms'>"
+ output = patch_response(content, pages_root, language)
+ expected = "<a rel='rel' href='/en/url/' class='cms'>"
+ self.assertEqual(output, expected)
+ # single quoted form tag
+ content = "<form action='/url/'>"
+ output = patch_response(content, pages_root, language)
+ expected = "<form action='/en/url/'>"
+ self.assertEqual(output, expected)
+ # double quoted form tag
+ content = '<form action="/url/">'
+ output = patch_response(content, pages_root, language)
+ expected = '<form action="/en/url/">'
+ self.assertEqual(output, expected)
View
15 cms/tests/page.py
@@ -291,3 +291,18 @@ def test_14_flat_urls(self):
response = self.client.get(grandchild.get_absolute_url())
self.assertEqual(response.status_code, 200)
self.assertFalse(child.get_absolute_url() in grandchild.get_absolute_url())
+
+ def test_15_templates(self):
+ """
+ Test the inheritance magic for templates
+ """
+ parent = self.new_create_page()
+ child = self.new_create_page(parent)
+ child.template = settings.CMS_TEMPLATE_INHERITANCE_MAGIC
+ child.save()
+ self.assertEqual(child.template, settings.CMS_TEMPLATE_INHERITANCE_MAGIC)
+ self.assertEqual(parent.get_template(), child.get_template())
+ parent.template = settings.CMS_TEMPLATE_INHERITANCE_MAGIC
+ parent.save()
+ self.assertEqual(parent.template, settings.CMS_TEMPLATE_INHERITANCE_MAGIC)
+ self.assertEqual(parent.get_template(), settings.CMS_TEMPLATES[0][0])
View
49 cms/tests/permmod.py
@@ -219,18 +219,7 @@ def request_moderation(self, page, level):
############################################################################
# page acessors
- @property
- def home_page(self):
- return Page.objects.drafts().get(title_set__slug="home")
-
- @property
- def slave_page(self):
- return Page.objects.drafts().get(title_set__slug="slave-home")
-
- @property
- def master_page(self):
- return Page.objects.drafts().get(title_set__slug="master")
-
+
############################################################################
# tests
@@ -242,31 +231,31 @@ def setUp(self):
self.user_super.save()
self.login_user(self.user_super)
- home = self.create_page(title="home", user=self.user_super)
- self.publish_page(home)
+ self.home_page = self.create_page(title="home", user=self.user_super)
+ self.publish_page(self.home_page )
# master page & master user
- master = self.create_page(title="master")
- self.publish_page(master)
+ self.master_page = self.create_page(title="master")
+ self.publish_page(self.master_page)
# create master user
self.user_master = self.create_page_user("master", grant_all=True)
# assign master user under home page
- self.assign_user_to_page(home, self.user_master, grant_on=ACCESS_DESCENDANTS,
+ self.assign_user_to_page(self.home_page, self.user_master, grant_on=ACCESS_DESCENDANTS,
grant_all=True)
# and to master page
- self.assign_user_to_page(master, self.user_master, grant_all=True)
+ self.assign_user_to_page(self.master_page, self.user_master, grant_all=True)
# slave page & slave user
- slave = self.create_page(title="slave-home", parent_page=master, user=self.user_super)
+ self.slave_page = self.create_page(title="slave-home", parent_page=self.master_page, user=self.user_super)
self.user_slave = self.create_page_user("slave",
can_add_page=True, can_change_page=True, can_delete_page=True)
- self.assign_user_to_page(slave, self.user_slave, grant_all=True)
+ self.assign_user_to_page(self.slave_page, self.user_slave, grant_all=True)
# create page_a - sample page from master
@@ -276,7 +265,7 @@ def setUp(self):
can_move_page=True, can_moderate=True)
# logg in as master, and request moderation for slave page and descendants
- self.request_moderation(slave, 7)
+ self.request_moderation(self.slave_page, 7)
self.client.logout()
@@ -312,7 +301,6 @@ def test_04_moderation_on_slave_home(self):
def test_05_slave_can_add_page_under_slave_home(self):
self.login_user(self.user_slave)
- slave_page = self.slave_page
# move to admin.py?
# url = URL_CMS_PAGE_ADD + "?target=%d&position=last-child" % slave_page.pk
@@ -322,7 +310,7 @@ def test_05_slave_can_add_page_under_slave_home(self):
# self.assertEqual(response.status_code, 200)
# add page
- page = self.create_page(slave_page, user=self.user_slave)
+ page = self.create_page(self.slave_page, user=self.user_slave)
# adds user_slave as page moderator for this page
# public model shouldn't be available yet, because of the moderation
# removed test cases since Title object does not inherit from Publisher anymore
@@ -348,16 +336,25 @@ def test_05_slave_can_add_page_under_slave_home(self):
def test_06_page_added_by_slave_can_be_published_approved_by_user_master(self):
self.login_user(self.user_master)
- slave_page = self.slave_page
-
+
# add page
- page = self.create_page(slave_page, user=self.user_slave)
+ page = self.create_page(self.slave_page, user=self.user_slave)
# same as test_05_slave_can_add_page_under_slave_home
self.assertEqual(page.get_moderator_queryset().count(), 1)
assert(page.moderator_state == Page.MODERATOR_NEED_APPROVEMENT)
# must not have public object yet
self.assertFalse(page.publisher_public)
+
+ print 'pk of created page: ' + str(page.pk)
+
+ print 'descendants of master page: ' + str([(spage, spage.pk) for spage in self.master_page.get_descendants()])
+
+ print 'ancestors of created page: ' + str([(spage, spage.pk) for spage in page.get_ancestors()])
+
+ print 'descendants of slave page: ' + str([(spage, spage.pk) for spage in self.slave_page.get_descendants()])
+
+ print 'ancestors of slave page: ' + str([(spage, spage.pk) for spage in self.slave_page.get_ancestors()])
self.assertTrue(has_generic_permission(page.pk, self.user_master, "publish", 1))
# should be True user_master should have publish permissions for childred aswell

0 comments on commit f8d9cb3

Please sign in to comment.
Something went wrong with that request. Please try again.