Merge branch 'main' into autocomplete

This commit is contained in:
Ilia vasilenko 2022-10-22 03:10:11 +03:00 committed by GitHub
commit 4de82a8863
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 133 additions and 23 deletions

2
.gitignore vendored
View File

@ -2,6 +2,8 @@ app/media/
app/static/ app/static/
.idea/ .idea/
product_100_000.json
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -9,6 +9,8 @@ from search.api.serializers import SearchSerializer, ResponseSerializer, HintRes
from search.services.search import process_string from search.services.search import process_string
from search.services.autocomplete_schema import autocomplete_schema from search.services.autocomplete_schema import autocomplete_schema
from search.services.hints import get_hints
user_response = openapi.Response("search results", ResponseSerializer) user_response = openapi.Response("search results", ResponseSerializer)
hint_response = openapi.Response("hints", HintResponseSerializer) hint_response = openapi.Response("hints", HintResponseSerializer)
autocomplete_response = openapi.Response("autocomplete schema", AutoCompleteResponseSerializer) autocomplete_response = openapi.Response("autocomplete schema", AutoCompleteResponseSerializer)
@ -31,7 +33,7 @@ class HintApi(APIView):
serializer.is_valid(raise_exception=True) serializer.is_valid(raise_exception=True)
return Response( return Response(
{ {
'type': 'category', 'type': get_hints(serializer.data['content']),
'value': serializer.data['content'] 'value': serializer.data['content']
}, },
status=status.HTTP_200_OK status=status.HTTP_200_OK

View File

@ -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',
},
),
]

View File

@ -2,8 +2,8 @@ from django.db import models
class Characteristic(models.Model): class Characteristic(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250) name = models.TextField("Имя", blank=False)
value = models.CharField("Значение", blank=False, max_length=250) value = models.TextField("Значение", blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -12,13 +12,13 @@ class Characteristic(models.Model):
return {"name": self.name, "value": self.value} return {"name": self.name, "value": self.value}
class Meta: class Meta:
db_table = "Characteristic" db_table = "characteristic"
class UnitCharacteristic(models.Model): class UnitCharacteristic(models.Model):
name = models.CharField("Имя", blank=False, max_length=250) name = models.TextField("Имя", blank=False)
value = models.CharField("Значение", blank=False, max_length=250) value = models.TextField("Значение", blank=False)
unit = models.CharField("Размерность", blank=False, max_length=250) unit = models.TextField("Размерность", blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
@ -27,24 +27,24 @@ class UnitCharacteristic(models.Model):
return {"name": self.name, "value": self.value, "unit": self.unit} return {"name": self.name, "value": self.value, "unit": self.unit}
class Meta: class Meta:
db_table = "UnitCharacteristic" db_table = "unit_characteristic"
class Category(models.Model): class Category(models.Model):
name = models.CharField("Имя", unique=True, blank=False, max_length=250) name = models.TextField("Имя", unique=True, blank=False)
def __str__(self): def __str__(self):
return str(self.name) return str(self.name)
class Meta: class Meta:
db_table = "Category" db_table = "category"
class Product(models.Model): class Product(models.Model):
id = models.IntegerField( id = models.IntegerField(
"ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True "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 = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE Category, related_name="products", on_delete=models.CASCADE
) )
@ -62,7 +62,7 @@ class Product(models.Model):
} }
class Meta: class Meta:
db_table = "Product" db_table = "product"
class ProductCharacteristic(models.Model): class ProductCharacteristic(models.Model):
@ -77,7 +77,7 @@ class ProductCharacteristic(models.Model):
return f"{self.product} in {self.characteristic}" return f"{self.product} in {self.characteristic}"
class Meta: class Meta:
db_table = "ProductCharacteristic" db_table = "product_characteristic"
class ProductUnitCharacteristic(models.Model): class ProductUnitCharacteristic(models.Model):
@ -92,4 +92,4 @@ class ProductUnitCharacteristic(models.Model):
return f"{self.product} in {self.characteristic}" return f"{self.product} in {self.characteristic}"
class Meta: class Meta:
db_table = "ProductUnitCharacteristic" db_table = "product_unit_characteristic"

View File

@ -1,8 +1,12 @@
from search.models import Product, Category from search.models import Product, Category, Characteristic
def get_hints(content: str) -> str: def get_hints(content: str) -> str:
category = 'Unknown' category = 'Unknown'
if content in list(map(lambda product: product.name, Product.objects.all())): if content in list(map(lambda product: product.name, Product.objects.all())):
category = 'Name' 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 return category

View File

@ -2,7 +2,14 @@ from ast import literal_eval
import pandas as pd import pandas as pd
from search.models import Product, Category from search.models import (
Product,
Category,
UnitCharacteristic,
ProductUnitCharacteristic,
ProductCharacteristic,
Characteristic,
)
def load(): def load():
@ -15,11 +22,40 @@ def load():
def load_excel(): 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]): for row in range(df.shape[0]):
product = Product.objects.get_or_create(id=df.iat[row, 0]) try:
product.name = df.iat[row, 1] print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2])
product.name = Category.objects.get_or_create(name=df.iat[row, 2]) if Product.objects.filter(id=df.iat[row, 0]).exists():
for cat in df.iat[row, 4]: Product.objects.filter(id=df.iat[row, 0]).delete()
pass 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

3
pylintrc Normal file
View File

@ -0,0 +1,3 @@
"python.linting.pylintArgs": [
"--load-plugins=pylint_django"
],