def render_with_toolbars(template, request, data, **kwargs): """ render_with_toolbars(template, request, data, **kwargs) -> HttpResposne Return a HttpResponse of the filebrowser template view, plus all the registered toolbars. This also allow callers to reuse templates in the FileBrowser. """ resp = render(template, request, data, **kwargs) toolbars = filebrowser.plugin.toolbar.all() context = filebrowser.plugin.toolbar.DisplayContext(template, request, data) for tb in toolbars: render_injected(resp, tb.display(context)) return resp
def test_popup_injection(): """Test that result injection works""" base = HttpResponse('<html><head></head><body>Hello</body></html>') resp = django_util.render_injected(base, ' Cookie monster') assert_true('Hello Cookie monster' in resp.content) redirect = HttpResponseRedirect('http://www.cnn.com') resp = django_util.render_injected(redirect, 'Cookie monster') assert_true('Cookie monster' not in resp.content) json = django_util.render_json('blah') resp = django_util.render_injected(json, 'Cookie monster') assert_true('Cookie monster' not in resp.content) assert_raises(AssertionError, django_util.render_injected, "foo", "bar")
def render_with_toolbars(template, request, data, **kwargs): """ render_with_toolbars(template, request, data, **kwargs) -> HttpResposne Return a HttpResponse of the filebrowser template view, plus all the registered toolbars. This also allow callers to reuse templates in the FileBrowser. """ resp = render(template, request, data, **kwargs) toolbars = filebrowser.plugin.toolbar.all() context = filebrowser.plugin.toolbar.DisplayContext( template, request, data) for tb in toolbars: render_injected(resp, tb.display(context)) return resp
def save_results(request, id): """ Save the results of a query to an HDFS directory """ id = int(id) query_history = models.QueryHistory.objects.get(id=id) if query_history.owner != request.user: raise PopupException('This action is only available to the user who submitted the query.') _, state = _get_server_id_and_state(query_history) query_history.save_state(state) error_msg, log = None, None if request.method == 'POST': # Make sure the result is available. # Note that we may still hit errors during the actual save if state != models.QueryHistory.STATE.available: if state in (models.QueryHistory.STATE.failed, models.QueryHistory.STATE.expired): msg = 'This query has %s. Results unavailable.' % (state,) else: msg = 'The result of this query is not available yet.' raise PopupException(msg) form = beeswax.forms.SaveResultsForm(request.POST) # Cancel goes back to results if request.POST.get('cancel'): return format_preserving_redirect(request, '/beeswax/watch/%s' % (id,)) if form.is_valid(): # Do save # 1. Get the results metadata assert request.POST.get('save') handle = QueryHandle(id=query_history.server_id, log_context=query_history.log_context) try: result_meta = db_utils.db_client().get_results_metadata(handle) except QueryNotFoundException, ex: LOG.exception(ex) raise PopupException('Cannot find query.') if result_meta.table_dir: result_meta.table_dir = request.fs.urlsplit(result_meta.table_dir)[2] # 2. Check for partitioned tables if result_meta.table_dir is None: raise PopupException( 'Saving results from a partitioned table is not supported. ' 'You may copy from the HDFS location manually.') # 3. Actual saving of results try: if form.cleaned_data['save_target'] == form.SAVE_TYPE_DIR: # To dir if result_meta.in_tablename: raise PopupException( 'Saving results from a table to a directory is not supported. ' 'You may copy from the HDFS location manually.') target_dir = form.cleaned_data['target_dir'] request.fs.rename_star(result_meta.table_dir, target_dir) LOG.debug("Moved results from %s to %s" % (result_meta.table_dir, target_dir)) query_history.save_state(models.QueryHistory.STATE.expired) fb_url = location_to_url(request, target_dir, strict=False) popup = PopupWithJframe('Query results stored in %s' % (target_dir,), launch_app_name='FileBrowser', launch_app_url=fb_url) return render_injected(list_query_history(request), popup) elif form.cleaned_data['save_target'] == form.SAVE_TYPE_TBL: # To new table try: return _save_results_ctas(request, query_history, form.cleaned_data['target_table'], result_meta) except BeeswaxException, bex: LOG.exception(bex) error_msg, log = expand_exception(bex) except IOError, ex: LOG.exception(ex) error_msg = str(ex)