Wagtail: How to perform site-wide search with custom SearchField declarations in subclassed pages? - wagtail

I'm using the built-in Wagtail search with POSTGRES backend. I don't want to go to Elastic Search as this would be overkill for the site concerned.
The problem I'm hitting and can't find any info on is how to perform a site-wide search that includes all the search fields declared in the subclassed pages.
For example:
class SEOPage(Page):
....
search_fields = Page.search_fields + [
index.SearchField('summary'),
]
class BlogDetailPage(SEOPage):
....
search_fields = SEOPage.search_fields + [
index.SearchField('body'),
]
But if do something like
Page.objects.search("findme")
or even
Page.objects.specific().search("findme")
then neither the summary or body fields are searched because they don't exist at the Page model level.
Is there any way to perform a site wide search that includes all the SearchField's in the site?

Thanks to Matt Westcott, the solution was to use the POSTGRES backend:
https://docs.wagtail.io/en/latest/reference/contrib/postgres_search.html
It's very easy to add multi-language support using this as well.

Related

Is there a way to show images in a Wagtail Model Admin Record Listing page?

I have reviewed the question on Is there any way to show a field on a listing page in Wagtail admin? but my situation seems to similar but also different enough that that particular solution won't work for me. Instead of on the Page listing I wish to achieve a similar thing on the Model Admin listing and I would think this should be such a common requirement that I am picking that someone must have done this before I have attempted it.
I haven't really figured out how to even try anything to get started but what I have looked at is the modeladmin template tags under wagtail.contrib.modeladmin on GitHub but I am completely guessing.
Can anyone point me to which templates I need to modify and whether I need to modify any of the template tags and how to override anything I need to override?
There's no need to override templates for this - this is standard functionality in ModelAdmin. Adding extra fields to the listing is done by setting list_display on the ModelAdmin class:
class BookAdmin(ModelAdmin):
model = Book
list_display = ('title', 'author')
For displaying images, ModelAdmin provides ThumbnailMixin:
from wagtail.contrib.modeladmin.mixins import ThumbnailMixin
from wagtail.contrib.modeladmin.options import ModelAdmin
class BookAdmin(ThumbnailMixin, ModelAdmin):
model = Book
thumb_image_field_name = 'cover_image'
list_display = ('title', 'author', 'admin_thumb')
('admin_thumb' is a special-purpose field name provided by ThumbnailMixin, and should be used rather than the actual image field on your model - cover_image in this example.)

Wagtail Search in CMS

I'm trying to search through all of my pages on the backend to find instances of urls with outdated domain names. But when I search, it only seems to get hits based on the title of the pages, and not their content. Is there something I need to configure to make this work?
Thank you!
To search on fields beyond the ones that are common to all page types (such as title), you'll need to define a search_fields property on your page model, such as:
from wagtail.search import index
class MyPage(Page):
body = StreamField(...)
search_fields = Page.search_fields + [
index.SearchField('body')
]
After setting this up, you'll need to update the search index to contain this new data, by running ./manage.py update_index.
If you're running 2.14.x or earlier, you'll also need to set up an alternative search backend such as Postgres or Elasticsearch - the default wagtail.search.backends.db search backend was very limited and only supported searching on the 'core' fields such as title. As of 2.15, a new wagtail.search.backends.database backend is available and enabled on new projects out of the box, and this has full support for searching across all fields.

Is it posible to use django_select2 widgets with a Wagtail ParentalManyToManyField

I need a complex selection widget because there are a lot of options in a multiple select widget. But I see HeavySelect2MultipleWidget needs views and urls to use it. I think there is not possible it in Wagtail by default.
This is the code:
class Resource(Page):
authors = ParentalManyToManyField('Authors', blank=True)
content_panels = Page.content_panels + [
FieldPanel('authors', widget=forms.CheckboxSelectMultiple)
]
It would be nice to use
FieldPanel('authors', widget=HeavySelect2MultipleWidget)
but it raises a
You must ether specify "data_view" or "data_url".
According to the Django-Select2 documentation you have to initiate (call) the widget with the attributes first.
As the error says - data_url or data_view has not been provided to the widget.
It will be up to you to generate that view or url specific to your use case. You could override the serve method of your page model to serve the data and provide the appropriate url pattern or simply create a different view altogether as documented here (see Heavy Components section).
For example:
FieldPanel(
'authors',
widget=HeavySelect2MultipleWidget(
data_url='url/to/json/resonse'
)
)
HeavySelect2MultipleWidget extends HeavySelect2Widget - see docs for more detail:
http://django-select2.readthedocs.io/en/latest/django_select2.html#django_select2.forms.HeavySelect2Widget
I have not used this specific widget but have used Django-Select2 in Wagtail in a similar set up and it has worked well.

wagtail modeladmin: is it possible to add "explore child pages" column?

I've been using wagtail-modeladmin to create custom lists of certain page types. That gives me the ability to edit those pages. But I'd also like to be able to click through somehow to the "normal" admin explorer version of those pages, and be able to view/add child pages to them.
essentially giving myself a column with the little arrows in on the right, just like in the normal wagtail admin page explorer...
OK I know it's bad form to answer your own question, but I've got this working by using a custom method on the model admin object that reverse wagtails admin urls:
class MySpecialPageModelAdmin(ModelAdmin):
def view_children(self, obj):
url = reverse('wagtailadmin_explore', args=[obj.id])
return format_html(f'View Children')
list_display = ('title', 'live', 'view_children')
but actually, I think I'm not going to end up using this, just replacing this particular modeladmin with a direct link to the right place in the explorer.

Drupal 7: Pathauto hierarchical taxonomy terms pattern

I have a Drupal 7.9 taxonomy vocabulary according to the following scheme:
category-1
category-1 > subcategory-1-1
category-1 > subcategory-1-2
category-1 > subcategory-1-3
category-2
category-2 > subcategory-2-1
I want to reflect this taxonomy hierarchy in my page url path like
category-1/subcategory-1-1/page-123
To achieve this I'm using the modul Pathauto version 7.x-1.0. but I don't know
which pattern I have to use.
Currently I'm using [node:%field_taxonomy%]/[node:title] but with this pattern the url path is just subcategory-1-1/page-123, so the complete hierarchy isn't reflected. Is there a taxonomy tree pattern? I can't find any updated information about this and valid patterns seem to change in every version of Pathauto.
After some trial and error I came up with a way which works pretty good for me while creating custom URL paths using taxonomy terms. All you need to do is to install an additional module called Entity API. You can find it here http://drupal.org/project/entity. After installing you should enable both Entity API and Entity tokens modules. Entity tokens provides token replacements for all properties that have no tokens and are known to the entity API.
First, go and create a new taxonomy vocabulary. Let's call it "Category" and add some terms. Then for a content type you want to have a custom URL path you need to create a new term reference field. I don't know why but it's not working with the standart field_tags. So, make sure to create a new one. Let's also call it category and use our previously created vocabulary "Category" as the source. And here's another thing you need to take into account: you have to label this field with a small letter; otherwise it's not working for an unknown to me reason ;). You'll end up with a field_category field.
Go to URL Aliases administration page -> Patterns. Expand replacement patterns for Content paths -> expand Nodes -> you'll see category at the bottom, expand it. Ok, now you're ready to use taxonomy terms tokens.
Use something like [node:field-category:name]/[node:title] where [node:field-category:name] is the name of the taxonomy term and you'll have a path like mysite.com/term/title.
By the way, I use Pathauto 7.x-1.0-beta1, Token 7.x-1.0-beta2 and Entity API 7.x-1.0-beta8.
Good Luck! ))
http://drupal.org/node/1044980#comment-4562844
You can create url with this pattern:
[node:field-category:vocabulary:name]/[node:field-category:parents:join:/]/[node:field-category:name]/[node:title]
In your particular situation you can go away with using following pattern for taxonomy items:
[term:vocabulary:name]/[term:parent:parent]/[term:parent]/[term:name]
And for node pattern:
[node:%field_taxonomy%:url:path]/[node:title]
There is downside tho - term pattern will handle up to three levels in this case. So you can not make it unlimited with this approach. (I believe you can add term:parent:parent:... as many as you want).
Although, it will handle LESS than 3 levels fine (just tested to make sure).
As far as I know, basic Drupal + Token + PathAuto is not able to create paths, like you want them to.
Check my answer here. You can use the taxonomy entity index module for achieving such behaviour.

Resources