Initial commit

This commit is contained in:
2024-08-27 20:33:44 +02:00
commit 1f1832267d
14794 changed files with 1599592 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _
class WagtailSearchTestsAppConfig(AppConfig):
default_auto_field = "django.db.models.AutoField"
name = "wagtail.test.search"
label = "searchtests"
verbose_name = _("Wagtail search tests")

View File

@@ -0,0 +1,545 @@
[
{
"pk": 1,
"model": "searchtests.author",
"fields": {
"name": "George R.R. Martin",
"date_of_birth": "1948-09-20"
}
},
{
"pk": 2,
"model": "searchtests.author",
"fields": {
"name": "J. R. R. Tolkien",
"date_of_birth": "1892-01-03"
}
},
{
"pk": 4,
"model": "searchtests.author",
"fields": {
"name": "Isaac Asimov",
"date_of_birth": "1920-01-02"
}
},
{
"pk": 5,
"model": "searchtests.author",
"fields": {
"name": "David Ascher",
"date_of_birth": null
}
},
{
"pk": 6,
"model": "searchtests.author",
"fields": {
"name": "Mark Lutz",
"date_of_birth": null
}
},
{
"pk": 7,
"model": "searchtests.author",
"fields": {
"name": "Daniel Roy Greenfeld",
"date_of_birth": null
}
},
{
"pk": 8,
"model": "searchtests.author",
"fields": {
"name": "Audrey Roy Greenfeld",
"date_of_birth": null
}
},
{
"pk": 9,
"model": "searchtests.author",
"fields": {
"name": "Douglas Crockford",
"date_of_birth": null
}
},
{
"pk": 10,
"model": "searchtests.author",
"fields": {
"name": "David Flanagan",
"date_of_birth": null
}
},
{
"pk": 11,
"model": "searchtests.author",
"fields": {
"name": "Steve Klabnik",
"date_of_birth": null
}
},
{
"pk": 12,
"model": "searchtests.author",
"fields": {
"name": "Carol Nichols",
"date_of_birth": null
}
},
{
"pk": 13,
"model": "searchtests.author",
"fields": {
"name": "Jim Blandy",
"date_of_birth": null
}
},
{
"pk": 14,
"model": "searchtests.author",
"fields": {
"name": "Jason Orendorff",
"date_of_birth": null
}
},
{
"pk": 1,
"model": "searchtests.book",
"fields": {
"title": "A Game of Thrones",
"authors": [1],
"publication_date": "1996-08-01",
"number_of_pages": 694
}
},
{
"pk": 2,
"model": "searchtests.book",
"fields": {
"title": "A Clash of Kings",
"authors": [1],
"publication_date": "1998-01-01",
"number_of_pages": 768
}
},
{
"pk": 3,
"model": "searchtests.book",
"fields": {
"title": "A Storm of Swords",
"authors": [1],
"publication_date": "2000-01-01",
"number_of_pages": 973
}
},
{
"pk": 4,
"model": "searchtests.book",
"fields": {
"title": "The Fellowship of the Ring",
"authors": [2],
"publication_date": "1954-07-29",
"number_of_pages": 423
}
},
{
"pk": 5,
"model": "searchtests.book",
"fields": {
"title": "The Two Towers",
"authors": [2],
"publication_date": "1954-11-11",
"number_of_pages": 352
}
},
{
"pk": 6,
"model": "searchtests.book",
"fields": {
"title": "The Return of the King",
"authors": [2],
"publication_date": "1955-10-20",
"number_of_pages": 440
}
},
{
"pk": 7,
"model": "searchtests.book",
"fields": {
"title": "The Hobbit",
"authors": [2],
"publication_date": "1937-09-21",
"number_of_pages": 320
}
},
{
"pk": 10,
"model": "searchtests.book",
"fields": {
"title": "Foundation",
"authors": [4],
"publication_date": "1951-01-01",
"number_of_pages": 255
}
},
{
"pk": 11,
"model": "searchtests.book",
"fields": {
"title": "Learning Python",
"authors": [5, 6],
"publication_date": "1999-01-01",
"number_of_pages": 1160
}
},
{
"pk": 12,
"model": "searchtests.book",
"fields": {
"title": "Two Scoops of Django 1.11",
"authors": [7, 8],
"publication_date": "2017-04-26",
"number_of_pages": 556
}
},
{
"pk": 13,
"model": "searchtests.book",
"fields": {
"title": "JavaScript: The good parts",
"authors": [9],
"publication_date": "2008-05-18",
"number_of_pages": 172
}
},
{
"pk": 14,
"model": "searchtests.book",
"fields": {
"title": "JavaScript: The Definitive Guide",
"authors": [10],
"publication_date": "1998-06-11",
"number_of_pages": 792
}
},
{
"pk": 15,
"model": "searchtests.book",
"fields": {
"title": "The Rust Programming Language",
"authors": [11, 12],
"publication_date": "2018-05-22",
"number_of_pages": 440
}
},
{
"pk": 16,
"model": "searchtests.book",
"fields": {
"title": "Programming Rust",
"authors": [13, 14],
"publication_date": "2017-12-01",
"number_of_pages": 622
}
},
{
"pk": 1,
"model": "searchtests.unindexedbook",
"fields": {
"title": "A Game of Thrones U",
"publication_date": "1996-08-01",
"number_of_pages": 694
}
},
{
"pk": 2,
"model": "searchtests.unindexedbook",
"fields": {
"title": "A Clash of Kings U",
"publication_date": "1998-01-01",
"number_of_pages": 768
}
},
{
"pk": 3,
"model": "searchtests.unindexedbook",
"fields": {
"title": "A Storm of Swords U",
"publication_date": "2000-01-01",
"number_of_pages": 973
}
},
{
"pk": 4,
"model": "searchtests.unindexedbook",
"fields": {
"title": "The Fellowship of the Ring U",
"publication_date": "1954-07-29",
"number_of_pages": 423
}
},
{
"pk": 1,
"model": "searchtests.character",
"fields": {
"name": "Daenerys Targaryen",
"novel": 1
}
},
{
"pk": 2,
"model": "searchtests.character",
"fields": {
"name": "Jon Snow",
"novel": 1
}
},
{
"pk": 3,
"model": "searchtests.character",
"fields": {
"name": "Eddard Stark",
"novel": 1
}
},
{
"pk": 4,
"model": "searchtests.character",
"fields": {
"name": "Daenerys Targaryen",
"novel": 2
}
},
{
"pk": 5,
"model": "searchtests.character",
"fields": {
"name": "Jon Snow",
"novel": 2
}
},
{
"pk": 6,
"model": "searchtests.character",
"fields": {
"name": "Daenerys Targaryen",
"novel": 3
}
},
{
"pk": 7,
"model": "searchtests.character",
"fields": {
"name": "Jon Snow",
"novel": 3
}
},
{
"pk": 8,
"model": "searchtests.character",
"fields": {
"name": "Frodo Baggins",
"novel": 4
}
},
{
"pk": 9,
"model": "searchtests.character",
"fields": {
"name": "Bilbo Baggins",
"novel": 4
}
},
{
"pk": 10,
"model": "searchtests.character",
"fields": {
"name": "Gandalf",
"novel": 4
}
},
{
"pk": 11,
"model": "searchtests.character",
"fields": {
"name": "Frodo Baggins",
"novel": 5
}
},
{
"pk": 12,
"model": "searchtests.character",
"fields": {
"name": "Bilbo Baggins",
"novel": 5
}
},
{
"pk": 13,
"model": "searchtests.character",
"fields": {
"name": "Gandalf",
"novel": 5
}
},
{
"pk": 14,
"model": "searchtests.character",
"fields": {
"name": "Frodo Baggins",
"novel": 6
}
},
{
"pk": 15,
"model": "searchtests.character",
"fields": {
"name": "Bilbo Baggins",
"novel": 6
}
},
{
"pk": 16,
"model": "searchtests.character",
"fields": {
"name": "Gandalf",
"novel": 6
}
},
{
"pk": 17,
"model": "searchtests.character",
"fields": {
"name": "Bilbo Baggins",
"novel": 7
}
},
{
"pk": 18,
"model": "searchtests.character",
"fields": {
"name": "Thorin Oakenshield",
"novel": 7
}
},
{
"pk": 19,
"model": "searchtests.character",
"fields": {
"name": "Hari Seldon",
"novel": 10
}
},
{
"pk": 20,
"model": "searchtests.character",
"fields": {
"name": "Salvor Hardin",
"novel": 10
}
},
{
"pk": 1,
"model": "searchtests.novel",
"fields": {
"setting": "Westeros",
"protagonist": null
}
},
{
"pk": 2,
"model": "searchtests.novel",
"fields": {
"setting": "Westeros",
"protagonist": null
}
},
{
"pk": 3,
"model": "searchtests.novel",
"fields": {
"setting": "Westeros",
"protagonist": null
}
},
{
"pk": 4,
"model": "searchtests.novel",
"fields": {
"setting": "Middle Earth",
"protagonist": 8
}
},
{
"pk": 5,
"model": "searchtests.novel",
"fields": {
"setting": "Middle Earth",
"protagonist": 11
}
},
{
"pk": 6,
"model": "searchtests.novel",
"fields": {
"setting": "Middle Earth",
"protagonist": 14
}
},
{
"pk": 7,
"model": "searchtests.novel",
"fields": {
"setting": "Middle Earth",
"protagonist": 17
}
},
{
"pk": 10,
"model": "searchtests.novel",
"fields": {
"setting": "Galactic Empire",
"protagonist": null
}
},
{
"pk": 11,
"model": "searchtests.programmingguide",
"fields": {
"programming_language": "py"
}
},
{
"pk": 12,
"model": "searchtests.programmingguide",
"fields": {
"programming_language": "py"
}
},
{
"pk": 13,
"model": "searchtests.programmingguide",
"fields": {
"programming_language": "js"
}
},
{
"pk": 14,
"model": "searchtests.programmingguide",
"fields": {
"programming_language": "js"
}
},
{
"pk": 15,
"model": "searchtests.programmingguide",
"fields": {
"programming_language": "rs"
}
}
]

View File

@@ -0,0 +1,146 @@
# Generated by Django 1.11.6 on 2017-10-18 12:59
from django.db import migrations, models
import django.db.models.deletion
import taggit.managers
import wagtail.search.index
class Migration(migrations.Migration):
initial = True
dependencies = [
("taggit", "0002_auto_20150616_2121"),
]
operations = [
migrations.CreateModel(
name="Author",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
("date_of_birth", models.DateField(null=True)),
],
bases=(models.Model, wagtail.search.index.Indexed),
),
migrations.CreateModel(
name="Book",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255)),
("publication_date", models.DateField()),
("number_of_pages", models.IntegerField()),
],
bases=(models.Model, wagtail.search.index.Indexed),
),
migrations.CreateModel(
name="Character",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name="Novel",
fields=[
(
"book_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="searchtests.Book",
),
),
("setting", models.CharField(max_length=255)),
(
"protagonist",
models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="searchtests.Character",
),
),
],
bases=("searchtests.book",),
),
migrations.CreateModel(
name="ProgrammingGuide",
fields=[
(
"book_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="searchtests.Book",
),
),
(
"programming_language",
models.CharField(
choices=[
("py", "Python"),
("js", "JavaScript"),
("rs", "Rust"),
],
max_length=255,
),
),
],
bases=("searchtests.book",),
),
migrations.AddField(
model_name="book",
name="authors",
field=models.ManyToManyField(related_name="books", to="searchtests.Author"),
),
migrations.AddField(
model_name="book",
name="tags",
field=taggit.managers.TaggableManager(
help_text="A comma-separated list of tags.",
through="taggit.TaggedItem",
to="taggit.Tag",
verbose_name="Tags",
),
),
migrations.AddField(
model_name="character",
name="novel",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="characters",
to="searchtests.Novel",
),
),
]

View File

@@ -0,0 +1,43 @@
# Generated by Django 4.1.4 on 2022-12-23 09:32
from django.db import migrations, models
import taggit.managers
import wagtail.search.index
class Migration(migrations.Migration):
dependencies = [
("taggit", "0001_initial"),
("searchtests", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="UnindexedBook",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255)),
("publication_date", models.DateField()),
("number_of_pages", models.IntegerField()),
(
"tags",
taggit.managers.TaggableManager(
help_text="A comma-separated list of tags.",
through="taggit.TaggedItem",
to="taggit.Tag",
verbose_name="Tags",
),
),
],
bases=(wagtail.search.index.Indexed, models.Model),
),
]

View File

@@ -0,0 +1,18 @@
# Generated by Django 4.0.10 on 2023-11-10 01:12
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("searchtests", "0002_bookunindexed"),
]
operations = [
migrations.AddField(
model_name="book",
name="summary",
field=models.TextField(blank=True),
),
]

View File

@@ -0,0 +1,137 @@
from django.db import models
from taggit.managers import TaggableManager
from wagtail.search import index
class Author(index.Indexed, models.Model):
name = models.CharField(max_length=255)
date_of_birth = models.DateField(null=True)
search_fields = [
index.SearchField("name"),
index.AutocompleteField("name"),
index.FilterField("date_of_birth"),
]
def __str__(self):
return self.name
class Book(index.Indexed, models.Model):
title = models.CharField(max_length=255)
summary = models.TextField(blank=True)
authors = models.ManyToManyField(Author, related_name="books")
publication_date = models.DateField()
number_of_pages = models.IntegerField()
tags = TaggableManager()
search_fields = [
index.SearchField("title", boost=2.0),
index.SearchField("summary"),
index.AutocompleteField("title"),
index.FilterField("title"),
index.FilterField("authors"),
index.RelatedFields("authors", Author.search_fields),
index.FilterField("publication_date"),
index.FilterField("number_of_pages"),
index.RelatedFields(
"tags",
[
index.SearchField("name"),
index.FilterField("slug"),
],
),
index.FilterField("tags"),
]
@classmethod
def get_indexed_objects(cls):
indexed_objects = super().get_indexed_objects()
# Don't index books using Book class that they have a more specific type
if cls is Book:
indexed_objects = indexed_objects.exclude(
id__in=Novel.objects.values_list("book_ptr_id", flat=True)
)
indexed_objects = indexed_objects.exclude(
id__in=ProgrammingGuide.objects.values_list("book_ptr_id", flat=True)
)
# Exclude Books that have the title "Don't index me!"
indexed_objects = indexed_objects.exclude(title="Don't index me!")
return indexed_objects
def get_indexed_instance(self):
# Check if this object is a Novel or ProgrammingGuide and return the specific object
novel = Novel.objects.filter(book_ptr_id=self.id).first()
programming_guide = ProgrammingGuide.objects.filter(book_ptr_id=self.id).first()
# Return the novel/programming guide object if there is one, otherwise return self
return novel or programming_guide or self
def __str__(self):
return self.title
class Character(models.Model):
name = models.CharField(max_length=255)
novel = models.ForeignKey(
"Novel", related_name="characters", on_delete=models.CASCADE
)
def __str__(self):
return self.name
class Novel(Book):
setting = models.CharField(max_length=255)
protagonist = models.OneToOneField(
Character, related_name="+", null=True, on_delete=models.SET_NULL
)
search_fields = Book.search_fields + [
index.SearchField("setting"),
index.AutocompleteField("setting"),
index.RelatedFields(
"characters",
[
index.SearchField("name", boost=0.25),
],
),
index.RelatedFields(
"protagonist",
[
index.SearchField("name", boost=0.5),
index.FilterField("novel"),
],
),
index.FilterField("protagonist"),
]
class ProgrammingGuide(Book):
programming_language = models.CharField(
max_length=255,
choices=[
("py", "Python"),
("js", "JavaScript"),
("rs", "Rust"),
],
)
search_fields = Book.search_fields + [
index.SearchField("get_programming_language_display"),
index.FilterField("programming_language"),
]
class UnindexedBook(index.Indexed, models.Model):
title = models.CharField(max_length=255)
publication_date = models.DateField()
number_of_pages = models.IntegerField()
tags = TaggableManager()
search_fields = []