Skip to content

Commit e264b50

Browse files
committed
Add sample app that uses distributed fastapi_allauth
1 parent ce788f8 commit e264b50

30 files changed

+1445
-0
lines changed

examples/DogeAPI/.gitignore

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
blog.db
64+
65+
# Flask stuff:
66+
instance/
67+
.webassets-cache
68+
69+
# Scrapy stuff:
70+
.scrapy
71+
72+
# Sphinx documentation
73+
docs/_build/
74+
75+
# PyBuilder
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
.python-version
87+
88+
# pipenv
89+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
90+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
91+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
92+
# install all needed dependencies.
93+
#Pipfile.lock
94+
95+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
96+
__pypackages__/
97+
98+
# Celery stuff
99+
celerybeat-schedule
100+
celerybeat.pid
101+
102+
# SageMath parsed files
103+
*.sage.py
104+
105+
# Environments
106+
.env
107+
.venv
108+
env/
109+
venv/
110+
ENV/
111+
env.bak/
112+
venv.bak/
113+
114+
# Spyder project settings
115+
.spyderproject
116+
.spyproject
117+
118+
# Rope project settings
119+
.ropeproject
120+
121+
# mkdocs documentation
122+
/site
123+
124+
# mypy
125+
.mypy_cache/
126+
.dmypy.json
127+
dmypy.json
128+
129+
# Pyre type checker
130+
.pyre/

examples/DogeAPI/api/blog.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#!/usr/bin/python3
2+
3+
from fastapi import HTTPException, status
4+
from sqlalchemy.orm import Session
5+
6+
from models import models
7+
from schema import schemas
8+
9+
10+
def get_all(db: Session):
11+
"""
12+
Get all blogs
13+
14+
Args:
15+
db (Session): Database session
16+
17+
Returns:
18+
List[models.Blog]: List of blogs
19+
"""
20+
return db.query(models.Blog).all()
21+
22+
23+
def create(request: schemas.Blog, db: Session):
24+
"""
25+
Create a new blog
26+
27+
Args:
28+
request (schemas.Blog): Blog object
29+
db (Session): Database session
30+
31+
Returns:
32+
models.Blog: Blog object
33+
"""
34+
new_blog = models.Blog(title=request.title, body=request.body)
35+
db.add(new_blog)
36+
db.commit()
37+
db.refresh(new_blog)
38+
return new_blog
39+
40+
41+
def destroy(id: int, db: Session):
42+
"""
43+
Delete a blog
44+
45+
Args:
46+
id (int): Blog id
47+
db (Session): Database session
48+
49+
Raises:
50+
HTTPException: 404 not found
51+
52+
Returns:
53+
str: Success message
54+
"""
55+
blog_to_delete = db.query(models.Blog).filter(models.Blog.id == id)
56+
57+
if not blog_to_delete.first():
58+
raise HTTPException(
59+
status_code=status.HTTP_404_NOT_FOUND,
60+
detail=f"Blog with id {id} not found.",
61+
)
62+
blog_to_delete.delete(synchronize_session=False)
63+
db.commit()
64+
return {"done"}
65+
66+
67+
def update(id: int, request: schemas.Blog, db: Session):
68+
"""
69+
Update a blog
70+
71+
Args:
72+
id (int): Blog id
73+
request (schemas.Blog): Blog object
74+
db (Session): Database session
75+
76+
Raises:
77+
HTTPException: 404 not found
78+
79+
Returns:
80+
models.Blog: Blog object
81+
"""
82+
blog = db.query(models.Blog).filter(models.Blog.id == id)
83+
if not blog.first():
84+
raise HTTPException(
85+
status_code=status.HTTP_404_NOT_FOUND, detail=f"Blog with id {id} not found"
86+
)
87+
blog.update(request.__dict__)
88+
db.commit()
89+
return "updated"
90+
91+
92+
def show(id: int, db: Session):
93+
"""
94+
Get a blog
95+
96+
Args:
97+
id (int): Blog id
98+
db (Session): Database session
99+
100+
Raises:
101+
HTTPException: 404 not found
102+
103+
Returns:
104+
models.Blog: Blog object
105+
"""
106+
blog = db.query(models.Blog).filter(models.Blog.id == id).first()
107+
if blog:
108+
return blog
109+
else:
110+
raise HTTPException(
111+
status_code=status.HTTP_404_NOT_FOUND,
112+
detail=f"Blog with the id {id} is not available",
113+
)

examples/DogeAPI/api/user.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/python3
2+
3+
from fastapi import HTTPException, status
4+
from sqlalchemy.orm import Session
5+
6+
from models import models
7+
from schema import schemas
8+
from schema.hash import Hash
9+
10+
11+
def create(request: schemas.User, db: Session):
12+
"""
13+
Create a new user
14+
15+
Args:
16+
request (schemas.User): User data
17+
db (Session): Database session
18+
19+
Returns:
20+
models.User: User created
21+
"""
22+
hashedPassword = Hash.bcrypt(request.password)
23+
user = models.User(name=request.name, email=request.email, password=hashedPassword)
24+
db.add(user)
25+
db.commit()
26+
db.refresh(user)
27+
return user
28+
29+
30+
def show(id: int, db: Session):
31+
"""
32+
Show a user
33+
34+
Args:
35+
id (int): User id
36+
db (Session): Database session
37+
38+
Raises:
39+
HTTPException: User not found
40+
41+
Returns:
42+
models.User: User found
43+
"""
44+
user = db.query(models.User).filter(models.User.id == id).first()
45+
if not user:
46+
raise HTTPException(
47+
status.HTTP_404_NOT_FOUND, detail=f"User with id {id} not found"
48+
)
49+
return user
50+
51+
52+
def get_all(db: Session):
53+
"""
54+
Get all users
55+
56+
Args:
57+
db (Session): Database session
58+
59+
Returns:
60+
list: List of users
61+
"""
62+
return db.query(models.User).all()

examples/DogeAPI/core/auth.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/python3
2+
3+
4+
from fastapi import APIRouter, Depends, HTTPException, status
5+
from fastapi.security.oauth2 import OAuth2PasswordRequestForm
6+
from sqlalchemy.orm import Session
7+
8+
from database import configuration
9+
from models import models
10+
from schema import schemas
11+
from schema.hash import Hash
12+
from schema.token import create_access_token
13+
14+
router = APIRouter(prefix="/login", tags=["Authentication"],)
15+
16+
17+
@router.post("/")
18+
def login(
19+
request: OAuth2PasswordRequestForm = Depends(),
20+
db: Session = Depends(configuration.get_db),
21+
):
22+
"""
23+
Login user
24+
25+
Args:
26+
request (OAuth2PasswordRequestForm, optional): OAuth2PasswordRequestForm.
27+
db (Session, optional): Session. Defaults to Depends(configuration.get_db).
28+
29+
Raises:
30+
HTTPException: 401 Unauthorized
31+
HTTPException: 404 Not Found
32+
33+
Returns:
34+
Hash: Hash
35+
"""
36+
user: schemas.User = db.query(models.User).filter(
37+
models.User.email == request.username
38+
).first()
39+
if not user:
40+
raise HTTPException(
41+
status_code=status.HTTP_404_NOT_FOUND, detail="Invalid Credentials"
42+
)
43+
44+
if not Hash.verify(user.password, request.password):
45+
raise HTTPException(
46+
status_code=status.HTTP_404_NOT_FOUND, detail="Incorrect password"
47+
)
48+
49+
# access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
50+
access_token = create_access_token(data={"sub": user.email})
51+
52+
# generate JWT token and return
53+
return {"access_token": access_token, "token_type": "bearer"}

0 commit comments

Comments
 (0)