Commit 0df9cd6eb6a448102b15a1b19a1fb372aeadbd66

Authored by Alexandre Barbosa
1 parent d7c967cc

Load menu from plugins settings dir

Signed-off-by: Alexandre Barbosa <alexandreab@live.com>
Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Signed-off-by: Sergio Oliveira <sergio@tracy.com.br>
colab/plugins/templates/plugins/menu_template.html
1 1 {% for title, links in menu_links.items %}
2 2 {% if links|length == 1 %}
3   - {% for text, link in links %}
  3 + {% for colab_url in links %}
4 4 <li>
5   - <a href="{{ link }}">{{ title }}</a>
  5 + <a href="{{ colab_url.url }}">{{ title }}</a>
6 6 </li>
7 7 {% endfor %}
8 8 {% else %}
9 9 <li class="dropdown">
10 10 <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ title }} <b class="caret"></b></a>
11 11 <ul class="dropdown-menu">
12   - {% for text, link in links %}
13   - <li><a href="{{ link }}">{{ text }}</a></li>
  12 + {% for colab_url in links %}
  13 + <li><a href="{{ colab_url.url }}">{{ colab_url.display }}</a></li>
14 14 {% endfor %}
15 15 </ul>
16 16 </li>
... ...
colab/plugins/templatetags/plugins.py
... ... @@ -3,18 +3,22 @@ from collections import OrderedDict
3 3 from django import template
4 4 from django.core.cache import cache
5 5 from django.template.loader import render_to_string
6   -from django.utils.translation import ugettext_lazy as _
  6 +from django.utils.translation import get_language
7 7  
8 8 register = template.Library()
9 9  
10 10  
11 11 @register.simple_tag(takes_context=True)
12 12 def plugins_menu(context):
  13 +
13 14 if context['user'].is_authenticated():
14 15 cache_key = 'colab-proxy-menu-authenticated'
15 16 else:
16 17 cache_key = 'colab-proxy-menu-anonymous'
17 18  
  19 + lang = get_language()
  20 + cache_key += '-{}'.format(lang)
  21 +
18 22 menu_from_cache = cache.get(cache_key)
19 23  
20 24 if menu_from_cache:
... ... @@ -23,25 +27,25 @@ def plugins_menu(context):
23 27 menu_links = OrderedDict()
24 28 proxied_apps = context.get('proxy', {})
25 29  
  30 + # TODO: change name from proxied_apps to something with plugins =)
26 31 for app_name, app in proxied_apps.items():
27   - if not app.get('menu'):
  32 + if not app.get('menu_urls'):
28 33 continue
29 34  
30   - menu = app.get('menu')
31   - title = menu.get('title', app_name)
32   - links = menu.get('links', tuple())
33   - if context['user'].is_active:
34   - links += menu.get('auth_links', tuple())
35   -
36   - if not links:
37   - continue
  35 + menu = app.get('menu_urls')
  36 + title = app.get('menu_title', app_name)
38 37  
39 38 if title not in menu_links:
40   - menu_links[_(title)] = []
  39 + menu_links[title] = []
  40 +
  41 + for colab_url in menu:
  42 + if not context['user'].is_active and colab_url.auth:
  43 + continue
  44 +
  45 + menu_links[title].append(colab_url)
41 46  
42   - for text, link in links:
43   - url = link
44   - menu_links[_(title)].append((_(text), url))
  47 + if not menu_links[title]:
  48 + del menu_links[title]
45 49  
46 50 menu = render_to_string('plugins/menu_template.html',
47 51 {'menu_links': menu_links})
... ...
colab/plugins/urls.py
... ... @@ -14,8 +14,8 @@ for app_name, app in settings.COLAB_APPS.items():
14 14 urls = app.get('urls')
15 15 if not urls.get('include'):
16 16 raise ImproperlyConfigured(undef_url_include_msg)
17   - print urls['include']
18 17 urlpatterns += patterns('',
19 18 url(urls.get('prefix', r''), include(urls['include'],
20 19 namespace=urls.get('namespace'))),
21   - )
22 20 \ No newline at end of file
  21 + )
  22 +
... ...
colab/plugins/utils/menu.py 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +from django.core.urlresolvers import reverse_lazy
  2 +
  3 +
  4 +class ColabUrl(object):
  5 + def __init__(self, display, url, auth):
  6 + self.display = display
  7 + self.url = url
  8 + self.auth = auth
  9 +
  10 +
  11 +def colab_url_factory(namespace):
  12 +
  13 + def url(display, viewname, namespace=namespace, args=tuple(),
  14 + kwargs={}, auth=False):
  15 +
  16 + if namespace:
  17 + rev_viewname = ':'.join((namespace, viewname))
  18 + else:
  19 + rev_viewname = viewname
  20 +
  21 + url = reverse_lazy(rev_viewname, args=args, kwargs=kwargs)
  22 +
  23 + return ColabUrl(display, url, auth)
  24 +
  25 + return url
... ...
colab/utils/conf.py
... ... @@ -140,8 +140,9 @@ def load_colab_apps():
140 140 if file_name.endswith('.py'):
141 141 file_module = file_name.split('.')[0]
142 142 py_settings_d = _load_py_file(file_module, plugins_dir)
143   - fields = ['urls', 'menu', 'upstream', 'middlewares',
144   - 'dependencies', 'context_processors']
  143 + fields = ['verbose_name', 'upstream', 'urls',
  144 + 'menu_urls', 'middlewares', 'dependencies',
  145 + 'context_processors']
145 146  
146 147 app_name = py_settings_d.get('name')
147 148 if not app_name:
... ... @@ -149,6 +150,9 @@ def load_colab_apps():
149 150 continue
150 151  
151 152 COLAB_APPS[app_name] = {}
  153 + COLAB_APPS[app_name]['menu_title'] = \
  154 + py_settings_d.get('menu_title')
  155 +
152 156 for key in fields:
153 157 value = py_settings_d.get(key)
154 158 if value:
... ...