|
| 1 | +import logging |
| 2 | + |
| 3 | +from django.utils.importlib import import_module |
| 4 | + |
| 5 | +log = logging.getLogger('dajaxice') |
| 6 | + |
| 7 | + |
| 8 | +class DajaxiceFunction(object): |
| 9 | + """ Basic representation of a dajaxice ajax function.""" |
| 10 | + |
| 11 | + def __init__(self, function, name, method): |
| 12 | + self.function = function |
| 13 | + self.name = name |
| 14 | + self.method = method |
| 15 | + |
| 16 | + def call(self, *args, **kwargs): |
| 17 | + """ Call the function. """ |
| 18 | + return self.function(*args, **kwargs) |
| 19 | + |
| 20 | + |
| 21 | +class DajaxiceModule(object): |
| 22 | + """ Basic representation of a dajaxice module. """ |
| 23 | + |
| 24 | + def __init__(self, name=None): |
| 25 | + self.name = name |
| 26 | + self.functions = {} |
| 27 | + self.submodules = {} |
| 28 | + |
| 29 | + def add(self, name, function): |
| 30 | + """ Add this function at the ``name`` deep. If the submodule already |
| 31 | + exists, recusively call the add method into the submodule. If not, |
| 32 | + create the module and call the add method.""" |
| 33 | + |
| 34 | + # If this is not the final function name (there are more modules) |
| 35 | + # split the name again an register a new submodule. |
| 36 | + if '.' in name: |
| 37 | + module, extra = name.split('.', 1) |
| 38 | + if module not in self.submodules: |
| 39 | + self.submodules[module] = DajaxiceModule(module) |
| 40 | + self.submodules[module].add(extra, function) |
| 41 | + else: |
| 42 | + self.functions[name] = function |
| 43 | + |
| 44 | + |
| 45 | +class Dajaxice(object): |
| 46 | + |
| 47 | + def __init__(self): |
| 48 | + self._registry = {} |
| 49 | + self._modules = None |
| 50 | + |
| 51 | + def register(self, function, name=None, method='POST'): |
| 52 | + """ |
| 53 | + Register this function as a dajaxice function. |
| 54 | +
|
| 55 | + If no name is provided, the module and the function name will be used. |
| 56 | + The final (customized or not) must be unique. """ |
| 57 | + |
| 58 | + method = self.clean_method(method) |
| 59 | + |
| 60 | + # Generate a default name |
| 61 | + if not name: |
| 62 | + module = ''.join(str(function.__module__).rsplit('.ajax', 1)) |
| 63 | + name = '.'.join((module, function.__name__)) |
| 64 | + |
| 65 | + if ':' in name: |
| 66 | + log.error('Ivalid function name %s.' % name) |
| 67 | + return |
| 68 | + |
| 69 | + # Check for already registered functions |
| 70 | + if name in self._registry: |
| 71 | + log.error('%s was already registered.' % name) |
| 72 | + return |
| 73 | + |
| 74 | + # Create the dajaxice function. |
| 75 | + function = DajaxiceFunction(function=function, |
| 76 | + name=name, |
| 77 | + method=method) |
| 78 | + |
| 79 | + # Register this new ajax function |
| 80 | + self._registry[name] = function |
| 81 | + |
| 82 | + def is_callable(self, name, method): |
| 83 | + """ Return if the function callable or not. """ |
| 84 | + return name in self._registry and self._registry[name].method == method |
| 85 | + |
| 86 | + def clean_method(self, method): |
| 87 | + """ Clean the http method. """ |
| 88 | + method = method.upper() |
| 89 | + if method not in ['GET', 'POST']: |
| 90 | + method = 'POST' |
| 91 | + return method |
| 92 | + |
| 93 | + def get(self, name): |
| 94 | + """ Return the dajaxice function.""" |
| 95 | + return self._registry[name] |
| 96 | + |
| 97 | + @property |
| 98 | + def modules(self): |
| 99 | + """ Return an easy to loop module hierarchy with all the functions.""" |
| 100 | + if not self._modules: |
| 101 | + self._modules = DajaxiceModule() |
| 102 | + for name, function in self._registry.items(): |
| 103 | + self._modules.add(name, function) |
| 104 | + return self._modules |
| 105 | + |
| 106 | +LOADING_DAJAXICE = False |
| 107 | + |
| 108 | + |
| 109 | +def dajaxice_autodiscover(): |
| 110 | + """ |
| 111 | + Auto-discover INSTALLED_APPS ajax.py modules and fail silently when |
| 112 | + not present. NOTE: dajaxice_autodiscover was inspired/copied from |
| 113 | + django.contrib.admin autodiscover |
| 114 | + """ |
| 115 | + global LOADING_DAJAXICE |
| 116 | + if LOADING_DAJAXICE: |
| 117 | + return |
| 118 | + LOADING_DAJAXICE = True |
| 119 | + |
| 120 | + import imp |
| 121 | + from django.conf import settings |
| 122 | + |
| 123 | + for app in settings.INSTALLED_APPS: |
| 124 | + |
| 125 | + try: |
| 126 | + app_path = import_module(app).__path__ |
| 127 | + except AttributeError: |
| 128 | + continue |
| 129 | + |
| 130 | + try: |
| 131 | + imp.find_module('ajax', app_path) |
| 132 | + except ImportError: |
| 133 | + continue |
| 134 | + |
| 135 | + import_module("%s.ajax" % app) |
| 136 | + |
| 137 | + LOADING_DAJAXICE = False |
0 commit comments