Skip to content

Commit

Permalink
Extensibility improvements (#36)
Browse files Browse the repository at this point in the history
* Possibility to add templates

* Fix random bogus

* Allow to add menu links
  • Loading branch information
MarekSuchanek committed Dec 3, 2018
1 parent 9e884e8 commit 81099ef
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
13 changes: 13 additions & 0 deletions repocribro/ext_core.py
Expand Up @@ -154,6 +154,19 @@ def provide_models():
from .models import all_models
return all_models

@staticmethod
def provide_template_loader():
from jinja2 import PackageLoader
return PackageLoader('repocribro', 'templates')

@staticmethod
def provide_dropdown_menu_items():
return {
'manage.dashboard': 'Dashboard',
'manage.repositories': 'Repositories',
'manage.organizations': 'Organizations'
}

@staticmethod
def provide_blueprints():
from .controllers import all_blueprints
Expand Down
14 changes: 14 additions & 0 deletions repocribro/extending/extension.py
Expand Up @@ -90,6 +90,14 @@ def provide_models():
"""
return []

@staticmethod
def provide_template_loader():
return None

@staticmethod
def provide_dropdown_menu_items():
return {}

@staticmethod
def provide_blueprints():
"""Extension can provide Flask blueprints to the app by this method
Expand Down Expand Up @@ -155,6 +163,12 @@ def init_security(self):
for action_name in self.provide_actions():
permissions.register_action(action_name)

def init_template_vars(self):
if not hasattr(self.app.config, 'dropdown_menu_items'):
self.app.config.dropdown_menu_items = {}
for route, title in self.provide_dropdown_menu_items().items():
self.app.config.dropdown_menu_items[route] = title

def introduce(self):
"""Hook operation for getting short introduction of extension (mostly
for debug/log purpose)
Expand Down
6 changes: 6 additions & 0 deletions repocribro/repocribro.py
@@ -1,4 +1,5 @@
import flask
import jinja2
import os

from .extending import ExtensionsMaster
Expand Down Expand Up @@ -125,11 +126,16 @@ def create_app(cfg_files=['DEFAULT']):
from .security import permissions
app.container.set_singleton('permissions', permissions)

app.jinja_loader = jinja2.ChoiceLoader(
ext_master.call('provide_template_loader')
)

ext_master.call('init_first')
ext_master.call('init_models')
ext_master.call('init_security')
ext_master.call('init_business')
ext_master.call('init_filters')
ext_master.call('init_template_vars')
ext_master.call('init_blueprints')
ext_master.call('init_container')

Expand Down
1 change: 0 additions & 1 deletion repocribro/security.py
Expand Up @@ -192,7 +192,6 @@ def on_identity_loaded(sender, identity):
"""
user = flask_login.current_user
identity.user = user
print(user)

if hasattr(user, 'id'):
identity.provides.add(
Expand Down
2 changes: 1 addition & 1 deletion repocribro/templates/core/search/repos_tab.html
Expand Up @@ -16,7 +16,7 @@ <h2>Repositories <small>search results</small></h2>
<tbody>
{% for repo in repos if current_user.sees_repo(repo) %}
<tr>
<td><a href="{{ url_for('core.repo_detail', login=repo.owner.login, reponame=repo.name) }}">{{ repo.full_name }}</td></td>
<td><a href="{{ url_for('core.repo_detail', login=repo.owner.login, reponame=repo.name) }}">{{ repo.full_name }}</td>
<td><a href="{{ url_for('core.user_detail', login=repo.owner.login) }}">{{ repo.owner.login }}</a></td>
<td>{{ repo.languages }}</td>
<td>{{ repo.releases|length }}</td>
Expand Down
6 changes: 3 additions & 3 deletions repocribro/templates/header.html
Expand Up @@ -30,9 +30,9 @@
{{ octicon('person') }} <strong>{{ current_user.github_user.login }}</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ url_for('manage.dashboard') }}">Dashboard</a>
<a class="dropdown-item" href="{{ url_for('manage.repositories') }}">Repositories</a>
<a class="dropdown-item" href="{{ url_for('manage.organizations') }}">Organizations</a>
{% for route, title in config.dropdown_menu_items.items() %}
<a class="dropdown-item" href="{{ url_for(route) }}">{{ title }}</a>
{% endfor %}
<div class="dropdown-divider"></div>
{% if current_user.has_role('admin') %}
<a class="dropdown-item" href="{{ url_for('admin.index') }}">Administration</a>
Expand Down
4 changes: 4 additions & 0 deletions repocribro/templates/layout.html
Expand Up @@ -12,6 +12,10 @@

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/octicons/4.4.0/octicons.min.css"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/octicons/4.4.0/font/octicons.min.css"/>

<style type="text/css">
{% block style %}{% endblock %}
</style>
</head>
<body>
<main>
Expand Down

0 comments on commit 81099ef

Please sign in to comment.