]> git.0d.be Git - chloro.git/commitdiff
run full text search on a plain text copy of the text attibute
authorFrédéric Péters <fpeters@0d.be>
Thu, 21 Jul 2022 21:05:01 +0000 (23:05 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 21 Jul 2022 21:05:01 +0000 (23:05 +0200)
chloro/phyll/migrations/0004_note_plain_text.py [new file with mode: 0644]
chloro/phyll/migrations/0005_set_plain_text.py [new file with mode: 0644]
chloro/phyll/models.py
chloro/phyll/views.py

diff --git a/chloro/phyll/migrations/0004_note_plain_text.py b/chloro/phyll/migrations/0004_note_plain_text.py
new file mode 100644 (file)
index 0000000..c756cb1
--- /dev/null
@@ -0,0 +1,18 @@
+# Generated by Django 3.2.13 on 2022-07-21 20:57
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('phyll', '0003_note_published'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='note',
+            name='plain_text',
+            field=models.TextField(blank=True, null=True),
+        ),
+    ]
diff --git a/chloro/phyll/migrations/0005_set_plain_text.py b/chloro/phyll/migrations/0005_set_plain_text.py
new file mode 100644 (file)
index 0000000..0e08f4c
--- /dev/null
@@ -0,0 +1,24 @@
+# Generated by Django 3.2.13 on 2022-07-21 20:57
+
+import html
+
+from django.db import migrations
+from django.utils.html import strip_tags
+
+
+def set_plain_text(apps, schema_editor):
+    Note = apps.get_model('phyll', 'Note')
+    for note in Note.objects.all():
+        note.plain_text = html.unescape(strip_tags(note.text))
+        note.save(update_fields=['plain_text'])
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('phyll', '0004_note_plain_text'),
+    ]
+
+    operations = [
+        migrations.RunPython(set_plain_text, reverse_code=migrations.RunPython.noop),
+    ]
index 3723f6b646303503e24e7dc6fbd8a552995e8860..b72bfc509d33e445b12daa1a42093799cc4f3ea3 100644 (file)
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import html
+
 from django.db import models, transaction
+from django.utils.html import strip_tags
 from django.utils.translation import ugettext_lazy as _
 from taggit.managers import TaggableManager
 
@@ -25,6 +28,7 @@ class Note(models.Model):
     title = models.CharField(_('Title'), max_length=150)
     slug = models.SlugField(_('Slug'), max_length=150)
     text = RichTextField(_('Text'), blank=True, null=True)
+    plain_text = models.TextField(blank=True, null=True)
     tags = TaggableManager(_('Tags'), blank=True)
     published = models.BooleanField(_('Published'), default=True)
     creation_timestamp = models.DateTimeField(auto_now_add=True)
@@ -40,3 +44,8 @@ class Note(models.Model):
         if self.tags.filter(name='lang-en').exists():
             return 'en'
         return 'fr'
+
+    def save(self, *args, **kwargs):
+        if kwargs.get('update_fields') is None or 'plain_text' in kwargs.get('update_fields'):
+            self.plain_text = html.unescape(strip_tags(self.text))
+        return super().save(*args, **kwargs)
index 829020cfc5d27b6a5313e00caa4554fadc2406a5..0a7b173c8a5b13d66e596671ef09efba82321d18 100644 (file)
@@ -191,7 +191,7 @@ def ajax_new_page(request, *args, **kwargs):
 
 
 def ajax_search(request, *args, **kwargs):
-    vector = SearchVector('title', weight='A') + SearchVector('text', weight='B')
+    vector = SearchVector('title', weight='A') + SearchVector('plain_text', weight='B')
     query = SearchQuery(request.GET.get('q', ''), config='french')
     results = (
         Note.objects.annotate(rank=SearchRank(vector, query)).filter(rank__gte=0.3).order_by('-rank')[:10]