Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/endpoints tariff #57

Merged
merged 55 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
884eba7
merge feat/test_auth
Jan 20, 2024
dc1c710
resolve profile.py
Jan 20, 2024
ba91414
tests profile
Jan 20, 2024
fc7629d
fix gitignore, test.fixtures.user
Jan 24, 2024
0b414f2
Фикстура для создания юзеров с профилями гернерит 5 юзеров
Jan 24, 2024
fc9c8e2
тест фильтрации
Jan 24, 2024
a363ac8
тест пагинации
Jan 24, 2024
afae6f4
Тест получения своего профиля
Jan 24, 2024
4481485
тест запрета получения чужого профиля обычным юзером
Jan 24, 2024
6d1e2e3
тест получения фото своего профиля
Jan 24, 2024
2a4c024
вынес получение юзера в отдельную функцию
Jan 24, 2024
24d418e
refactoring
Jan 24, 2024
879aae1
тест апдейта профиля
Jan 25, 2024
9e084c3
Добавил тесты
Jan 25, 2024
ead1366
test udate photo
Jan 28, 2024
5afb149
isort
Jan 28, 2024
0e4842d
gitignore и пустой метод get для получения группы по id
Jan 28, 2024
da5e065
resolved
Jan 28, 2024
35d5af5
добавил тесты на создание группы суперюзером и запрет для юзера
Jan 28, 2024
7a023c4
эндпоинты, круд, тесты
Jan 29, 2024
204482f
Эндпоинты, круд, модели, тесты
Jan 29, 2024
587c5c8
refactoring, isort
Jan 29, 2024
b658d8b
flake8
Jan 29, 2024
3bd713c
isort tests
Jan 29, 2024
4a55710
test create achievements by supruser, fix status code in response
Jan 29, 2024
bca4e5f
тест создания ачивмент с пропущенными данными
Jan 29, 2024
b504d47
тест запрета создания ачивмент юзером
Jan 29, 2024
30643a1
тест запрета создания ачивмент неавторизованным
Jan 29, 2024
46b3a66
test get all achievements superuser
Jan 29, 2024
8da75b4
endpoints, tests
Jan 29, 2024
2a49ebe
endpoints, crud for achievement
Jan 30, 2024
b166e52
тест получения юзером своих ачивментс
Jan 30, 2024
c4c94a7
endpoints, crud, tests for achievement
Jan 30, 2024
b7862d0
добавил тесты запрета удаления ачивмент юзером и неавтризованным
Jan 30, 2024
e9e7f70
добавил тест удаления ачивмент суперюзером
Jan 30, 2024
771eee7
добавил тесты запрета апдейта ачивмент юзером и неавторизованным
Jan 30, 2024
5c24126
endpoints, schemas, tests for achievement
Jan 30, 2024
1321f84
isort
Jan 30, 2024
7e5c9de
endpoints, tests for tariff
Jan 30, 2024
b8bcf6c
добавил фикстуру тарифов
Jan 30, 2024
0db71c8
endpoints, tests for tariff
Jan 30, 2024
ef55cf1
endpoints, tests for tariff
Jan 30, 2024
29c3e8f
validators. endpoints, schemas, tests for tariff
Jan 30, 2024
7fb9f90
endpoints, tests for tariff delete
Jan 30, 2024
ea0d74c
isort
Jan 30, 2024
fc0098d
рефакторинг
Jan 30, 2024
1988797
resolve
Jan 30, 2024
5bb1292
refactoring
Jan 30, 2024
e8f0910
pep8
Jan 30, 2024
496f6f2
Merge branch 'feat/endpoints_group' into feat/test_achievements
Jan 30, 2024
fd61fa5
refactoring
Jan 30, 2024
c906470
Merge branch 'feat/test_achievements' into feat/endpoints_tariff
Jan 30, 2024
6b6ecaf
refactoring
Jan 30, 2024
424065d
refactoring
Jan 30, 2024
b53f571
isort
Jan 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
test.db
test.db-journal

.vscode/
*.jpg
*.PNG
infra/.env
test.db

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
71 changes: 66 additions & 5 deletions app/api/endpoints/achievement.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate
from app.core.db import get_async_session
from app.core.user import current_superuser, current_user
from app.crud import achievement_crud
from app.schemas.achievement import AchievementCreate, AchievementRead
from app.models import Achievement, User
from app.schemas.achievement import (AchievementCreate, AchievementRead,
AchievementUpdate)
from app.services.endpoints_services import delete_obj

router = APIRouter()
Expand All @@ -14,7 +16,7 @@
@router.get(
"/",
response_model=list[AchievementRead],
dependencies=[Depends(current_user)]
dependencies=[Depends(current_superuser)]
)
async def get_all_achievements(
session: AsyncSession = Depends(get_async_session),
Expand All @@ -23,10 +25,51 @@ async def get_all_achievements(
return await achievement_crud.get_multi(session)


@router.get(
'/me',
response_model=list[AchievementRead],
dependencies=[Depends(current_user)]
)
async def get_self_achievements(
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session)
):
"""Возвращает ачивментс юзера."""
return await achievement_crud.get_users_obj(user.id, session)


@router.get(
'/me/{achievement_id}',
response_model=AchievementRead,
dependencies=[Depends(current_user)]
)
async def get_self_achievement_by_id(
achievement_id: int,
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session)
):
"""Возвращает ачивмент юзера по id."""
achievement: Achievement = await achievement_crud.get(
achievement_id, session
)
if achievement is None:
raise HTTPException(
status_code=404,
detail='Achievement не существует.'
)
if user.id not in [_.id for _ in achievement.profiles]:
raise HTTPException(
status_code=403,
detail='У выс нет этого achievement.'
)
return achievement


@router.post(
"/",
response_model=AchievementRead,
dependencies=[Depends(current_superuser)]
dependencies=[Depends(current_superuser)],
status_code=status.HTTP_201_CREATED
)
async def create_achievement(
achievement: AchievementCreate,
Expand All @@ -37,7 +80,25 @@ async def create_achievement(
return await achievement_crud.create(obj_in=achievement, session=session)


@router.delete("/{obj_id}", dependencies=[Depends(current_superuser)])
@router.patch(
'/{achievement_id}',
response_model=AchievementRead,
dependencies=[Depends(current_superuser)]
)
async def update_achievement(
achievement_id: int,
data: AchievementUpdate,
session: AsyncSession = Depends(get_async_session)
):
"""Апдейт ачивмент."""
_achievement = await achievement_crud.get(achievement_id, session)
return await achievement_crud.update(
_achievement, data, session
)


@router.delete("/{obj_id}", dependencies=[Depends(current_superuser)],
status_code=status.HTTP_204_NO_CONTENT)
async def delete_achievement(
obj_id: int,
session: AsyncSession = Depends(get_async_session),
Expand Down
79 changes: 74 additions & 5 deletions app/api/endpoints/group.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate
from app.core.db import get_async_session
from app.core.user import current_superuser, current_user
from app.crud import group_crud
from app.schemas.group import GroupCreate, GroupRead
from app.models import Group, User
from app.schemas.group import GroupCreate, GroupRead, GroupUpdate
from app.services.endpoints_services import delete_obj

router = APIRouter()
Expand All @@ -14,7 +15,7 @@
@router.get(
"/",
response_model=list[GroupRead],
dependencies=[Depends(current_user)]
dependencies=[Depends(current_superuser)]
)
async def get_all_groups(
session: AsyncSession = Depends(get_async_session),
Expand All @@ -23,10 +24,62 @@ async def get_all_groups(
return await group_crud.get_multi(session)


@router.get(
'/me',
response_model=list[GroupRead],
dependencies=[Depends(current_user)]
)
async def get_self_groups(
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session)
):
"""Получение групп юзером."""
return await group_crud.get_users_obj(user.id, session)


@router.get(
'/me/{group_id}',
response_model=GroupRead,
dependencies=[Depends(current_user)]
)
async def get_self_group_by_id(
group_id: int,
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session)
):
"""Получение группы по id юзером."""
group: Group | None = await group_crud.get(group_id, session)
if group is None:
raise HTTPException(
status_code=404,
detail='Такой группы не существует.'
)
if user not in group.users:
raise HTTPException(
status_code=403,
detail='Вы не состоите в этой группе.'
)
return group


@router.get(
'/{group_id}',
response_model=GroupRead,
dependencies=[Depends(current_superuser)]
)
async def get_group(
group_id: int,
session: AsyncSession = Depends(get_async_session)
):
"""Получение группы по id"""
return await group_crud.get(group_id, session)


@router.post(
"/",
response_model=GroupRead,
dependencies=[Depends(current_superuser)]
dependencies=[Depends(current_superuser)],
status_code=201
)
async def create_group(
group: GroupCreate, session: AsyncSession = Depends(get_async_session)
Expand All @@ -36,9 +89,25 @@ async def create_group(
return await group_crud.create(obj_in=group, session=session)


@router.patch(
'/{group_id}',
dependencies=[Depends(current_superuser)],
response_model=GroupRead
)
async def update_group(
group_id: int,
group: GroupUpdate,
session: AsyncSession = Depends(get_async_session)
):
"""Апдейт группы."""
_group = await group_crud.get(group_id, session)
return await group_crud.update(_group, group, session)


@router.delete(
"/{obj_id}",
dependencies=[Depends(current_superuser)]
dependencies=[Depends(current_superuser)],
status_code=204
)
async def delete_group(
obj_id: int,
Expand Down
18 changes: 12 additions & 6 deletions app/api/endpoints/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ async def get_current_user_profile(
)


@router.get(
'/me//photo',
dependencies=[Depends(current_user)]
)
@router.get('/{profile_id}', response_model=ProfileRead,
dependencies=[Depends(current_superuser)])
async def get_profile(
profile_id: int,
session: AsyncSession = Depends(get_async_session)
):
return await profile_crud.get(profile_id, session)


@router.get('/me/photo', dependencies=[Depends(current_user)])
async def get_user_photo(
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session)
Expand All @@ -76,7 +82,7 @@ async def update_profile(


@router.patch(
'/me//update_photo',
'/me/update_photo',
response_model=ProfileRead,
dependencies=[Depends(current_user)]
)
Expand Down Expand Up @@ -109,7 +115,7 @@ def create_profile():


@router.delete('/{obj_id}', deprecated=True)
def delete_profile(obg_id: str):
def delete_profile():
"""Удалить объект"""
raise HTTPException(
status_code=status.HTTP_405_METHOD_NOT_ALLOWED,
Expand Down
73 changes: 51 additions & 22 deletions app/api/endpoints/tariff.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.validators import check_name_duplicate, check_obj_exists
from app.core.db import get_async_session
from app.core.user import current_superuser, current_user
from app.crud import tariff_crud, user_crud
from app.models import User
from app.schemas.tariff import TariffCreate, TariffRead
from app.schemas.user import UserRead
from app.core.user import current_superuser
from app.crud import tariff_crud
from app.schemas.tariff import TariffCreate, TariffRead, TariffUpdate
from app.services.endpoints_services import delete_obj

router = APIRouter()
Expand All @@ -16,7 +14,6 @@
@router.get(
"/",
response_model=list[TariffRead],
dependencies=[Depends(current_user)]
)
async def get_all_tariffs(
session: AsyncSession = Depends(get_async_session),
Expand All @@ -25,28 +22,59 @@ async def get_all_tariffs(
return await tariff_crud.get_multi(session)


@router.put(
"/",
response_model=UserRead,
dependencies=[Depends(current_user)]
@router.get(
'/{tariff_id}',
response_model=TariffRead
)
async def update_users_tariff(
async def get_tariff(
tariff_id: int,
user: User = Depends(current_user),
session: AsyncSession = Depends(get_async_session),
) -> UserRead:
"""Привязывает тариф к юзеру"""
await check_obj_exists(tariff_id, tariff_crud, session)
user = await user_crud.update_id(
session=session, db_obj=user, field="tariff_id", field_value=tariff_id
session: AsyncSession = Depends(get_async_session)
):
return await tariff_crud.get(tariff_id, session)


@router.patch(
'/{tariff_id}',
response_model=TariffRead,
dependencies=[Depends(current_superuser)]
)
async def update_tariff(
tariff_id: int,
data: TariffUpdate,
session: AsyncSession = Depends(get_async_session)
):
"""Апдейт тарифа."""
_tariff = await check_obj_exists(tariff_id, tariff_crud, session)
return await tariff_crud.update(
_tariff, data, session
)
return user


# Перенести в эндпоинты юзера?
# @router.patch(
# "/{tariff_id}",
# response_model=UserRead,
# dependencies=[Depends(current_user)]
# )
# async def update_users_tariff(
# tariff_id: int,
# user: User = Depends(current_user),
# session: AsyncSession = Depends(get_async_session),
# ) -> UserRead:
# """Привязывает тариф к юзеру"""
# await check_obj_exists(tariff_id, tariff_crud, session)
# user = await user_crud.update_id(
# session=session, db_obj=user, field="tariff_id",
# field_value=tariff_id
# )
# return user


@router.post(
"/",
response_model=TariffRead,
dependencies=[Depends(current_superuser)]
dependencies=[Depends(current_superuser)],
status_code=status.HTTP_201_CREATED
)
async def create_tariff(
tariff: TariffCreate, session: AsyncSession = Depends(get_async_session)
Expand All @@ -58,7 +86,8 @@ async def create_tariff(

@router.delete(
"/{obj_id}",
dependencies=[Depends(current_superuser)]
dependencies=[Depends(current_superuser)],
status_code=status.HTTP_204_NO_CONTENT
)
async def delete_tariff(
obj_id: int,
Expand Down
1 change: 1 addition & 0 deletions app/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async def check_obj_exists(
detail=f"Объект {crud.model.__tablename__} "
f"f с id {obj_id} не найден.",
)
return obj


async def check_name_duplicate(
Expand Down
Loading
Loading