Skip to content

Commit a4f6689

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 209622c commit a4f6689

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

src/backend/core/admin.py

Lines changed: 36 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,36 @@ 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+
try:
242+
call_command(
243+
"index",
244+
batch_size=int(request.POST.get("batch_size")),
245+
lower_time_bound=request.POST.get("lower_time_bound"),
246+
upper_time_bound=request.POST.get("upper_time_bound"),
247+
crash_safe_mode=request.POST.get("crash_safe_mode"),
248+
)
249+
messages.success(request, _("Indexing triggered!"))
250+
except Exception as e:
251+
messages.error(
252+
request,
253+
_("Error running indexing command: %(error)s") % {"error": str(e)},
254+
)
255+
256+
return render(
257+
request=request,
258+
template_name="runindexing.html",
259+
context={
260+
**self.admin_site.each_context(request),
261+
"title": "Run Indexing Command",
262+
"default_batch_size": settings.SEARCH_INDEXER_BATCH_SIZE,
263+
},
264+
)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
8+
dependencies = [
9+
('core', '0031_clean_onboarding_accesses'),
10+
]
11+
12+
operations = [
13+
migrations.CreateModel(
14+
name='RunIndexing',
15+
fields=[
16+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
17+
],
18+
options={
19+
'verbose_name': 'Run Indexing',
20+
'managed': False,
21+
},
22+
),
23+
]

src/backend/core/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,3 +2029,13 @@ 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")
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)