Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 7 additions & 25 deletions rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import aiohttp
from auth_lib.fastapi import UnionAuth
from fastapi import APIRouter, Depends, Query
from fastapi import APIRouter, BackgroundTasks, Depends, Query
from fastapi_sqlalchemy import db

from rating_api.exceptions import (
Expand All @@ -31,14 +31,17 @@
CommentUpdate,
)
from rating_api.settings import Settings, get_settings
from rating_api.utils.achievements import award_first_comment_achievement


settings: Settings = get_settings()
comment = APIRouter(prefix="/comment", tags=["Comment"])


@comment.post("", response_model=CommentGet)
async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depends(UnionAuth())) -> CommentGet:
async def create_comment(
lecturer_id: int, comment_info: CommentPost, background_tasks: BackgroundTasks, user=Depends(UnionAuth())
) -> CommentGet:
"""
Создает комментарий к преподавателю в базе данных RatingAPI
Для создания комментария нужно быть авторизованным
Expand Down Expand Up @@ -110,29 +113,8 @@ async def create_comment(lecturer_id: int, comment_info: CommentPost, user=Depen
user_id=user_id,
review_status=ReviewStatus.PENDING,
)

# Выдача аччивки юзеру за первый комментарий
async with aiohttp.ClientSession() as session:
give_achievement = True
async with session.get(
settings.API_URL + f"achievement/user/{user.get('id'):}",
headers={"Accept": "application/json"},
) as response:
if response.status == 200:
user_achievements = await response.json()
for achievement in user_achievements.get("achievement", []):
if achievement.get("id") == settings.FIRST_COMMENT_ACHIEVEMENT_ID:
give_achievement = False
break
else:
give_achievement = False
if give_achievement:
session.post(
settings.API_URL
+ f"achievement/achievement/{settings.FIRST_COMMENT_ACHIEVEMENT_ID}/reciever/{user.get('id'):}",
headers={"Accept": "application/json", "Authorization": settings.ACHIEVEMENT_GIVE_TOKEN},
)

# give achievement for first comment
background_tasks.add_task(award_first_comment_achievement, user.get('id'))
return CommentGet.model_validate(new_comment)


Expand Down
11 changes: 8 additions & 3 deletions rating_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ class Settings(BaseSettings):
CORS_ALLOW_HEADERS: list[str] = ['*']
MAX_COMMENT_LENGTH: int = 3000

'''Temp settings'''
APP_ENV: str = os.getenv("APP_ENV", "dev") # Can be "dev", "test", or "prod"

PROD_API_URL: str = "https://api.profcomff.com/"
TEST_API_URL: str = "https://api.test.profcomff.com/"
ACHIEVEMENT_GIVE_TOKEN: str = os.getenv("ACHIEVEMENT_API", "")

'''Temp settings'''
API_URL: str = "https://api.test.profcomff.com/"
FIRST_COMMENT_ACHIEVEMENT_ID: int = 12
ACHIEVEMENT_GIVE_TOKEN: str = ""
FIRST_COMMENT_ACHIEVEMENT_ID_TEST: int = 48
FIRST_COMMENT_ACHIEVEMENT_ID_PROD: int = 12

model_config = ConfigDict(case_sensitive=True, env_file=".env", extra="ignore")

Expand Down
43 changes: 43 additions & 0 deletions rating_api/utils/achievements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging
from typing import Optional

import aiohttp

from rating_api.settings import get_settings


settings = get_settings()


async def award_first_comment_achievement(user_id: int):
# Skip in dev environment
if settings.APP_ENV not in ["prod", "test"]:
return

if settings.APP_ENV == "prod":
api_url = settings.PROD_API_URL
achievement_id = settings.FIRST_COMMENT_ACHIEVEMENT_ID_PROD
else:
api_url = settings.TEST_API_URL
achievement_id = settings.FIRST_COMMENT_ACHIEVEMENT_ID_TEST
token = settings.ACHIEVEMENT_GIVE_TOKEN

async with aiohttp.ClientSession() as session:
give_achievement = True
async with session.get(
api_url + f"achievement/user/{user_id}",
headers={"Accept": "application/json"},
) as response:
if response.status == 200:
user_achievements = await response.json()
for achievement in user_achievements.get("achievement", []):
if achievement.get("id") == achievement_id:
give_achievement = False
break
else:
give_achievement = False
if give_achievement:
session.post(
api_url + f"achievement/achievement/{achievement_id}/reciever/{user_id}",
headers={"Accept": "application/json", "Authorization": token},
)
Loading