From 62d0381f91d1db3d6a38bc18f98fcecc6a36bb3d Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Tue, 27 Feb 2018 19:56:02 +0100
Subject: [PATCH] Fixed #81: Search now unaccent letters for queries

---
 api/config/settings/common.py                 |  1 +
 .../common/migrations/0001_initial.py         | 12 +++++++++
 .../common/migrations/__init__.py             |  0
 api/funkwhale_api/music/views.py              | 25 ++++++++++++-------
 changes/changelog.d/81.feature                |  1 +
 5 files changed, 30 insertions(+), 9 deletions(-)
 create mode 100644 api/funkwhale_api/common/migrations/0001_initial.py
 create mode 100644 api/funkwhale_api/common/migrations/__init__.py
 create mode 100644 changes/changelog.d/81.feature

diff --git a/api/config/settings/common.py b/api/config/settings/common.py
index 491babdd..f5ddec00 100644
--- a/api/config/settings/common.py
+++ b/api/config/settings/common.py
@@ -37,6 +37,7 @@ DJANGO_APPS = (
     'django.contrib.sites',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+    'django.contrib.postgres',
 
     # Useful template tags:
     # 'django.contrib.humanize',
diff --git a/api/funkwhale_api/common/migrations/0001_initial.py b/api/funkwhale_api/common/migrations/0001_initial.py
new file mode 100644
index 00000000..e95cc11e
--- /dev/null
+++ b/api/funkwhale_api/common/migrations/0001_initial.py
@@ -0,0 +1,12 @@
+# Generated by Django 2.0.2 on 2018-02-27 18:43
+from django.db import migrations
+from django.contrib.postgres.operations import UnaccentExtension
+
+
+class Migration(migrations.Migration):
+
+    dependencies = []
+
+    operations = [
+        UnaccentExtension()
+    ]
diff --git a/api/funkwhale_api/common/migrations/__init__.py b/api/funkwhale_api/common/migrations/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/api/funkwhale_api/music/views.py b/api/funkwhale_api/music/views.py
index ac76667d..d026c984 100644
--- a/api/funkwhale_api/music/views.py
+++ b/api/funkwhale_api/music/views.py
@@ -63,7 +63,7 @@ class ArtistViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet):
                                 'albums__tracks__tags'))
     serializer_class = serializers.ArtistSerializerNested
     permission_classes = [ConditionalAuthentication]
-    search_fields = ['name']
+    search_fields = ['name__unaccent']
     filter_class = filters.ArtistFilter
     ordering_fields = ('id', 'name', 'creation_date')
 
@@ -76,7 +76,7 @@ class AlbumViewSet(SearchMixin, viewsets.ReadOnlyModelViewSet):
                                               'tracks__files'))
     serializer_class = serializers.AlbumSerializerNested
     permission_classes = [ConditionalAuthentication]
-    search_fields = ['title']
+    search_fields = ['title__unaccent']
     ordering_fields = ('creation_date',)
 
 
@@ -133,9 +133,9 @@ class TrackViewSet(TagViewSetMixin, SearchMixin, viewsets.ReadOnlyModelViewSet):
     search_fields = ['title', 'artist__name']
     ordering_fields = (
         'creation_date',
-        'title',
-        'album__title',
-        'artist__name',
+        'title__unaccent',
+        'album__title__unaccent',
+        'artist__name__unaccent',
     )
 
     def get_queryset(self):
@@ -249,7 +249,11 @@ class Search(views.APIView):
         return Response(results, status=200)
 
     def get_tracks(self, query):
-        search_fields = ['mbid', 'title', 'album__title', 'artist__name']
+        search_fields = [
+            'mbid',
+            'title__unaccent',
+            'album__title__unaccent',
+            'artist__name__unaccent']
         query_obj = utils.get_query(query, search_fields)
         return (
             models.Track.objects.all()
@@ -263,7 +267,10 @@ class Search(views.APIView):
 
 
     def get_albums(self, query):
-        search_fields = ['mbid', 'title', 'artist__name']
+        search_fields = [
+            'mbid',
+            'title__unaccent',
+            'artist__name__unaccent']
         query_obj = utils.get_query(query, search_fields)
         return (
             models.Album.objects.all()
@@ -277,7 +284,7 @@ class Search(views.APIView):
 
 
     def get_artists(self, query):
-        search_fields = ['mbid', 'name']
+        search_fields = ['mbid', 'name__unaccent']
         query_obj = utils.get_query(query, search_fields)
         return (
             models.Artist.objects.all()
@@ -292,7 +299,7 @@ class Search(views.APIView):
 
 
     def get_tags(self, query):
-        search_fields = ['slug', 'name']
+        search_fields = ['slug', 'name__unaccent']
         query_obj = utils.get_query(query, search_fields)
 
         # We want the shortest tag first
diff --git a/changes/changelog.d/81.feature b/changes/changelog.d/81.feature
new file mode 100644
index 00000000..56c44d3c
--- /dev/null
+++ b/changes/changelog.d/81.feature
@@ -0,0 +1 @@
+Search now unaccent letters for queries like "The Dø" or "Björk", yielding more results (#81)
-- 
GitLab