def massaged_workflow_actions_for_json(workflow_actions, oozie_coordinator, oozie_bundle): actions = [] action_link_params = {} if oozie_coordinator is not None: action_link_params['coordinator_job_id'] = oozie_coordinator.id if oozie_bundle is not None: action_link_params['bundle_job_id'] = oozie_bundle.id for action in workflow_actions: action_link_params.update({'action': action.id}) massaged_action = { 'id': action.id, 'log': action.externalId and reverse('jobbrowser.views.job_single_logs', kwargs={'job': action.externalId}) or '', 'url': reverse('oozie:list_oozie_workflow_action', kwargs=action_link_params), 'name': escapejs(action.name), 'type': action.type, 'status': action.status, 'externalIdUrl': action.externalId and reverse('jobbrowser.views.single_job', kwargs={'job': action.externalId}) or '', 'externalId': action.externalId and '_'.join(action.externalId.split('_')[-2:]) or '', 'startTime': format_time(action.startTime), 'endTime': format_time(action.endTime), 'retries': action.retries, 'errorCode': escapejs(action.errorCode), 'errorMessage': escapejs(action.errorMessage), 'transition': action.transition, 'data': escapejs(action.data) } actions.append(massaged_action) return actions
def form_dialog_fields(form): """ Translates a Django Form object into a JavaScript list of fields. The resulting list of fields can be used to represent the form dynamically. """ s = '' for field in form: s += "{ name: '%s', " % escapejs(field.name) if field.is_hidden: s += "hidden: true, " else: s += "label: '%s', " % escapejs(field.label_tag(field.label + ":")) if field.field.required: s += "required: true, " if field.field.help_text: s += "help_text: '%s', " % escapejs(field.field.help_text) s += "widget: '%s' }," % escapejs(six.text_type(field)) # Chop off the last ',' return "[ %s ]" % s[:-1]
def massaged_coordinator_actions_for_json(coordinator, oozie_bundle): coordinator_id = coordinator.id coordinator_actions = coordinator.get_working_actions() actions = [] action_link_params = {} if oozie_bundle is not None: action_link_params['bundle_job_id'] = oozie_bundle.id for action in coordinator_actions: action_link_params.update({'job_id': action.externalId, 'coordinator_job_id': coordinator_id}) massaged_action = { 'id': action.id, 'url': action.externalId and reverse('oozie:list_oozie_workflow', kwargs=action_link_params) or '', 'number': action.actionNumber, 'type': action.type, 'status': action.status, 'externalId': action.externalId or '-', 'externalIdUrl': action.externalId and reverse('oozie:list_oozie_workflow_action', kwargs={'action': action.externalId}) or '', 'nominalTime': format_time(action.nominalTime), 'title': action.title, 'createdTime': format_time(action.createdTime), 'lastModifiedTime': format_time(action.lastModifiedTime), 'errorCode': escapejs(action.errorCode), 'errorMessage': escapejs(action.errorMessage), 'missingDependencies': escapejs(action.missingDependencies) } actions.insert(0, massaged_action) return actions
def disqus_show_comments(title=None, url=None, snippet=None, shortname=""): """ Returns the HTML code necessary to display DISQUS comments. """ if not shortname: shortname = settings.DISQUS_WEBSITE_SHORTNAME if title or url or snippet: s = '<script type="text/javascript">' if title: s += 'var disqus_title = "%s";' % escapejs(title) if url: s += 'var disqus_url = "http://%s%s";' % (Site.objects.get_current().domain, escapejs(url)) if snippet: s += 'var disqus_message = "%s";' % escapejs(snippet) s += "</script>" else: s = "" return ( s + """ <div id="disqus_thread"></div> <script type="text/javascript" src="http://disqus.com/forums/%(shortname)s/embed.js"></script> <noscript><p><a href="http://%(shortname)s.disqus.com/?url=ref">View the discussion thread.</a></p></noscript> <p><a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a></p> """ % dict(shortname=shortname) )
def GET(self, path=None): web.header('Content-Type', 'text/html; charset=utf-8', unique=True) self.api = API() i = web.input() if "q" in i.keys(): path = i["q"] if not path: path = "Random OR Image OR Explorer" if path: if not path.find('OR') > -1 and not path.find('AND') > -1: path = escapejs(path).replace(" ", ' OR ') else: path = escapejs(path) now = time.time() self.api.query(path, False) done = time.time() return (Template(template.results, default_filters=['unicode'], input_encoding='utf-8').render_unicode( numfound=self.api.numfound, records=self.api.records, now=now, done=done, keyword=path, baseurl=BASEURL, api=self.api)) return 'Hello, world!'
def edit_plugin(self, request, plugin_id): plugin_id = int(plugin_id) # get the plugin to edit of bail out cms_plugin = get_object_or_404(CMSPlugin, pk=plugin_id) if not has_plugin_permission(request.user, cms_plugin.plugin_type, "change"): return HttpResponseForbidden(_("You don't have permission to add plugins")) # check that the user has permission to change this plugin if not cms_plugin.placeholder.has_change_permission(request): return HttpResponseForbidden(_("You don't have permission to add content here.")) instance, plugin_admin = cms_plugin.get_plugin_instance(self.admin_site) plugin_admin.cms_plugin_instance = cms_plugin plugin_admin.placeholder = cms_plugin.placeholder if request.method == "POST": # set the continue flag, otherwise will plugin_admin make redirect to list # view, which actually does'nt exists post_request = request.POST.copy() post_request['_continue'] = True request.POST = post_request if not instance: # instance doesn't exist, call add view response = plugin_admin.add_view(request) else: # already saved before, call change view # we actually have the instance here, but since i won't override # change_view method, is better if it will be loaded again, so # just pass id to plugin_admin response = plugin_admin.change_view(request, str(plugin_id)) if request.method == "POST" and plugin_admin.object_successfully_changed: # read the saved object from plugin_admin - ugly but works saved_object = plugin_admin.saved_object context = { 'CMS_MEDIA_URL': settings.CMS_MEDIA_URL, 'plugin': saved_object, 'is_popup': True, 'name': unicode(saved_object), "type": saved_object.get_plugin_name(), 'plugin_id': plugin_id, 'icon': force_escape(escapejs(saved_object.get_instance_icon_src())), 'alt': force_escape(escapejs(saved_object.get_instance_icon_alt())), } return render_to_response('admin/cms/page/plugin_forms_ok.html', context, RequestContext(request)) return response
def render(self, context): kwargs = self._prepare_args(context, 'map', accept_args = map_args) provider = context.get('_BAMBU_MAPPING_PROVIDER') if not provider: raise template.TemplateSyntaxError("'marker' tag must be placed within a 'map' tag") if self not in context.render_context: count = 1 else: count = context.render_context[self] options = '{%s}' % ', '.join( [ '%s: %s' % v for v in { 'lat': kwargs.get('lat', 'null'), 'lon': kwargs.get('lon', 'null'), 'draggable': kwargs.get('draggable') and 'true' or 'false', 'title': 'title' in kwargs and ("'%s'" % escapejs(kwargs['title'])) or 'null', 'icon': 'icon' in kwargs and ("'%s'" % escapejs(kwargs['icon'])) or 'null', 'content': 'content' in kwargs and ( "'%s'" % escapejs( linebreaks(kwargs['content']) ) ) or 'null' }.items() ] ) portions = [] portions.append( "bambu_mapping.markers['%s'].push((%s(%s)));" % ( provider.container_id, provider.add_marker(), options ) ) if kwargs.get('callback'): portions.append( provider.add_marker_callback( "bambu_mapping.markers['%s'][%d]" % ( provider.container_id, count - 1 ), kwargs['callback'] ) ) count += 1 context.render_context[self] = count return ''.join(portions)
def _normalize_text_for_edit(context, text, rich_text, escape_js=False): text = normalize_text_for_edit(context["request"].user, text, rich_text, escape_html=not escape_js) if escape_js: text = escapejs(text) return text
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) context = { 'current_repr': mark_safe("'%s'" % escapejs(value)), 'current_id': value, 'help_text': self.help_text, 'html_id': self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': reverse('ajax_lookup', args=[self.channel]), 'name': name, 'extra_attrs':mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-",""), } context.update(bootstrap()) templates = ('autocomplete_%s.html' % self.channel, 'autocomplete.html') return mark_safe(render_to_string(templates, context))
def GET(self, path): try: # Get parsed Europeana fullrecord-data from url. if path.find('/www.europeana.eu/portal/') > -1: fullrecord = self.api.get_parse_europeana_object( escapejs(path)) except: # Else return empty record. return ({}) words = [] for item in fullrecord.keys(): word_list = self.__strclean__(fullrecord[item]) for word in word_list: if len(word) > 4: words.append(word) fullrecord["word"] = words referer = web.ctx.env.get('HTTP_REFERER') if referer: if referer.startswith( 'http://www.europeana.fe2.nl') or referer.startswith( 'http://europeana.fe2.nl'): return (simplejson.dumps(fullrecord)) else: return ( '<a href="http://europeana.fe2.nl/">http://europeana.fe2.nl/</a>' ) return ( '<a href="http://europeana.fe2.nl/">http://europeana.fe2.nl/</a>')
def as_ul(self, jsChangeCallback = None): forms = u' '.join([self._surround_with_div(form.name, i, form.as_ul()) for i, form in enumerate(self)]) return mark_safe(u'\n'.join([unicode(self.management_form), forms]) + \ REG_FORMSET_JS % { 'div_empty_form' : escapejs(self.div_empty_form), 'form_name' : self.empty_form.name, 'form_class' : self.form_css_class, 'change_callback': jsChangeCallback or 'null'})
def escape_js_string(js_string): """ Escape a javascript string that is safe to be embedded in HTML. Usage: Can be used inside a Mako template inside a <SCRIPT> as follows: var my_js_string = "${escape_js_string(my_js_string) | n}" Must include the surrounding quotes for the string. Use the "n" Mako filter above. It is possible that the default filter may include html escaping in the future, and we must make sure to get the proper escaping. Mako's default filter decode.utf8 is applied here since this default filter is skipped in the Mako template with "n". Arguments: js_string (string): The javascript string to be escaped Returns: (string) Escaped javascript as unicode """ js_string = decode.utf8(js_string) js_string = escapejs(js_string) return js_string
def show_tables(request, database=None): if database is None: database = _get_last_database(request, database) if request.method == 'POST': resp = {} try: tables = _get_table_list(request, database) table_list_rendered = django_mako.render_to_string( "table_list.mako", dict( app_name=get_app_name(request), database=database, tables=tables, )) except Exception as ex: resp['error'] = escapejs(ex.message) else: resp['table_list_rendered'] = table_list_rendered resp['tables'] = tables return HttpResponse(json.dumps(resp)) db = dbms.get(request.user) databases = db.get_databases() db_form = hcatalog.forms.DbForm(initial={'database': database}, databases=databases) response = render("show_tables.mako", request, { 'database': database, 'db_form': db_form, }) response.set_cookie("hueHcatalogLastDatabase", database, expires=90) return response
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) if value: objs = lookup.get_objects([value]) try: obj = objs[0] except IndexError: raise Exception("%s cannot find object:%s" % (lookup, value)) display = lookup.format_item_display(obj) current_repr = mark_safe( """new Array("%s",%s)""" % (escapejs(display),obj.pk) ) else: current_repr = 'null' context = { 'name': name, 'html_id' : self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': reverse('ajax_lookup',kwargs={'channel':self.channel}), 'current_id': value, 'current_repr': current_repr, 'help_text': self.help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-",""), 'add_link' : self.add_link, } context.update(bootstrap()) return mark_safe(render_to_string(('autocompleteselect_%s.html' % self.channel, 'autocompleteselect.html'),context))
def events_embed_legacy(request): # prepare template context try: feedlist, feedtitle = get_feed_list(request) except ObjectDoesNotExist: raise Http404() try: count = int(request.GET.get('count', '')) except ValueError: count = 10 events = Feed.get_events_for(feedlist, count) context = { "feeds": feedlist, "events": events, "title": feedtitle, "link": feedlist[0].link if len(feedlist) == 1 else None } # render the HTML from django.template import Template, Context, RequestContext, loader html = loader.get_template("events/events_embed_legacy.html")\ .render(RequestContext(request, context)) # convert to Javascript document.write statements. from django.utils.html import strip_spaces_between_tags from django.template.defaultfilters import escapejs html = strip_spaces_between_tags(html) js = "" while len(html) > 0: js += "document.write(\"" + escapejs(html[0:128]) + "\");\n" html = html[128:] return HttpResponse(js, mimetype="application/x-javascript; charset=UTF-8")
def massaged_oozie_jobs_for_json(oozie_jobs, user): jobs = [] for job in oozie_jobs: if job.is_running(): if job.type == 'Workflow': job = get_oozie().get_job(job.id) else: job = get_oozie().get_coordinator(job.id) massaged_job = { 'id': job.id, 'lastModTime': hasattr(job, 'lastModTime') and job.lastModTime and format_time(job.lastModTime) or None, 'endTime': job.endTime and format_time(job.endTime) or None, 'status': job.status, 'isRunning': job.is_running(), 'duration': job.endTime and job.startTime and format_duration_in_millis(( time.mktime(job.endTime) - time.mktime(job.startTime) ) * 1000) or None, 'appName': escapejs(job.appName), 'progress': job.get_progress(), 'user': job.user, 'absoluteUrl': job.get_absolute_url(), 'canEdit': has_job_edition_permission(job, user), 'killUrl': reverse('oozie:manage_oozie_jobs', kwargs={'job_id':job.id, 'action':'kill'}), } jobs.append(massaged_job) return jobs
def show_tables(request, database=None): if database is None: database = _get_last_database(request, database) if request.method == 'POST': resp = {} try: tables = _get_table_list(request, database) table_list_rendered = django_mako.render_to_string("table_list.mako", dict( app_name=get_app_name(request), database=database, tables=tables, )) except Exception as ex: resp['error'] = escapejs(ex.message) else: resp['table_list_rendered'] = table_list_rendered resp['tables'] = tables return HttpResponse(json.dumps(resp)) db = dbms.get(request.user) databases = db.get_databases() db_form = hcatalog.forms.DbForm(initial={'database': database}, databases=databases) response = render("show_tables.mako", request, { 'database': database, 'db_form': db_form, }) response.set_cookie("hueHcatalogLastDatabase", database, expires=90) return response
def massaged_oozie_jobs_for_json(oozie_jobs, user): jobs = [] for job in oozie_jobs: if job.is_running(): if job.type == 'Workflow': job = get_oozie().get_job(job.id) else: job = get_oozie().get_coordinator(job.id) massaged_job = { 'id': job.id, 'lastModTime': time.mktime(job.lastModTime), 'endTime': time.mktime(job.endTime), 'status': job.status, 'isRunning': job.is_running(), 'duration': job.endTime and job.startTime and ( time.mktime(job.endTime) - time.mktime(job.startTime) ) * 1000 or None, 'appName': escapejs(job.appName), 'progress': job.get_progress(), 'user': job.user, 'absoluteUrl': job.get_absolute_url(), 'canEdit': has_job_edition_permission(job, user), } jobs.append(massaged_job) return jobs
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) context = { 'current_repr': mark_safe("'%s'" % escapejs(value)), 'current_id': value, 'help_text': self.help_text, 'html_id': self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': reverse('ajax_lookup', args=[self.channel]), 'name': name, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), } context.update(bootstrap()) templates = ('autocomplete_%s.html' % self.channel, 'autocomplete.html') return mark_safe(render_to_string(templates, context))
def events_embed_legacy(request): # prepare template context feedlist, feedtitle = get_feed_list(request) try: count = int(request.GET.get('count', '')) except ValueError: count = 10 events = Feed.get_events_for(feedlist, count) context = { "feeds": feedlist, "events": events, "title": feedtitle, "link": feedlist[0].link if len(feedlist) == 1 else None } # render the HTML from django.template import Template, Context, RequestContext, loader html = loader.get_template("events/events_embed_legacy.html")\ .render(RequestContext(request, context)) # convert to Javascript document.write statements. from django.utils.html import strip_spaces_between_tags from django.template.defaultfilters import escapejs html = strip_spaces_between_tags(html) js = "" while len(html) > 0: js += "document.write(\"" + escapejs(html[0:128]) + "\");\n" html = html[128:] return HttpResponse(js, mimetype="application/x-javascript; charset=UTF-8")
def index(request): preloadData = dict() if request.user.is_authenticated(): preloadData["user"] = ShortProfielSerializer(request.profiel).data return render(request, "main.html", context={"preload_data": escapejs(json.dumps(preloadData))})
def response_add(self, request, obj, post_url_continue='../%s/'): """ Determines the HttpResponse for the add_view stage. """ opts = obj._meta pk_value = obj._get_pk_val() msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj)} # Here, we distinguish between different save types by checking for # the presence of keys in request.POST. if request.POST.has_key("_continue"): self.message_user(request, msg + ' ' + _("You may edit it again below.")) if request.POST.has_key("_popup"): post_url_continue += "?_popup=1" return HttpResponseRedirect(post_url_continue % pk_value) if request.POST.has_key("_popup"): return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ # escape() calls force_unicode. (escape(pk_value), escapejs(obj))) elif request.POST.has_key("_addanother"): self.message_user(request, msg + ' ' + (_("You may add another %s below.") % force_unicode(opts.verbose_name))) return HttpResponseRedirect(request.path) else: self.message_user(request, msg) # Redirecciono para la vista del contenedor del padre del objeto creado if obj.parent is not None: obj_parent = obj.parent url_to_redirect = '/%s/%s/%s/%s/details' % (atlsettings.ADMIN_PREFIX, obj_parent.content_type.app_label, obj_parent.content_type.model, obj_parent.id) else: url_to_redirect = '/%s' % atlsettings.ADMIN_PREFIX return HttpResponseRedirect(url_to_redirect)
def _normalize_text_for_edit(context, text, rich_text, escape_js=False): text = normalize_text_for_edit(context['request'].user, text, rich_text, escape_html=not escape_js) if escape_js: text = escapejs(text) return text
def list_designs(request): ''' List all workflow designs. Result sorted by last modification time. Query params: owner - Substring filter by owner field name - Substring filter by design name field ''' data = models.OozieDesign.objects owner = request.GET.get('owner', '') name = request.GET.get('name', '') if owner: data = data.filter(owner__username__icontains=owner) if name: data = data.filter(name__icontains=name) data = data.order_by('-last_modified') show_install_examples = \ request.user.is_superuser and not jobsub_setup.Command().has_been_setup() designs = [] for design in data: ko_design = { 'id': design.id, 'owner': escapejs(design.owner.username), 'name': escapejs(design.name), 'description': escapejs(design.description), 'type': design.root_action.action_type, 'last_modified': py_time.mktime(design.last_modified.timetuple()), 'url_params': urlresolvers.reverse(jobsub.views.get_design_params, kwargs={'design_id': design.id}), 'url_submit': urlresolvers.reverse(jobsub.views.submit_design, kwargs={'design_id': design.id}), 'url_edit': urlresolvers.reverse(jobsub.views.edit_design, kwargs={'design_id': design.id}), 'url_delete': urlresolvers.reverse(jobsub.views.delete_design, kwargs={'design_id': design.id}), 'url_clone': urlresolvers.reverse(jobsub.views.clone_design, kwargs={'design_id': design.id}), 'can_submit': request.user.username == design.owner.username, 'can_delete': request.user.is_superuser or request.user.username == design.owner.username } designs.append(ko_design) return render("list_designs.mako", request, { 'currentuser': request.user, 'owner': owner, 'name': name, 'designs': json.dumps(designs), 'show_install_examples': show_install_examples, })
def index(request): preloadData = dict() if request.user.is_authenticated(): preloadData["user"] = ShortProfielSerializer(request.profiel).data return render(request, 'main.html', context={'preload_data': escapejs(json.dumps(preloadData))})
def as_ul(self, jsChangeCallback=None): forms = u' '.join([ self._surround_with_div(form.name, i, form.as_ul()) for i, form in enumerate(self) ]) return mark_safe(u'\n'.join([unicode(self.management_form), forms]) + \ REG_FORMSET_JS % { 'div_empty_form' : escapejs(self.div_empty_form), 'form_name' : self.empty_form.name, 'form_class' : self.form_css_class, 'change_callback': jsChangeCallback or 'null'})
def required_validator(self, field): message = field.error_messages[u'required'] return u'''{ validate: function (value) { if (!value && value !== 0) { return "%s"; } }, stopOnError: true }''' % escapejs(message)
def respond_script(self, obj=None): if obj is None: obj = self.object value = obj.serializable_value(obj._meta.pk.attname) html = ['<!DOCTYPE html>', '<html><body>'] html.append('<script type="text/javascript">') html.append( 'opener.dismissAddAnotherPopup( window, "%s", "%s" );' % ( escapejs(six.text_type(value)), escapejs(six.text_type(obj)) ) ) html.append('</script></body></html>') html = ''.join(html) return http.HttpResponse(html, status=201)
def response_add(self, request, obj, post_url_continue=None): """ Overload the admin.ModelAdmin method. This method is called by the ModelAdmin.add_view. It make the Http response if forms is valid and object created. The template scope is to close the modal and reload the page. """ context = { 'CMS_MEDIA_URL': settings.CMS_MEDIA_URL, 'plugin': obj, 'is_popup': True, 'name': unicode(obj), "type": obj.get_plugin_name(), 'plugin_id': obj.pk, 'icon': force_escape(escapejs(obj.get_instance_icon_src())), 'alt': force_escape(escapejs(obj.get_instance_icon_alt())), } return render_to_response('admin/cms/page/plugin_forms_ok.html', context, RequestContext(request))
def rating_one_dp(value): def change_to_5star(i): return (float(i) / 100) * 5 try: import math return str(math.floor(float(change_to_5star(value)) * 10) / 10) except Exception as e: return '0.0' return escapejs(json.dumps(value))
def _select(self, obj): from django.contrib.admin.views.main import IS_POPUP_VAR if IS_POPUP_VAR in self.request.GET and self.request.GET[IS_POPUP_VAR] == '3': return '<span class="fb_icon"> \ <button class="button fb_selectlink" onclick="OpenFile(\'' \ + escapejs(obj.url) + \ '\');return false;">' + _u('Select') + '</button> \ </span>' else: return '-'
def render(self, request): if not self.settings.get('ID'): LOGGER.warn(u'No Google Analytics ID specified') return render_to_string( 'analytics/universal.inc.html' ) queue = [] for (event, args) in self.events: event_args = [ mark_safe(u"'%s'" % event) ] for arg in args: if isinstance(arg, (str, unicode)): event_args.append( mark_safe(u"'%s'" % arg) ) elif isinstance(arg, dict): event_args.append( mark_safe( json.dumps(arg) ) ) elif isinstance(arg, bool): event_args.append( arg and u'true' or u'false' ) else: event_args.append( escapejs(arg) ) queue.append(event_args) if self.trans and not self.trans_tracked: queue.append( ('require', "'ecommerce'", "'ecommerce.js'") ); self.trans_tracked = True self.events = [] self.trans = False self.trans_tracked = False return render_to_string( 'analytics/universal.inc.html', { 'id': self.settings['ID'], 'domain': request.META.get('HTTP_HOST'), 'queue': queue, 'DEBUG': getattr(settings, 'DEBUG', True) } )
def get_json_str_add_user_via_email(email, success, msg_for_user): """ Return a JSON object with 'email', 'success', and 'msg' attributes """ if success: success_str = 'true' else: success_str = 'false' json_str = """{ "email" : "%s", "success": %s, "msg" : "%s" }""" % (escapejs(email), success_str, msg_for_user.replace('"','')) return json_str
def page_component(context, key, default='', formatting="html"): pc, created = PageComponent.objects.get_or_create(key=key) if created and default: pc.title = key.replace('_', ' ').title() pc.html = default pc.save() if formatting == "html": return mark_safe(u"".join((open_admin_edit_region(context, pc), pc.html or " ", close_admin_edit_region()))) else: return escapejs(striptags(pc.html))
def get_design(request, design_id): workflow = _get_design(design_id) node = workflow.start.get_child('to') node_dict = model_to_dict(node) node_dict['id'] = design_id for key in node_dict: if key not in JSON_FIELDS: if key not in SKIP_ESCAPE: node_dict[key] = escapejs(node_dict[key]) node_dict['editable'] = workflow.owner.id == request.user.id return render_json(node_dict);
def get_design(request, design_id): workflow = _get_design(design_id) _check_permission(request, workflow.owner.username, _("Access denied: edit design %(id)s.") % {"id": design_id}) node = workflow.start.get_child("to") node_dict = model_to_dict(node) node_dict["id"] = design_id for key in node_dict: if key not in JSON_FIELDS: node_dict[key] = escapejs(node_dict[key]) node_dict["editable"] = True return render_json(node_dict)
def config_photo_dialog(self, request, photo_slug): photo = self.page.photos.get(slug=photo_slug) context = { 'photo' : photo, 'back' : request.REQUEST.get('back', None) } if request.method == 'GET': context['form'] = ConfigPhotoForm(photo) elif request.method == 'POST': form = ConfigPhotoForm(photo, request.POST, request.FILES) if form.is_valid(): caption = form.cleaned_data.get('caption', None) if caption: # Saving this will rebase the slug on the caption photo.caption = caption photo.save() img_args_list = [ photo.slug, 'thumb' ] try: width = int(form.cleaned_data.get('size', '')) if width > 0 and width != default_thumb_width: img_args_list.append(str(min(600, width)) + 'px') except ValueError: pass align = form.cleaned_data.get('align', None) # Don't specify 'right' since that is the default for thumb if align in ('left', 'center'): img_args_list.append(align) if caption: img_args_list.append(wiki_img_clean_caption(caption)) img_args = '|'.join(img_args_list) markup = '<<img %s>>' % img_args html = wiki_img_args_to_html(self.page, True, img_args) context['script'] = 'parent.done_uploading("%s", "%s");' % ( escapejs(markup), escapejs(html)) return django_render_to_response( 'common/run_script.html', context) else: context['form'] = form return django_render_to_response( 'common/config_photo_dialog.html', context)
def edit_plugin(self, request, plugin_id): plugin_id = int(plugin_id) cms_plugin = get_object_or_404(CMSPlugin, pk=plugin_id) instance, plugin_admin = cms_plugin.get_plugin_instance(self.admin_site) plugin_admin.cms_plugin_instance = cms_plugin plugin_admin.placeholder = cms_plugin.placeholder if request.method == "POST": # set the continue flag, otherwise will plugin_admin make redirect to list # view, which actually does'nt exists request.POST["_continue"] = True if not instance: # instance doesn't exist, call add view response = plugin_admin.add_view(request) else: # already saved before, call change view # we actually have the instance here, but since i won't override # change_view method, is better if it will be loaded again, so # just pass id to plugin_admin response = plugin_admin.change_view(request, str(plugin_id)) if request.method == "POST" and plugin_admin.object_successfully_changed: # read the saved object from plugin_admin - ugly but works saved_object = plugin_admin.saved_object context = { "CMS_MEDIA_URL": settings.CMS_MEDIA_URL, "plugin": saved_object, "is_popup": True, "name": unicode(saved_object), "type": saved_object.get_plugin_name(), "plugin_id": plugin_id, "icon": force_escape(escapejs(saved_object.get_instance_icon_src())), "alt": force_escape(escapejs(saved_object.get_instance_icon_alt())), } return render_to_response("admin/cms/page/plugin_forms_ok.html", context, RequestContext(request)) return response
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) current_name = "" # the text field starts empty # eg. value = [3002L, 1194L] if value: current_ids = "|" + "|".join( str(pk) for pk in value) + "|" # |pk|pk| of current else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items current_repr_json = [] for obj in objects: repr = lookup.format_item(obj) current_repr_json.append("""new Array("%s",%s)""" % (escapejs(repr), obj.pk)) current_reprs = mark_safe("new Array(%s)" % ",".join(current_repr_json)) if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'lookup_url': reverse('ajax_lookup', kwargs={'channel': self.channel}), 'current': value, 'current_name': current_name, 'current_ids': current_ids, 'current_reprs': current_reprs, 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, 'admin_media_prefix': settings.ADMIN_MEDIA_PREFIX } return mark_safe( render_to_string( ('autocompleteselectmultiple_%s.html' % self.channel, 'autocompleteselectmultiple.html'), context))
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) # eg. value = [3002L, 1194L] if value: current_ids = "|" + "|".join( str(pk) for pk in value) + "|" # |pk|pk| of current else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items current_repr_json = [] for obj in objects: display = lookup.format_item_display(obj) current_repr_json.append("""new Array("%s",%s)""" % (escapejs(display), obj.pk)) current_reprs = mark_safe("new Array(%s)" % ",".join(current_repr_json)) if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id': self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': reverse('ajax_lookup', kwargs={'channel': self.channel}), 'current': value, 'current_ids': current_ids, 'current_reprs': current_reprs, 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-", ""), 'add_link': self.add_link, } context.update(bootstrap()) return mark_safe( render_to_string( ('autocompleteselectmultiple_%s.html' % self.channel, 'autocompleteselectmultiple.html'), context))
def infoBoxCodeForMap(self, index=None, indexCount=None): if (index is None) or (indexCount is None) or not self.canBeMapped(): return None js_url = self.get_absolute_url() prev_index = (index + indexCount - 1) % indexCount next_index = (index + indexCount + 1) % indexCount picture = self.get_random_picture() if picture: div_name = 'mapinfo' picture_code = '<div class="thumbnail"><img src="%s" alt="%s"/></div>' % ( picture.thumbnail.url, escapejs(doslinebreaksbr(escape(picture.desc)))) else: div_name = 'mapinfo-nopic' picture_code = '' code = r""" { lat: %s, lng: %s, current_project: %d, code: ' \ <div class="%s"> \ <h3> <a target="_blank" href="%s"> %s, %s </a> </h3> \ %s \ <p> %s </p> \ <div class="bottom"> \ <div class="bottom-left"><a target="_blank" href="%s">Read More</a></div> \ <div class="bottom-right"> \ </div> \ <br style="clear: both;" /> \ </div> \ </div>' }""" % (self.location.latitude, self.location.longitude, self.is_current(), div_name, js_url, escapejs(escape( self.name)), escapejs(escape( self.location.name)), picture_code, escapejs(doslinebreaksbr(escape(truncatewords(self.desc, 30)))), js_url) return code
def jsdata(value, name): return mark_safe('\n'.join([ '<script id="%s-json" type="application/json">' % escape(name), escape(json.dumps(value, indent=4)), '</script>', dedent(''' <script type="text/javascript"> var script = document.getElementById('%(name)s-json'); var div = document.createElement('div'); div.innerHTML = script.innerHTML; var text = div.textContent || div.innerText; window['%(name)s'] = JSON.parse(text); </script>''') % dict(name=escapejs(name)) ]))
def massaged_workflow_actions_for_json(workflow_actions): actions = [] for action in workflow_actions: massaged_action = { 'id': action.id, 'log': action.externalId and reverse('jobbrowser.views.job_single_logs', kwargs={'job': action.externalId}) or '', 'url': reverse('oozie:list_oozie_workflow_action', kwargs={'action': action.id}), 'name': escapejs(action.name), 'type': action.type, 'status': action.status, 'externalIdUrl': action.externalId and reverse('jobbrowser.views.single_job', kwargs={'job': action.externalId}) or '', 'externalId': action.externalId and '_'.join(action.externalId.split('_')[-2:]) or '', 'startTime': format_time(action.startTime), 'endTime': format_time(action.endTime), 'retries': action.retries, 'errorMessage': escapejs(action.errorMessage), 'transition': action.transition, 'data': escapejs(action.data) } actions.append(massaged_action) return actions
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) # eg. value = [3002L, 1194L] if value: current_ids = "|" + "|".join( str(pk) for pk in value ) + "|" # |pk|pk| of current else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items current_repr_json = [] for obj in objects: display = lookup.format_item_display(obj) current_repr_json.append( """new Array("%s",%s)""" % (escapejs(display),obj.pk) ) current_reprs = mark_safe("new Array(%s)" % ",".join(current_repr_json)) if self.show_help_text: help_text = self.help_text else: help_text = '' lookup_url = reverse('ajax_lookup',kwargs={'channel':self.channel}) lookup_params = urllib.urlencode(self.extra_params_url) if lookup_params: lookup_url += '?%s' % lookup_params context = { 'name':name, 'html_id':self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': lookup_url, 'current':value, 'current_ids':current_ids, 'current_reprs': current_reprs, 'help_text':help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-",""), 'add_link' : self.add_link, } context.update(bootstrap()) context.update(self.extra_context) return mark_safe(render_to_string(('autocompleteselectmultiple_%s.html' % self.channel, 'autocompleteselectmultiple.html'),context))
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) lookup = get_lookup(self.channel) current_name = "" # the text field starts empty # eg. value = [3002L, 1194L] if value: current_ids = "|" + "|".join(str(pk) for pk in value) + "|" # |pk|pk| of current else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items current_repr_json = [] for obj in objects: repr = lookup.format_item(obj) current_repr_json.append("""new Array("%s",%s)""" % (escapejs(repr), obj.pk)) current_reprs = mark_safe("new Array(%s)" % ",".join(current_repr_json)) if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "name": name, "html_id": self.html_id, "lookup_url": reverse("ajax_lookup", kwargs={"channel": self.channel}), "current": value, "current_name": current_name, "current_ids": current_ids, "current_reprs": current_reprs, "help_text": help_text, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), "add_link": self.add_link, "admin_media_prefix": settings.ADMIN_MEDIA_PREFIX, } return mark_safe( render_to_string( ("autocompleteselectmultiple_%s.html" % self.channel, "autocompleteselectmultiple.html"), context ) )
def render(self, name, value, attrs=None): if value is None: value = [] final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop("id", name) lookup = get_lookup(self.channel) # eg. value = [3002L, 1194L] if value: current_ids = "|" + "|".join(str(pk) for pk in value) + "|" # |pk|pk| of current else: current_ids = "|" objects = lookup.get_objects(value) # text repr of currently selected items current_repr_json = [] for obj in objects: display = lookup.format_item_display(obj) current_repr_json.append("""new Array("%s",%s)""" % (escapejs(display), obj.pk)) current_reprs = mark_safe("new Array(%s)" % ",".join(current_repr_json)) if self.show_help_text: help_text = self.help_text else: help_text = "" context = { "name": name, "html_id": self.html_id, "min_length": getattr(lookup, "min_length", 1), "lookup_url": reverse("ajax_lookup", kwargs={"channel": self.channel}), "current": value, "current_ids": current_ids, "current_reprs": current_reprs, "help_text": help_text, "extra_attrs": mark_safe(flatatt(final_attrs)), "func_slug": self.html_id.replace("-", ""), "add_link": self.add_link, } context.update(bootstrap()) return mark_safe( render_to_string( ("autocompleteselectmultiple_%s.html" % self.channel, "autocompleteselectmultiple.html"), context ) )
def render(self, context): request = context.get("request", None) footer = [ u''' <script type="text/javascript"> $(document).ready(function(){ ''' ] footer += [ u''' $("#coop-bar").draggable(); $("a.colorbox-form").colorboxify({ close_popup_and_media_slide: function() { $.colorbox.close(); $("#coopbar_medialibrary").click(); } }); ''' ] # messages framework display logger = getLogger('default') from django.contrib import messages msg_list = messages.get_messages(request) if msg_list: from django.template.defaultfilters import escapejs for m in msg_list: logger.debug(unicode(m)) footer += [ u''' humanMsg.displayMsg("''' + escapejs(unicode(m)) + u'''", "''' + unicode(m.tags) + u'''"); ''' ] if len(msg_list) > 1 or bar_settings.DISPLAY_MESSAGES_LOG: footer += [ u''' $("#humanMsgLog p").show(); ''' ] footer += CoopBar().get_footer( request, context) # TODO profiler cette méthode (cache ?) footer += [u''' }); </script>'''] return "\n".join(footer)
def get(self, request, **kwargs): if 'geo_field' not in kwargs: # If it's not a geo request, let ModelListView handle it. return super(ModelGeoListView, self).get(request, **kwargs) field = kwargs.pop('geo_field') if field not in self.allowed_geo_fields: raise Http404 qs = self.get_qs(request, **kwargs) try: qs = self.filter(request, qs) except ValueError: raise BadRequest(_("Invalid filter value")) if qs.count() > app_settings.MAX_GEO_LIST_RESULTS: return HttpResponseForbidden( _("Spatial-list queries cannot return more than %(expected)d resources; this query would return %(actual)s. Please filter your query." ) % { 'expected': app_settings.MAX_GEO_LIST_RESULTS, 'actual': qs.count() }) format = request.GET.get('format', 'json') if format in ('json', 'apibrowser'): strings = ['{"objects": ['] strings.append(','.join( ('{"name": "%s", "%s": %s}' % (escapejs(x[1]), field, x[0].geojson) for x in qs.values_list(field, self.name_field)))) strings.append(']}') return RawJSONResponse(''.join(strings)) elif format == 'wkt': return HttpResponse("\n".join( (geom.wkt for geom in qs.values_list(field, flat=True))), content_type="text/plain") elif format == 'kml': placemarks = [ kml.generate_placemark(x[1], x[0]) for x in qs.values_list(field, self.name_field) ] resp = HttpResponse( kml.generate_kml_document(placemarks), content_type="application/vnd.google-earth.kml+xml") resp['Content-Disposition'] = 'attachment; filename="shape.kml"' return resp else: raise NotImplementedError
def show_databases(request): if request.method == 'POST': resp = {} try: databases = HCatClient( request.user.username).get_databases(like="*") databases_list_rendered = django_mako.render_to_string( "database_list.mako", dict(app_name=get_app_name(request), databases=databases)) except Exception as ex: resp['error'] = escapejs(ex.message) else: resp['database_list_rendered'] = databases_list_rendered resp['databases'] = databases return HttpResponse(json.dumps(resp)) return render("show_databases.mako", request, {})
def render(self, name, value, attrs=None): if value is None: value = '' final_attrs = self.build_attrs(attrs, name=name) textarea = u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), conditional_escape(force_unicode(value))) js = """ <script type='text/javascript'> var settings = { id:'%s', height:260, width: '%s', content: '%s', cssclass:'te', controlclass:'tecontrol', rowclass:'teheader', dividerclass:'tedivider', controls:['bold','italic','underline','|', 'orderedlist','unorderedlist','|', 'outdent','indent','|', 'image','hr','link','unlink','|', 'unformat', 'n', 'undo','redo','|', 'cut','copy','paste'], footer:true, fonts:['Verdana','Arial','Georgia','Trebuchet MS'], xhtml:true, bodyid:'editor', footerclass:'tefooter', toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'}, resize:{cssclass:'resize'} } $(document).ready(function() { if(window['first'] == undefined || first==true){ var editor = new TINY.editor.edit('editor', settings); var form = $('#%s').closest('form'); if(form.length) { form.bind('submit', function() {editor.post()}); } } first=false; }); </script> """ js %= (attrs['id'], self.width, escapejs(force_unicode(value)), attrs['id']) return mark_safe(textarea + js)
def GET(self, path): try: # Get parsed Europeana fullrecord-data from url. fullrecord = self.api.get_parse_europeana_object(escapejs(path)) except: # Else return empty record. return ({}) words = [] for item in fullrecord.keys(): word_list = self.__strclean__(fullrecord[item]) for word in word_list: if len(word) > 4: words.append(word) fullrecord["word"] = words return (simplejson.dumps(fullrecord))
def drop_database(request): if request.method == 'GET': title = _("Do you really want to delete the database(s)?") return render('confirm.html', request, dict(url=request.path, title=title)) elif request.method == 'POST': resp = {} try: for database in request.POST.getlist('database_selection'): HCatClient(request.user.username).drop_database( database, if_exists=True, option="cascade") except Exception as ex: resp['error'] = escapejs(ex.message) else: on_success_url = urlresolvers.reverse( get_app_name(request) + ':show_databases') resp['on_success_url'] = on_success_url return HttpResponse(json.dumps(resp))
def drop_table(request, database=None): if request.method == 'GET': title = _("Do you really want to delete the table(s)?") return render('confirm.html', request, dict(url=request.path, title=title)) elif request.method == 'POST': database = _get_last_database(request, database) resp = {} try: tables = request.POST.getlist('table_selection') for table in tables: HCatClient(request.user.username).drop_table(table, db=database) except Exception as ex: resp['error'] = escapejs(ex.message) else: on_success_url = urlresolvers.reverse( get_app_name(request) + ':index') resp['on_success_url'] = on_success_url return HttpResponse(json.dumps(resp))
def render(self, name, value, attrs=None): value = value or '' final_attrs = self.build_attrs(attrs) self.html_id = final_attrs.pop('id', name) lookup = get_lookup(self.channel) if value: objs = lookup.get_objects([value]) try: obj = objs[0] except IndexError: raise Exception("%s cannot find object:%s" % (lookup, value)) display = lookup.format_item_display(obj) current_repr = mark_safe( """new Array("%s",%s)""" % (escapejs(display),obj.pk) ) else: current_repr = 'null' if self.show_help_text: help_text = self.help_text else: help_text = '' context = { 'name': name, 'html_id' : self.html_id, 'min_length': getattr(lookup, 'min_length', 1), 'lookup_url': reverse('ajax_lookup',kwargs={'channel':self.channel}), 'current_id': value, 'current_repr': current_repr, 'help_text': help_text, 'extra_attrs': mark_safe(flatatt(final_attrs)), 'func_slug': self.html_id.replace("-",""), 'add_link' : self.add_link, } context.update(bootstrap()) return mark_safe(render_to_string(('autocompleteselect_%s.html' % self.channel, 'autocompleteselect.html'),context))
def render(self, request): if not self.settings.get('ID'): LOGGER.warn(u'No Google Analytics ID specified') return queue = [] for (event, args) in self.events: event_args = [mark_safe(u"'%s'" % event)] for arg in args: if isinstance(arg, (str, unicode)): event_args.append(mark_safe(u"'%s'" % arg)) elif isinstance(arg, dict): event_args.append(mark_safe(json.dumps(arg))) elif isinstance(arg, bool): event_args.append(arg and u'true' or u'false') else: event_args.append(escapejs(arg)) queue.append(event_args) if self.trans and not self.trans_tracked: queue.append(('require', "'ecommerce'", "'ecommerce.js'")) self.trans_tracked = True self.events = [] self.trans = False self.trans_tracked = False return render_to_string( 'analytics/universal.inc.html', { 'id': self.settings['ID'], 'domain': request.META.get('HTTP_HOST'), 'queue': queue, 'DEBUG': getattr(settings, 'DEBUG', True) })
def create_from_file(request, database=None): """Create a table by import from file""" if database is None: database = _get_last_database(request) form = MultiForm(table=hcatalog.forms.CreateTableFromFileForm, ) db = dbms.get(request.user) databases = db.get_databases() db_form = hcatalog.forms.DbForm(initial={'database': database}, databases=databases) if request.method == "POST": form.bind(request.POST) if form.is_valid(): parser_options = {} # table options table_name = form.table.cleaned_data['name'] replace_delimiter_with = form.table.cleaned_data[ 'replace_delimiter_with'] parser_options['replace_delimiter_with'] = replace_delimiter_with # common options parser_options['path'] = form.table.cleaned_data['path'] file_type = request.POST.get('file_type', hcatalog.forms.IMPORT_FILE_TYPE_NONE) parser_options['file_type'] = file_type parser_options['preview_start_idx'] = 0 parser_options['preview_end_idx'] = 0 # csv/tsv options parser_options['do_import_data'] = form.table.cleaned_data[ 'import_data'] parser_options['encoding'] = form.table.cleaned_data['encoding'] parser_options['autodetect_delimiter'] = form.table.cleaned_data[ 'autodetect_delimiter'] parser_options['read_column_headers'] = form.table.cleaned_data[ 'read_column_headers'] parser_options['apply_excel_dialect'] = True parser_options['ignore_whitespaces'] = form.table.cleaned_data[ 'ignore_whitespaces'] parser_options['ignore_tabs'] = form.table.cleaned_data[ 'ignore_tabs'] parser_options['single_line_comment'] = form.table.cleaned_data[ 'single_line_comment'] parser_options['java_style_comments'] = form.table.cleaned_data[ 'java_style_comments'] # xls/xlsx options parser_options['xls_sheet'] = form.table.cleaned_data['xls_sheet'] parser_options['xls_cell_range'] = form.table.cleaned_data[ 'xls_cell_range'] parser_options[ 'xls_read_column_headers'] = form.table.cleaned_data[ 'xls_read_column_headers'] parser_options['delimiter'] = form.table.cleaned_data['delimiter'] if parser_options['autodetect_delimiter']: parser_options['delimiters'] = DELIMITERS else: parser_options['delimiters'] = (parser_options['delimiter'], ) if parser_options['xls_read_column_headers'] and parser_options[ 'preview_start_idx'] == 0: parser_options['preview_start_idx'] = 1 is_preview_action = 'submitPreviewAction' in request.POST is_preview_next = 'submitPreviewNext' in request.POST is_preview_beginning = 'submitPreviewBeginning' in request.POST if is_preview_action or is_preview_next or is_preview_beginning: # preview action preview_results = {} preview_table_resp = '' fields_list, n_cols, col_names = ([], 0, []) # validate input parameters if file_type == hcatalog.forms.IMPORT_FILE_TYPE_NONE: preview_results['error'] = unicode( 'Cannot define file type.') return HttpResponse(json.dumps(preview_results)) if is_preview_next and 'preview_start_idx' in request.POST and 'preview_end_idx' in request.POST: parser_options['preview_start_idx'] = int( request.POST.get('preview_end_idx')) + 1 parser_options['preview_end_idx'] = int( request.POST.get( 'preview_end_idx')) + IMPORT_PEEK_NLINES else: parser_options['preview_start_idx'] = 0 parser_options['preview_end_idx'] = IMPORT_PEEK_NLINES - 1 if parser_options['xls_read_column_headers']: parser_options['preview_start_idx'] += 1 parser_options['preview_end_idx'] += 1 try: parser_options = _on_table_preview( request.fs, [processor.TYPE for processor in FILE_PROCESSORS], parser_options) row_start_index = parser_options['preview_start_idx'] if parser_options['xls_read_column_headers']: row_start_index -= 1 preview_table_resp = django_mako.render_to_string( "file_import_preview_table.mako", dict(fields_list=parser_options['fields_list'], column_formset=parser_options['col_formset'], row_start_index=row_start_index)) isTableValid, tableValidErrMsg = hcatalog.common.validateHiveTable( parser_options['col_names']) except Exception as ex: preview_results['error'] = escapejs(ex.message) else: preview_results['results'] = preview_table_resp if not isTableValid: preview_results['error'] = escapejs(tableValidErrMsg) options = {} if file_type == hcatalog.forms.IMPORT_FILE_TYPE_TEXT: options['delimiter_0'] = parser_options['delimiter_0'] options['delimiter_1'] = parser_options['delimiter_1'] options['file_processor_type'] = parser_options[ 'file_processor_type'] elif file_type == hcatalog.forms.IMPORT_FILE_TYPE_SPREADSHEET: options['xls_sheet'] = parser_options['xls_sheet'] options['xls_sheet_list'] = parser_options[ 'xls_sheet_list'] options['preview_start_idx'] = parser_options[ 'preview_start_idx'] options['preview_end_idx'] = parser_options[ 'preview_end_idx'] options['preview_has_more'] = parser_options[ 'preview_has_more'] preview_results['options'] = options return HttpResponse( json.dumps(preview_results, cls=JSONEncoderForHTML)) else: # create table action # validate input parameters if file_type == hcatalog.forms.IMPORT_FILE_TYPE_NONE: err_msg = unicode('Cannot define file type.') return render( "create_table_from_file.mako", request, dict(action="#", database=database, db_form=db_form, table_form=form.table, error=err_msg)) # getting back file_processor_type from preview parser_options['file_processor_type'] = request.POST.get( 'file_processor_type', None) user_def_columns = [] user_def_column_names = [] column_count = 0 while 'cols-%d-column_name' % column_count in request.POST \ and 'cols-%d-column_type' % column_count in request.POST: user_def_column_names.append( request.POST.get('cols-%d-column_name' % column_count)) user_def_columns.append( dict( column_name=request.POST.get( 'cols-%d-column_name' % column_count), column_type=request.POST.get( 'cols-%d-column_type' % column_count), )) column_count += 1 try: isTableValid, tableValidErrMsg = hcatalog.common.validateHiveTable( user_def_column_names) if not isTableValid: return render( "create_table_from_file.mako", request, dict(action="#", database=database, db_form=db_form, table_form=form.table, error=escapejs(tableValidErrMsg))) LOG.debug('Creating table by hcatalog') proposed_query = django_mako.render_to_string( "create_table_statement.mako", { 'table': dict(name=table_name, comment=form.table.cleaned_data['comment'], row_format='Delimited', field_terminator=replace_delimiter_with), 'columns': user_def_columns, 'partition_columns': [] }) proposed_query = proposed_query.decode('utf-8') hcat_cli = HCatClient(request.user.username) hcat_cli.create_table(database, table_name, proposed_query) do_import_data = parser_options.get('do_import_data', True) LOG.debug('Data processing stage') if do_import_data: parser_options = _on_table_create( request.fs, parser_options) path = parser_options.get('results_path', None) if not path or not request.fs.exists(path): msg = 'Missing needed result file to load data into table' LOG.error(msg) raise Exception(msg) if not table_name: msg = 'Internal error: Missing needed parameter to load data into table' LOG.error(msg) raise Exception(msg) LOG.info("Auto loading data from %s into table %s%s" % (path, database, table_name)) hql = "LOAD DATA INPATH '%s' INTO TABLE `%s.%s`" % ( path, database, table_name) job_id = hcat_cli.do_hive_query(execute=hql) on_success_url = urlresolvers.reverse( get_app_name(request) + ':index') return render( "create_table_from_file.mako", request, dict( action="#", job_id=job_id, on_success_url=on_success_url, database=database, db_form=db_form, table_form=form.table, error=None, )) # clean up tmp dir tmp_dir = parser_options.get('tmp_dir', None) if tmp_dir and request.fs.exists: request.fs.rmtree(tmp_dir) databases = hcat_cli.get_databases(like="*") db_form = hcatalog.forms.DbForm( initial={'database': database}, databases=databases) return render("show_tables.mako", request, { 'database': database, 'db_form': db_form, }) except Exception as ex: return render( "create_table_from_file.mako", request, dict(action="#", database=database, db_form=db_form, table_form=form.table, error=escapejs(ex.message))) return render( "create_table_from_file.mako", request, dict( action="#", database=database, db_form=db_form, table_form=form.table, error= "User form is not valid. Please check all input parameters.", )) else: form.bind() return render( "create_table_from_file.mako", request, dict( action="#", database=database, db_form=db_form, table_form=form.table, error=None, ))
def escape_template(context, template): t = env.get_template(template).render(context) return jinja2.Markup(escapejs(t))