diff --git a/app/search/admin.py b/app/search/admin.py index 8c38f3f..db4b65c 100644 --- a/app/search/admin.py +++ b/app/search/admin.py @@ -1,3 +1,20 @@ from django.contrib import admin +from search.models import ( + Characteristic, + UnitCharacteristic, + Category, + Product, + ProductCharacteristic, + ProductUnitCharacteristic, +) + # Register your models here. + + +admin.site.register(Characteristic) +admin.site.register(UnitCharacteristic) +admin.site.register(Category) +admin.site.register(Product) +admin.site.register(ProductCharacteristic) +admin.site.register(ProductUnitCharacteristic) diff --git a/app/search/migrations/0012_alter_product_name.py b/app/search/migrations/0012_alter_product_name.py new file mode 100644 index 0000000..4e15864 --- /dev/null +++ b/app/search/migrations/0012_alter_product_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.8 on 2022-10-22 16:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('search', '0011_alter_product_options'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='name', + field=models.TextField(verbose_name='Название CTE'), + ), + ] diff --git a/app/search/services/autocomplete_schema.py b/app/search/services/autocomplete_schema.py index b2be1c4..96ae0f3 100644 --- a/app/search/services/autocomplete_schema.py +++ b/app/search/services/autocomplete_schema.py @@ -12,49 +12,67 @@ def autocomplete_schema(val: str, exclude: List[Dict]): schema.extend( [ { - "coordinate": cat["name"].replace("ё", "е").lower().index(val.lower()), + "coordinate": cat["name"] + .replace("ё", "е") + .lower() + .index(val.lower()), "value": {"type": "Category", "value": cat["name"]}, } - for cat in Category.objects.filter(name__unaccent__icontains=val)[ - :10 - ].values("name") + for cat in ( + Category.objects.filter(name__unaccent__istartswith=val) + | Category.objects.filter(name__unaccent__icontains=val) + ) + .distinct()[:10] + .values("name") ] ) if not name_exclude: schema.extend( [ { - "coordinate": product["name"].replace("ё", "е").lower().index(val.lower()), + "coordinate": product["name"] + .replace("ё", "е") + .lower() + .index(val.lower()), "value": { "type": "Name", "value": product["name"], }, } - for product in Product.objects.filter(name__unaccent__icontains=val)[ - :30 - ].values("name") + for product in ( + Product.objects.filter(name__unaccent__istartswith=val) + | Product.objects.filter(name__unaccent__icontains=val) + ) + .distinct()[:30] + .values("name") ] ) schema.extend( [ { - "coordinate": char["value"].replace("ё", "е").lower().index(val.lower()), + "coordinate": char["value"] + .replace("ё", "е") + .lower() + .index(val.lower()), "value": {"type": char["name"], "value": char["value"]}, } - for char in Characteristic.objects.filter(value__unaccent__icontains=val)[ - :20 - ].values("name", "value") + for char in ( + Characteristic.objects.filter(value__unaccent__istartswith=val) + | Characteristic.objects.filter(value__unaccent__icontains=val) + ) + .distinct()[:20] + .values("name", "value") ] ) schema.extend( [ { "coordinate": char["name"].lower().replace("ё", "е").index(val.lower()), - "value": { - "type": char["name"] + "_numeric", - "value": char["name"] - } - } for char in UnitCharacteristic.objects.filter(name__unaccent__icontains=val)[:20].values("name", "value") + "value": {"type": char["name"] + "_numeric", "value": char["name"]}, + } + for char in UnitCharacteristic.objects.filter( + name__unaccent__icontains=val + )[:20].values("name", "value") ] ) return schema diff --git a/app/search/tests.py b/app/search/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/app/search/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/app/search/views.py b/app/search/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/app/search/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here.