diff --git a/app/search/api/views.py b/app/search/api/views.py index 952a655..4e50995 100644 --- a/app/search/api/views.py +++ b/app/search/api/views.py @@ -7,6 +7,7 @@ from search.api.serializers import HintRequestSerializer from search.api.serializers import SearchSerializer, ResponseSerializer, HintResponseSerializer from search.services.search import process_string +from search.services.hints import get_hints user_response = openapi.Response("search results", ResponseSerializer) hint_response = openapi.Response("hints", HintResponseSerializer) @@ -28,7 +29,7 @@ class HintApi(APIView): serializer.is_valid(raise_exception=True) return Response( { - 'type': 'category', + 'type': get_hints(serializer.data['content']), 'value': serializer.data['content'] }, status=status.HTTP_200_OK diff --git a/app/search/migrations/0002_characteristic_unitcharacteristic_and_more.py b/app/search/migrations/0002_characteristic_unitcharacteristic_and_more.py new file mode 100644 index 0000000..498372b --- /dev/null +++ b/app/search/migrations/0002_characteristic_unitcharacteristic_and_more.py @@ -0,0 +1,63 @@ +# Generated by Django 4.0.8 on 2022-10-21 20:39 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('search', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Characteristic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=250, unique=True, verbose_name='Имя')), + ('value', models.CharField(max_length=250, verbose_name='Значение')), + ], + options={ + 'db_table': 'Characteristic', + }, + ), + migrations.CreateModel( + name='UnitCharacteristic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=250, verbose_name='Имя')), + ('value', models.CharField(max_length=250, verbose_name='Значение')), + ('unit', models.CharField(max_length=250, verbose_name='Размерность')), + ], + options={ + 'db_table': 'UnitCharacteristic', + }, + ), + migrations.RemoveField( + model_name='product', + name='characteristic', + ), + migrations.CreateModel( + name='ProductUnitCharacteristic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('characteristic', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='products', to='search.unitcharacteristic')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='unit_characteristics', to='search.product')), + ], + options={ + 'db_table': 'ProductUnitCharacteristic', + }, + ), + migrations.CreateModel( + name='ProductCharacteristic', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('characteristic', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='products', to='search.characteristic')), + ('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='characteristics', to='search.product')), + ], + options={ + 'db_table': 'ProductCharacteristic', + }, + ), + ] diff --git a/app/search/services/hints.py b/app/search/services/hints.py index 062c71f..d7d7c42 100644 --- a/app/search/services/hints.py +++ b/app/search/services/hints.py @@ -1,8 +1,12 @@ -from search.models import Product, Category +from search.models import Product, Category, Characteristic def get_hints(content: str) -> str: category = 'Unknown' if content in list(map(lambda product: product.name, Product.objects.all())): category = 'Name' + elif content in list(map(lambda category: category.name, Category.objects.all())): + category = 'Category' + elif content in list(map(lambda char: char.value, Characteristic.objects.all())): + category = Characteristic.objects.get(value=content).name return category