class SearchTestChild(SearchTest): subtitle = models.CharField(max_length=255, null=True, blank=True) extra_content = models.TextField() search_fields = SearchTest.search_fields + [ indexed.SearchField('subtitle', partial_match=True), indexed.SearchField('extra_content'), ]
class TagSearchable(indexed.Indexed): """ Mixin to provide a 'search' method, searching on the 'title' field and tags, for models that provide those things. """ search_fields = ( indexed.SearchField('title', partial_match=True, boost=10), indexed.SearchField('get_tags', partial_match=True, boost=10) ) @property def get_tags(self): return ' '.join([tag.name for tag in self.tags.all()]) @classmethod def search(cls, q, results_per_page=None, page=1, prefetch_tags=False, filters={}): # Run search query search_backend = get_search_backend() if prefetch_tags: results = search_backend.search(q, cls, prefetch_related=['tagged_items__tag'], filters=filters) else: results = search_backend.search(q, cls, filters=filters) # If results_per_page is set, return a paginator if results_per_page is not None: paginator = Paginator(results, results_per_page) try: return paginator.page(page) except PageNotAnInteger: return paginator.page(1) except EmptyPage: return paginator.page(paginator.num_pages) else: return results def prefetched_tags(self): # a hack to do the equivalent of self.tags.all() but take advantage of the # prefetch_related('tagged_items__tag') in the above search method, so that we can # output the list of tags on each result without doing a further query return [tagged_item.tag for tagged_item in self.tagged_items.all()] @classmethod def popular_tags(cls): content_type = ContentType.objects.get_for_model(cls) return Tag.objects.filter( taggit_taggeditem_items__content_type=content_type ).annotate( item_count=Count('taggit_taggeditem_items') ).order_by('-item_count')[:10]
class SearchTest(models.Model, indexed.Indexed): title = models.CharField(max_length=255) content = models.TextField() live = models.BooleanField(default=False) published_date = models.DateField(null=True) search_fields = [ indexed.SearchField('title', partial_match=True), indexed.SearchField('content'), indexed.SearchField('callable_indexed_field'), indexed.FilterField('title'), indexed.FilterField('live'), indexed.FilterField('published_date'), ] def callable_indexed_field(self): return "Callable"
class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, indexed.Indexed)): title = models.CharField(max_length=255, help_text=_("The page title as you'd like it to be seen by the public")) slug = models.SlugField(help_text=_("The name of the page as it will appear in URLs e.g http://domain.com/blog/[my-slug]/")) # TODO: enforce uniqueness on slug field per parent (will have to be done at the Django # level rather than db, since there is no explicit parent relation in the db) content_type = models.ForeignKey('contenttypes.ContentType', related_name='pages') live = models.BooleanField(default=True, editable=False) has_unpublished_changes = models.BooleanField(default=False, editable=False) url_path = models.CharField(max_length=255, blank=True, editable=False) owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, editable=False, related_name='owned_pages') seo_title = models.CharField(verbose_name=_("Page title"), max_length=255, blank=True, help_text=_("Optional. 'Search Engine Friendly' title. This will appear at the top of the browser window.")) show_in_menus = models.BooleanField(default=False, help_text=_("Whether a link to this page will appear in automatically generated menus")) search_description = models.TextField(blank=True) go_live_at = models.DateTimeField(verbose_name=_("Go live date/time"), help_text=_("Please add a date-time in the form YYYY-MM-DD hh:mm."), blank=True, null=True) expire_at = models.DateTimeField(verbose_name=_("Expiry date/time"), help_text=_("Please add a date-time in the form YYYY-MM-DD hh:mm."), blank=True, null=True) expired = models.BooleanField(default=False, editable=False) search_fields = ( indexed.SearchField('title', partial_match=True, boost=100), indexed.FilterField('live'), indexed.FilterField('path'), ) def __init__(self, *args, **kwargs): super(Page, self).__init__(*args, **kwargs) if not self.id and not self.content_type_id: # this model is being newly created rather than retrieved from the db; # set content type to correctly represent the model class that this was # created as self.content_type = ContentType.objects.get_for_model(self) def __str__(self): return self.title is_abstract = True # don't offer Page in the list of page types a superuser can create def set_url_path(self, parent): """ Populate the url_path field based on this page's slug and the specified parent page. (We pass a parent in here, rather than retrieving it via get_parent, so that we can give new unsaved pages a meaningful URL when previewing them; at that point the page has not been assigned a position in the tree, as far as treebeard is concerned. """ if parent: self.url_path = parent.url_path + self.slug + '/' else: # a page without a parent is the tree root, which always has a url_path of '/' self.url_path = '/' return self.url_path @transaction.atomic # ensure that changes are only committed when we have updated all descendant URL paths, to preserve consistency def save(self, *args, **kwargs): update_descendant_url_paths = False if self.id is None: # we are creating a record. If we're doing things properly, this should happen # through a treebeard method like add_child, in which case the 'path' field # has been set and so we can safely call get_parent self.set_url_path(self.get_parent()) else: # see if the slug has changed from the record in the db, in which case we need to # update url_path of self and all descendants old_record = Page.objects.get(id=self.id) if old_record.slug != self.slug: self.set_url_path(self.get_parent()) update_descendant_url_paths = True old_url_path = old_record.url_path new_url_path = self.url_path result = super(Page, self).save(*args, **kwargs) if update_descendant_url_paths: self._update_descendant_url_paths(old_url_path, new_url_path) # Check if this is a root page of any sites and clear the 'wagtail_site_root_paths' key if so if Site.objects.filter(root_page=self).exists(): cache.delete('wagtail_site_root_paths') return result def _update_descendant_url_paths(self, old_url_path, new_url_path): cursor = connection.cursor() if connection.vendor == 'sqlite': update_statement = """ UPDATE wagtailcore_page SET url_path = %s || substr(url_path, %s) WHERE path LIKE %s AND id <> %s """ elif connection.vendor == 'mysql': update_statement = """ UPDATE wagtailcore_page SET url_path= CONCAT(%s, substring(url_path, %s)) WHERE path LIKE %s AND id <> %s """ else: update_statement = """ UPDATE wagtailcore_page SET url_path = %s || substring(url_path from %s) WHERE path LIKE %s AND id <> %s """ cursor.execute(update_statement, [new_url_path, len(old_url_path) + 1, self.path + '%', self.id]) @cached_property def specific(self): """ Return this page in its most specific subclassed form. """ # the ContentType.objects manager keeps a cache, so this should potentially # avoid a database lookup over doing self.content_type. I think. content_type = ContentType.objects.get_for_id(self.content_type_id) if isinstance(self, content_type.model_class()): # self is already the an instance of the most specific class return self else: return content_type.get_object_for_this_type(id=self.id) @cached_property def specific_class(self): """ return the class that this page would be if instantiated in its most specific form """ content_type = ContentType.objects.get_for_id(self.content_type_id) return content_type.model_class() def route(self, request, path_components): if path_components: # request is for a child of this page child_slug = path_components[0] remaining_components = path_components[1:] try: subpage = self.get_children().get(slug=child_slug) except Page.DoesNotExist: raise Http404 return subpage.specific.route(request, remaining_components) else: # request is for this very page if self.live: return RouteResult(self) else: raise Http404 def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None): return self.revisions.create( content_json=self.to_json(), user=user, submitted_for_moderation=submitted_for_moderation, approved_go_live_at=approved_go_live_at, ) def get_latest_revision(self): return self.revisions.order_by('-created_at').first() def get_latest_revision_as_page(self): latest_revision = self.get_latest_revision() if latest_revision: return latest_revision.as_page_object() else: return self.specific def get_context(self, request, *args, **kwargs): return { 'self': self, 'request': request, } def get_template(self, request, *args, **kwargs): if request.is_ajax(): return self.ajax_template or self.template else: return self.template def serve(self, request, *args, **kwargs): return TemplateResponse( request, self.get_template(request, *args, **kwargs), self.get_context(request, *args, **kwargs) ) def is_navigable(self): """ Return true if it's meaningful to browse subpages of this page - i.e. it currently has subpages, or it's at the top level (this rule necessary for empty out-of-the-box sites to have working navigation) """ 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) @property def full_url(self): """Return the full URL (including protocol / domain) to this page, or None if it is not routable""" for (id, root_path, root_url) in Site.get_site_root_paths(): if self.url_path.startswith(root_path): return root_url + self.url_path[len(root_path) - 1:] @property def url(self): """ Return the 'most appropriate' URL for referring to this page from the pages we serve, within the Wagtail backend and actual website templates; this is the local URL (starting with '/') if we're only running a single site (i.e. we know that whatever the current page is being served from, this link will be on the same domain), and the full URL (with domain) if not. Return None if the page is not routable. """ root_paths = Site.get_site_root_paths() for (id, root_path, root_url) in Site.get_site_root_paths(): if self.url_path.startswith(root_path): return ('' if len(root_paths) == 1 else root_url) + self.url_path[len(root_path) - 1:] def relative_url(self, current_site): """ Return the 'most appropriate' URL for this page taking into account the site we're currently on; a local URL if the site matches, or a fully qualified one otherwise. Return None if the page is not routable. """ for (id, root_path, root_url) in Site.get_site_root_paths(): if self.url_path.startswith(root_path): return ('' if current_site.id == id else root_url) + self.url_path[len(root_path) - 1:] @classmethod def search(cls, query_string, show_unpublished=False, search_title_only=False, extra_filters={}, prefetch_related=[], path=None): # Filters filters = extra_filters.copy() if not show_unpublished: filters['live'] = True # Path if path: filters['path__startswith'] = path # Fields fields = None if search_title_only: fields = ['title'] # Search s = get_search_backend() return s.search(query_string, cls, fields=fields, filters=filters, prefetch_related=prefetch_related) @classmethod def clean_subpage_types(cls): """ Returns the list of subpage types, with strings converted to class objects where required """ if cls._clean_subpage_types is None: subpage_types = getattr(cls, 'subpage_types', None) if subpage_types is None: # if subpage_types is not specified on the Page class, allow all page types as subpages res = get_page_types() else: res = [] for page_type in cls.subpage_types: if isinstance(page_type, string_types): try: app_label, model_name = page_type.split(".") except ValueError: # If we can't split, assume a model in current app app_label = cls._meta.app_label model_name = page_type model = get_model(app_label, model_name) if model: res.append(ContentType.objects.get_for_model(model)) else: raise NameError(_("name '{0}' (used in subpage_types list) is not defined.").format(page_type)) else: # assume it's already a model class res.append(ContentType.objects.get_for_model(page_type)) cls._clean_subpage_types = res return cls._clean_subpage_types @classmethod def allowed_parent_page_types(cls): """ Returns the list of page types that this page type can be a subpage of """ return [ct for ct in get_page_types() if cls in ct.model_class().clean_subpage_types()] @classmethod def allowed_parent_pages(cls): """ Returns the list of pages that this page type can be a subpage of """ return Page.objects.filter(content_type__in=cls.allowed_parent_page_types()) @classmethod def get_verbose_name(cls): # This is similar to doing cls._meta.verbose_name.title() # except this doesn't convert any characters to lowercase return ' '.join([word[0].upper() + word[1:] for word in cls._meta.verbose_name.split()]) @property def status_string(self): if not self.live: if self.expired: return "expired" elif self.approved_schedule: return "scheduled" else: return "draft" else: if self.has_unpublished_changes: return "live + draft" else: return "live" @property def approved_schedule(self): return self.revisions.exclude(approved_go_live_at__isnull=True).exists() def has_unpublished_subtree(self): """ An awkwardly-defined flag used in determining whether unprivileged editors have permission to delete this article. Returns true if and only if this page is non-live, and it has no live children. """ return (not self.live) and (not self.get_descendants().filter(live=True).exists()) @transaction.atomic # only commit when all descendants are properly updated def move(self, target, pos=None): """ Extension to the treebeard 'move' method to ensure that url_path is updated too. """ old_url_path = Page.objects.get(id=self.id).url_path super(Page, self).move(target, pos=pos) # treebeard's move method doesn't actually update the in-memory instance, so we need to work # with a freshly loaded one now new_self = Page.objects.get(id=self.id) new_url_path = new_self.set_url_path(new_self.get_parent()) new_self.save() new_self._update_descendant_url_paths(old_url_path, new_url_path) def copy(self, recursive=False, to=None, update_attrs=None): # Make a copy page_copy = Page.objects.get(id=self.id).specific page_copy.pk = None page_copy.id = None page_copy.depth = None page_copy.numchild = 0 page_copy.path = None if update_attrs: for field, value in update_attrs.items(): setattr(page_copy, field, value) if to: page_copy = to.add_child(instance=page_copy) else: page_copy = self.add_sibling(instance=page_copy) # Copy child objects specific_self = self.specific for child_relation in getattr(specific_self._meta, 'child_relations', []): parental_key_name = child_relation.field.attname child_objects = getattr(specific_self, child_relation.get_accessor_name(), None) if child_objects: for child_object in child_objects.all(): child_object.pk = None setattr(child_object, parental_key_name, page_copy.id) child_object.save() # Copy child pages if recursive: for child_page in self.get_children(): child_page.specific.copy(recursive=True, to=page_copy) return page_copy def permissions_for_user(self, user): """ Return a PagePermissionsTester object defining what actions the user can perform on this page """ user_perms = UserPagePermissionsProxy(user) return user_perms.for_page(self) def dummy_request(self): """ Construct a HttpRequest object that is, as far as possible, representative of ones that would receive this page as a response. Used for previewing / moderation and any other place where we want to display a view of this page in the admin interface without going through the regular page routing logic. """ url = self.full_url if url: url_info = urlparse(url) hostname = url_info.hostname path = url_info.path port = url_info.port or 80 else: # Cannot determine a URL to this page - cobble one together based on # whatever we find in ALLOWED_HOSTS try: hostname = settings.ALLOWED_HOSTS[0] except IndexError: hostname = 'localhost' path = '/' port = 80 request = WSGIRequest({ 'REQUEST_METHOD': 'GET', 'PATH_INFO': path, 'SERVER_NAME': hostname, 'SERVER_PORT': port, 'wsgi.input': StringIO(), }) # Apply middleware to the request - see http://www.mellowmorning.com/2011/04/18/mock-django-request-for-testing/ handler = BaseHandler() handler.load_middleware() for middleware_method in handler._request_middleware: if middleware_method(request): raise Exception("Couldn't create request mock object - " "request middleware returned a response") return request DEFAULT_PREVIEW_MODES = [('', 'Default')] @property def preview_modes(self): """ A list of (internal_name, display_name) tuples for the modes in which this page can be displayed for preview/moderation purposes. Ordinarily a page will only have one display mode, but subclasses of Page can override this - for example, a page containing a form might have a default view of the form, and a post-submission 'thankyou' page """ modes = self.get_page_modes() if modes is not Page.DEFAULT_PREVIEW_MODES: # User has overriden get_page_modes instead of using preview_modes warnings.warn("Overriding get_page_modes is deprecated. Define a preview_modes property instead", DeprecationWarning) return modes def get_page_modes(self): # Deprecated accessor for the preview_modes property return Page.DEFAULT_PREVIEW_MODES @property def default_preview_mode(self): return self.preview_modes[0][0] def serve_preview(self, request, mode_name): """ Return an HTTP response for use in page previews. Normally this would be equivalent to self.serve(request), since we obviously want the preview to be indicative of how it looks on the live site. However, there are a couple of cases where this is not appropriate, and custom behaviour is required: 1) The page has custom routing logic that derives some additional required args/kwargs to be passed to serve(). The routing mechanism is bypassed when previewing, so there's no way to know what args we should pass. In such a case, the page model needs to implement its own version of serve_preview. 2) The page has several different renderings that we would like to be able to see when previewing - for example, a form page might have one rendering that displays the form, and another rendering to display a landing page when the form is posted. This can be done by setting a custom preview_modes list on the page model - Wagtail will allow the user to specify one of those modes when previewing, and pass the chosen mode_name to serve_preview so that the page model can decide how to render it appropriately. (Page models that do not specify their own preview_modes list will always receive an empty string as mode_name.) Any templates rendered during this process should use the 'request' object passed here - this ensures that request.user and other properties are set appropriately for the wagtail user bar to be displayed. This request will always be a GET. """ return self.serve(request) def show_as_mode(self, mode_name): # Deprecated API for rendering previews. If this returns something other than None, # we know that a subclass of Page has overridden this, and we should try to work with # that response if possible. return None def get_cached_paths(self): """ This returns a list of paths to invalidate in a frontend cache """ return ['/'] def get_sitemap_urls(self): latest_revision = self.get_latest_revision() return [ { 'location': self.full_url, 'lastmod': latest_revision.created_at if latest_revision else None } ] def get_static_site_paths(self): """ This is a generator of URL paths to feed into a static site generator Override this if you would like to create static versions of subpages """ # Yield path for this page yield '/' # Yield paths for child pages for child in self.get_children().live(): for path in child.specific.get_static_site_paths(): yield '/' + child.slug + path def get_ancestors(self, inclusive=False): return Page.objects.ancestor_of(self, inclusive) def get_descendants(self, inclusive=False): return Page.objects.descendant_of(self, inclusive) def get_siblings(self, inclusive=True): return Page.objects.sibling_of(self, inclusive) def get_next_siblings(self, inclusive=False): return self.get_siblings(inclusive).filter(path__gte=self.path).order_by('path') def get_prev_siblings(self, inclusive=False): return self.get_siblings(inclusive).filter(path__lte=self.path).order_by('-path') def get_view_restrictions(self): """Return a query set of all page view restrictions that apply to this page""" return PageViewRestriction.objects.filter(page__in=self.get_ancestors(inclusive=True)) password_required_template = getattr(settings, 'PASSWORD_REQUIRED_TEMPLATE', 'wagtailcore/password_required.html') def serve_password_required_response(self, request, form, action_url): """ Serve a response indicating that the user has been denied access to view this page, and must supply a password. form = a Django form object containing the password input (and zero or more hidden fields that also need to be output on the template) action_url = URL that this form should be POSTed to """ context = self.get_context(request) context['form'] = form context['action_url'] = action_url return TemplateResponse(request, self.password_required_template, context)