From 0c34959bf5fd5a5c48bc1bdddb663f24a53f593e Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 25 Jun 2014 14:28:52 +0100 Subject: [PATCH 1/7] Simplified UserPagePermissionsProxy.editable_pages method --- wagtail/wagtailcore/models.py | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 6b273e4b65..fc40b20bdc 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -803,29 +803,20 @@ class UserPagePermissionsProxy(object): if self.user.is_superuser: return Page.objects.all() - # Translate each of the user's permission rules into a Q-expression - q_expressions = [] - for perm in self.permissions: - if perm.permission_type == 'add': - # user has edit permission on any subpage of perm.page - # (including perm.page itself) that is owned by them - q_expressions.append( - Q(path__startswith=perm.page.path, owner=self.user) - ) - elif perm.permission_type == 'edit': - # user has edit permission on any subpage of perm.page - # (including perm.page itself) regardless of owner - q_expressions.append( - Q(path__startswith=perm.page.path) - ) + editable_pages = Page.objects.none() + + for perm in self.permissions.filter(permission_type='add'): + # user has edit permission on any subpage of perm.page + # (including perm.page itself) that is owned by them + editable_pages |= Page.objects.descendant_of(perm.page, inclusive=True).filter(owner=self.user) + + for perm in self.permissions.filter(permission_type='edit'): + # user has edit permission on any subpage of perm.page + # (including perm.page itself) regardless of owner + editable_pages |= Page.objects.descendant_of(perm.page, inclusive=True) + + return editable_pages - if q_expressions: - all_rules = q_expressions[0] - for expr in q_expressions[1:]: - all_rules = all_rules | expr - return Page.objects.filter(all_rules) - else: - return Page.objects.none() class PagePermissionTester(object): def __init__(self, user_perms, page): From e1bc2425b65f521a3f75f596061fe45ef746f3ba Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:32:01 +0100 Subject: [PATCH 2/7] Return new revision object from Page.save_revision --- wagtail/wagtailcore/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index fc40b20bdc..0ab47ebc1b 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -367,7 +367,7 @@ class Page(MP_Node, ClusterableModel, Indexed): raise Http404 def save_revision(self, user=None, submitted_for_moderation=False): - self.revisions.create(content_json=self.to_json(), user=user, submitted_for_moderation=submitted_for_moderation) + return self.revisions.create(content_json=self.to_json(), user=user, submitted_for_moderation=submitted_for_moderation) def get_latest_revision(self): try: From c7c001c9fe725b94c87d44178e1175c090427cfa Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:32:45 +0100 Subject: [PATCH 3/7] Cleanup Page.get_latest_revision --- wagtail/wagtailcore/models.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 0ab47ebc1b..8b5dd5a6ce 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -370,12 +370,7 @@ class Page(MP_Node, ClusterableModel, Indexed): return self.revisions.create(content_json=self.to_json(), user=user, submitted_for_moderation=submitted_for_moderation) def get_latest_revision(self): - try: - revision = self.revisions.order_by('-created_at')[0] - except IndexError: - return False - - return revision + return self.revisions.order_by('-created_at').first() def get_latest_revision_as_page(self): try: From d89398f2140c6d492f9f66027b6b067ababdd8f9 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:38:29 +0100 Subject: [PATCH 4/7] Cleanup Page.get_latest_revision_as_page --- wagtail/wagtailcore/models.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 8b5dd5a6ce..5546d6635c 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -373,12 +373,12 @@ class Page(MP_Node, ClusterableModel, Indexed): return self.revisions.order_by('-created_at').first() def get_latest_revision_as_page(self): - try: - revision = self.revisions.order_by('-created_at')[0] - except IndexError: - return self.specific + latest_revision = self.get_latest_revision() - return revision.as_page_object() + if latest_revision: + return latest_revision.as_page_object() + else: + return self.specific def get_context(self, request, *args, **kwargs): return { From a15939add624ac19aac4003779a112451d1e0d5f Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:39:06 +0100 Subject: [PATCH 5/7] Make Page.specific and Page.specific_class cached properties --- wagtail/wagtailcore/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 5546d6635c..9d82e81091 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -15,6 +15,7 @@ from django.contrib.auth.models import Group from django.conf import settings from django.template.response import TemplateResponse from django.utils.translation import ugettext_lazy as _ +from django.utils.functional import cached_property from treebeard.mp_tree import MP_Node @@ -323,7 +324,7 @@ class Page(MP_Node, ClusterableModel, Indexed): cursor.execute(update_statement, [new_url_path, len(old_url_path) + 1, self.path + '%', self.id]) - @property + @cached_property def specific(self): """ Return this page in its most specific subclassed form. @@ -337,7 +338,7 @@ class Page(MP_Node, ClusterableModel, Indexed): else: return content_type.get_object_for_this_type(id=self.id) - @property + @cached_property def specific_class(self): """ return the class that this page would be if instantiated in its From deb2e0f9d8141df3b23bc335e373e6e674295b42 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:41:09 +0100 Subject: [PATCH 6/7] Deprecate Page.get_other_siblings --- docs/building_your_site/djangodevelopers.rst | 1 - wagtail/wagtailcore/models.py | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/building_your_site/djangodevelopers.rst b/docs/building_your_site/djangodevelopers.rst index b4899d70f3..82e4c6b89d 100644 --- a/docs/building_your_site/djangodevelopers.rst +++ b/docs/building_your_site/djangodevelopers.rst @@ -203,7 +203,6 @@ Methods: * get_context * get_template * is_navigable -* get_other_siblings * get_ancestors * get_descendants * get_siblings diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 9d82e81091..c13d7d1e43 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -409,6 +409,10 @@ class Page(MP_Node, ClusterableModel, Indexed): return (not self.is_leaf()) or self.depth == 2 def get_other_siblings(self): + warnings.warn( + "The 'Page.get_other_siblings()' method has been replaced. " + "Use 'Page.get_siblings(inclusive=False)' instead.", DeprecationWarning) + # get sibling pages excluding self return self.get_siblings().exclude(id=self.id) From 1a0ffa799d51a17d3eb438b95a9124edd3ece974 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Thu, 26 Jun 2014 14:49:18 +0100 Subject: [PATCH 7/7] Added unicode method to PageRevision --- wagtail/wagtailcore/models.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index c13d7d1e43..a4c6eaf771 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -745,6 +745,9 @@ class PageRevision(models.Model): self.submitted_for_moderation = False page.revisions.update(submitted_for_moderation=False) + def __unicode__(self): + return '"' + unicode(self.page) + '" at ' + unicode(self.created_at) + PAGE_PERMISSION_TYPE_CHOICES = [ ('add', 'Add'), ('edit', 'Edit'),