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

Commit

Permalink
Externalize strings
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Jun 8, 2019
1 parent d292534 commit 912be51
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 5 additions & 1 deletion project_amber/const.py
Expand Up @@ -2,4 +2,8 @@

EMPTY_RESP = dumps({}) # Empty response, to be used in requests.

TASK_MSG_NOT_FOUND = "This task does not exist."
MSG_NO_TOKEN = "No X-Auth-Token present"
MSG_INVALID_TOKEN = "Invalid token"
MSG_USER_NOT_FOUND = "This user does not exist"

MSG_TASK_NOT_FOUND = "This task does not exist"
9 changes: 5 additions & 4 deletions project_amber/helpers/auth.py
Expand Up @@ -5,6 +5,7 @@
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.db import db
from project_amber.errors import Unauthorized, BadRequest, NotFound, InternalServerError
from project_amber.models.auth import User, Session
Expand Down Expand Up @@ -34,13 +35,13 @@ def handleChecks() -> LoginUser:
raise BadRequest
token = request.headers.get("X-Auth-Token")
if token is None:
raise Unauthorized("No X-Auth-Token present")
raise Unauthorized(MSG_NO_TOKEN)
user_session = db.session.query(Session).filter_by(token=token).first()
if user_session is None:
raise Unauthorized("Invalid token")
raise Unauthorized(MSG_INVALID_TOKEN)
user = db.session.query(User).filter_by(id=user_session.user).first()
if user is None:
raise InternalServerError("The user is missing")
raise InternalServerError(MSG_USER_NOT_FOUND)
user_details = LoginUser(user.name, user.id, token)
return user_details

Expand All @@ -61,7 +62,7 @@ def removeUser(uid: int) -> int:
"""
user = db.session.query(User).filter_by(id=uid).first()
if user is None:
raise NotFound("User not found")
raise NotFound(MSG_USER_NOT_FOUND)
db.session.delete(user)
db.session.commit()
return uid
Expand Down
6 changes: 3 additions & 3 deletions project_amber/helpers/task.py
@@ -1,6 +1,6 @@
from time import time

from project_amber.const import TASK_MSG_NOT_FOUND
from project_amber.const import MSG_TASK_NOT_FOUND
from project_amber.db import db
from project_amber.errors import NotFound
from project_amber.models.task import Task
Expand All @@ -21,7 +21,7 @@ def updateTask(new_text: str, task_id: int, uid: int) -> int:
"""
task = db.session.query(Task).filter_by(id=task_id, owner=uid).first()
if task is None:
raise NotFound(TASK_MSG_NOT_FOUND)
raise NotFound(MSG_TASK_NOT_FOUND)
task.text = new_text
db.session.commit()
return task_id
Expand All @@ -33,7 +33,7 @@ def removeTask(task_id: int, uid: int) -> int:
"""
task = db.session.query(Task).filter_by(id=task_id, owner=uid).first()
if task is None:
raise NotFound(TASK_MSG_NOT_FOUND)
raise NotFound(MSG_TASK_NOT_FOUND)
db.session.delete(task)
db.session.commit()
return task_id

0 comments on commit 912be51

Please sign in to comment.