From 006145a19680657b5a8ddb0caa49025b98d9fbce Mon Sep 17 00:00:00 2001 From: Neal Todd Date: Thu, 6 Feb 2014 11:33:32 +0000 Subject: [PATCH] Linted wagtailcore --- wagtail/wagtailcore/admin.py | 3 ++- wagtail/wagtailcore/fields.py | 1 + .../management/commands/fixtree.py | 1 + .../management/commands/move_pages.py | 3 ++- .../management/commands/replace_text.py | 2 ++ .../management/commands/set_url_paths.py | 1 + wagtail/wagtailcore/middleware.py | 1 + wagtail/wagtailcore/models.py | 22 +++++++++++++++---- wagtail/wagtailcore/rich_text.py | 9 +++++--- wagtail/wagtailcore/templatetags/pageurl.py | 1 + wagtail/wagtailcore/templatetags/rich_text.py | 1 + wagtail/wagtailcore/urls.py | 5 +++-- wagtail/wagtailcore/util.py | 1 + wagtail/wagtailcore/whitelist.py | 2 ++ 14 files changed, 42 insertions(+), 11 deletions(-) diff --git a/wagtail/wagtailcore/admin.py b/wagtail/wagtailcore/admin.py index f52ad84508..b42d7ff441 100644 --- a/wagtail/wagtailcore/admin.py +++ b/wagtail/wagtailcore/admin.py @@ -1,5 +1,4 @@ from django.contrib import admin - from django.contrib.auth.models import Group from django.contrib.auth.admin import GroupAdmin @@ -8,6 +7,7 @@ from wagtail.wagtailcore.models import Site, Page, GroupPagePermission admin.site.register(Site) admin.site.register(Page) + # Extend GroupAdmin to include page permissions as an inline class GroupPagePermissionInline(admin.TabularInline): model = GroupPagePermission @@ -15,6 +15,7 @@ class GroupPagePermissionInline(admin.TabularInline): verbose_name = 'page permission' verbose_name_plural = 'page permissions' + class GroupAdminWithPagePermissions(GroupAdmin): inlines = GroupAdmin.inlines + [GroupPagePermissionInline] diff --git a/wagtail/wagtailcore/fields.py b/wagtail/wagtailcore/fields.py index 130c91d5ce..64235a4130 100644 --- a/wagtail/wagtailcore/fields.py +++ b/wagtail/wagtailcore/fields.py @@ -4,6 +4,7 @@ from south.modelsinspector import add_introspection_rules from wagtail.wagtailcore.rich_text import DbWhitelister, expand_db_html + class RichTextArea(Textarea): def get_panel(self): from wagtail.wagtailadmin.edit_handlers import RichTextFieldPanel diff --git a/wagtail/wagtailcore/management/commands/fixtree.py b/wagtail/wagtailcore/management/commands/fixtree.py index ac82553acb..4bf989d1ef 100644 --- a/wagtail/wagtailcore/management/commands/fixtree.py +++ b/wagtail/wagtailcore/management/commands/fixtree.py @@ -3,6 +3,7 @@ from django.core.exceptions import ObjectDoesNotExist from wagtail.wagtailcore.models import Page + class Command(NoArgsCommand): def handle_noargs(self, **options): problems_found = False diff --git a/wagtail/wagtailcore/management/commands/move_pages.py b/wagtail/wagtailcore/management/commands/move_pages.py index 162249a8c0..344604f060 100644 --- a/wagtail/wagtailcore/management/commands/move_pages.py +++ b/wagtail/wagtailcore/management/commands/move_pages.py @@ -1,4 +1,5 @@ from django.core.management.base import BaseCommand + from wagtail.wagtailcore.models import Page @@ -18,4 +19,4 @@ class Command(BaseCommand): for page in pages: page.move(to_page, pos='last-child') - print 'Done' \ No newline at end of file + print 'Done' diff --git a/wagtail/wagtailcore/management/commands/replace_text.py b/wagtail/wagtailcore/management/commands/replace_text.py index 777e5a29f1..23788d3700 100644 --- a/wagtail/wagtailcore/management/commands/replace_text.py +++ b/wagtail/wagtailcore/management/commands/replace_text.py @@ -1,5 +1,6 @@ from django.core.management.base import BaseCommand from django.db import models + from wagtail.wagtailcore.models import PageRevision, get_page_types @@ -15,6 +16,7 @@ def replace_in_model(model, from_text, to_text): if updated_fields: model.save(update_fields=updated_fields) + class Command(BaseCommand): def handle(self, from_text, to_text, **options): for revision in PageRevision.objects.filter(content_json__contains=from_text): diff --git a/wagtail/wagtailcore/management/commands/set_url_paths.py b/wagtail/wagtailcore/management/commands/set_url_paths.py index 226c63401f..1c74d7018a 100644 --- a/wagtail/wagtailcore/management/commands/set_url_paths.py +++ b/wagtail/wagtailcore/management/commands/set_url_paths.py @@ -2,6 +2,7 @@ from django.core.management.base import NoArgsCommand from wagtail.wagtailcore.models import Page + class Command(NoArgsCommand): def set_subtree(self, root, root_path): root.url_path = root_path diff --git a/wagtail/wagtailcore/middleware.py b/wagtail/wagtailcore/middleware.py index a5345e762c..fbb10e5f1e 100644 --- a/wagtail/wagtailcore/middleware.py +++ b/wagtail/wagtailcore/middleware.py @@ -1,5 +1,6 @@ from wagtail.wagtailcore.models import Site + class SiteMiddleware(object): def process_request(self, request): """ diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 71e1474481..86ddcceffe 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -1,17 +1,18 @@ +from modelcluster.models import ClusterableModel +from treebeard.mp_tree import MP_Node + from django.db import models, connection, transaction from django.db.models import get_model, Q from django.http import Http404 from django.shortcuts import render from django.core.cache import cache - from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import Group -from treebeard.mp_tree import MP_Node -from modelcluster.models import ClusterableModel -from wagtail.wagtailsearch import Indexed, Searcher from wagtail.wagtailcore.util import camelcase_to_underscore +from wagtail.wagtailsearch import Indexed, Searcher + class SiteManager(models.Manager): def get_by_natural_key(self, hostname): @@ -76,6 +77,8 @@ class Site(models.Model): PAGE_MODEL_CLASSES = [] _PAGE_CONTENT_TYPES = [] + + def get_page_types(): global _PAGE_CONTENT_TYPES if len(_PAGE_CONTENT_TYPES) != len(PAGE_MODEL_CLASSES): @@ -84,8 +87,11 @@ def get_page_types(): ] return _PAGE_CONTENT_TYPES + LEAF_PAGE_MODEL_CLASSES = [] _LEAF_PAGE_CONTENT_TYPE_IDS = [] + + def get_leaf_page_content_type_ids(): global _LEAF_PAGE_CONTENT_TYPE_IDS if len(_LEAF_PAGE_CONTENT_TYPE_IDS) != len(LEAF_PAGE_MODEL_CLASSES): @@ -94,8 +100,11 @@ def get_leaf_page_content_type_ids(): ] return _LEAF_PAGE_CONTENT_TYPE_IDS + NAVIGABLE_PAGE_MODEL_CLASSES = [] _NAVIGABLE_PAGE_CONTENT_TYPE_IDS = [] + + def get_navigable_page_content_type_ids(): global _NAVIGABLE_PAGE_CONTENT_TYPE_IDS if len(_NAVIGABLE_PAGE_CONTENT_TYPE_IDS) != len(NAVIGABLE_PAGE_MODEL_CLASSES): @@ -104,6 +113,7 @@ def get_navigable_page_content_type_ids(): ] return _NAVIGABLE_PAGE_CONTENT_TYPE_IDS + class PageBase(models.base.ModelBase): """Metaclass for Page""" def __init__(cls, name, bases, dct): @@ -420,6 +430,7 @@ class Page(MP_Node, ClusterableModel, Indexed): user_perms = UserPagePermissionsProxy(user) return user_perms.for_page(self) + def get_navigation_menu_items(): # Get all pages that appear in the navigation menu: ones which have children, # or are a non-leaf type (indicating that they *could* have children), @@ -487,6 +498,7 @@ class SubmittedRevisionsManager(models.Manager): def get_query_set(self): return super(SubmittedRevisionsManager, self).get_query_set().filter(submitted_for_moderation=True) + class PageRevision(models.Model): page = models.ForeignKey('Page', related_name='revisions') submitted_for_moderation = models.BooleanField(default=False) @@ -537,6 +549,7 @@ PAGE_PERMISSION_TYPE_CHOICES = [ ('publish', 'Publish'), ] + class GroupPagePermission(models.Model): group = models.ForeignKey(Group, related_name='page_permissions') page = models.ForeignKey('Page', related_name='group_permissions') @@ -580,6 +593,7 @@ class UserPagePermissionsProxy(object): permission to perform specific tasks on the given page""" return PagePermissionTester(self, page) + class PagePermissionTester(object): def __init__(self, user_perms, page): self.user = user_perms.user diff --git a/wagtail/wagtailcore/rich_text.py b/wagtail/wagtailcore/rich_text.py index 61a42a0f54..bd00ee503e 100644 --- a/wagtail/wagtailcore/rich_text.py +++ b/wagtail/wagtailcore/rich_text.py @@ -1,17 +1,18 @@ -from django.utils.html import escape - import re # parsing HTML with regexes LIKE A BOSS. +from django.utils.html import escape + from wagtail.wagtailcore.whitelist import Whitelister from wagtail.wagtailcore.models import Page +from wagtail.wagtaildocs.models import Document + # FIXME: we don't really want to import wagtailimages within core. # For that matter, we probably don't want core to be concerned about translating # HTML for the benefit of the hallo.js editor... from wagtail.wagtailimages.models import get_image_model from wagtail.wagtailimages.formats import get_image_format -from wagtail.wagtaildocs.models import Document # Define a set of 'embed handlers' and 'link handlers'. These handle the translation # of 'special' HTML elements in rich text - ones which we do not want to include @@ -187,6 +188,7 @@ FIND_A_TAG = re.compile(r']*)>') FIND_EMBED_TAG = re.compile(r']*)/>') FIND_ATTRS = re.compile(r'([\w-]+)\="([^"]*)"') + def extract_attrs(attr_string): """ helper method to extract tag attributes as a dict. Does not escape HTML entities! @@ -196,6 +198,7 @@ def extract_attrs(attr_string): attributes[name] = val return attributes + def expand_db_html(html, for_editor=False): """ Expand database-representation HTML into proper HTML usable in either diff --git a/wagtail/wagtailcore/templatetags/pageurl.py b/wagtail/wagtailcore/templatetags/pageurl.py index 4c2bd36878..c0abbff38d 100644 --- a/wagtail/wagtailcore/templatetags/pageurl.py +++ b/wagtail/wagtailcore/templatetags/pageurl.py @@ -2,6 +2,7 @@ from django import template register = template.Library() + @register.simple_tag(takes_context=True) def pageurl(context, page): """ diff --git a/wagtail/wagtailcore/templatetags/rich_text.py b/wagtail/wagtailcore/templatetags/rich_text.py index 675f570566..8cc2c91eed 100644 --- a/wagtail/wagtailcore/templatetags/rich_text.py +++ b/wagtail/wagtailcore/templatetags/rich_text.py @@ -5,6 +5,7 @@ from wagtail.wagtailcore.rich_text import expand_db_html register = template.Library() + @register.filter def richtext(value): return mark_safe(expand_db_html(value)) diff --git a/wagtail/wagtailcore/urls.py b/wagtail/wagtailcore/urls.py index 704967f423..8a5a12babd 100644 --- a/wagtail/wagtailcore/urls.py +++ b/wagtail/wagtailcore/urls.py @@ -1,10 +1,11 @@ from django.conf.urls import patterns, url -urlpatterns = patterns('wagtail.wagtailcore.views', +urlpatterns = patterns( + 'wagtail.wagtailcore.views', # All front-end views are handled through Wagtail's core.views.serve mechanism. # Here we match a (possibly empty) list of path segments, each followed by # a '/'. If a trailing slash is not present, we leave CommonMiddleware to # handle it as usual (i.e. redirect it to the trailing slash version if # settings.APPEND_SLASH is True) - url(r'^((?:[\w\-]+/)*)$', 'serve' ) + url(r'^((?:[\w\-]+/)*)$', 'serve') ) diff --git a/wagtail/wagtailcore/util.py b/wagtail/wagtailcore/util.py index 20d9f1be54..c5ad7ea8da 100644 --- a/wagtail/wagtailcore/util.py +++ b/wagtail/wagtailcore/util.py @@ -1,5 +1,6 @@ import re + def camelcase_to_underscore(str): # http://djangosnippets.org/snippets/585/ return re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', str).lower().strip('_') diff --git a/wagtail/wagtailcore/whitelist.py b/wagtail/wagtailcore/whitelist.py index e307f3212d..f2eba727b4 100644 --- a/wagtail/wagtailcore/whitelist.py +++ b/wagtail/wagtailcore/whitelist.py @@ -8,6 +8,7 @@ from urlparse import urlparse ALLOWED_URL_SCHEMES = ['', 'http', 'https', 'ftp', 'mailto', 'tel'] + def check_url(url_string): # TODO: more paranoid checks (urlparse doesn't catch "jav\tascript:alert('XSS')") url = urlparse(url_string) @@ -46,6 +47,7 @@ def attribute_rule(allowed_attrs): allow_without_attributes = attribute_rule({}) + class Whitelister(object): element_rules = { '[document]': allow_without_attributes,