Skip to content
Snippets Groups Projects
Commit 0cc2e906 authored by slobinger's avatar slobinger
Browse files

Merge branch '11-write-example-app' into 'master'

Resolve "Example App schreiben"

Closes #11

See merge request !10
parents a8e10463 577d6239
No related branches found
No related tags found
1 merge request!10Resolve "Example App schreiben"
Showing
with 3 additions and 259 deletions
#from test_app import hub
#hub.app.run(debug = True)
from importlib import import_module
import_module('foo')
\ No newline at end of file
from ExampleApp import hub
hub.flaskApp.run()
\ No newline at end of file
from flask import redirect as _redirect, url_for, render_template, g, session
from functools import wraps
#from samsHUB import app
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
#app.secret_key = 'berry'
if not 'name' in session:
return _redirect('/login')
return f(*args, **kwargs)
return decorated_function
\ No newline at end of file
from flask_microservices import MicroServicesApp, url
from flask import render_template as flask_render_template, request
from . import views
from . import functions
from jinja2 import StrictUndefined
app = MicroServicesApp(__name__)
app.secret_key = 'berry'
app.enabled_modules = functions.get_enabled_modules()
app.config['default_language'] = 'de';
functions.apply_config(app)
if app.config['jinja2_strict_undefined']:
app.jinja_env.undefined = StrictUndefined
# By default, this will assume your modules directory is "./modules"
# if a second argument is not provided.
app.register_urls(app.enabled_modules)
app.blueprints['hallo_module'].register_urls(
[url(
'/test', view_func=views.home
, name='hallo_module_subview2')])
app.add_url_rule('/', 'home', views.home)
app.add_url_rule('/login', 'login', views.login, methods=['GET'])
app.add_url_rule('/login', 'login_post', views.login_post, methods=['POST'])
print (app.url_map)
@app.context_processor
def context_processor():
return functions.before_template(app=app)
#app.config['EXPLAIN_TEMPLATE_LOADING'] = True
\ No newline at end of file
DEBUG: true
jinja2_strict_undefined: true
#samsHUB:
# module_base_path: /modules/ # default
\ No newline at end of file
- hallo_module
\ No newline at end of file
class NoDefaultLanguage(Exception):
"""Raised when the DefaultLanguage of a Module, specified in the manifest.yaml
has no corresponding language file in modules/<modulename>/languages/
or if hub default language is not correctly configured
"""
def __init__(self, expression = None):
self.expression = expression
\ No newline at end of file
import yaml
import fnmatch
from flask import request, session
from .exceptions import NoDefaultLanguage
import os
from flask_microservices import url
def get_enabled_modules():
ymlFile = open('samsHUB/config/modules.yaml', 'r')
ymlObj = yaml.load(ymlFile)
ymlFile.close()
return ymlObj
def parse_manifest_urls(import_name):
yamlFile = open('samsHUB/modules/' + import_name + '/manifest.yaml')
manifest = yaml.load(yamlFile)
urlpatterns = []
for view in views:
urlpatterns.append(url(rule, view_func, name, methods))
pass
def apply_config(app):
yaml_file = open('samsHUB/config/config.yaml')
app.config.update(yaml.load(yaml_file))
app.config.get('samsHUB', {})
yaml_file.close()
def load_dict_from_yaml(filepath):
yaml_file = open(filepath)
yaml_dict = yaml.load(yaml_file)
yaml_file.close()
return yaml_dict
def get_lang_path(module='main'):
if module == 'main':
return 'samsHUB/languages/'
if module == None:
return None
return 'samsHUB/modules/' + module + '/languages/'
def get_mod_default_lang(module):
if module == None:
return None
manifest = load_dict_from_yaml(
filepath = 'samsHUB/modules/' + request.blueprint + '/manifest.yaml')
return manifest['default_language']
def generate_lang(lang_path, default_lang):
if lang_path == None:
return None
session.setdefault('lang', default_lang)
try:
default_lang_dict = load_dict_from_yaml(
filepath = lang_path + default_lang + '.yaml')
except FileNotFoundError as err:
raise NoDefaultLanguage(err)
if not os.path.exists(lang_path + session['lang'] + '.yaml'):
return default_lang_dict
default_lang_dict.update(load_dict_from_yaml(
filepath = lang_path + session['lang'] + '.yaml'))
return default_lang_dict
def generate_menu(modules, hub_lang, module_base_path = '/modules/'):
menu_list = [{'name': hub_lang['home_title'], 'url': '/', 'subentries': None}]
for module in modules:
manifest = load_dict_from_yaml(
filepath = 'samsHUB/modules/' + module + '/manifest.yaml')
mod_lang = generate_lang(
lang_path = get_lang_path(module = module)
, default_lang = manifest['default_language'])
menu = manifest.get('menu'
, [{'name_string': 'module_name', 'url': '/modules/' + module}])
menu_list.extend(convert_to_menentries(
menu = menu, mod_lang = mod_lang, module_base_path = module_base_path))
print(menu_list)
return menu_list
def convert_to_menentries(menu, mod_lang, module_base_path):
if menu == None:
return None
entries = []
for entry in menu:
parsed_entry = {'url': entry['url'], 'name': mod_lang[entry['name_string']]}
parsed_entry['subentries'] = convert_to_menentries(
menu = entry.get(
'subviews'), mod_lang = mod_lang, module_base_path = module_base_path)
entries.append(parsed_entry)
return entries
def before_template(app):
inject_dict = {}
inject_dict['mod_lang'] = generate_lang(
lang_path = get_lang_path(module = request.blueprint)
, default_lang = get_mod_default_lang(module = request.blueprint))
inject_dict['hub_lang'] = generate_lang(
lang_path = get_lang_path(module='main')
, default_lang = app.config['default_language'])
inject_dict['menu_list'] = generate_menu(
modules = app.enabled_modules, hub_lang = inject_dict['hub_lang'])
return inject_dict
\ No newline at end of file
home_title: 'Startseite'
home_wellcome_message: 'Willkommen im samsHUB'
zid_long: '<b>Z</b>IB <b>I</b>nformations <b>D</b>atenbank'
username: Benutzername
password: Kennwort
\ No newline at end of file
default_language: de
views:
- url: /
name_var: main_name #entspricht nicht dem default module_name
# subviews:
# - url: /subview
# name_var: subview_name
\ No newline at end of file
from flask_microservices import Router
from . import urls
from samsHUB.functions import get_enabled_modules
MODULE_NAME = 'hallo_module'
IMPORT_NAME = __name__
# These blueprints are what is collected when you run app.register_urls()
blueprint = Router.create_blueprint(MODULE_NAME, IMPORT_NAME)
#blueprint.register_urls(urls.urlpatterns)
module_title: &module_title Hallo Modul
module_name: *module_title
hallo module: >-
Hallo Modul!
beschreibung: >-
Eine Beshreibung zu was auch immer das 'hallo module' macht.
teststring: Ein Teststring
subview_description: >-
Dies ist ein Subview des Hallo Moduls. Das Modul kann vile weitere Subviews
haben, aber dieses ist bietet die Möglichkeit über ein Menü im SAMShub
erreichbar zu sein.
\ No newline at end of file
module_title: &module_title Hello Module
module_name: *module_title
hallo module: >-
Hello Module!
beschreibung: >-
This is the description of what ever this 'hello module' does.
teststring: A Teststring
subview_title: Subview
subview_name: Subentry
subviews_description: >-
This is a Subview of the 'hello module'. The Module may use much more subviews,
but this one is potentialy reachable in a menu of the SAMAShub.
home_string: Start
\ No newline at end of file
default_language: en
views:
- url: &hallo-module /modules/hallo-module
view_func: views.hallo_module
menu:
- url: *hallo-module
name_string: module_title
submenu:
- url: /modules/hallo-module/subview
name_string: subview_name
subviews:
- url: /
name_string: home_string
{% extends "base-struct.html" %}
{% block title %}Index{% endblock %}
{% block head %} {{ super() }} {% endblock %}
{% block content %}
<h1> {{ mod_lang['hallo module'] }}</h1>
<p> {{ mod_lang['beschreibung'] }}</p>
{% endblock %}
{% block header %}samsHUB {{ mod_lang['teststring'] }} {% endblock %}
\ No newline at end of file
from flask_microservices import url
from . import views
urlpatterns = [
url('/modules/hallo-module', view_func=views.hallo_module, name='hallo_module')
, url(
'/modules/hallo-module/subview', view_func=views.subview
, name='hallo_module_subview')
]
\ No newline at end of file
from flask import render_template
def hallo_module():
return render_template('hallo_module.html')
def subview():
return render_template('subview.html')
\ No newline at end of file
from flask import render_template, session, redirect
from samsHUB.Wrappers import login_required
@login_required
def home():
return render_template('home.html')
def login():
return render_template('login.html')
def login_post():
session['name'] = 'Platzhalter Name'
return redirect('/')
\ No newline at end of file
from ExampleApp import app
app.run()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment