def install_resource(wgt_file, executor_user): if not isinstance(wgt_file, WgtFile): raise TypeError('wgt_file must be a WgtFile') file_contents = wgt_file.get_underlying_file() template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resources = CatalogueResource.objects.filter( vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1] # Create/recreate/recover catalogue resource if '-dev' in template.get_resource_version() and len(resources) == 1: # TODO: Update widget visually resources[0].delete() resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) elif len(resources) == 1: resource = resources[0] else: resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) return resource
def install_resource(wgt_file, executor_user, restricted=False): if not isinstance(wgt_file, WgtFile): raise TypeError('wgt_file must be a WgtFile') file_contents = wgt_file.get_underlying_file() template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_version = template.get_resource_version() if restricted: if '-dev' in resource_version: raise PermissionDenied("dev versions cannot be published") vendor = template.get_resource_vendor() if check_vendor_permissions(executor_user, vendor) is False: raise PermissionDenied("You don't have persmissions to publish in name of {}".format(vendor)) resources = CatalogueResource.objects.filter(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1] # Create/recreate/recover catalogue resource if '-dev' in resource_version and len(resources) == 1: # dev version are automatically overwritten resources[0].delete() resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) elif len(resources) == 1: resource = resources[0] else: resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) return resource
def add_widget_from_wgt(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_id = ( template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version(), ) file_name = '_'.join(resource_id) + '.wgt' local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: return add_resource_from_template(file_name, template, user, fromWGT=True, overrides=overrides)
def undeploy_tenant_ac(request): result = _parse_ac_request(request) if isinstance(result, HttpResponse): return result id_4CaaSt, wgt_file, fileURL = result # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) try: if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt: raise Http404 except TenantProfile.DoesNotExist: raise Http404 # If the resource is a mashup, remove the assigned workspace template = TemplateParser(wgt_file.get_template()) if template.get_resource_type() == 'mashup': Workspace.objects.filter( creator=user, name=template.get_resource_info()['title']).delete() # Uninstall de resource template = TemplateParser(wgt_file.get_template()) resource = CatalogueResource.objects.get( vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version()) resource.users.remove(user) return HttpResponse(status=204)
def handle(self, *args, **options): if len(args) < 1: raise CommandError(_('Wrong number of arguments')) user = None if not options['deploy_only']: user = User.objects.get(pk=1) for file_name in args: try: f = open(file_name, 'rb') wgt_file = WgtFile(f) except: print _('Failed to read from %(file_name)s') % {'file_name': file_name} continue try: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) try: add_widget_from_wgt(f, user, wgt_file=wgt_file, template=template, deploy_only=options['deploy_only']) except IntegrityError: if not options['reinstall']: raise else: old_resource = CatalogueResource.objects.get(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version() ) delete_resource(old_resource, user) add_widget_from_wgt(f, user, wgt_file=wgt_file, template=template) wgt_file.close() f.close() print _('Successfully imported %(name)s widget') % {'name': template.get_resource_name()} except IntegrityError: print _('Version %(version)s of the %(name)s widget (from %(vendor)s) already exists') % { 'name': template.get_resource_name(), 'version': template.get_resource_version(), 'vendor': template.get_resource_vendor(), } except: print _('Failed to import widget from %(file_name)s') % {'file_name': file_name}
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_info = template.get_resource_info() check_packaged_resource(wgt_file, resource_info) resource_id = ( template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version(), ) file_name = '_'.join(resource_id) + '.wgt' local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: resource_info.update(overrides) resource = CatalogueResource.objects.create( short_name=resource_info['name'], vendor=resource_info['vendor'], version=resource_info['version'], type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']), creator=user, template_uri=file_name, creation_date=now(), popularity='0.0', json_description=json.dumps(resource_info)) return resource
def handle(self, *args, **options): if len(args) < 1: raise CommandError(_('Wrong number of arguments')) if not options['deploy_only']: user = User.objects.get(pk=1) else: user = FakeUser('admin') for file_name in args: try: wgt_file = WgtFile(file_name) except: print _('Failed to parse %(file_name)s') % {'file_name': file_name} continue try: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) try: create_widget_from_wgt(wgt_file, user, deploy_only=options['deploy_only']) except IntegrityError: if not options['reinstall']: raise else: deleteWidget(user, template.get_resource_name(), template.get_resource_vendor(), template.get_resource_version() ) create_widget_from_wgt(wgt_file, user) wgt_file.close() print _('Successfully imported %(name)s widget') % {'name': template.get_resource_name()} except IntegrityError: print _('Version %(version)s of the %(name)s widget (from %(vendor)s) already exists') % { 'name': template.get_resource_name(), 'version': template.get_resource_version(), 'vendor': template.get_resource_vendor(), } except: print _('Failed to import widget from %(file_name)s') % {'file_name': file_name}
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_info = template.get_resource_info() check_packaged_resource(wgt_file, resource_info) resource_id = ( template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version(), ) file_name = '_'.join(resource_id) + '.wgt' local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: resource_info.update(overrides) resource = CatalogueResource.objects.create( short_name=resource_info['name'], vendor=resource_info['vendor'], version=resource_info['version'], type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']), creator=user, template_uri=file_name, creation_date=now(), popularity='0.0', json_description=json.dumps(resource_info) ) return resource
def deploy(self, wgt_file): template_content = wgt_file.get_template() template_parser = TemplateParser(template_content) widget_rel_dir = os.path.join( template_parser.get_resource_vendor(), template_parser.get_resource_name(), template_parser.get_resource_version(), ) widget_dir = os.path.join(self._root_dir, widget_rel_dir) template_parser.set_base(pathname2url(widget_rel_dir) + '/') self._create_folders(widget_dir) wgt_file.extract(widget_dir) return template_parser
def deploy(self, wgt_file): template_content = wgt_file.get_template() template_parser = TemplateParser(template_content) widget_rel_dir = os.path.join( template_parser.get_resource_vendor(), template_parser.get_resource_name(), template_parser.get_resource_version(), ) widget_dir = os.path.join(self._root_dir, widget_rel_dir) template_parser.set_base(pathname2url(widget_rel_dir) + '/') self._create_folders(widget_dir) wgt_file.extract(widget_dir) return template_parser
def add_widget_from_wgt(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) if template.get_resource_type() == 'widget': resource_info = template.get_resource_info() code_url = resource_info['code_url'] if not code_url.startswith(('http://', 'https://')): code = wgt_file.read(code_url) try: unicode(code, resource_info['code_charset']) except UnicodeDecodeError: msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).') raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['code_charset']}) resource_id = ( template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version(), ) file_name = '_'.join(resource_id) + '.wgt' local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: return add_resource_from_template(file_name, template, user, fromWGT=True, overrides=overrides)
def install_resource(wgt_file, executor_user): if not isinstance(wgt_file, WgtFile): raise TypeError('wgt_file must be a WgtFile') file_contents = wgt_file.get_underlying_file() template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resources = CatalogueResource.objects.filter(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1] # Create/recreate/recover catalogue resource if '-dev' in template.get_resource_version() and len(resources) == 1: # TODO: Update widget visually resources[0].delete() resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) elif len(resources) == 1: resource = resources[0] else: resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) return resource
def undeploy_tenant_ac(request): id_4CaaSt, wgt_file, fileURL = _parse_ac_request(request) # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) try: if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt: raise Http404 except TenantProfile.DoesNotExist: raise Http404 # If the resource is a mashup, remove the assigned workspace template = TemplateParser(wgt_file.get_template()) if template.get_resource_type() == 'mashup': Workspace.objects.filter(creator=user, name=template.get_resource_info()['display_name']).delete() # Uninstall de resource template = TemplateParser(wgt_file.get_template()) resource = CatalogueResource.objects.get(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version()) resource.users.remove(user) return HttpResponse(status=204)
def undeploy_tenant_ac(request): id_4CaaSt, wgt_file, fileURL = _parse_ac_request(request) # Process 4CaaSt Id username = parse_username(id_4CaaSt) user = get_object_or_404(User, username=username) try: if user.tenantprofile_4CaaSt.id_4CaaSt != id_4CaaSt: raise Http404 except TenantProfile.DoesNotExist: raise Http404 template = TemplateParser(wgt_file.get_template()) resource = CatalogueResource.objects.get(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version()) resource.users.remove(user) return HttpResponse(status=204)
def create_widget_from_template(template, user, request=None, base=None): """Creates a widget from a template""" if isinstance(template, TemplateParser): parser = template else: template_content = download_http_content(template, user=user) if base is None: base = template parser = TemplateParser(template_content, base=base) if parser.get_resource_type() != 'widget': raise Exception() widget_info = parser.get_resource_info() check_requirements(widget_info) widget = Widget() widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version()) widget_code = parser.get_absolute_url(widget_info['contents']['src'], base) widget.xhtml = XHTML.objects.create( uri=widget.uri + "/xhtml", url=widget_code, content_type=widget_info['contents']['contenttype'], use_platform_style=widget_info['contents']['useplatformstyle'], cacheable=widget_info['contents']['cacheable'] ) widget.width = widget_info['widget_width'] widget.height = widget_info['widget_height'] widget.save() for preference in widget_info['preferences']: vDef = VariableDef.objects.create( name=preference['name'], type=parser.typeText2typeCode(preference['type']), aspect='PREF', readonly=preference['readonly'], default_value=preference['default'], value=preference['value'], widget=widget, secure=preference['secure'] ) for prop in widget_info['properties']: vDef = VariableDef.objects.create( name=prop['name'], type=parser.typeText2typeCode(prop['type']), aspect='PROP', default_value=prop['default'], widget=widget, secure=prop['secure'], ) return widget
def install_resource(file_contents, templateURL, executor_user, packaged): if packaged: if isinstance(file_contents, basestring): file_contents = StringIO(file_contents) wgt_file = WgtFile(file_contents) elif isinstance(file_contents, WgtFile): wgt_file = file_contents file_contents = wgt_file.get_underlying_file() else: raise Exception template_contents = wgt_file.get_template() else: template_contents = file_contents template = TemplateParser(template_contents) resources = CatalogueResource.objects.filter(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1] # Create/recover catalogue resource if len(resources) == 1: resource = resources[0] else: if packaged: resource = add_widget_from_wgt(file_contents, executor_user, wgt_file=wgt_file) else: resource = add_resource_from_template(templateURL, template_contents, executor_user) return resource
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_info = template.get_resource_info() if resource_info['type'] == 'widget': code_url = resource_info['contents']['src'] if not code_url.startswith(('http://', 'https://')): try: code = wgt_file.read(code_url) except KeyError: msg = _('Missing contents file: %(file_name)s.') raise InvalidContents(msg % {'file_name': code_url}) try: unicode(code, resource_info['contents']['charset']) except UnicodeDecodeError: msg = _('%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file).') raise InvalidContents(msg % {'file_name': code_url, 'charset': resource_info['contents']['charset']}) resource_id = ( template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version(), ) file_name = '_'.join(resource_id) + '.wgt' local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: resource_info.update(overrides) resource = CatalogueResource.objects.create( short_name=resource_info['name'], vendor=resource_info['vendor'], version=resource_info['version'], type=CatalogueResource.RESOURCE_TYPES.index(resource_info['type']), creator=user, template_uri=file_name, creation_date=now(), popularity='0.0', json_description=json.dumps(resource_info) ) return resource
def add_packaged_resource(file, user, wgt_file=None, template=None, deploy_only=False): close_wgt = False if wgt_file is None: wgt_file = WgtFile(file) close_wgt = True if template is None: template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resource_info = template.get_resource_info() if resource_info["type"] == "widget": code_url = resource_info["contents"]["src"] if not code_url.startswith(("http://", "https://")): try: code = wgt_file.read(code_url) except KeyError: msg = _("Missing contents file: %(file_name)s.") raise InvalidContents(msg % {"file_name": code_url}) try: code.decode(resource_info["contents"]["charset"]) except UnicodeDecodeError: msg = _( "%(file_name)s was not encoded using the specified charset (%(charset)s according to the widget descriptor file)." ) raise InvalidContents(msg % {"file_name": code_url, "charset": resource_info["contents"]["charset"]}) check_invalid_doc_content(wgt_file, resource_info, "longdescription") check_invalid_doc_content(wgt_file, resource_info, "doc") check_invalid_doc_content(wgt_file, resource_info, "changelog") resource_id = (template.get_resource_vendor(), template.get_resource_name(), template.get_resource_version()) file_name = "_".join(resource_id) + ".wgt" local_dir = wgt_deployer.get_base_dir(*resource_id) local_wgt = os.path.join(local_dir, file_name) if not os.path.exists(local_dir): os.makedirs(local_dir) overrides = extract_resource_media_from_package(template, wgt_file, local_dir) if close_wgt: wgt_file.close() f = open(local_wgt, "wb") file.seek(0) f.write(file.read()) f.close() if not deploy_only: resource_info.update(overrides) resource = CatalogueResource.objects.create( short_name=resource_info["name"], vendor=resource_info["vendor"], version=resource_info["version"], type=CatalogueResource.RESOURCE_TYPES.index(resource_info["type"]), creator=user, template_uri=file_name, creation_date=now(), popularity="0.0", json_description=json.dumps(resource_info), ) return resource
def create_widget_from_template(template, user, request=None, base=None): """Creates a widget from a template""" if isinstance(template, TemplateParser): parser = template else: template_content = downloader.download_http_content(template, user=user) if base is None: base = template parser = TemplateParser(template_content, base=base) if parser.get_resource_type() != 'widget': raise Exception() widget_info = parser.get_resource_info() check_requirements(widget_info) widget = Widget() widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version()) widget_code = parser.get_absolute_url(widget_info['code_url'], base) widget.xhtml = XHTML.objects.create( uri=widget.uri + "/xhtml", url=widget_code, content_type=widget_info['code_content_type'], use_platform_style=widget_info['code_uses_platform_style'], cacheable=widget_info['code_cacheable'] ) widget.width = widget_info['widget_width'] widget.height = widget_info['widget_height'] widget.save() variable_definitions = {} user_options = {} order = 0 for preference in widget_info['preferences']: vDef = VariableDef.objects.create( name=preference['name'], order=order, description=preference['description'], type=parser.typeText2typeCode(preference['type']), aspect='PREF', friend_code=None, readonly=preference['readonly'], label=preference['label'], default_value=preference['default_value'], value=preference['value'], widget=widget, secure=preference['secure'] ) variable_definitions[vDef.name] = vDef user_options[vDef.name] = {} for option in preference.get('options', ()): upo = UserPrefOption.objects.create( value=option['value'], name=option['label'], variableDef=vDef ) user_options[vDef.name][upo.name] = upo order += 1 order = 0 for prop in widget_info['properties']: vDef = VariableDef.objects.create( name=prop['name'], order=order, description=prop['description'], type=parser.typeText2typeCode(prop['type']), aspect='PROP', friend_code=None, label=prop['label'], default_value=prop['default_value'], widget=widget, secure=prop['secure'], ) variable_definitions[vDef.name] = vDef order += 1 order = 0 for input_endpoint in widget_info['wiring']['inputs']: vDef = VariableDef.objects.create( name=input_endpoint['name'], order=order, description=input_endpoint['description'], type=parser.typeText2typeCode(input_endpoint['type']), aspect='SLOT', friend_code=input_endpoint['friendcode'], label=input_endpoint['label'], action_label=input_endpoint['actionlabel'], widget=widget, ) variable_definitions[vDef.name] = vDef order += 1 order = 0 for output_endpoint in widget_info['wiring']['outputs']: vDef = VariableDef.objects.create( name=output_endpoint['name'], order=order, description=output_endpoint['description'], type=parser.typeText2typeCode(output_endpoint['type']), aspect='EVEN', friend_code=output_endpoint['friendcode'], label=output_endpoint['label'], widget=widget, ) variable_definitions[vDef.name] = vDef order += 1 for context in widget_info['context']: vDef = VariableDef.objects.create( name=context['name'], type=parser.typeText2typeCode(context['type']), aspect=context['aspect'], widget=widget, ) ContextOption.objects.create(concept=context['concept'], varDef=vDef) for lang in widget_info['translations']: translation = widget_info['translations'][lang] for index in translation: value = translation[index] usages = widget_info['translation_index_usage'][index] for use in usages: if use['type'] == 'vdef': vDef = variable_definitions[use['variable']] table = vDef._get_table_id() element_id = vDef.id elif use['type'] == 'upo': upo = user_options[use['variable']][use['option']] table = upo._get_table_id() element_id = upo.id else: continue Translation.objects.create( text_id=index, element_id=element_id, table=table, language=lang, value=value, default=widget_info['default_lang'] == lang ) return widget
def install_resource(file_contents, executor_user): if isinstance(file_contents, string_types): file_contents = BytesIO(file_contents) wgt_file = WgtFile(file_contents) elif isinstance(file_contents, WgtFile): wgt_file = file_contents file_contents = wgt_file.get_underlying_file() else: raise Exception template_contents = wgt_file.get_template() template = TemplateParser(template_contents) resources = CatalogueResource.objects.filter(vendor=template.get_resource_vendor(), short_name=template.get_resource_name(), version=template.get_resource_version())[:1] # Create/recover catalogue resource if len(resources) == 1: resource = resources[0] else: resource = add_packaged_resource(file_contents, executor_user, wgt_file=wgt_file) return resource