Format code with black
This commit is contained in:
committed by
GitHub
parent
0ea8e06f1d
commit
0d58769ace
@@ -96,10 +96,14 @@ async def import_quiz(quiz_id: str, user: User) -> Quiz | str:
|
|||||||
user_id=user.id,
|
user_id=user.id,
|
||||||
questions=json.dumps(quiz_questions),
|
questions=json.dumps(quiz_questions),
|
||||||
)
|
)
|
||||||
meilisearch.index(settings.meilisearch_index).add_documents([{
|
meilisearch.index(settings.meilisearch_index).add_documents(
|
||||||
"id": str(quiz_data.id),
|
[
|
||||||
"title": quiz_data.title,
|
{
|
||||||
"description": quiz_data.description,
|
"id": str(quiz_data.id),
|
||||||
"user": (await User.objects.filter(id=quiz_data.user_id).first()).username,
|
"title": quiz_data.title,
|
||||||
}])
|
"description": quiz_data.description,
|
||||||
|
"user": (await User.objects.filter(id=quiz_data.user_id).first()).username,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
return await quiz_data.save()
|
return await quiz_data.save()
|
||||||
|
|||||||
+20
-12
@@ -30,12 +30,16 @@ async def create_quiz_lol(quiz_input: QuizInput, user: User = Depends(get_curren
|
|||||||
raise HTTPException(status_code=400, detail="image url is not valid")
|
raise HTTPException(status_code=400, detail="image url is not valid")
|
||||||
quiz = Quiz(**quiz_input.dict(), user_id=user.id, id=uuid.uuid4())
|
quiz = Quiz(**quiz_input.dict(), user_id=user.id, id=uuid.uuid4())
|
||||||
await redis.delete("global_quiz_count")
|
await redis.delete("global_quiz_count")
|
||||||
meilisearch.index(settings.meilisearch_index).add_documents([{
|
meilisearch.index(settings.meilisearch_index).add_documents(
|
||||||
"id": str(quiz.id),
|
[
|
||||||
"title": quiz.title,
|
{
|
||||||
"description": quiz.description,
|
"id": str(quiz.id),
|
||||||
"user": (await User.objects.filter(id=quiz.user_id).first()).username,
|
"title": quiz.title,
|
||||||
}])
|
"description": quiz.description,
|
||||||
|
"user": (await User.objects.filter(id=quiz.user_id).first()).username,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
return await quiz.save()
|
return await quiz.save()
|
||||||
|
|
||||||
|
|
||||||
@@ -137,12 +141,16 @@ async def update_quiz(quiz_id: str, quiz_input: QuizInput, user: User = Depends(
|
|||||||
quiz.description = quiz_input.description
|
quiz.description = quiz_input.description
|
||||||
quiz.updated_at = datetime.now()
|
quiz.updated_at = datetime.now()
|
||||||
quiz.questions = quiz_input.dict()["questions"]
|
quiz.questions = quiz_input.dict()["questions"]
|
||||||
meilisearch.index(settings.meilisearch_index).update_documents([{
|
meilisearch.index(settings.meilisearch_index).update_documents(
|
||||||
"id": str(quiz.id),
|
[
|
||||||
"title": quiz.title,
|
{
|
||||||
"description": quiz.description,
|
"id": str(quiz.id),
|
||||||
"user": (await User.objects.filter(id=quiz.user_id).first()).username,
|
"title": quiz.title,
|
||||||
}])
|
"description": quiz.description,
|
||||||
|
"user": (await User.objects.filter(id=quiz.user_id).first()).username,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
return await quiz.update()
|
return await quiz.update()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+37
-24
@@ -26,7 +26,7 @@ class Hit(pydantic.BaseModel):
|
|||||||
title: str
|
title: str
|
||||||
description: str
|
description: str
|
||||||
user: str
|
user: str
|
||||||
formatted: Optional['Hit'] = pydantic.Field(None, alias='_formatted')
|
formatted: Optional["Hit"] = pydantic.Field(None, alias="_formatted")
|
||||||
|
|
||||||
|
|
||||||
class SearchResponse(pydantic.BaseModel):
|
class SearchResponse(pydantic.BaseModel):
|
||||||
@@ -46,7 +46,7 @@ class SearchData(pydantic.BaseModel):
|
|||||||
filter: Optional[str] = None
|
filter: Optional[str] = None
|
||||||
facetsDistribution: Optional[list[str]] = None
|
facetsDistribution: Optional[list[str]] = None
|
||||||
attributesToRetrieve: Optional[list[str]] = ["*"]
|
attributesToRetrieve: Optional[list[str]] = ["*"]
|
||||||
attributesToCrop: Optional[list[str]]= None
|
attributesToCrop: Optional[list[str]] = None
|
||||||
cropLength: Optional[int] = 200
|
cropLength: Optional[int] = 200
|
||||||
attributesToHighlight: Optional[list[str]] = None
|
attributesToHighlight: Optional[list[str]] = None
|
||||||
matches: Optional[bool] = False
|
matches: Optional[bool] = False
|
||||||
@@ -56,31 +56,44 @@ class SearchData(pydantic.BaseModel):
|
|||||||
@router.post("/", response_model=SearchResponse)
|
@router.post("/", response_model=SearchResponse)
|
||||||
async def search(data: SearchData):
|
async def search(data: SearchData):
|
||||||
index = meilisearch.get_index(settings.meilisearch_index)
|
index = meilisearch.get_index(settings.meilisearch_index)
|
||||||
query = index.search(data.q, {
|
query = index.search(
|
||||||
"offset": data.offset,
|
data.q,
|
||||||
"limit": data.limit,
|
{
|
||||||
"filter": data.filter,
|
"offset": data.offset,
|
||||||
"cropLength": data.cropLength,
|
"limit": data.limit,
|
||||||
"matches": data.matches,
|
"filter": data.filter,
|
||||||
"facetsDistribution": data.facetsDistribution,
|
"cropLength": data.cropLength,
|
||||||
"attributesToRetrieve": data.attributesToRetrieve,
|
"matches": data.matches,
|
||||||
"attributesToCrop": data.attributesToCrop,
|
"facetsDistribution": data.facetsDistribution,
|
||||||
"sort": data.sort,
|
"attributesToRetrieve": data.attributesToRetrieve,
|
||||||
"attributesToHighlight": data.attributesToHighlight
|
"attributesToCrop": data.attributesToCrop,
|
||||||
})
|
"sort": data.sort,
|
||||||
|
"attributesToHighlight": data.attributesToHighlight,
|
||||||
|
},
|
||||||
|
)
|
||||||
return SearchResponse(**query)
|
return SearchResponse(**query)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/", response_model=SearchResponse)
|
@router.get("/", response_model=SearchResponse)
|
||||||
async def search_get(q: str, offset: int = 0, limit: int = 20, filter: str | None = None,
|
async def search_get(
|
||||||
cropLength: int = 200, matches: bool = False, attributesToHighlight: Optional[str] = "*"):
|
q: str,
|
||||||
|
offset: int = 0,
|
||||||
|
limit: int = 20,
|
||||||
|
filter: str | None = None,
|
||||||
|
cropLength: int = 200,
|
||||||
|
matches: bool = False,
|
||||||
|
attributesToHighlight: Optional[str] = "*",
|
||||||
|
):
|
||||||
index = meilisearch.get_index(settings.meilisearch_index)
|
index = meilisearch.get_index(settings.meilisearch_index)
|
||||||
query = index.search(q, {
|
query = index.search(
|
||||||
"offset": offset,
|
q,
|
||||||
"limit": limit,
|
{
|
||||||
"filter": filter,
|
"offset": offset,
|
||||||
"cropLength": cropLength,
|
"limit": limit,
|
||||||
"matches": matches,
|
"filter": filter,
|
||||||
"attributesToHighlight": [attributesToHighlight]
|
"cropLength": cropLength,
|
||||||
})
|
"matches": matches,
|
||||||
|
"attributesToHighlight": [attributesToHighlight],
|
||||||
|
},
|
||||||
|
)
|
||||||
return SearchResponse(**query)
|
return SearchResponse(**query)
|
||||||
|
|||||||
+8
-6
@@ -11,12 +11,14 @@ async def __main__():
|
|||||||
meili_data = []
|
meili_data = []
|
||||||
questions = await Quiz.objects.filter(public=True).all()
|
questions = await Quiz.objects.filter(public=True).all()
|
||||||
for question in questions:
|
for question in questions:
|
||||||
meili_data.append({
|
meili_data.append(
|
||||||
"id": str(question.id),
|
{
|
||||||
"title": question.title,
|
"id": str(question.id),
|
||||||
"description": question.description,
|
"title": question.title,
|
||||||
"user": (await User.objects.filter(id=question.user_id).first()).username,
|
"description": question.description,
|
||||||
})
|
"user": (await User.objects.filter(id=question.user_id).first()).username,
|
||||||
|
}
|
||||||
|
)
|
||||||
print(meili_data)
|
print(meili_data)
|
||||||
client = meilisearch.Client(settings.meilisearch_url)
|
client = meilisearch.Client(settings.meilisearch_url)
|
||||||
client.index(settings.meilisearch_index).add_documents(meili_data)
|
client.index(settings.meilisearch_index).add_documents(meili_data)
|
||||||
|
|||||||
Reference in New Issue
Block a user