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

Commit

Permalink
Merge branch develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Dec 27, 2019
2 parents f058744 + e4701b7 commit b4f0651
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 129 deletions.
6 changes: 4 additions & 2 deletions .drone.yml
Expand Up @@ -7,7 +7,7 @@ steps:
commands:
- pip install pylint
- pip install -r requirements.txt
- pylint project_amber
- pylint --rcfile .pylintrc project_amber project_amber/handlers project_amber/helpers project_amber/models
when:
branch:
exclude:
Expand All @@ -21,8 +21,10 @@ steps:
from_secret: password
repo: registry.git.tdem.in/amber
registry: registry.git.tdem.in
auto_tag: true
dockerfile: Dockerfile
tags:
- ${DRONE_TAG}
- latest
when:
branch:
- master
Expand Down
5 changes: 2 additions & 3 deletions .pylintrc
Expand Up @@ -17,9 +17,8 @@ disable=
missing-docstring,
protected-access,
too-few-public-methods,
unnecessary-lambda
# handled by black
format
unnecessary-lambda,
multiple-statements

[REPORTS]
output-format=colorized
Expand Down
7 changes: 4 additions & 3 deletions Pipfile
Expand Up @@ -6,12 +6,13 @@ verify_ssl = true
[dev-packages]
pylint = "*"
yapf = "*"
rope = "*"

[packages]
bcrypt = "==3.1.6"
flask-sqlalchemy = "==2.4.0"
bcrypt = "==3.1.7"
flask-sqlalchemy = "==2.4.1"
flask-cors = "==3.0.8"
flask = "==1.0.3"
flask = "==1.1.1"

[requires]
python_version = "3.7"
88 changes: 35 additions & 53 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 15 additions & 30 deletions project_amber/app.py
Expand Up @@ -6,43 +6,28 @@
from project_amber.config import config
from project_amber.db import db
from project_amber.errors import HTTPError
from project_amber.helpers import handleLogin, middleware as checkRequest
from project_amber.handlers.auth import login, logout
from project_amber.handlers.session import handle_session_req, \
handle_session_id_req
from project_amber.handlers.misc import version as handle_version_request
from project_amber.handlers.task import handle_task_id_request, \
handle_task_request
from project_amber.handlers.users import signup, update_user_data
from project_amber.helpers import handleLogin, middleware as check_request
from project_amber.handlers.const import API_V0
from project_amber.handlers.auth import auth_handlers as auth
from project_amber.handlers.session import session_handlers as session
from project_amber.handlers.misc import misc_handlers as misc
from project_amber.handlers.task import task_handlers as task
from project_amber.handlers.users import user_handlers as user

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = config["database"]
app.config["SQLALCHEMY_DATABASE_URI"] = config.database
db.init_app(app)
CORS(app, resources={r"/*": {"origins": config["domain"]}})
CORS(app, resources={r"/*": {"origins": config.domain}})


@app.before_request
def middleware():
if checkRequest().authenticated: request.user = handleLogin()


app.add_url_rule("/v0/login", "login", login, methods=["POST"])
app.add_url_rule("/v0/logout", "logout", logout, methods=["POST"])
app.add_url_rule("/v0/task", "task", handle_task_request, \
methods=["GET", "POST"])
app.add_url_rule("/v0/task/<task_id>", "task_id", handle_task_id_request, \
methods=["GET", "PATCH", "DELETE"])
app.add_url_rule("/v0/user", "user", update_user_data, methods=["PATCH"])
app.add_url_rule(
"/v0/session", "session", handle_session_req, methods=["GET"]
)
app.add_url_rule("/v0/session/<session_id>", "session_id", \
handle_session_id_req, methods=["GET", "DELETE"])
app.add_url_rule("/v0/version", "version", handle_version_request, \
methods=["GET"])

if config["allow_signup"]:
app.add_url_rule("/v0/signup", "signup", signup, methods=["POST"])
if check_request().authenticated:
request.user = handleLogin()


for blueprint in (auth, session, misc, task, user):
app.register_blueprint(blueprint, url_prefix=API_V0)


@app.before_first_request
Expand Down
43 changes: 24 additions & 19 deletions project_amber/config.py
@@ -1,23 +1,28 @@
import os
import sys
from json import load

if os.name == "posix":
configPaths = ["./config.json", "/etc/amber.json"]
else:
configPaths = ["config.json"]
class Config:
database: str = ""
loglevel: int = 0
allow_signup: bool = False
domain: str = "*"

config = Config()

config = {"database": "", "loglevel": 0, "allow_signup": False, "domain": "*"}
configPaths = ["config.json"]
if os.name == "posix":
configPaths.append("/etc/amber.json")

# search for every file name and load the config from the first file
# that exists
# search for every file name and load the config from the first file that exists
for testedFileName in configPaths:
if os.path.isfile(testedFileName):
# holds the actual config file name
configFileName = testedFileName
break
if not "configFileName" in globals():
print("No configuration file found, exiting")
exit(1)
sys.exit(1)

try:
with open(configFileName, encoding="utf8") as configFile:
Expand All @@ -27,11 +32,11 @@
except OSError as ioerr:
print("Could not open config file", configFileName)
print(ioerr.strerror)
exit(1)
sys.exit(1)

for entry in config:
for entry in vars(config):
if entry in loadedConfig:
config[entry] = loadedConfig[entry]
setattr(config, entry, loadedConfig[entry])


def string_to_bool(val: str) -> bool:
Expand All @@ -50,16 +55,16 @@ def string_to_bool(val: str) -> bool:
# tuple is the environment variable itself, the second is the corresponding
# `config` key, and the third one is the function to convert the possible values
for mapping in (
("AMBER_DATABASE", "database", lambda val: val), # str -> str
# pylint: disable=unnecessary-lambda
("AMBER_LOGLEVEL", "loglevel", lambda val: int(val)), # str -> int
("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool), # str -> bool
("AMBER_DOMAIN", "domain", lambda val: val) # str -> str
("AMBER_DATABASE", "database", lambda val: val), # str -> str
# pylint: disable=unnecessary-lambda
("AMBER_LOGLEVEL", "loglevel", lambda val: int(val)), # str -> int
("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool), # str -> bool
("AMBER_DOMAIN", "domain", lambda val: val) # str -> str
):
env_value = os.getenv(mapping[0])
if not env_value is None:
config[mapping[1]] = mapping[2](env_value)
setattr(config, mapping[1], mapping[2](env_value))

if config["database"] == "":
if not config.database:
print("No database specified. Exiting.")
exit(1)
sys.exit(1)
3 changes: 2 additions & 1 deletion project_amber/const.py
Expand Up @@ -13,6 +13,7 @@
MSG_USER_NOT_FOUND = "This user does not exist"
MSG_USER_EXISTS = "The user with this name already exists"
MSG_IMMATURE_SESSION = "This session is too new, and cannot remove others"
MSG_SIGNUP_FORBIDDEN = "Signup is disabled on this server"

MSG_TASK_NOT_FOUND = "This task does not exist"
MSG_TEXT_NOT_SPECIFIED = "No text specified"
Expand All @@ -21,4 +22,4 @@
# A regex matching all paths that can be accessed without an auth token.
PUBLIC_PATHS = r"/v\d/(login|signup|version)"

VERSION = "0.0.2.1"
VERSION = "0.0.3"
6 changes: 5 additions & 1 deletion project_amber/handlers/auth.py
@@ -1,14 +1,17 @@
from json import dumps

from flask import request
from flask import request, Blueprint

from project_amber.const import EMPTY_RESP, MSG_MISSING_AUTH_INFO
from project_amber.errors import BadRequest
from project_amber.handlers.const import API_PASSWORD, API_USER, API_TOKEN
from project_amber.helpers.auth import removeSession, createSession
from project_amber.logging import log

auth_handlers = Blueprint("auth_handlers", __name__)


@auth_handlers.route("/login", methods=["POST"])
def login():
"""
Login handler. Accepts this JSON:
Expand All @@ -32,6 +35,7 @@ def login():
return dumps({API_TOKEN: token})


@auth_handlers.route("/logout", methods=["POST"])
def logout():
"""
Logout handler. Accepts empty JSON. Returns HTTP 200 on success.
Expand Down
3 changes: 3 additions & 0 deletions project_amber/handlers/const.py
Expand Up @@ -13,3 +13,6 @@
API_REMINDER = "reminder"
API_QUERY = "query"
API_VERSION = "version"
API_SIGNUP = "signup"

API_V0 = "/v0"
10 changes: 8 additions & 2 deletions project_amber/handlers/misc.py
@@ -1,8 +1,14 @@
from json import dumps

from flask import Blueprint

from project_amber.config import config
from project_amber.const import VERSION
from project_amber.handlers.const import API_VERSION
from project_amber.handlers.const import API_VERSION, API_SIGNUP

misc_handlers = Blueprint("misc_handlers", __name__)


@misc_handlers.route("/version", methods=["GET"])
def version():
return dumps({API_VERSION: VERSION})
return dumps({API_VERSION: VERSION, API_SIGNUP: config.allow_signup})

0 comments on commit b4f0651

Please sign in to comment.