def add_gadget_script(request): """ Page for adding gadgets to catalogue without loading EzWeb """ if (request.user.is_authenticated() and not request.user.username.startswith('anonymous')): if (request.REQUEST.has_key('template_uri')): template_uri = request.REQUEST['template_uri'] else: #template_uri not specified! return render_to_response('catalogue_adder.html', {'msg': _('A template URL must be specified!')}, context_instance=RequestContext(request)) if (request.method.lower() == "get"): #GET Request: Render HTML interface! try: #Parsing gadget info from the template! templateParser = TemplateParser(template_uri, request.user, save=False) templateParser.parse() gadget = templateParser.get_gadget() return render_to_response('catalogue_adder.html', {'msg': _('Adding a gadget to EzWeb!'), 'template_uri': template_uri, 'gadget': gadget}, context_instance=RequestContext(request)) except Exception, e: #Error parsing the template return render_to_response('catalogue_adder.html', {'msg': _('Invalid template URL! Please, specify a valid one!')}, context_instance=RequestContext(request)) if (request.method.lower() == "post"): #POST Request: Adding gadget to catalogue and to workspace if specified! try: #Adding to catalogue if it doesn't exist! gc = GadgetsCollection() http_response = gc.create(request, request.user.username) catalogue_response = simplejson.loads(http_response.content) if (catalogue_response['result'].lower() != 'ok'): #Error adding in the catalogue! return render_to_response('catalogue_adder.html', {'msg': _('Error ocurred processing template!')}, context_instance=RequestContext(request)) #Retrieving info from catalogue for creating the "post_load_script" if the user wants to instantiate the gadget! vendor = catalogue_response['vendor'] version = catalogue_response['version'] name = catalogue_response['gadgetName'] if (request.REQUEST.has_key('add_to_ws') and request.REQUEST['add_to_ws'] == 'on'): #The gadget must be instantiated in the user workspace! #Loading ezweb for automating gadget instantiation post_load_script = '[{"command": "instantiate_resource", "template": "%s", "vendor_name": "%s", "name": "%s", "version": "%s"}]' \ % (template_uri, vendor, name, version) return render_ezweb(request, template="index.html", user_name=request.user.username, post_load_script=post_load_script) else: # No gadget instantiation, redirecting to information interface! # Gadget added to catalogue only! return render_to_response('catalogue_adder.html', {'msg': _('Gadget added correctly to Catalogue!') }, context_instance=RequestContext(request)) except Exception, e: return render_to_response('catalogue_adder.html', {'msg': _('Error ocurred processing template: %s!') % e.msg}, context_instance=RequestContext(request))
def testTranslations(self): templateParser = TemplateParser(self.template_uri, self.user) templateParser.parse() gadget = templateParser.get_gadget() data = get_gadgetresource_data(gadget, self.user) self.assertEqual(data['displayName'], 'Test Gadget') self.assertEqual(data['description'], 'Test Gadget description') self.changeLanguage('es') self.assertEqual(gadget.display_name, u'Gadget de pruebas') self.assertEqual(gadget.description, u'Descripción del Gadget de pruebas')
def testTranslations(self): templateParser = TemplateParser(self.template_uri, self.user) templateParser.parse() gadget = templateParser.get_gadget() data = get_gadgetresource_data(gadget, self.user) self.assertEqual(data['displayName'], 'Test Gadget') self.assertEqual(data['description'], 'Test Gadget description') self.changeLanguage('es') self.assertEqual(gadget.display_name, u'Gadget de pruebas') self.assertEqual(gadget.description, u'Descripción del Gadget de pruebas')
def add_gadget_script(request, fromWGT=False, user_action=True): """ Page for adding gadgets to catalogue without loading EzWeb """ if (request.user.is_authenticated() and not request.user.username.startswith('anonymous')): if ('template_uri' in request.REQUEST): template_uri = request.REQUEST['template_uri'] else: #Send pingback ERROR send_pingback(request, {'result_code': -3, 'result_message': _('A template URL must be specified!')}) #template_uri not specified! t = loader.get_template('catalogue_adder.html') c = Context({'msg': _('A template URL must be specified!')}) return HttpResponseBadRequest(t.render(c), mimetype="application/xhtml+xml") if (request.method.lower() == "get"): #GET Request: Render HTML interface! try: #Parsing gadget info from the template! templateParser = TemplateParser(template_uri, request.user, save=False) templateParser.parse() gadget = templateParser.get_gadget() params = {'msg': _('Adding a gadget to EzWeb!'), 'template_uri': template_uri, 'gadget': gadget} if 'pingback' in request.REQUEST: params['pingback'] = request.REQUEST['pingback'] return render_to_response('catalogue_adder.html', params, context_instance=RequestContext(request)) except Exception, e: #Send pingback ERROR send_pingback(request, {'result_code': -3, 'result_message': _('Invalid template URL! Please, specify a valid one!')}) #Error parsing the template t = loader.get_template('catalogue_adder.html') c = Context({'msg': _('Invalid template URL! Please, specify a valid one!')}) return HttpResponseBadRequest(t.render(c), mimetype="application/xhtml+xml") if (request.method.lower() == "post"): #POST Request: Adding gadget to catalogue and to workspace if specified! catalogue_response = {} try: #Adding to catalogue if it doesn't exist! gc = GadgetsCollection() http_response = gc.create(request, request.user.username, fromWGT=fromWGT) if not user_action: return http_response catalogue_response = simplejson.loads(http_response.content) #Cancel by the user if 'pingback_cancel' in request.REQUEST and (request.REQUEST['pingback_cancel'] == 'true'): if 'pingback' in request.REQUEST: #Send pingback CANCEL catalogue_response['result_code'] = 1 catalogue_response['result_message'] = _('Cancel by the user') send_pingback(request, catalogue_response) #Redirect return HttpResponseRedirect('/') if (catalogue_response['result'].lower() != 'ok'): #Send pingback ERROR if 'gadgetName' in catalogue_response: catalogue_response['result_code'] = -2 catalogue_response['result_message'] = _('Gadget already exists!') else: catalogue_response['result_code'] = -1 catalogue_response['result_message'] = _('Error ocurred processing template!') send_pingback(request, catalogue_response) #Error adding in the catalogue! t = loader.get_template('catalogue_adder.html') c = Context({'msg': catalogue_response['result_message']}) return HttpResponseBadRequest(t.render(c), mimetype="application/xhtml+xml") #Send pingback OK catalogue_response['result_code'] = 0 catalogue_response['result_message'] = _('Success') send_pingback(request, catalogue_response) if ('add_to_ws' in request.REQUEST and request.REQUEST['add_to_ws'] == 'on'): #The gadget must be instantiated in the user workspace! #Loading ezweb for automating gadget instantiation vendor = catalogue_response['vendor'] version = catalogue_response['version'] name = catalogue_response['gadgetName'] post_load_script = '[{"command": "instantiate_resource", "template": "%s", "vendor_name": "%s", "name": "%s", "version": "%s"}]' % (template_uri, vendor, name, version) return render_ezweb(request, template="/", user_name=request.user.username, post_load_script=post_load_script) else: # No gadget instantiation, redirecting to information interface! # Gadget added to catalogue only! t = loader.get_template('catalogue_adder.html') c = Context({'msg': _('Gadget added correctly to Catalogue!')}) return HttpResponse(t.render(c), mimetype="application/xhtml+xml") except Exception, e: #Send pingback ERROR catalogue_response['result_code'] = -1 catalogue_response['result_message'] = _('Error ocurred processing template!') send_pingback(request, catalogue_response) t = loader.get_template('catalogue_adder.html') c = Context({'msg': _('Error ocurred processing template: %s!') % e.message}) return HttpResponseBadRequest(t.render(c), mimetype="application/xhtml+xml")
def add_gadget_script(request): """ Page for adding gadgets to catalogue without loading EzWeb """ if (request.user.is_authenticated() and not request.user.username.startswith('anonymous')): if (request.REQUEST.has_key('template_uri')): template_uri = request.REQUEST['template_uri'] else: #template_uri not specified! return render_to_response( 'catalogue_adder.html', {'msg': _('A template URL must be specified!')}, context_instance=RequestContext(request)) if (request.method.lower() == "get"): #GET Request: Render HTML interface! try: #Parsing gadget info from the template! templateParser = TemplateParser(template_uri, request.user, save=False) templateParser.parse() gadget = templateParser.get_gadget() return render_to_response( 'catalogue_adder.html', { 'msg': _('Adding a gadget to EzWeb!'), 'template_uri': template_uri, 'gadget': gadget }, context_instance=RequestContext(request)) except Exception, e: #Error parsing the template return render_to_response( 'catalogue_adder.html', { 'msg': _('Invalid template URL! Please, specify a valid one!') }, context_instance=RequestContext(request)) if (request.method.lower() == "post"): #POST Request: Adding gadget to catalogue and to workspace if specified! try: #Adding to catalogue if it doesn't exist! gc = GadgetsCollection() http_response = gc.create(request, request.user.username) catalogue_response = simplejson.loads(http_response.content) if (catalogue_response['result'].lower() != 'ok'): #Error adding in the catalogue! return render_to_response( 'catalogue_adder.html', {'msg': _('Error ocurred processing template!')}, context_instance=RequestContext(request)) #Retrieving info from catalogue for creating the "post_load_script" if the user wants to instantiate the gadget! vendor = catalogue_response['vendor'] version = catalogue_response['version'] name = catalogue_response['gadgetName'] if (request.REQUEST.has_key('add_to_ws') and request.REQUEST['add_to_ws'] == 'on'): #The gadget must be instantiated in the user workspace! #Loading ezweb for automating gadget instantiation post_load_script = '[{"command": "instantiate_resource", "template": "%s", "vendor_name": "%s", "name": "%s", "version": "%s"}]' \ % (template_uri, vendor, name, version) return render_ezweb(request, template="index.html", user_name=request.user.username, post_load_script=post_load_script) else: # No gadget instantiation, redirecting to information interface! # Gadget added to catalogue only! return render_to_response( 'catalogue_adder.html', {'msg': _('Gadget added correctly to Catalogue!')}, context_instance=RequestContext(request)) except Exception, e: return render_to_response( 'catalogue_adder.html', { 'msg': _('Error ocurred processing template: %s!') % e.msg }, context_instance=RequestContext(request))
def add_resource_from_template(template_uri, template, user, fromWGT=False): templateParser = TemplateParser(template_uri, template, user, fromWGT=fromWGT) templateParser.parse() return templateParser, templateParser.get_gadget()