-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor api #2
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
BhavikaGouri
wants to merge
8
commits into
main
Choose a base branch
from
refactor-api
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
Refactor api #2
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3308520
Update main.py
BhavikaGouri eb66ee2
Update main.py
BhavikaGouri f5e7cf8
Create db.py
BhavikaGouri 7e975d2
Update main.py
BhavikaGouri 8b07f14
Update and rename db.py to database.py
BhavikaGouri 74a85b1
Update main.py
BhavikaGouri a5a4760
.
BhavikaGouri 72b9182
Structure FastAPI Code
BhavikaGouri 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from sqlmodel import SQLModel, Field, Session, create_engine, select | ||
from passlib.context import CryptContext | ||
from typing import Generator | ||
import random | ||
|
||
|
||
SECRET_KEY = "your_secret_key_here" | ||
ALGORITHM = "HS256" | ||
ACCESS_TOKEN_EXPIRE_MINUTES = 30 | ||
|
||
hash_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | ||
|
||
connect_args = {"check_same_thread": False} | ||
engine = create_engine("sqlite:///orm-user.db", connect_args=connect_args) | ||
|
||
|
||
class User(SQLModel, table=True): | ||
name: str | ||
username: str | None = Field(default=None, primary_key=True) | ||
email: str | None = Field(default=None) | ||
hashed_password: str | None = Field(default=None) | ||
disabled: bool | None = Field(default=None) | ||
|
||
|
||
def create_db_and_table(): | ||
SQLModel.metadata.create_all(engine) | ||
|
||
|
||
def get_session() -> Generator: | ||
with Session(engine) as session: | ||
yield session | ||
|
||
|
||
def get_hash(password: str) -> str: | ||
return hash_context.hash(password) | ||
|
||
def verify_password(hashed_password, password): | ||
return hash_context.verify(password, hashed_password) | ||
|
||
def generate_unique_username(name: str, session: Session) -> str: | ||
while True: | ||
username = name + str(random.randint(1, 100)) | ||
statement = select(User).where(User.username == username) | ||
if not session.exec(statement).first(): | ||
return username | ||
|
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
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.