This commit is contained in:
2023-08-12 12:42:52 +03:00
16 changed files with 177 additions and 166 deletions

View File

@ -191,17 +191,14 @@ def add_points(user_id: int, db: Annotated[Session, Depends(utils.get_db)]):
# Отправляем стихи
@app.get("/api/user/poem") # пока не работает
@app.get("/api/user/poem", response_model=schemas.Poem) # пока не работает
def poems_to_front(db: Annotated[Session, Depends(utils.get_db)]): # db: Annotated[Session, Depends(utils.get_db)]
kolvo_stixov = db.query(models.Poems).count() # пока количество стихотворений = 101
if kolvo_stixov > 1:
rand_id = random.randint(1, kolvo_stixov) # номер стихотворения
poem_json = dict()
poem = db.query(models.Poems).filter(models.Poems.id == rand_id).first()
poem_json = {"id": rand_id, "title": poem.title, "text": poem.text, "author": poem.author}
return poem_json
else:
raise HTTPException(status_code=404, detail="Poems not found")
num_of_poems = db.query(models.Poems).count() # определяем кол-во стихов в бд
rand_id = random.randint(1, num_of_poems) # генерируем номер стихотворения
poem = db.query(models.Poems).filter(models.Poems.id == rand_id).first() # находим стих в бд
if not poem:
raise HTTPException(status_code=404, detail="Poem not found")
return poem
@app.get("/api/trashbox", response_model=List[schemas.TrashboxResponse])

View File

@ -48,6 +48,7 @@ class User(BaseModel):
reg_date: date
disabled: Union[bool, None] = False
items: list[Announcement] = []
points: int
class Config:
orm_mode = True
@ -57,10 +58,14 @@ class UserInDB(User):
hashed_password: str
class Poem(BaseModel):
id: int
title: str
text: str
author: str
class Config:
orm_mode = True
# Для "/api/trashbox"
class TrashboxBase(BaseModel):
Lat: float

View File

@ -42,26 +42,9 @@ def add_poems_to_db(db: Session):
db.refresh(poem)
# close the file
f1.close()
def generate_poem(db: Session):
# генерируем 1 случайное id и выбираем объект бд с этим id
rand_id = random.randint(1, 102)
poem = db.query(models.Poems).filter(models.Poems.id == rand_id).first()
# возвращаем название и текст стихотворения
return {"name": poem.title, "text": poem.poem_text, "author":""} # добавить поле author в Poems
#Вова тестирует получение поэм, Димоны, помогите пж
# def poems_to_front(db: Annotated[Session, Depends(utils.get_db)]):
# kolvo_stixov = 109 # пока количество стихотворений = 101
# rand_id = random.randint(1, kolvo_stixov) # номер стихотворения
# poem_json = dict()
# poem = database.query(models.Poems).filter(models.Poems.id == rand_id).first()
# poem_json = {"title": poem.title, "text": poem.text, "author":poem.author}
# return poem_json
def get_query_results(schema: schemas.SortAnnouncements, db: Annotated[Session, Depends(utils.get_db)]):
def filter_ann(schema: schemas.SortAnnouncements, db: Annotated[Session, Depends(utils.get_db)]):
"""Функция для последовательного применения различных фильтров (через схему SortAnnouncements)"""
res = db.query(models.Announcement)
fields = schema.__dict__ # параметры передоваемой схемы SortAnnouncements (ключи и значения)

View File

@ -88,7 +88,7 @@ async def get_current_user(db: Annotated[Session, Depends(get_db)], token: Annot
if user is None:
raise credentials_exception
return schemas.User(id=user.id, nickname=user.nickname, name=user.name, surname=user.surname,
disabled=user.disabled, items=user.announcements, reg_date=user.reg_date)
disabled=user.disabled, items=user.announcements, reg_date=user.reg_date, points=user.points)
async def get_current_active_user(