Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactor some more stuff
  • Loading branch information
tdemin committed Jun 12, 2019
1 parent d74f592 commit 41d13eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions project_amber/const.py
Expand Up @@ -5,5 +5,6 @@
MSG_NO_TOKEN = "No X-Auth-Token present"
MSG_INVALID_TOKEN = "Invalid token"
MSG_USER_NOT_FOUND = "This user does not exist"
MSG_USER_EXISTS = "The user with this name already exists"

MSG_TASK_NOT_FOUND = "This task does not exist"
18 changes: 7 additions & 11 deletions project_amber/handlers/task.py
Expand Up @@ -42,9 +42,8 @@ def handle_task_request():
"""
user = handleChecks()
if request.method == "GET":
query = None
if "query" in request.json:
query = request.json["query"]
query = request.json.get("query")
# `query` is OK to be `None`
tasks = getTasks(user.id, query)
tasksList = []
for task in tasks:
Expand All @@ -58,9 +57,9 @@ def handle_task_request():
"tasks": tasksList
})
if request.method == "POST":
if not "text" in request.json:
text = request.json.get("text")
if text is None:
raise BadRequest("No text specified")
text = request.json["text"]
status = request.json.get("status")
# if only I could `get("status", d=0)` like we do that with dicts
if status is None:
Expand Down Expand Up @@ -100,12 +99,9 @@ def handle_task_id_request(task_id: int):
"last_mod": task.last_mod_time
})
if request.method == "PATCH":
text = None
status = None
if "text" in request.json:
text = request.json["text"]
if "status" in request.json:
status = request.json["status"]
text = request.json.get("text")
status = request.json.get("status")
# these are fine to be `None`
updateTask(task_id, user.id, text=text, status=status)
return EMPTY_RESP
if request.method == "DELETE":
Expand Down
5 changes: 3 additions & 2 deletions project_amber/helpers/auth.py
Expand Up @@ -5,7 +5,8 @@
from bcrypt import hashpw, gensalt, checkpw
from flask import request

from project_amber.const import MSG_NO_TOKEN, MSG_INVALID_TOKEN, MSG_USER_NOT_FOUND
from project_amber.const import MSG_NO_TOKEN, MSG_INVALID_TOKEN, \
MSG_USER_NOT_FOUND, MSG_USER_EXISTS
from project_amber.db import db
from project_amber.errors import Unauthorized, BadRequest, NotFound, \
InternalServerError, Conflict
Expand Down Expand Up @@ -53,7 +54,7 @@ def addUser(name: str, password: str) -> int:
"""
# does a user with this name already exist?
if not db.session.query(User).filter_by(name=name).one_or_none() is None:
raise Conflict("The user with this name already exists")
raise Conflict(MSG_USER_EXISTS)
prehashed_pw = b64encode(sha256(password.encode("utf8")).digest())
hashed_pw = hashpw(prehashed_pw, gensalt())
user = User(name=name, password=hashed_pw)
Expand Down

0 comments on commit 41d13eb

Please sign in to comment.