Skip to content

Commit

Permalink
Use annotator object when creating
Browse files Browse the repository at this point in the history
Fixes #6.
  • Loading branch information
ToucheSir committed Sep 26, 2020
1 parent 9b107e7 commit 3285006
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
24 changes: 16 additions & 8 deletions backend/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ async def get_current_user(
db: DatabaseContext = Depends(get_db),
) -> Annotator:
user = await db.get_annotator(credentials.username)
#Get users IP address
# Get users IP address
hostname = socket.gethostname()
ip_address = socket.gethostbyname(hostname)
#If user has an active session skip bcrypt validation:
# If user has an active session skip bcrypt validation:
if await db.active_session(ip_address):
return user
#Else try to login user
# Else try to login user
if not user or not verify_password(credentials.password, user.hashed_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
#Create active session for newly logged in user
# Create active session for newly logged in user
await db.create_session(ip_address)
return user

Expand Down Expand Up @@ -103,7 +103,8 @@ def form_get():
# There must be no blank cells
@router.post("/admin/updatecampaigns")
async def create_upload_file(
campaigns: UploadFile = File(...), db: DatabaseContext = Depends(get_db),
campaigns: UploadFile = File(...),
db: DatabaseContext = Depends(get_db),
):
new_campaigns = json.load(campaigns.file)
for (username, campaign) in new_campaigns.items():
Expand All @@ -128,7 +129,7 @@ def form_get():


@router.post("/admin/resetpassword")
async def passwordreset(
async def password_reset(
username: str = Form(...),
newpassword: str = Form(...),
db: DatabaseContext = Depends(get_db),
Expand All @@ -152,15 +153,22 @@ def form_get():


@router.post("/admin/adduser")
async def passwordreset(
async def add_user(
name: str = Form(...),
username: str = Form(...),
designation: str = Form(...),
password: str = Form(...),
db: DatabaseContext = Depends(get_db),
):
encrypted = bcrypt.hash(password)
await db.add_user(name, username, designation, encrypted)
await db.add_user(
Annotator(
name=name,
username=username,
designation=designation,
hashed_password=encrypted,
)
)
return "A new user has been added"


Expand Down
11 changes: 2 additions & 9 deletions backend/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ async def reset_password(self, username: str, hashed_password: str):
{"username": username}, {"$set": {"hashed_password": hashed_password}}
)

async def add_user(self, name: str, username: str, designation: str, password: str):
return await self.annotators.insert_one(
{
"name": name,
"username": username,
"designation": designation,
"hashed_password": password,
}
)
async def add_user(self, user: Annotator):
return await self.annotators.insert_one(user.dict())

async def get_annotator(self, username: str) -> Optional[Annotator]:
res = await self.annotators.find_one({"username": username})
Expand Down

0 comments on commit 3285006

Please sign in to comment.