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 {% for title, links in menu_links.items %} 1 {% for title, links in menu_links.items %}
2 {% if links|length == 1 %} 2 {% if links|length == 1 %}
3 - {% for text, link in links %} 3 + {% for colab_url in links %}
4 <li> 4 <li>
5 - <a href="{{ link }}">{{ title }}</a> 5 + <a href="{{ colab_url.url }}">{{ title }}</a>
6 </li> 6 </li>
7 {% endfor %} 7 {% endfor %}
8 {% else %} 8 {% else %}
9 <li class="dropdown"> 9 <li class="dropdown">
10 <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ title }} <b class="caret"></b></a> 10 <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{ title }} <b class="caret"></b></a>
11 <ul class="dropdown-menu"> 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 {% endfor %} 14 {% endfor %}
15 </ul> 15 </ul>
16 </li> 16 </li>
colab/plugins/templatetags/plugins.py
@@ -3,18 +3,22 @@ from collections import OrderedDict @@ -3,18 +3,22 @@ from collections import OrderedDict
3 from django import template 3 from django import template
4 from django.core.cache import cache 4 from django.core.cache import cache
5 from django.template.loader import render_to_string 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 register = template.Library() 8 register = template.Library()
9 9
10 10
11 @register.simple_tag(takes_context=True) 11 @register.simple_tag(takes_context=True)
12 def plugins_menu(context): 12 def plugins_menu(context):
  13 +
13 if context['user'].is_authenticated(): 14 if context['user'].is_authenticated():
14 cache_key = 'colab-proxy-menu-authenticated' 15 cache_key = 'colab-proxy-menu-authenticated'
15 else: 16 else:
16 cache_key = 'colab-proxy-menu-anonymous' 17 cache_key = 'colab-proxy-menu-anonymous'
17 18
  19 + lang = get_language()
  20 + cache_key += '-{}'.format(lang)
  21 +
18 menu_from_cache = cache.get(cache_key) 22 menu_from_cache = cache.get(cache_key)
19 23
20 if menu_from_cache: 24 if menu_from_cache:
@@ -23,25 +27,25 @@ def plugins_menu(context): @@ -23,25 +27,25 @@ def plugins_menu(context):
23 menu_links = OrderedDict() 27 menu_links = OrderedDict()
24 proxied_apps = context.get('proxy', {}) 28 proxied_apps = context.get('proxy', {})
25 29
  30 + # TODO: change name from proxied_apps to something with plugins =)
26 for app_name, app in proxied_apps.items(): 31 for app_name, app in proxied_apps.items():
27 - if not app.get('menu'): 32 + if not app.get('menu_urls'):
28 continue 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 if title not in menu_links: 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 menu = render_to_string('plugins/menu_template.html', 50 menu = render_to_string('plugins/menu_template.html',
47 {'menu_links': menu_links}) 51 {'menu_links': menu_links})
colab/plugins/urls.py
@@ -14,8 +14,8 @@ for app_name, app in settings.COLAB_APPS.items(): @@ -14,8 +14,8 @@ for app_name, app in settings.COLAB_APPS.items():
14 urls = app.get('urls') 14 urls = app.get('urls')
15 if not urls.get('include'): 15 if not urls.get('include'):
16 raise ImproperlyConfigured(undef_url_include_msg) 16 raise ImproperlyConfigured(undef_url_include_msg)
17 - print urls['include']  
18 urlpatterns += patterns('', 17 urlpatterns += patterns('',
19 url(urls.get('prefix', r''), include(urls['include'], 18 url(urls.get('prefix', r''), include(urls['include'],
20 namespace=urls.get('namespace'))), 19 namespace=urls.get('namespace'))),
21 - )  
22 \ No newline at end of file 20 \ No newline at end of file
  21 + )
  22 +
colab/plugins/utils/menu.py 0 → 100644
@@ -0,0 +1,25 @@ @@ -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,8 +140,9 @@ def load_colab_apps():
140 if file_name.endswith('.py'): 140 if file_name.endswith('.py'):
141 file_module = file_name.split('.')[0] 141 file_module = file_name.split('.')[0]
142 py_settings_d = _load_py_file(file_module, plugins_dir) 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 app_name = py_settings_d.get('name') 147 app_name = py_settings_d.get('name')
147 if not app_name: 148 if not app_name:
@@ -149,6 +150,9 @@ def load_colab_apps(): @@ -149,6 +150,9 @@ def load_colab_apps():
149 continue 150 continue
150 151
151 COLAB_APPS[app_name] = {} 152 COLAB_APPS[app_name] = {}
  153 + COLAB_APPS[app_name]['menu_title'] = \
  154 + py_settings_d.get('menu_title')
  155 +
152 for key in fields: 156 for key in fields:
153 value = py_settings_d.get(key) 157 value = py_settings_d.get(key)
154 if value: 158 if value: