Commit b588f3b4ffde969967196c63f78ce5a7855430aa
1 parent
370aa526
Exists in
proxy_hotspot
Add hotspots architecture
Showing
4 changed files
with
126 additions
and
1 deletions
Show diff stats
colab/proxy/management/commands/import_proxy_data.py
@@ -5,7 +5,7 @@ import importlib | @@ -5,7 +5,7 @@ import importlib | ||
5 | from django.core.management.base import BaseCommand | 5 | from django.core.management.base import BaseCommand |
6 | from django.conf import settings | 6 | from django.conf import settings |
7 | 7 | ||
8 | -from colab.proxy.proxybase.proxy_data_api import ProxyDataAPI | 8 | +from colab.proxy.utils.proxy_data_api import ProxyDataAPI |
9 | 9 | ||
10 | 10 | ||
11 | class Command(BaseCommand): | 11 | class Command(BaseCommand): |
colab/proxy/templatetags/proxy.py
1 | +import importlib | ||
1 | 2 | ||
2 | from django.core.urlresolvers import reverse | 3 | from django.core.urlresolvers import reverse |
3 | from django import template | 4 | from django import template |
4 | from django.core.cache import cache | 5 | from django.core.cache import cache |
5 | from django.template.loader import render_to_string | 6 | from django.template.loader import render_to_string |
6 | 7 | ||
8 | +from colab.proxy.utils.plugin import ColabPlugin | ||
9 | + | ||
7 | register = template.Library() | 10 | register = template.Library() |
8 | 11 | ||
9 | 12 | ||
@@ -46,3 +49,45 @@ def proxy_menu(context): | @@ -46,3 +49,45 @@ def proxy_menu(context): | ||
46 | 49 | ||
47 | cache.set(cache_key, menu) | 50 | cache.set(cache_key, menu) |
48 | return menu | 51 | return menu |
52 | + | ||
53 | + | ||
54 | +class PluginHotspot(template.Node): | ||
55 | + def __init__(self, plugin_label, hotspot): | ||
56 | + self.hotspot = hotspot | ||
57 | + self.plugin_label = template.Variable(plugin_label) | ||
58 | + | ||
59 | + def render(self, context): | ||
60 | + self.plugin_label = self.plugin_label.resolve(context) | ||
61 | + module_name = \ | ||
62 | + 'colab.proxy.' + self.plugin_label + '.' + self.plugin_label + \ | ||
63 | + '_plugin' | ||
64 | + | ||
65 | + module = importlib.import_module(module_name) | ||
66 | + | ||
67 | + for module_item_name in dir(module): | ||
68 | + | ||
69 | + module_class = getattr(module, module_item_name) | ||
70 | + | ||
71 | + if not isinstance(module_class, type): | ||
72 | + continue | ||
73 | + | ||
74 | + if not issubclass(module_class, ColabPlugin): | ||
75 | + continue | ||
76 | + | ||
77 | + if module_class != ColabPlugin: | ||
78 | + api = module_class() | ||
79 | + break | ||
80 | + | ||
81 | + plugin_function = getattr(api, self.hotspot) | ||
82 | + | ||
83 | + return plugin_function(context) | ||
84 | + | ||
85 | + | ||
86 | +def plugin_hotspot(parser, token): | ||
87 | + bits = token.contents.split() | ||
88 | + if len(bits) != 3: | ||
89 | + raise TypeError("plugin_hotspot tag takes exactly two arguments") | ||
90 | + | ||
91 | + return PluginHotspot(bits[1], bits[2]) | ||
92 | + | ||
93 | +plugin_hotspot = register.tag(plugin_hotspot) |
@@ -0,0 +1,73 @@ | @@ -0,0 +1,73 @@ | ||
1 | +import importlib | ||
2 | + | ||
3 | + | ||
4 | +class ColabPlugin(object): | ||
5 | + ''' | ||
6 | + Class for declaring hotspots | ||
7 | + A hotspot that is called from the html will always pass the context of that | ||
8 | + file. | ||
9 | + A hotspot executed from the code can have any number of parameters. | ||
10 | + ''' | ||
11 | + | ||
12 | + def html_proxy_hotspot(self, context): | ||
13 | + ''' | ||
14 | + Example of html proxy, can be called in the html with: | ||
15 | + | ||
16 | + {% load proxy %} | ||
17 | + | ||
18 | + {% for proxy_label in proxy %} | ||
19 | + {% plugin_hotspot proxy_label menu %} | ||
20 | + {% endfor %} | ||
21 | + | ||
22 | + And at the plugin define a a function at plugiName_plugin.py | ||
23 | + | ||
24 | + def html_proxy_hotspot(self, context): | ||
25 | + return render_to_string('proxy/html_template.html', | ||
26 | + {'context_variable': python_variable}) | ||
27 | + ''' | ||
28 | + pass | ||
29 | + | ||
30 | + def update_profile(self, user, request): | ||
31 | + ''' | ||
32 | + Update the profile data, receives the user object and the UpdateView | ||
33 | + object. Returns nothing | ||
34 | + | ||
35 | + Example of code hotspot, might be called somewhere in the code from: | ||
36 | + | ||
37 | + from colab.proxy.proxy.plugin import plugin_hotspot | ||
38 | + from colab.settings import PROXIED_APPS | ||
39 | + | ||
40 | + | ||
41 | + for plugin in PROXIED_APPS: | ||
42 | + plugin_hotspot(plugin, 'update_profile', var1, var2) | ||
43 | + ''' | ||
44 | + pass | ||
45 | + | ||
46 | + | ||
47 | +def plugin_hotspot(plugin_label, hotspot, *args): | ||
48 | + module_name = \ | ||
49 | + 'colab.proxy.' + plugin_label + '.' + plugin_label + \ | ||
50 | + '_plugin' | ||
51 | + | ||
52 | + module = importlib.import_module(module_name) | ||
53 | + | ||
54 | + for module_item_name in dir(module): | ||
55 | + | ||
56 | + module_class = getattr(module, module_item_name) | ||
57 | + | ||
58 | + if not isinstance(module_class, type): | ||
59 | + continue | ||
60 | + | ||
61 | + if not issubclass(module_class, ColabPlugin): | ||
62 | + continue | ||
63 | + | ||
64 | + if module_class != ColabPlugin: | ||
65 | + api = module_class() | ||
66 | + break | ||
67 | + | ||
68 | + plugin_function = getattr(api, hotspot, None) | ||
69 | + | ||
70 | + if not plugin_function: | ||
71 | + return None | ||
72 | + | ||
73 | + return plugin_function(*args) |