-
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 #25 from HumzahJavid/SEPP-34-refactor-code-and-add…
…itional-cleanup SEPP-34 Refactor code and additional cleanup
- Loading branch information
Showing
12 changed files
with
168 additions
and
202 deletions.
There are no files selected for viewing
Empty file.
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,74 @@ | ||
from fastapi import APIRouter, Form, Response, status | ||
|
||
import dating_app.services as services | ||
from dating_app.db import mongo | ||
from dating_app.schemas.User import ( | ||
LoginResponse, | ||
LoginResponseBase, | ||
RegisterResponse, | ||
RegisterResponseBase, | ||
UserCreate, | ||
UserModel, | ||
) | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post( | ||
"/register", | ||
status_code=status.HTTP_201_CREATED, | ||
response_model=RegisterResponse, | ||
responses=RegisterResponseBase.Config.schema_extra, # type: ignore | ||
) | ||
async def register( | ||
response: Response, | ||
email: str = Form(...), | ||
password: str = Form(...), | ||
confirmPassword: str = Form(...), | ||
): | ||
user = UserCreate( | ||
email=email, password=password, confirmed_password=confirmPassword | ||
) | ||
db = mongo.get_db() | ||
print(db["users"]) | ||
|
||
register_response = await services.create_user(db, user) | ||
|
||
if "already in use." in register_response.message: | ||
response.status_code = status.HTTP_409_CONFLICT | ||
|
||
return register_response | ||
|
||
|
||
@router.post( | ||
"/login", | ||
status_code=status.HTTP_200_OK, | ||
response_model=LoginResponse, | ||
responses=LoginResponseBase.Config.schema_extra, # type: ignore | ||
) | ||
async def login( | ||
response: Response, | ||
email: str = Form(...), | ||
password: str = Form(...), | ||
): | ||
|
||
user_form = UserModel(email=email, password=password) | ||
db = mongo.get_db() | ||
print(db["users"]) | ||
login_response = await services.authenticate_user(db, user_form) | ||
print(f"user is {login_response}") | ||
if "Invalid" in login_response.message: | ||
print(f"Invalid credentials 401: {email}") | ||
response.status_code = status.HTTP_401_UNAUTHORIZED | ||
else: | ||
response.set_cookie(key="X-Authorization", value=email, httponly=True) | ||
|
||
return login_response | ||
|
||
|
||
@router.post("/logout", status_code=status.HTTP_200_OK) | ||
async def logout(response: Response): | ||
db = mongo.get_db() | ||
logout_response = await services.logout(db) | ||
print(logout_response) | ||
return logout_response |
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,3 @@ | ||
from dating_app.db.database import MongoDB | ||
|
||
mongo = MongoDB() |
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
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,32 @@ | ||
""" | ||
MongoModel handles Creating MongoDB compatible pydantic models | ||
https://www.mongodb.com/developer/quickstart/python-quickstart-fastapi/#the-_id-attribute-and-objectids | ||
""" | ||
from bson import ObjectId | ||
from pydantic import BaseModel, Field | ||
|
||
|
||
class PyObjectId(ObjectId): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v): | ||
if not ObjectId.is_valid(v): | ||
raise ValueError("Invalid objectid") | ||
return ObjectId(v) | ||
|
||
@classmethod | ||
def __modify_schema__(cls, field_schema): | ||
field_schema.update(type="string") | ||
|
||
|
||
class MongoModel(BaseModel): | ||
id: PyObjectId = Field(default_factory=PyObjectId, alias="_id") | ||
|
||
class Config: | ||
allow_population_by_field_name = True | ||
arbitrary_types_allowed = True | ||
json_encoders = {ObjectId: str} |
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
Oops, something went wrong.