Skip to content

Commit

Permalink
Change: move all helpers into a helpers folder
Browse files Browse the repository at this point in the history
This hopefully avoids the LGTM warning that "import click" and
"import .click" are the same.
  • Loading branch information
TrueBrain committed Apr 18, 2020
1 parent ba2e37f commit be13bfd
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 60 deletions.
12 changes: 7 additions & 5 deletions webclient/__main__.py
Expand Up @@ -6,10 +6,11 @@

from . import pages # noqa
from .app import app
from .click import click_additional_options
from .helpers import click_urls
from .sentry import click_sentry
from .session import (
from .helpers.api import click_api_url
from .helpers.click import click_additional_options
from .helpers.flask import click_frontend_url
from .helpers.sentry import click_sentry
from .helpers.session import (
click_auth_backend,
click_max_age,
)
Expand All @@ -36,7 +37,8 @@ def click_logging():
@click.group(cls=flask.cli.FlaskGroup, create_app=lambda: app)
@click_logging
@click_sentry
@click_urls
@click_api_url
@click_frontend_url
@click_auth_backend
@click_max_age
def cli():
Expand Down
Empty file added webclient/helpers/__init__.py
Empty file.
41 changes: 3 additions & 38 deletions webclient/helpers.py → webclient/helpers/api.py
@@ -1,55 +1,20 @@
import click
import flask
import requests
import urllib

from .app import app
from .click import click_additional_options
from ..app import app

_api_url = None
_frontend_url = None


@click_additional_options
@click.option(
"--api-url", help="BaNaNaS API URL.", default="https://api.bananas.openttd.org", show_default=True, metavar="URL",
)
@click.option(
"--frontend-url",
help="Frontend URL (this server).",
default="https://bananas.openttd.org",
show_default=True,
metavar="URL",
)
def click_urls(api_url, frontend_url):
global _api_url, _frontend_url
def click_api_url(api_url):
global _api_url
_api_url = api_url
_frontend_url = frontend_url


def template(*args, **kwargs):
if "message" in kwargs:
kwargs.setdefault("messages", []).append(kwargs["message"])

response = flask.make_response(flask.render_template(*args, **kwargs))
response.headers["Content-Security-Policy"] = "default-src 'self'"
return response


def external_url_for(*args, **kwargs):
return _frontend_url + flask.url_for(*args, **kwargs)


def redirect(*args, **kwargs):
return flask.redirect(flask.url_for(*args, **kwargs))


def not_found():
flask.abort(404)


def api_error():
flask.abort(500)


def api_call(method, path, params=None, json=None, session=None, return_errors=False):
Expand Down
File renamed without changes.
47 changes: 47 additions & 0 deletions webclient/helpers/flask.py
@@ -0,0 +1,47 @@
import click
import flask
import requests
import urllib

from .click import click_additional_options
from ..app import app

_frontend_url = None


@click_additional_options
@click.option(
"--frontend-url",
help="Frontend URL (this server).",
default="https://bananas.openttd.org",
show_default=True,
metavar="URL",
)
def click_frontend_url(frontend_url):
global _frontend_url
_frontend_url = frontend_url


def template(*args, **kwargs):
if "message" in kwargs:
kwargs.setdefault("messages", []).append(kwargs["message"])

response = flask.make_response(flask.render_template(*args, **kwargs))
response.headers["Content-Security-Policy"] = "default-src 'self'"
return response


def external_url_for(*args, **kwargs):
return _frontend_url + flask.url_for(*args, **kwargs)


def redirect(*args, **kwargs):
return flask.redirect(flask.url_for(*args, **kwargs))


def not_found():
flask.abort(404)


def api_error():
flask.abort(500)
File renamed without changes.
6 changes: 2 additions & 4 deletions webclient/session.py → webclient/helpers/session.py
Expand Up @@ -3,11 +3,9 @@
import flask
import secrets

from .api import api_get
from .click import click_additional_options
from .helpers import (
api_get,
redirect,
)
from .flask import redirect

_max_session_age = None
_max_csrf_age = None
Expand Down
6 changes: 4 additions & 2 deletions webclient/pages/login.py
@@ -1,13 +1,15 @@
import flask

from ..app import app
from ..helpers import (
from ..helpers.api import (
api_get,
api_post,
)
from ..helpers.flask import (
external_url_for,
redirect,
)
from ..session import (
from ..helpers.session import (
auth_backend,
get_session,
start_session,
Expand Down
6 changes: 3 additions & 3 deletions webclient/pages/package_info.py
@@ -1,12 +1,12 @@
import flask

from ..app import app
from ..helpers import (
from ..helpers.api import (
api_get,
api_put,
template,
)
from ..session import protected
from ..helpers.flask import template
from ..helpers.session import protected


def record_change(changes, data, key, value):
Expand Down
8 changes: 3 additions & 5 deletions webclient/pages/package_list.py
@@ -1,9 +1,7 @@
from ..app import app
from ..helpers import (
api_get,
template,
)
from ..session import protected
from ..helpers.api import api_get
from ..helpers.flask import template
from ..helpers.session import protected


@app.route("/package/<content_type>")
Expand Down
2 changes: 1 addition & 1 deletion webclient/pages/static.py
@@ -1,7 +1,7 @@
import flask

from ..app import app
from ..helpers import (
from ..helpers.flask import (
not_found,
redirect,
template,
Expand Down
6 changes: 4 additions & 2 deletions webclient/pages/version_info.py
Expand Up @@ -2,15 +2,17 @@
import re

from ..app import app
from ..helpers import (
from ..helpers.api import (
api_delete,
api_get,
api_post,
api_put,
)
from ..helpers.flask import (
redirect,
template,
)
from ..session import protected
from ..helpers.session import protected

_licenses = [
"GPL v2",
Expand Down

0 comments on commit be13bfd

Please sign in to comment.