-
Notifications
You must be signed in to change notification settings - Fork 0
Пользовательская ручка view #38 #40
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| from auth_lib.fastapi import UnionAuth | ||
| from fastapi import APIRouter, Depends | ||
| from fastapi_sqlalchemy import db | ||
|
|
||
| from modal_backend.schemas.base import StatusResponseModel | ||
| from modal_backend.settings import Settings, get_settings | ||
| from modal_backend.utils.user_logic import UserService | ||
|
|
||
| settings: Settings = get_settings() | ||
| user_router = APIRouter(prefix="/user", tags=["User"]) | ||
|
|
||
|
|
||
| @user_router.post("/{id}/view", response_model=StatusResponseModel) | ||
| async def mark_note_view(id: int, service_id: int, user=Depends(UnionAuth())) -> StatusResponseModel: | ||
| """ | ||
| Отмечает, что модалка реально была показана пользователю. | ||
|
|
||
| Увеличивает shown_count в таблице note_view и запоминает номер захода | ||
| (last_visit_number), от которого потом считается frequency. | ||
| Если записи в note_view ещё нет — создаёт. | ||
|
|
||
| Повторный вызов не ошибка | ||
| """ | ||
| await UserService.mark_view(db, note_id=id, user_id=user.get("id"), service_id=service_id) | ||
| return StatusResponseModel(status="success", message="View recorded", ru="Показ засчитан") | ||
This file contains hidden or 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 hidden or 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,57 @@ | ||
| from datetime import datetime, timezone | ||
|
|
||
| from requests import Session | ||
|
|
||
| from modal_backend.exceptions import ForbiddenAction, ObjectNotFound | ||
| from modal_backend.models.db import ModalStatus, Note, NoteView, Service, UserVisit | ||
|
|
||
|
|
||
| class UserService: | ||
| """ | ||
| Пользовательский сервис для учёта показов модалок | ||
| """ | ||
|
|
||
| @classmethod | ||
| async def mark_view(cls, db: Session, note_id: int, user_id: int, service_id: int): | ||
| note = Note.get(session=db.session, id=note_id) | ||
| if note.status != ModalStatus.ACTIVE: | ||
| raise ForbiddenAction(Note) | ||
|
|
||
| now = datetime.now(timezone.utc).replace(tzinfo=None) | ||
| if note.is_always == False and now >= note.end_ts: | ||
| raise ForbiddenAction(Note) | ||
|
|
||
| service = Service.query(session=db.session).filter(Service.service_id == service_id).one_or_none() | ||
| if service is None: | ||
| raise ObjectNotFound(Service, service_id) | ||
|
|
||
| user_visit = ( | ||
| UserVisit.query(session=db.session) | ||
| .filter(UserVisit.user_id == user_id, UserVisit.service_id == service_id) | ||
| .one_or_none() | ||
| ) | ||
| visit_count = user_visit.visit_count if user_visit else 0 | ||
|
|
||
| note_view = ( | ||
| NoteView.query(session=db.session) | ||
| .filter(NoteView.note_id == note_id, NoteView.user_id == user_id) | ||
| .one_or_none() | ||
| ) | ||
| if note_view: | ||
| NoteView.update( | ||
| note_view.id, | ||
| session=db.session, | ||
| shown_count=note_view.shown_count + 1, | ||
| last_visit_number=visit_count, | ||
| last_shown_at=now, | ||
| ) | ||
| else: | ||
| NoteView.create( | ||
| session=db.session, | ||
| note_id=note_id, | ||
| user_id=user_id, | ||
| shown_count=1, | ||
| last_visit_number=1, | ||
| first_shown_at=now, | ||
| last_shown_at=now, | ||
| ) |
This file contains hidden or 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,51 @@ | ||
| import pytest | ||
| from starlette import status | ||
|
|
||
| from modal_backend.models.db import NoteView | ||
|
|
||
| url = "/user" | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def note_view_cleanup(dbsession, authlib_user_data): | ||
| yield | ||
| dbsession.query(NoteView).filter(NoteView.user_id == authlib_user_data["id"]).delete() | ||
| dbsession.commit() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "note_index, view_count, expected_status, expected_shown_count", | ||
| [ | ||
| pytest.param(0, 1, status.HTTP_200_OK, 1, id="first_view_creates_note_view"), | ||
| pytest.param(0, 2, status.HTTP_200_OK, 2, id="second_view_increments_shown_count"), | ||
| pytest.param(None, 1, status.HTTP_404_NOT_FOUND, None, id="nonexistent_note_returns_404"), | ||
| pytest.param(3, 1, status.HTTP_403_FORBIDDEN, None, id="archived_note_returns_403"), | ||
| ], | ||
| ) | ||
| def test_mark_note_view( | ||
| client, | ||
| dbsession, | ||
| notes, | ||
| services, | ||
| authlib_user_data, | ||
| note_view_cleanup, | ||
| note_index, | ||
| view_count, | ||
| expected_status, | ||
| expected_shown_count, | ||
| ): | ||
| note_id = notes[note_index].id if note_index is not None else 999999 | ||
| service_id = services[0].service_id | ||
|
|
||
| for _ in range(view_count): | ||
| response = client.post(f"{url}/{note_id}/view", params={"service_id": service_id}) | ||
| assert response.status_code == expected_status | ||
|
|
||
| if expected_shown_count is not None: | ||
| view = ( | ||
| dbsession.query(NoteView) | ||
| .filter(NoteView.note_id == note_id, NoteView.user_id == authlib_user_data["id"]) | ||
| .one_or_none() | ||
| ) | ||
| assert view is not None | ||
| assert view.shown_count == expected_shown_count |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.