-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Xiao75896453/feat/account_password_manage…
…ment/create_account feat (account_password_management/account): create account API #6
- Loading branch information
Showing
19 changed files
with
438 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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 typing import List | ||
|
||
from sqlalchemy import text | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
def init_tables(db_session: Session, tables: List[str]): | ||
for table in tables: | ||
db_session.execute(text("TRUNCATE " + table + " RESTART IDENTITY CASCADE;")) | ||
db_session.commit() |
This file contains 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,6 @@ | ||
class EverythingEquals: | ||
def __eq__(self, other): | ||
return True | ||
|
||
|
||
everything_equals = EverythingEquals() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
35 changes: 35 additions & 0 deletions
35
projects/account_password_management/alembic/versions/1_account_create_table.py
This file contains 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,35 @@ | ||
"""account create table | ||
Revision ID: 1 | ||
Revises: | ||
Create Date: 2024-08-05 14:12:39.901690 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '1' | ||
down_revision: Union[str, None] = None | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('account', | ||
sa.Column('id', sa.Integer(), nullable=False), | ||
sa.Column('username', sa.String(length=32), nullable=False), | ||
sa.Column('password', sa.String(length=64), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('username') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('account') | ||
# ### end Alembic commands ### |
This file contains 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
23 changes: 16 additions & 7 deletions
23
projects/account_password_management/src/api/account/query.py
This file contains 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 |
---|---|---|
@@ -1,20 +1,29 @@ | ||
from sqlalchemy.exc import IntegrityError, NoResultFound | ||
from sqlalchemy.orm import Session | ||
from src.models.account import Account | ||
|
||
from lib.exceptions import ConflictException, NotFound | ||
|
||
USERNAME_NOT_EXISTS_REASON = "Username not exists" | ||
USERNAME_ALREADY_EXISTS_REASON = "Username already exists" | ||
|
||
|
||
async def create_account(account) -> None: | ||
async def create_account(account: Account, db_session: Session) -> None: | ||
try: | ||
return | ||
db_session.add(account) | ||
db_session.flush() | ||
|
||
except: | ||
except IntegrityError: | ||
raise ConflictException(detail=USERNAME_ALREADY_EXISTS_REASON) | ||
|
||
|
||
async def get_account(username: str) -> None: | ||
async def get_account(username: str, db_session: Session) -> Account: | ||
try: | ||
return | ||
|
||
except: | ||
return ( | ||
db_session.query(Account) | ||
.filter(Account.username == username) | ||
.with_for_update() | ||
.one() | ||
) | ||
except NoResultFound: | ||
raise NotFound(detail=USERNAME_NOT_EXISTS_REASON) |
This file contains 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
1 change: 0 additions & 1 deletion
1
projects/account_password_management/src/api/authentication/controller.py
This file contains 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
8 changes: 2 additions & 6 deletions
8
projects/account_password_management/src/api/authentication/routers.py
This file contains 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 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 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 @@ | ||
from src.models import account, alembic_version |
10 changes: 10 additions & 0 deletions
10
projects/account_password_management/src/models/account.py
This file contains 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 sqlalchemy import Column, Integer, String | ||
from src.utils.db_connector import db | ||
|
||
|
||
class Account(db.get_base()): | ||
__tablename__ = "account" | ||
|
||
id = Column(Integer, primary_key=True) | ||
username = Column(String(32), nullable=False, unique=True) | ||
password = Column(String(64), nullable=False) |
11 changes: 11 additions & 0 deletions
11
projects/account_password_management/src/models/alembic_version.py
This file contains 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,11 @@ | ||
from sqlalchemy import Column, String | ||
from src.utils.db_connector import db | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class AlembicVersion( | ||
db.get_base() | ||
): # for pytest conftest init_db_schema to drop alembic_version table | ||
__tablename__ = "alembic_version" | ||
|
||
version_num = Column(String(32), primary_key=True, nullable=False) |
This file contains 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,4 @@ | ||
from passlib.context import CryptContext | ||
|
||
# autogenerate salt https://passlib.readthedocs.io/en/stable/lib/passlib.hash.bcrypt.html#passlib.hash.bcrypt | ||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
Empty file.
Oops, something went wrong.