Skip to content

Commit 2641629

Browse files
committed
✨(backend): add command admin
we want to run the indexing from the admin. in `dmin/core/runindexing/`is a form to do so. Signed-off-by: charles <charles.englebert@protonmail.com>
1 parent 2c12fe7 commit 2641629

4 files changed

Lines changed: 117 additions & 1 deletion

File tree

src/backend/core/admin.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Admin classes and registrations for core app."""
22

3+
from django.conf import settings
34
from django.contrib import admin, messages
45
from django.contrib.auth import admin as auth_admin
5-
from django.shortcuts import redirect
6+
from django.core.management import call_command
7+
from django.shortcuts import redirect, render
68
from django.utils.translation import gettext_lazy as _
79

810
from treebeard.admin import TreeAdmin
@@ -227,3 +229,30 @@ class InvitationAdmin(admin.ModelAdmin):
227229
def save_model(self, request, obj, form, change):
228230
obj.issuer = request.user
229231
obj.save()
232+
233+
234+
@admin.register(models.RunIndexing)
235+
class RunIndexingAdmin(admin.ModelAdmin):
236+
"""Admin for running indexing commands."""
237+
238+
def changelist_view(self, request, extra_context=None):
239+
"""Override to avoid querying the database and handle form submission."""
240+
if request.method == "POST":
241+
call_command(
242+
"index",
243+
batch_size=int(request.POST.get("batch_size")),
244+
lower_time_bound=request.POST.get("lower_time_bound"),
245+
upper_time_bound=request.POST.get("upper_time_bound"),
246+
crash_safe_mode=request.POST.get("crash_safe_mode"),
247+
)
248+
messages.success(request, _("Indexing triggered!"))
249+
250+
return render(
251+
request=request,
252+
template_name="runindexing.html",
253+
context={
254+
**self.admin_site.each_context(request),
255+
"title": "Run Indexing Command",
256+
"default_batch_size": settings.SEARCH_INDEXER_BATCH_SIZE,
257+
},
258+
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Generated by Django 5.2.12 on 2026-03-26 16:34
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("core", "0031_clean_onboarding_accesses"),
9+
]
10+
11+
operations = [
12+
migrations.CreateModel(
13+
name="RunIndexing",
14+
fields=[
15+
(
16+
"id",
17+
models.AutoField(
18+
auto_created=True,
19+
primary_key=True,
20+
serialize=False,
21+
verbose_name="ID",
22+
),
23+
),
24+
],
25+
options={
26+
"verbose_name": "Run Indexing",
27+
"managed": False,
28+
},
29+
),
30+
]

src/backend/core/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,3 +2029,17 @@ def get_abilities(self, user):
20292029
"partial_update": is_admin_or_owner,
20302030
"retrieve": is_admin_or_owner,
20312031
}
2032+
2033+
2034+
class RunIndexing(models.Model):
2035+
"""Proxy model for indexing management in admin."""
2036+
2037+
class Meta:
2038+
"""Meta options."""
2039+
2040+
managed = False
2041+
verbose_name = _("Run Indexing")
2042+
2043+
def __str__(self):
2044+
"""String representation."""
2045+
return self.Meta.verbose_name
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{% extends "admin/base_site.html" %}
2+
{% load i18n %}
3+
4+
{% block content %}
5+
6+
<form method="POST" >
7+
{% csrf_token %}
8+
9+
<hr style="margin-bottom: 10px;">
10+
11+
<p>
12+
This command triggers the indexing of all documents within the specified time bound.
13+
</p>
14+
<p>
15+
See <b>docs/commands/index.md</b> for more details.
16+
</p>
17+
18+
<hr style="margin-bottom: 20px;">
19+
20+
<div>
21+
<label for="batch_size" style="margin-right: 10px">Batch size</label>
22+
<input id="batch_size" type="number" name="batch_size" value={{ default_batch_size }} style="width: 80px;" >
23+
</div>
24+
25+
<div>
26+
<label for="lower_time_bound" style="margin-right: 10px">Lower time bound (optional)</label>
27+
<input id="lower_time_bound" type="datetime-local" name="lower_time_bound" >
28+
</div>
29+
30+
<div>
31+
<label for="upper_time_bound" style="margin-right: 10px">Upper time bound (optional)</label>
32+
<input id="upper_time_bound" type="datetime-local" name="upper_time_bound" >
33+
</div>
34+
35+
<div>
36+
<label for="crash_safe_mode" style="margin-right: 10px">Crash save mode</label>
37+
<input id="crash_safe_mode" type="checkbox" name="crash_safe_mode" value="true" checked>
38+
</div>
39+
40+
<input type="submit" value="{% translate 'Run Indexing' %}" style="margin-top: 20px;">
41+
</form>
42+
43+
{% endblock %}

0 commit comments

Comments
 (0)