Skip to content

Commit

Permalink
fixes issue #749 (#752)
Browse files Browse the repository at this point in the history
* replaced absulute urls by url_for

* removed links to pages

* replaced redirect by render_template

* url_for for menu added

* overview imaage definition added

* fixed scriptname for image in about
  • Loading branch information
ReimarBauer authored Apr 7, 2021
1 parent 1e22cb1 commit e8e6a21
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 29 deletions.
2 changes: 1 addition & 1 deletion mslib/_tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_xstatic():

def test_app_loader():
assert index.DOCS_SERVER_PATH.endswith('mslib')
app = index.app_loader("example")
app = index.app_loader(__name__)
assert app is not None
with app.test_client() as c:
response = c.get('/xstatic/bootstrap/css/bootstrap.css')
Expand Down
5 changes: 5 additions & 0 deletions mslib/_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def callback_ok_xml(status, response_headers):
assert response_headers[0] == ('Content-type', 'text/xml')


def callback_ok_html(status, response_headers):
assert status == "200 OK"
assert response_headers[0] == ('Content-Type', 'text/html; charset=utf-8')


def callback_404_plain(status, response_headers):
assert status == "404 NOT FOUND"
assert response_headers[0] == ('Content-type', 'text/plain')
Expand Down
42 changes: 35 additions & 7 deletions mslib/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@

from flask import render_template
from flask import Flask
from flask import send_from_directory, send_file
from flask import send_from_directory, send_file, url_for
from flask import abort
from markdown import Markdown
from xstatic.main import XStatic
from mslib.msui.icons import icons

# set the project root directory as the static folder
DOCS_SERVER_PATH = os.path.dirname(os.path.abspath(mslib.__file__))
# This can be used to set a location by SCRIPT_NAME for testing. e.g. export SCRIPT_NAME=/demo/
SCRIPT_NAME = os.environ.get('SCRIPT_NAME', '/')


def _xstatic(name):
Expand All @@ -58,8 +60,23 @@ def _xstatic(name):
return None


def prefix_route(route_function, prefix='', mask='{0}{1}'):
'''
https://stackoverflow.com/questions/18967441/add-a-prefix-to-all-flask-routes/18969161#18969161
Defines a new route function with a prefix.
The mask argument is a `format string` formatted with, in that order:
prefix, route
'''
def newroute(route, *args, **kwargs):
''' prefix route '''
return route_function(mask.format(prefix, route), *args, **kwargs)
return newroute


def app_loader(name):
APP = Flask(name, template_folder=os.path.join(DOCS_SERVER_PATH, 'static', 'templates'))
APP.config.from_object(name)
APP.route = prefix_route(APP.route, SCRIPT_NAME)

@APP.route('/xstatic/<name>/', defaults=dict(filename=''))
@APP.route('/xstatic/<name>/<path:filename>')
Expand All @@ -82,10 +99,10 @@ def mss_theme(name, filename):

def get_topmenu():
menu = [
('/mss', 'Mission Support System',
(('/mss/about', 'About'),
('/mss/install', 'Install'),
('/mss/help', 'Help'),
(url_for('index'), 'Mission Support System',
((url_for('about'), 'About'),
(url_for('install'), 'Install'),
(url_for('help'), 'Help'),
)),
]
return menu
Expand All @@ -99,6 +116,9 @@ def get_content(filename, overrides=None):
with codecs.open(filename, 'r', 'utf-8') as f:
md_data = f.read()
md_data = md_data.replace(':ref:', '')
if overrides is not None:
v1, v2 = overrides
md_data = md_data.replace(v1, v2)
content = markdown.convert(md_data)
return content

Expand All @@ -110,7 +130,10 @@ def index():
@APP.route("/mss")
def about():
_file = os.path.join(DOCS_SERVER_PATH, 'static', 'docs', 'about.md')
content = get_content(_file)
img_url = url_for('overview')
overrides = ['![image](/mss/overview.png)', f'![image]({img_url})']
content = get_content(_file,
overrides=overrides)
return render_template("/content.html", act="about", content=content)

@APP.route("/mss/install")
Expand All @@ -132,7 +155,7 @@ def imprint():
return render_template("/content.html", act="imprint", content=content)

@APP.route('/mss/favicon.ico')
def favions():
def favicons():
base_path = icons("16x16", "favicon.ico")
return send_file(base_path)

Expand All @@ -141,4 +164,9 @@ def logo():
base_path = icons("64x64", "mss-logo.png")
return send_file(base_path)

@APP.route('/mss/overview.png')
def overview():
base_path = os.path.join(DOCS_SERVER_PATH, 'static', 'img', 'wise12_overview.png')
return send_file(base_path)

return APP
7 changes: 3 additions & 4 deletions mslib/mscolab/_tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from mslib.mscolab import server
from mslib.mscolab.models import User
from mslib._tests.constants import MSCOLAB_URL_TEST
from mslib._tests.utils import (callback_307_html, mscolab_register_user,
from mslib._tests.utils import (mscolab_register_user,
mscolab_register_and_login, mscolab_create_content,
mscolab_create_project, mscolab_delete_all_projects,
mscolab_delete_user, mscolab_login)
Expand All @@ -58,6 +58,7 @@ def setup(self):
self.app = APP
self.app.config['SQLALCHEMY_DATABASE_URI'] = mscolab_settings.SQLALCHEMY_DB_URI
self.app.config['MSCOLAB_DATA_DIR'] = mscolab_settings.MSCOLAB_DATA_DIR
self.app.config['SERVER_NAME'] = 'localhost:8084'
_app, self.sockio, self.cm, self.fm = server.initialize_managers(self.app)

def teardown(self):
Expand Down Expand Up @@ -99,9 +100,7 @@ def test_verify_user(self):
def test_home(self):
with self.app.app_context():
result = server.home()
callback_307_html(result.status, result.headers)
assert isinstance(result.data, bytes), result
assert result.data.count(b"") == 220, result
assert "!DOCTYPE html" in result

def test_status(self):
with self.app.app_context():
Expand Down
5 changes: 2 additions & 3 deletions mslib/mscolab/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import secrets
import fs
import socketio
from flask import g, jsonify, request, redirect
from flask import g, jsonify, request, render_template
from flask import send_from_directory, abort
from flask_httpauth import HTTPBasicAuth
from validate_email import validate_email
Expand Down Expand Up @@ -147,9 +147,8 @@ def wrapper(*args, **kwargs):


@APP.route('/')
# currently we do use same redirect as on wms server
def home():
return redirect('/index', 307)
return render_template("/index.html")


# ToDo setup codes in return statements
Expand Down
10 changes: 5 additions & 5 deletions mslib/mswms/_tests/test_wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"""

import mslib.mswms.mswms as mswms
from mslib._tests.utils import callback_ok_image, callback_ok_xml, callback_307_html
from mslib._tests.utils import callback_ok_image, callback_ok_xml, callback_ok_html


class Test_WMS(object):
Expand All @@ -39,7 +39,7 @@ def test_get_query_string_missing_parameters(self):

self.client = mswms.application.test_client()
result = self.client.get('/?{}'.format(environ["QUERY_STRING"]))
callback_307_html(result.status, result.headers)
callback_ok_html(result.status, result.headers)
assert isinstance(result.data, bytes), result

def test_get_query_string_wrong_values(self):
Expand All @@ -51,7 +51,7 @@ def test_get_query_string_wrong_values(self):

self.client = mswms.application.test_client()
result = self.client.get('/?{}'.format(environ["QUERY_STRING"]))
callback_307_html(result.status, result.headers)
callback_ok_html(result.status, result.headers)
assert isinstance(result.data, bytes), result

def test_get_capabilities(self):
Expand Down Expand Up @@ -226,7 +226,7 @@ def test_application_norequest(self):

self.client = mswms.application.test_client()
result = self.client.get('/?{}'.format(environ["QUERY_STRING"]))
callback_307_html(result.status, result.headers)
callback_ok_html(result.status, result.headers)
assert isinstance(result.data, bytes), result
assert result.data.count(b"") >= 1, result

Expand All @@ -238,6 +238,6 @@ def test_application_unkown_request(self):
}
self.client = mswms.application.test_client()
result = self.client.get('/?{}'.format(environ["QUERY_STRING"]))
callback_307_html(result.status, result.headers)
callback_ok_html(result.status, result.headers)
assert isinstance(result.data, bytes), result
assert result.data.count(b"") > 0, result
4 changes: 2 additions & 2 deletions mslib/mswms/wms.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import urllib.parse
from chameleon import PageTemplateLoader

from flask import request, make_response, redirect
from flask import request, make_response, render_template
from flask_httpauth import HTTPBasicAuth
from multidict import CIMultiDict
from mslib.utils import conditional_decorator
Expand Down Expand Up @@ -535,4 +535,4 @@ def application():
except Exception as ex:
error_message = "{}: {}\n".format(type(ex), ex)
logging.error("Unexpected error: %s", error_message)
return redirect('/index', 307)
return render_template("/index.html")
5 changes: 1 addition & 4 deletions mslib/static/docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apply to research flights, that concern flight altitude and range,
ambient temperature, flight permissions in different flight information
regions and aircraft traffic routes.

> ![image](../mss_theme/img/wise12_overview.png)
> ![image](/mss/overview.png)
MSS helps to optimize the scientific outcome of the research flights by
displaying the planned flight route and the corresponding model
Expand Down Expand Up @@ -62,6 +62,3 @@ Jülich. Improving the software will improve the quality of the research
flights and will also enable its use for other scientific areas, e.g.
planning of ship routes.

> - [Getting started](https://github.com/Open-MSS/MSS/wiki/Getting-Started)
> - [Contact](https://github.com/Open-MSS/MSS/wiki/Contact)
2 changes: 1 addition & 1 deletion mslib/static/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</div>
<div class="container-fluid">
<div class="row" style="background-color:lavender;">
<div class="col-sm-5 col-md-6" style="background-color:f2f2f2;"><a href="/mss/imprint">
<div class="col-sm-5 col-md-6" style="background-color:f2f2f2;"><a href="{{ url_for('imprint') }}">
<p class="text-dark">Imprint</p></a>
</div>
<div class="col-sm-5 col-sm-offset-2 col-md-6 col-md-offset-0" style="background-color:f2f2f2;">
Expand Down
4 changes: 2 additions & 2 deletions mslib/static/templates/theme.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<meta name="description" content="Mission Support System">
<meta name="keywords" content="MSS, MSCOLAB">
<meta name="author" content="authors of MSS">
<link rel='shortcut icon' type='image/x-icon' href='/mss/favicon.ico'>
<link rel='shortcut icon' type='image/x-icon' href="{{ url_for('favicons') }}">
<title>Mission Support System Collaboration Platform</title><!-- Bootstrap core CSS -->
<link href="{{ url_for('files', name='bootstrap', filename='css/bootstrap.css') }}" rel="stylesheet">
</head>
Expand All @@ -19,7 +19,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li style="list-style: none; display: inline">
<a class="navbar-brand" href="/index"><img src="/mss/logo.png" title="Mission Support System" alt="logo Mission Support System"></a>
<a class="navbar-brand" href="{{ url_for('index') }}"><img src="{{ url_for('logo') }}" title="Mission Support System" alt="logo Mission Support System"></a>
</li>
<li class="nav-item dropdown">{% for href, caption, submenu in get_topmenu(): %} {% if submenu[0] %}</li>
<li class="dropdown">
Expand Down

0 comments on commit e8e6a21

Please sign in to comment.