-
Notifications
You must be signed in to change notification settings - Fork 9
Misc Contacts for PE5 #693
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
Open
warix8
wants to merge
7
commits into
main
Choose a base branch
from
miscellaneous
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d046f73
Misc Contacts for PE5
warix8 9802998
format files
warix8 f0f5895
Missing migration
warix8 34606f4
Failling test due to bad group checking
warix8 6808057
Comments + AuthClient
warix8 0851298
Ruff format
warix8 99cdeed
Moved to coredata
warix8 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
Empty file.
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,8 @@ | ||
| from app.types.core_data import BaseCoreData | ||
|
|
||
|
|
||
| # <-- Contacts for PE5 SafetyCards 2025 --> | ||
| class ContactSafetyCards(BaseCoreData): | ||
| contacts: str = "" | ||
|
|
||
| # <-- End of Contacts for PE5 SafetyCards 2025 --> |
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,71 @@ | ||
| import json | ||
|
|
||
| from fastapi import APIRouter, Depends | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.core.groups.groups_type import AccountType, GroupType | ||
| from app.core.users import models_users | ||
| from app.dependencies import get_db, is_user_a_member, is_user_in | ||
| from app.modules.misc import core_data_misc, schemas_misc | ||
| from app.types.module import Module | ||
| from app.utils import tools | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| # <-- Contacts for PE5 SafetyCards 2025 --> | ||
| module = Module( | ||
| root="contacts_safety_cards", | ||
| tag="Contact", | ||
| default_allowed_account_types=[AccountType.student, AccountType.staff], | ||
| ) | ||
|
|
||
|
|
||
| @module.router.get( | ||
| "/contacts_safety_cards/contacts", | ||
| response_model=list[schemas_misc.ContactBase], | ||
| status_code=200, | ||
| ) | ||
| async def get_contacts( | ||
| db: AsyncSession = Depends(get_db), | ||
| user: models_users.CoreUser = Depends(is_user_a_member), | ||
| ): | ||
| """ | ||
| Get contacts. | ||
|
|
||
| **The user must be authenticated to use this endpoint** | ||
| This the main purpose of this endpoint, because contacts (phone numbers, emails) should not be leaked in the prevention website | ||
| """ | ||
|
|
||
| contacts_from_core_data = await tools.get_core_data( | ||
| core_data_misc.ContactSafetyCards, | ||
| db, | ||
| ) | ||
|
|
||
| serialized_json_contacts = contacts_from_core_data.contacts | ||
|
|
||
| return json.loads(serialized_json_contacts) | ||
|
|
||
|
|
||
| @module.router.put( | ||
| "/contacts_safety_cards/contacts", | ||
| status_code=201, | ||
| ) | ||
| async def set_contacts( | ||
| contacts: list[schemas_misc.ContactBase], | ||
| db: AsyncSession = Depends(get_db), | ||
| user: models_users.CoreUser = Depends(is_user_in(GroupType.eclair)), | ||
| ): | ||
| """ | ||
| Create a contact. | ||
|
|
||
| **This endpoint is only usable by members of the group eclair** | ||
| """ | ||
|
|
||
| contacts_serialized_json = json.dumps(contacts) | ||
|
|
||
| await tools.set_core_data( | ||
| core_data_misc.ContactSafetyCards(contacts=contacts_serialized_json), | ||
| db, | ||
| ) | ||
| # <-- End of Contacts for PE5 SafetyCards 2025 --> | ||
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,10 @@ | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| # <-- Contacts for PE5 SafetyCards 2025 --> | ||
| class ContactBase(BaseModel): | ||
| name: str | ||
| email: str | None = None | ||
| phone: str | None = None | ||
| location: str | None = None | ||
| # <--End of Contacts for PE5 SafetyCards 2025 --> |
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,53 @@ | ||
| import pytest_asyncio | ||
| from fastapi.testclient import TestClient | ||
|
|
||
| from app.core.groups.groups_type import GroupType | ||
| from tests.commons import create_api_access_token, create_user_with_groups | ||
|
|
||
| token_simple: str | ||
| token_eclair: str | ||
|
|
||
|
|
||
| @pytest_asyncio.fixture(scope="module", autouse=True) | ||
| async def init_objects() -> None: | ||
| user_simple = await create_user_with_groups( | ||
| [], | ||
| ) | ||
|
|
||
| global token_simple | ||
| token_simple = create_api_access_token(user_simple) | ||
|
|
||
| user_eclair = await create_user_with_groups([GroupType.eclair]) | ||
|
|
||
| global token_eclair | ||
| token_eclair = create_api_access_token(user_eclair) | ||
|
|
||
|
|
||
| def test_get_contacts(client: TestClient) -> None: | ||
| response = client.get( | ||
| "/contacts_safety_cards/contacts", | ||
| headers={"Authorization": f"Bearer {token_simple}"}, | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
|
|
||
| def test_set_contacts(client: TestClient) -> None: | ||
| response = client.put( | ||
| "/contacts_safety_cards/contacts/", | ||
| json=[ | ||
| { | ||
| "name": "John Doe", | ||
| "phone": "123456789", | ||
| "email": "johndoe@example.com", | ||
| "location": "Lyon", | ||
| }, | ||
| { | ||
| "name": "John Doe bis", | ||
| "phone": "323456789", | ||
| "email": "johndoebis@example.com", | ||
| "location": "Ecully", | ||
| }, | ||
| ], | ||
| headers={"Authorization": f"Bearer {token_eclair}"}, | ||
| ) | ||
| assert response.status_code == 201 |
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.