✨(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 <[email protected]>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""Admin classes and registrations for core app."""
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import admin, messages
|
||||
from django.contrib.auth import admin as auth_admin
|
||||
from django.shortcuts import redirect
|
||||
from django.core.management import call_command
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from treebeard.admin import TreeAdmin
|
||||
@@ -227,3 +229,36 @@ class InvitationAdmin(admin.ModelAdmin):
|
||||
def save_model(self, request, obj, form, change):
|
||||
obj.issuer = request.user
|
||||
obj.save()
|
||||
|
||||
|
||||
@admin.register(models.RunIndexing)
|
||||
class RunIndexingAdmin(admin.ModelAdmin):
|
||||
"""Admin for running indexing commands."""
|
||||
|
||||
def changelist_view(self, request, extra_context=None):
|
||||
"""Override to avoid querying the database and handle form submission."""
|
||||
if request.method == "POST":
|
||||
try:
|
||||
call_command(
|
||||
"index",
|
||||
batch_size=int(request.POST.get("batch_size")),
|
||||
lower_time_bound=request.POST.get("lower_time_bound"),
|
||||
upper_time_bound=request.POST.get("upper_time_bound"),
|
||||
crash_safe_mode=request.POST.get("crash_safe_mode"),
|
||||
)
|
||||
messages.success(request, _("Indexing triggered!"))
|
||||
except Exception as e:
|
||||
messages.error(
|
||||
request,
|
||||
_("Error running indexing command: %(error)s") % {"error": str(e)},
|
||||
)
|
||||
|
||||
return render(
|
||||
request=request,
|
||||
template_name="runindexing.html",
|
||||
context={
|
||||
**self.admin_site.each_context(request),
|
||||
"title": "Run Indexing Command",
|
||||
"default_batch_size": settings.SEARCH_INDEXER_BATCH_SIZE,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.12 on 2026-03-26 16:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0031_clean_onboarding_accesses'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='RunIndexing',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Run Indexing',
|
||||
'managed': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -2029,3 +2029,13 @@ class Invitation(BaseModel):
|
||||
"partial_update": is_admin_or_owner,
|
||||
"retrieve": is_admin_or_owner,
|
||||
}
|
||||
|
||||
|
||||
class RunIndexing(models.Model):
|
||||
"""Proxy model for indexing management in admin."""
|
||||
|
||||
class Meta:
|
||||
"""Meta options."""
|
||||
|
||||
managed = False
|
||||
verbose_name = _("Run Indexing")
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
{% extends "admin/base_site.html" %}
|
||||
{% load i18n %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<form method="POST" >
|
||||
{% csrf_token %}
|
||||
|
||||
<hr style="margin-bottom: 10px;">
|
||||
|
||||
<p>
|
||||
This command triggers the indexing of all documents within the specified time bound.
|
||||
</p>
|
||||
<p>
|
||||
See <b>docs/commands/index.md</b> for more details.
|
||||
</p>
|
||||
|
||||
<hr style="margin-bottom: 20px;">
|
||||
|
||||
<div>
|
||||
<label for="batch_size" style="margin-right: 10px">Batch size</label>
|
||||
<input id="batch_size" type="number" name="batch_size" value={{ default_batch_size }} style="width: 80px;" >
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="lower_time_bound" style="margin-right: 10px">Lower time bound (optional)</label>
|
||||
<input id="lower_time_bound" type="datetime-local" name="lower_time_bound" >
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="upper_time_bound" style="margin-right: 10px">Upper time bound (optional)</label>
|
||||
<input id="upper_time_bound" type="datetime-local" name="upper_time_bound" >
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="crash_safe_mode" style="margin-right: 10px">Crash save mode</label>
|
||||
<input id="crash_safe_mode" type="checkbox" name="crash_safe_mode" value="true" checked>
|
||||
</div>
|
||||
|
||||
<input type="submit" value="{% translate 'Run Indexing' %}" style="margin-top: 20px;">
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user