Skip to content

Commit 03c5801

Browse files
committed
Added is_searchable and made published accept datetime cut-off parameter.
1 parent f3e9c02 commit 03c5801

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

blog/admin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@
1111

1212
@admin.register(Entry)
1313
class EntryAdmin(admin.ModelAdmin):
14-
list_display = ("headline", "pub_date", "is_active", "is_published", "author")
15-
list_filter = ("is_active",)
14+
list_display = (
15+
"headline",
16+
"pub_date",
17+
"is_active",
18+
"is_published",
19+
"is_searchable",
20+
"author",
21+
)
22+
list_filter = ("is_active", "is_searchable")
1623
exclude = ("summary_html", "body_html")
1724
prepopulated_fields = {"slug": ("headline",)}
1825
raw_id_fields = ["social_media_card"]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2 on 2025-07-24 07:21
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('blog', '0005_entry_social_media_card'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='entry',
15+
name='is_searchable',
16+
field=models.BooleanField(help_text='Tick to make this entry allow this entry to appear in the Django documentation search.', null=True),
17+
),
18+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 5.2 on 2025-07-24 07:22
2+
3+
from django.db import migrations
4+
5+
6+
def set_is_searchable(apps, schema_editor):
7+
Entry = apps.get_model("blog", "Entry")
8+
# If this is large, this should be split into batched updates
9+
Entry.objects.filter(is_searchable__isnull=True).update(is_searchable=False)
10+
11+
class Migration(migrations.Migration):
12+
13+
dependencies = [
14+
('blog', '0006_entry_is_searchable'),
15+
]
16+
17+
operations = [
18+
migrations.RunPython(set_is_searchable, reverse_code=migrations.RunPython.noop, elidable=True),
19+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2 on 2025-07-24 07:22
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('blog', '0007_set_is_searchable'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='entry',
15+
name='is_searchable',
16+
field=models.BooleanField(default=False, help_text='Tick to make this entry allow this entry to appear in the Django documentation search.'),
17+
),
18+
]

blog/models.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ def _md_slugify(value, separator):
3131

3232

3333
class EntryQuerySet(models.QuerySet):
34-
def published(self):
35-
return self.active().filter(pub_date__lte=timezone.now())
34+
def published(self, pub_date=None):
35+
if pub_date is None:
36+
pub_date = timezone.now()
37+
return self.active().filter(pub_date__lte=pub_date)
3638

3739
def active(self):
3840
return self.filter(is_active=True)
3941

42+
def searchable(self):
43+
return self.filter(is_searchable=True)
44+
4045

4146
class ContentFormat(models.TextChoices):
4247
REST = "reST", "reStructuredText"
@@ -126,6 +131,13 @@ class Entry(models.Model):
126131
),
127132
default=False,
128133
)
134+
is_searchable = models.BooleanField(
135+
default=False,
136+
help_text=_(
137+
"Tick to make this entry allow this entry to appear in the "
138+
"Django documentation search."
139+
),
140+
)
129141
pub_date = models.DateTimeField(
130142
verbose_name=_("Publication date"),
131143
help_text=_(

blog/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ def test_manager_published(self):
6464
["past active"],
6565
transform=lambda entry: entry.headline,
6666
)
67+
self.assertQuerySetEqual(
68+
Entry.objects.published(self.tomorrow),
69+
["future active", "past active"],
70+
transform=lambda entry: entry.headline,
71+
)
72+
73+
def test_manager_searchable(self):
74+
"""
75+
Make sure that the Entry manager's `searchable` method works
76+
"""
77+
Entry.objects.create(
78+
pub_date=self.yesterday,
79+
is_searchable=False,
80+
headline="not searchable",
81+
slug="a",
82+
)
83+
Entry.objects.create(
84+
pub_date=self.yesterday, is_searchable=True, headline="searchable", slug="b"
85+
)
86+
87+
self.assertQuerySetEqual(
88+
Entry.objects.searchable(),
89+
["searchable"],
90+
transform=lambda entry: entry.headline,
91+
)
6792

6893
def test_docutils_safe(self):
6994
"""

0 commit comments

Comments
 (0)