Skip to content

Commit

Permalink
Fixing #18 #19 and #20
Browse files Browse the repository at this point in the history
 - First dajaxice 0.1.6 commit
 - Dajaxice core was rewrited to solve bad path registration at any module depth.
 - Dajaxice now works with django<=1.0.4 (django.utils.importlib isn't required, but it's recommended).
 - Fixing some characters missing using '+'
 - Some Tests should be rewrited.
  • Loading branch information
Jorge Bastida committed Aug 18, 2010
1 parent c24e0eb commit d5d941b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 79 deletions.
2 changes: 1 addition & 1 deletion dajaxice/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (0, 1, 5, 'beta')
VERSION = (0, 1, 6, 'beta')
106 changes: 81 additions & 25 deletions dajaxice/core/Dajaxice.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@
import logging

from django.conf import settings
from django.utils.importlib import import_module

# Python 2.7 has an importlib with import_module.
# For older Pythons, Django's bundled copy provides it.
# For older Django's dajaxice reduced_import_module.
try:
from importlib import import_module
except:
try:
from django.utils.importlib import import_module
except:
from dajaxice.utils import simple_import_module as import_module

log = logging.getLogger('dajaxice.DajaxiceRequest')

class DajaxiceModule(object):
def __init__(self, module, path):
class DajaxiceFunction(object):

def __init__(self, name, path):
self.name = name
self.path = path

def get_callable_path(self):
return '%s.%s' % (self.path.replace('.ajax',''), self.name)

class DajaxiceModule(object):
def __init__(self, module):
self.functions = []
self.sub_modules = []

module = module.split('.')
self.name = module[0]
self.add(module)

sub_module = module[1:]
if len(sub_module)!=0:
self.add_submodule(sub_module)

def get_module(self, module):
"""
Recursively get_module util we found it.
"""
if len(module) == 0:
return self

for dajaxice_module in self.sub_modules:
if dajaxice_module.name == module[0]:
return dajaxice_module.get_module(module[1:])
return None

def add_function(self, function):
self.functions.append(function)

def has_sub_modules(self):
return len(self.sub_modules) > 0

def add(self, module):
if not hasattr(module,'__iter__'):
module = module.split('.')

if len(module) == 2:
self.add_function(module[1])
def add_submodule(self, module):
"""
Recursively add_submodule, if it's not registered, create it.
"""
if len(module) == 0:
return
else:
sub_module = self.exist_submodule(module[1])
module = '.'.join(module[1:])
sub_module = self.exist_submodule(module[0])

if type(sub_module) == int:
self.sub_modules[sub_module].add(module)
self.sub_modules[sub_module].add_submodule(module[1:])
else:
self.sub_modules.append(DajaxiceModule(module, self.path))
self.sub_modules.append(DajaxiceModule(module))

def exist_submodule(self, name):
"""
Check if submodule name was already registered.
"""
for module in self.sub_modules:
if module.name == name:
return self.sub_modules.index(module)
Expand All @@ -55,21 +88,44 @@ def register(self, function):
self.register_function(function.__module__, function.__name__)

def register_function(self, module, name):
callable_function = '%s.%s' % (module, name)
if callable_function in self._callable:
log.warning('%s already registered as dajaxice function.' % callable_function)
"""
Register function at 'module' depth
"""
#Create the dajaxice function.
function = DajaxiceFunction(name=name, path=module)

#Check for already registered functions.
full_path = '%s.%s' % (module, name)
if full_path in self._callable:
log.warning('%s already registered as dajaxice function.' % full_path)
return

self._callable.append(callable_function)
self._callable.append(full_path)

#Dajaxice path without ajax.
module_without_ajax = module.replace('.ajax','').split('.')

module_without_ajax = module.replace('.ajax','')
module = '%s.%s' % (module_without_ajax, name)
#Register module if necessary.
exist_module = self._exist_module(module_without_ajax[0])

exist_module = self._exist_module(module.split('.')[0])
if type(exist_module) == int:
self._registry[exist_module].add(module)
self._registry[exist_module].add_submodule(module_without_ajax[1:])
else:
self._registry.append(DajaxiceModule(module, module_without_ajax))
self._registry.append(DajaxiceModule(module_without_ajax))

#Register Function
module = self.get_module(module_without_ajax)
if module:
module.add_function(function)

def get_module(self, module):
"""
Recursively get module from registry
"""
for dajaxice_module in self._registry:
if dajaxice_module.name == module[0]:
return dajaxice_module.get_module(module[1:])
return None

def is_callable(self, name):
return name in self._callable
Expand Down
44 changes: 7 additions & 37 deletions dajaxice/core/DajaxiceRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,17 @@

log = logging.getLogger('dajaxice.DajaxiceRequest')

# Python 2.7 has an importlib with import_module; for older Pythons,
# Django's bundled copy provides it.
# Python 2.7 has an importlib with import_module.
# For older Pythons, Django's bundled copy provides it.
# For older Django's dajaxice reduced_import_module.
try:
from importlib import import_module
DAJAXICE_MODERN_IMPORT = True
except:
try:
from django.utils import importlib
DAJAXICE_MODERN_IMPORT = True
from django.utils.importlib import import_module
except:
DAJAXICE_MODERN_IMPORT = False
from dajaxice.utils import simple_import_module as import_module

log.info('DAJAXICE_MODERN_IMPORT=%s' % DAJAXICE_MODERN_IMPORT)

def safe_dict(d):
"""
Expand Down Expand Up @@ -133,34 +131,7 @@ def _get_ajax_function(self):
Return a callable ajax function.
This function should be imported according the Django version.
"""
if DAJAXICE_MODERN_IMPORT:
return self._modern_get_ajax_function()
else:
return self._old_get_ajax_function()

def _old_get_ajax_function(self):
"""
Return a callable ajax function.
This function doesn't uses django.utils.importlib
"""

self.module_import_name = "%s.%s" % ( self.project_name, self.module)
try:
return self._old_import()
except:
self.module_import_name = self.module
return self._old_import()

def _old_import(self):
"""
Import this.module_import_name
This function doesn't uses django.utils.importlib
"""
try:
mod = __import__(self.module_import_name , None, None, [self.method])
return mod.__getattribute__(self.method)
except:
raise DajaxiceImportError()
return self._modern_get_ajax_function()

def _modern_get_ajax_function(self):
"""
Expand All @@ -175,9 +146,8 @@ def _modern_get_ajax_function(self):
return self._modern_import()

def _modern_import(self):
from django.utils import importlib
try:
mod = importlib.import_module(self.module_import_name)
mod = import_module(self.module_import_name)
return mod.__getattribute__(self.method)
except:
raise DajaxiceImportError()
Expand Down
2 changes: 1 addition & 1 deletion dajaxice/templates/dajaxice/dajaxice.core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dajaxice/templates/dajaxice/dajaxice_core_loop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ module.name }}: {
{% for function in module.functions %}
{{ function }}: function(callback_function, argv){
Dajaxice.call('{{module.path}}.{{function}}', callback_function, argv);
{{ function.name }}: function(callback_function, argv){
Dajaxice.call('{{function.get_callable_path}}', callback_function, argv);
}{% if not forloop.last %},{% endif %}
{% endfor %}

Expand Down
10 changes: 0 additions & 10 deletions dajaxice/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

from dajaxice.exceptions import FunctionNotCallableError, DajaxiceImportError
from dajaxice.core import DajaxiceRequest
from dajaxice.core.DajaxiceRequest import DAJAXICE_MODERN_IMPORT
from dajaxice.core.Dajaxice import Dajaxice, DajaxiceModule
from dajaxice.core import dajaxice_functions

Expand Down Expand Up @@ -116,18 +115,9 @@ def test_get_ajax_function(self):
function = dr._modern_get_ajax_function()
self.failUnless(hasattr(function, '__call__') )

# Test old Import with a real ajax function
dr = DajaxiceRequest(None, 'dajaxice.tests.test_foo')
function = dr._old_get_ajax_function()
self.failUnless(hasattr(function, '__call__') )

# Test modern Import without a real ajax function
dr = DajaxiceRequest(None, 'dajaxice.tests.test_foo2')
self.failUnlessRaises(DajaxiceImportError, dr._modern_get_ajax_function)

# Test old Import without a real ajax function
dr = DajaxiceRequest(None, 'dajaxice.tests.test_foo2')
self.failUnlessRaises(DajaxiceImportError, dr._old_get_ajax_function)


class DajaxiceModuleTest(unittest.TestCase):
Expand Down
11 changes: 10 additions & 1 deletion dajaxice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#----------------------------------------------------------------------

def deserialize_form(data):
"""
Create a new QueryDict from a serialized form.
"""
from django.http import QueryDict
data = QueryDict(query_string=unicode(data).encode('utf-8'))
return data
return data

def simple_import_module(name):
"""
Reduced version of import_module
"""
import sys
__import__(name)
return sys.modules[name]
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

setup(
name = "django-dajaxice",
version = "0.1.5",
version = "0.1.6",
author = "Benito Jorge Bastida Perez",
author_email = "jorge@thecodefarm.com",
description = "Agnostic and easy to use ajax library for django",
download_url = "http://cloud.github.com/downloads/jorgebastida/django-dajaxice/django-dajaxice-0.1.5.tar.gz",
download_url = "http://cloud.github.com/downloads/jorgebastida/django-dajaxice/django-dajaxice-0.1.6.tar.gz",
url = "http://dajaxproject.com",
packages= ['dajaxice', 'dajaxice.templatetags', 'dajaxice.core', 'dajaxice.management', 'dajaxice.management.commands'],
package_data = {'dajaxice': ['templates/dajaxice/*']},
Expand Down

0 comments on commit d5d941b

Please sign in to comment.