def path(self, name): if name == "": return "" if name != 'dajaxice/dajaxice.core.js': return context = Context({ 'dajaxice_js_functions': DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT': DajaxiceRequest\ .get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT': DajaxiceRequest\ .get_json2_js_import(), 'DAJAXICE_EXCEPTION': DajaxiceRequest.get_exception_message(), 'DAJAXICE_JS_DOCSTRINGS': DajaxiceRequest.get_js_docstrings(), }) template = loader.get_template('dajaxice/dajaxice.core.js') content = template.render(context) digest = get_hexdigest(content) filename = os.path.join(get_cache_path(), 'dajaxice.core.%s.js' % digest) if self._filename is None or self._filename != filename: with open(filename, 'wb') as fout: fout.write(content) self._filename = filename return filename
def test_is_callable(self): dr = DajaxiceRequest(None, 'dajaxice.tests.test_registered_function') self.failUnless(dr._is_callable()) dr = DajaxiceRequest(None, 'dajaxice.tests.test_ajax_not_registered') self.failIf(dr._is_callable())
def test_is_callable(self): settings.DAJAXICE_FUNCTIONS = ('tests.ajax.test_ajax_registered',) dr = DajaxiceRequest(None, 'tests.test_ajax_registered') self.failUnless(dr._is_callable()) dr = DajaxiceRequest(None, 'tests.test_ajax_not_registered') self.failIf(dr._is_callable())
def test_get_ajax_function(self): # Test modern Import with a real ajax function dr = DajaxiceRequest(None, 'dajaxice.tests.test_foo') function = dr._modern_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)
def js_core(request): """ Return the dajax JS code according settings.DAJAXICE_FUNCTIONS registered functions. """ data = {'dajaxice_js_functions':DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT':DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT':DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION':DajaxiceRequest.get_exception_message() } return render_to_response('dajaxice/dajaxice.core.js', data, mimetype="text/javascript" )
def handle(self, *app_labels, **options): compile_output = options.get('compile','yes') data = {'dajaxice_js_functions':DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT':DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT':DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION':DajaxiceRequest.get_exception_message() } js = render_to_string('dajaxice/dajaxice.core.js', data ) if compile_output.lower() == "closure": print self.complie_js_with_closure(js) else: print js
def dajaxice_js_import(context, core_url=None): # We must force this request to add the csrftoken cookie. request = context.get('request', None) if request: get_token(request) else: log.warning("The 'request' object must be accesible within the context. \ You must add 'django.contrib.messages.context_processors.request' \ to your TEMPLATE_CONTEXT_PROCESSORS and render your views\ using a RequestContext.") if not core_url or DajaxiceRequest.get_debug(): core_url = '/%s/dajaxice.core.js' % DajaxiceRequest.get_media_prefix() else: core_url = '%s%s' % (settings.STATIC_URL or '', core_url,) return {'core_url': core_url}
def test_get_js_functions(self): js_functions = DajaxiceRequest.get_js_functions() functions = [DajaxiceFunction('test_registered_function', 'dajaxice.tests.ajax.test_registered_function'), DajaxiceFunction('test_string', 'dajaxice.tests.ajax.test_string'), DajaxiceFunction('test_ajax_exception', 'dajaxice.tests.ajax.test_ajax_exception'), DajaxiceFunction('test_foo', 'dajaxice.tests.ajax.test_foo'), DajaxiceFunction('test_foo_with_params', 'dajaxice.tests.ajax.test_foo_with_params'), DajaxiceFunction('test_submodule_registered_function', 'dajaxice.tests.submodules.ajax.test_submodule_registered_function')] callables = [f.path for f in functions] self.failUnlessEqual(len(js_functions), 1) self.failUnlessEqual(dajaxice_functions._callable, callables) sub = js_functions[0] self.failUnlessEqual(len(sub.sub_modules), 1) self.failUnlessEqual(len(sub.functions), 0) self.failUnlessEqual(sub.name, 'dajaxice') sub = js_functions[0].sub_modules[0] self.failUnlessEqual(len(sub.sub_modules), 1) self.failUnlessEqual(len(sub.functions), 5) self.failUnlessEqual(sub.functions, functions[:-1]) self.failUnlessEqual(sub.name, 'tests') sub = js_functions[0].sub_modules[0].sub_modules[0] self.failUnlessEqual(len(sub.sub_modules), 0) self.failUnlessEqual(len(sub.functions), 1) self.failUnlessEqual(sub.functions, functions[-1:]) self.failUnlessEqual(sub.name, 'submodules')
def test_get_js_functions(self): js_functions = DajaxiceRequest.get_js_functions() callables = ['dajaxice.tests.ajax.test_registered_function', 'dajaxice.tests.ajax.test_string', 'dajaxice.tests.ajax.test_ajax_exception', 'dajaxice.tests.ajax.test_foo', 'dajaxice.tests.ajax.test_foo_with_params'] functions = [f.rsplit('.',1)[1] for f in callables] self.failUnlessEqual(len(js_functions), 1) self.failUnlessEqual(dajaxice_functions._callable, callables) sub = js_functions[0] self.failUnlessEqual(len(sub.sub_modules), 1) self.failUnlessEqual(len(sub.functions), 0) self.failUnlessEqual(sub.name, 'dajaxice') sub = js_functions[0].sub_modules[0] self.failUnlessEqual(len(sub.sub_modules), 0) self.failUnlessEqual(len(sub.functions), 5) self.failUnlessEqual(sub.functions, functions) self.failUnlessEqual(sub.name, 'tests')
def dajaxice_request(request, call): """ dajaxice_request Uses DajaxRequest to handle dajax request. Return the apropiate json according app_name and method. """ return DajaxiceRequest(request, call).process()
def dajaxice_js_import(context, core_url=None): # We must force this request to add the csrftoken cookie. request = context.get('request', None) if request: get_token(request) else: log.warning( "The 'request' object must be accesible within the context. \ You must add 'django.contrib.messages.context_processors.request' \ to your TEMPLATE_CONTEXT_PROCESSORS and render your views\ using a RequestContext.") if not core_url or DajaxiceRequest.get_debug(): core_url = '/%s/dajaxice.core.js' % DajaxiceRequest.get_media_prefix() else: core_url = '%s%s' % ( settings.STATIC_URL or '', core_url, ) return {'core_url': core_url}
def handle(self, *app_labels, **options): compile_output = options.get('compile', 'yes') data = { 'dajaxice_js_functions': DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT': DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT': DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION': DajaxiceRequest.get_exception_message() } js = render_to_string('dajaxice/dajaxice.core.js', data) if compile_output.lower() == "closure": print self.complie_js_with_closure(js) else: print js
def js_core(request): """ Return the dajax JS code according settings.DAJAXICE_FUNCTIONS registered functions. """ data = { 'dajaxice_js_functions': DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT': DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT': DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION': DajaxiceRequest.get_exception_message(), 'DAJAXICE_JS_DOCSTRINGS': DajaxiceRequest.get_js_docstrings(), 'DEBUG': settings.DEBUG, } return render(request, 'dajaxice/dajaxice.core.js', data, content_type="text/javascript")
def path(self, name): if name == "": return "" if name != 'dajaxice/dajaxice.core.js': return context = Context({'dajaxice_js_functions': DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT': DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT': DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION': DajaxiceRequest.get_exception_message(), 'DAJAXICE_JS_DOCSTRINGS': DajaxiceRequest.get_js_docstrings()}) template = loader.get_template('dajaxice/dajaxice.core.js') self._tmp_file = NamedTemporaryFile(suffix='.js') self._tmp_file.write(template.render(context)) self._tmp_file.flush() return self._tmp_file.name
def test_get_js_functions(self): js_functions = DajaxiceRequest.get_js_functions() functions = [ DajaxiceFunction('test_registered_function', 'dajaxice.tests.ajax.test_registered_function'), DajaxiceFunction('test_string', 'dajaxice.tests.ajax.test_string'), DajaxiceFunction('test_ajax_exception', 'dajaxice.tests.ajax.test_ajax_exception'), DajaxiceFunction('test_foo', 'dajaxice.tests.ajax.test_foo'), DajaxiceFunction('test_foo_with_params', 'dajaxice.tests.ajax.test_foo_with_params'), DajaxiceFunction( 'test_submodule_registered_function', 'dajaxice.tests.submodules.ajax.test_submodule_registered_function' ) ] callables = [f.path for f in functions] self.failUnlessEqual(len(js_functions), 1) self.failUnlessEqual(dajaxice_functions._callable, callables) sub = js_functions[0] self.failUnlessEqual(len(sub.sub_modules), 1) self.failUnlessEqual(len(sub.functions), 0) self.failUnlessEqual(sub.name, 'dajaxice') sub = js_functions[0].sub_modules[0] self.failUnlessEqual(len(sub.sub_modules), 1) self.failUnlessEqual(len(sub.functions), 5) self.failUnlessEqual(sub.functions, functions[:-1]) self.failUnlessEqual(sub.name, 'tests') sub = js_functions[0].sub_modules[0].sub_modules[0] self.failUnlessEqual(len(sub.sub_modules), 0) self.failUnlessEqual(len(sub.functions), 1) self.failUnlessEqual(sub.functions, functions[-1:]) self.failUnlessEqual(sub.name, 'submodules')
def test_get_js_functions(self): settings.DAJAXICE_FUNCTIONS = ('test1.ajax.a', 'test2.ajax.b', 'test2.ajax.c', 'test3.ajax.d',) js_functions = DajaxiceRequest.get_js_functions() self.failUnlessEqual(len(js_functions), 3) self.failUnless('test1' in js_functions.keys()) self.failUnless('test2' in js_functions.keys()) self.failUnless('test3' in js_functions.keys()) self.failUnlessEqual(len(js_functions.get('test1')), 1) self.failUnless('a' in js_functions.get('test1')) self.failUnlessEqual(len(js_functions.get('test2')), 2) self.failUnless('b' in js_functions.get('test2')) self.failUnless('c' in js_functions.get('test2')) self.failUnlessEqual(len(js_functions.get('test3')), 1) self.failUnless('d' in js_functions.get('test3'))
def test_get_ajax_function(self): settings.DAJAXICE_FUNCTIONS = ('tests.ajax.test_foo',) # Test modern Import with a real ajax function dr = DajaxiceRequest(None, 'tests.test_foo') function = dr._modern_get_ajax_function() self.failUnless(hasattr(function, '__call__') ) # Test old Import with a real ajax function dr = DajaxiceRequest(None, '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, 'tests.test_foo2') self.failUnlessRaises(ImportError, dr._modern_get_ajax_function) # Test old Import without a real ajax function dr = DajaxiceRequest(None, 'tests.test_foo2') self.failUnlessRaises(ImportError, dr._old_get_ajax_function)
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH # DAMAGE. #---------------------------------------------------------------------- from django.shortcuts import render_to_response from django.views.decorators.cache import cache_control from dajaxice.core import DajaxiceRequest def dajaxice_request(request, call): """ dajaxice_request Uses DajaxRequest to handle dajax request. Return the apropiate json according app_name and method. """ return DajaxiceRequest(request, call).process() @cache_control(max_age=DajaxiceRequest.get_cache_control()) def js_core(request): """ Return the dajax JS code according settings.DAJAXICE_FUNCTIONS registered functions. """ data = {'dajaxice_js_functions':DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT':DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT':DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION':DajaxiceRequest.get_exception_message() } return render_to_response('dajaxice/dajaxice.core.js', data, mimetype="text/javascript" )
from django.shortcuts import render_to_response from django.views.decorators.cache import cache_control from dajaxice.core import DajaxiceRequest def dajaxice_request(request, call): """ dajaxice_request Uses DajaxRequest to handle dajax request. Return the apropiate json according app_name and method. """ return DajaxiceRequest(request, call).process() @cache_control(max_age=DajaxiceRequest.get_cache_control()) def js_core(request): """ Return the dajax JS code according settings.DAJAXICE_FUNCTIONS registered functions. """ data = { 'dajaxice_js_functions': DajaxiceRequest.get_js_functions(), 'DAJAXICE_URL_PREFIX': DajaxiceRequest.get_media_prefix(), 'DAJAXICE_XMLHTTPREQUEST_JS_IMPORT': DajaxiceRequest.get_xmlhttprequest_js_import(), 'DAJAXICE_JSON2_JS_IMPORT': DajaxiceRequest.get_json2_js_import(), 'DAJAXICE_EXCEPTION':
def dajaxice_js_import(context): return { 'DAJAXICE_MEDIA_PREFIX': DajaxiceRequest.get_media_prefix() }
def dajaxice_js_import(context): return {'DAJAXICE_MEDIA_PREFIX': DajaxiceRequest.get_media_prefix()}