forked from mawoka-myblock/ClassQuiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22b51e7
commit d43795b
Showing
9 changed files
with
170 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import json | ||
import re | ||
import uuid | ||
from datetime import datetime | ||
from random import randint | ||
|
||
import pydantic | ||
from fastapi import APIRouter, Depends, HTTPException | ||
from fastapi.responses import JSONResponse | ||
from pydantic import ValidationError | ||
|
||
from classquiz.auth import get_current_user, get_current_user_optional | ||
from classquiz.config import redis, settings, storage, meilisearch | ||
from uuid import UUID | ||
from typing import Optional, List | ||
from classquiz.db.models import Quiz, QuizInput, User, PlayGame | ||
from classquiz.kahoot_importer.import_quiz import import_quiz | ||
|
||
settings = settings() | ||
|
||
router = APIRouter() | ||
|
||
|
||
class Hit(pydantic.BaseModel): | ||
id: UUID | ||
title: str | ||
description: str | ||
user: str | ||
formatted: Optional['Hit'] = pydantic.Field(None, alias='_formatted') | ||
|
||
|
||
class SearchResponse(pydantic.BaseModel): | ||
hits: List[Hit] | list[None] | ||
nbHits: int | ||
exhaustiveNbHits: bool | ||
query: str | ||
limit: int | ||
offset: int | ||
processingTimeMs: int | ||
|
||
|
||
class SearchData(pydantic.BaseModel): | ||
q: str | ||
offset: Optional[int] = 0 | ||
limit: Optional[int] = 20 | ||
filter: Optional[str] = None | ||
facetsDistribution: Optional[list[str]] = None | ||
attributesToRetrieve: Optional[list[str]] = ["*"] | ||
attributesToCrop: Optional[list[str]]= None | ||
cropLength: Optional[int] = 200 | ||
attributesToHighlight: Optional[list[str]] = None | ||
matches: Optional[bool] = False | ||
sort: Optional[list[str]] = None | ||
|
||
|
||
@router.post("/search", response_model=SearchResponse) | ||
async def search(data: SearchData): | ||
index = meilisearch.get_index(settings.meilisearch_index) | ||
query = index.search(data.q, { | ||
"offset": data.offset, | ||
"limit": data.limit, | ||
"filter": data.filter, | ||
"cropLength": data.cropLength, | ||
"matches": data.matches, | ||
"facetsDistribution": data.facetsDistribution, | ||
"attributesToRetrieve": data.attributesToRetrieve, | ||
"attributesToCrop": data.attributesToCrop, | ||
"sort": data.sort, | ||
"attributesToHighlight": data.attributesToHighlight | ||
}) | ||
return query.serialize() | ||
|
||
|
||
@router.get("/search", response_model=SearchResponse) | ||
async def search_get(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) | ||
query = index.search(q, { | ||
"offset": offset, | ||
"limit": limit, | ||
"filter": filter, | ||
"cropLength": cropLength, | ||
"matches": matches, | ||
"attributesToHighlight": [attributesToHighlight] | ||
}) | ||
return SearchResponse(**query) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import meilisearch | ||
from classquiz.db.models import Quiz, User | ||
from pprint import pprint | ||
from classquiz.config import settings | ||
from asyncio import run | ||
|
||
settings = settings() | ||
|
||
|
||
async def __main__(): | ||
meili_data = [] | ||
questions = await Quiz.objects.filter(public=True).all() | ||
for question in questions: | ||
meili_data.append({ | ||
"id": str(question.id), | ||
"title": question.title, | ||
"description": question.description, | ||
"user": (await User.objects.filter(id=question.user_id).first()).username, | ||
}) | ||
print(meili_data) | ||
client = meilisearch.Client(settings.meilisearch_url) | ||
client.index(settings.meilisearch_index).add_documents(meili_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
run(__main__()) |