diff --git a/.gitignore b/.gitignore index 4de1b5a..51c51f6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ app/media/ app/static/ .idea/ +product_100_000.json + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/app/search/api/views.py b/app/search/api/views.py index 0c215c6..81e5993 100644 --- a/app/search/api/views.py +++ b/app/search/api/views.py @@ -9,6 +9,8 @@ from search.api.serializers import SearchSerializer, ResponseSerializer, HintRes from search.services.search import process_string from search.services.autocomplete_schema import autocomplete_schema +from search.services.hints import get_hints + user_response = openapi.Response("search results", ResponseSerializer) hint_response = openapi.Response("hints", HintResponseSerializer) autocomplete_response = openapi.Response("autocomplete schema", AutoCompleteResponseSerializer) @@ -31,7 +33,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/models.py b/app/search/models.py index 58ef2eb..6dc7e69 100644 --- a/app/search/models.py +++ b/app/search/models.py @@ -2,8 +2,8 @@ from django.db import models class Characteristic(models.Model): - name = models.CharField("Имя", unique=True, blank=False, max_length=250) - value = models.CharField("Значение", blank=False, max_length=250) + name = models.TextField("Имя", blank=False) + value = models.TextField("Значение", blank=False) def __str__(self): return str(self.name) @@ -12,13 +12,13 @@ class Characteristic(models.Model): return {"name": self.name, "value": self.value} class Meta: - db_table = "Characteristic" + db_table = "characteristic" class UnitCharacteristic(models.Model): - name = models.CharField("Имя", blank=False, max_length=250) - value = models.CharField("Значение", blank=False, max_length=250) - unit = models.CharField("Размерность", blank=False, max_length=250) + name = models.TextField("Имя", blank=False) + value = models.TextField("Значение", blank=False) + unit = models.TextField("Размерность", blank=False) def __str__(self): return str(self.name) @@ -27,24 +27,24 @@ class UnitCharacteristic(models.Model): return {"name": self.name, "value": self.value, "unit": self.unit} class Meta: - db_table = "UnitCharacteristic" + db_table = "unit_characteristic" class Category(models.Model): - name = models.CharField("Имя", unique=True, blank=False, max_length=250) + name = models.TextField("Имя", unique=True, blank=False) def __str__(self): return str(self.name) class Meta: - db_table = "Category" + db_table = "category" class Product(models.Model): id = models.IntegerField( "ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True ) - name = models.CharField("Название CTE", unique=True, blank=False, max_length=250) + name = models.TextField("Название CTE", unique=True, blank=False) category = models.ForeignKey( Category, related_name="products", on_delete=models.CASCADE ) @@ -62,7 +62,7 @@ class Product(models.Model): } class Meta: - db_table = "Product" + db_table = "product" class ProductCharacteristic(models.Model): @@ -77,7 +77,7 @@ class ProductCharacteristic(models.Model): return f"{self.product} in {self.characteristic}" class Meta: - db_table = "ProductCharacteristic" + db_table = "product_characteristic" class ProductUnitCharacteristic(models.Model): @@ -92,4 +92,4 @@ class ProductUnitCharacteristic(models.Model): return f"{self.product} in {self.characteristic}" class Meta: - db_table = "ProductUnitCharacteristic" + db_table = "product_unit_characteristic" 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 diff --git a/app/search/services/load_products.py b/app/search/services/load_products.py index 363b953..c28f54c 100644 --- a/app/search/services/load_products.py +++ b/app/search/services/load_products.py @@ -2,7 +2,14 @@ from ast import literal_eval import pandas as pd -from search.models import Product, Category +from search.models import ( + Product, + Category, + UnitCharacteristic, + ProductUnitCharacteristic, + ProductCharacteristic, + Characteristic, +) def load(): @@ -15,11 +22,40 @@ def load(): def load_excel(): - df = pd.read_excel("data1.xlsx", sheet_name="Sheet1") + df = pd.read_excel("data.xlsx", sheet_name="Запрос1") for row in range(df.shape[0]): - product = Product.objects.get_or_create(id=df.iat[row, 0]) - product.name = df.iat[row, 1] - product.name = Category.objects.get_or_create(name=df.iat[row, 2]) - for cat in df.iat[row, 4]: - pass - + try: + print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2]) + if Product.objects.filter(id=df.iat[row, 0]).exists(): + Product.objects.filter(id=df.iat[row, 0]).delete() + product = Product(id=df.iat[row, 0]) + product.name = df.iat[row, 1] + category = Category.objects.get_or_create(name=df.iat[row, 2])[0] + product.category = category + product.save() + if df.iat[row, 4]: + for cat in literal_eval(df.iat[row, 4]): + try: + if "Unit" in cat: + ProductUnitCharacteristic.objects.get_or_create( + characteristic=UnitCharacteristic.objects.get_or_create( + name=cat["Name"], + value=cat["Value"], + unit=cat["Unit"], + )[0], + product=product, + ) + else: + ProductCharacteristic.objects.get_or_create( + characteristic=Characteristic.objects.get_or_create( + name=cat["Name"], value=cat["Value"] + )[0], + product=product, + ) + except KeyError: + # Empty Value + continue + except BaseException: + # malformed node or string: nan \ duplicate key + print("СКОРОСШИВАТЕЛЬ") + continue diff --git a/pylintrc b/pylintrc new file mode 100644 index 0000000..84cb059 --- /dev/null +++ b/pylintrc @@ -0,0 +1,3 @@ +"python.linting.pylintArgs": [ + "--load-plugins=pylint_django" +],