def _update_usdl(resource, user): if len(resource.resource_usdl) > 0: usdl, resource_uri = _build_usdl(resource) repository_adaptor = unreg_repository_adaptor_factory( resource.resource_usdl) repository_adaptor.set_credentials(user.userprofile.access_token) repository_adaptor.upload('application/rdf+xml', usdl)
def bind_resources(offering, data, provider): # Check that the offering supports binding if offering.state != 'uploaded' and not (offering.open and offering.state == 'published'): raise PermissionDenied('This offering cannot be modified') added_resources = [] offering_resources = [] for of_res in offering.resources: offering_resources.append(of_res) for res in data: try: resource = Resource.objects.get(name=res['name'], version=res['version'], provider=provider.userprofile.current_organization) except: raise ValueError('Resource not found: ' + res['name'] + ' ' + res['version']) # Check resource state if resource.state == 'deleted': raise PermissionDenied('Invalid resource, the resource ' + res['name'] + ' ' + res['version'] + ' is deleted') # Check open if not resource.open and offering.open: raise PermissionDenied('It is not allowed to include not open resources in an open offering') if not ObjectId(resource.pk) in offering_resources: added_resources.append(resource.pk) else: offering_resources.remove(ObjectId(resource.pk)) # added_resources contains the resources to be added to the offering # and offering_resources the resources to be deleted from the offering for add_res in added_resources: resource = Resource.objects.get(pk=add_res) resource.offerings.append(offering.pk) resource.save() offering.resources.append(ObjectId(add_res)) for del_res in offering_resources: resource = Resource.objects.get(pk=del_res) resource.offerings.remove(offering.pk) resource.save() offering.resources.remove(del_res) offering.offering_description['modified'] = unicode(datetime.now()) offering.save() # Update USDL document if needed if offering.open and offering.state == 'published' and len(offering.description_url): usdl_generator = USDLGenerator() repository_adaptor = unreg_repository_adaptor_factory(offering.description_url) if settings.OILAUTH: repository_adaptor.set_credentials(provider.userprofile.access_token) repository_adaptor.upload( 'application/rdf+xml', usdl_generator.generate_offering_usdl(offering)[0] )
def test_delete(self, name, url, side_effect=None, err_msg=None): repository_adaptor = repositoryAdaptor.unreg_repository_adaptor_factory( url) repository_adaptor.set_credentials('11111') self._execute_test(repository_adaptor.delete, (None, ), self._check_delete_response, (url, ), side_effect, err_msg)
def test_download(self, name, url, side_effect=None, err_msg=None): # Build repository adaptor object repository_adaptor = repositoryAdaptor.unreg_repository_adaptor_factory( url) repository_adaptor.set_credentials('11111') self._execute_test(repository_adaptor.download, (None, ), self._check_download_response, (url, ), side_effect, err_msg)
def test_delete(self, name, url, side_effect=None, err_msg=None): repository_adaptor = repositoryAdaptor.unreg_repository_adaptor_factory(url) repository_adaptor.set_credentials('11111') self._execute_test( repository_adaptor.delete, (None, ), self._check_delete_response, (url, ), side_effect, err_msg )
def test_download(self, name, url, side_effect=None, err_msg=None): # Build repository adaptor object repository_adaptor = repositoryAdaptor.unreg_repository_adaptor_factory(url) repository_adaptor.set_credentials('11111') self._execute_test( repository_adaptor.download, (None,), self._check_download_response, (url, ), side_effect, err_msg )
def _remove_usdls(resource, user): usdl_urls = [resource.resource_usdl] for old in resource.old_versions: # Save usdl urls of old versions usdl_urls.append(old.resource_usdl) # Remove the usdl descriptions from the repository for url in usdl_urls: if len(url) > 0: repository_adaptor = unreg_repository_adaptor_factory(url) repository_adaptor.set_credentials(user.userprofile.access_token) repository_adaptor.delete()
def read(self, request, organization, name, version): # Get offering try: org = Organization.objects.get(name=organization) offering = Offering.objects.get(owner_organization=org, name=name, version=version) except: return build_response(request, 404, 'Not found') # Get usdl from repository try: adaptor = unreg_repository_adaptor_factory(offering.description_url) result = adaptor.download() except: return build_response(request, 502, 'Bad Gateway') # Return the USDL with the origin format return HttpResponse(result['data'], status=200, mimetype=result['content_type'])
def read(self, request, organization, name, version): # Get offering try: org = Organization.objects.get(name=organization) offering = Offering.objects.get(owner_organization=org, name=name, version=version) except: return build_response(request, 404, 'Not found') # Get usdl from repository try: adaptor = unreg_repository_adaptor_factory( offering.description_url) result = adaptor.download() except: return build_response(request, 502, 'Bad Gateway') # Return the USDL with the origin format return HttpResponse(result['data'], status=200, mimetype=result['content_type'])
def delete_offering(user, offering): # If the offering has been purchased it is not deleted # it is marked as deleted in order to allow customers that # have purchased the offering to install it if needed # delete the usdl description from the repository if offering.state == 'deleted': raise PermissionDenied('The offering is already deleted') if offering.state == 'published' and len(offering.description_url): repository_adaptor = unreg_repository_adaptor_factory(offering.description_url) if settings.OILAUTH: repository_adaptor.set_credentials(user.userprofile.access_token) repository_adaptor.delete() index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') se = SearchEngine(index_path) if offering.state == 'uploaded': _remove_offering(offering, se) else: offering.state = 'deleted' offering.save() # Delete the offering from marketplaces for market in offering.marketplaces: market_adaptor = marketadaptor_factory(market.marketplace, user) market_adaptor.delete_service(market.offering_name) # Update offering indexes if not offering.open: se.update_index(offering) context = Context.objects.all()[0] # Check if the offering is in the newest list if offering.pk in context.newest: # Remove the offering from the newest list newest = context.newest if len(newest) < 8: newest.remove(offering.pk) else: # Get the 8 newest offerings using the publication date for sorting db = get_database_connection() offerings = db.wstore_offering newest_off = offerings.find({'state': 'published'}).sort('publication_date', -1).limit(8) newest = [] for n in newest_off: newest.append(str(n['_id'])) context.newest = newest context.save() # Check if the offering is in the top rated list if offering.pk in context.top_rated: # Remove the offering from the top rated list top_rated = context.top_rated if len(top_rated) < 8: top_rated.remove(offering.pk) else: # Get the 4 top rated offerings db = get_database_connection() offerings = db.wstore_offering top_off = offerings.find({'state': 'published', 'rating': {'$gt': 0}}).sort('rating', -1).limit(8) top_rated = [] for t in top_off: top_rated.append(str(t['_id'])) context.top_rated = top_rated context.save() if offering.open: _remove_offering(offering, se)
def update_offering(user, offering, data): # Check if the offering has been published, # if published the offering cannot be updated if offering.state != 'uploaded' and not offering.open: raise PermissionDenied('The offering cannot be edited') dir_name = offering.owner_organization.name + '__' + offering.name + '__' + offering.version path = os.path.join(settings.MEDIA_ROOT, dir_name) # Update the logo if 'image' in data: logo_path = offering.image_url logo_path = os.path.join(settings.BASEDIR, logo_path[1:]) # Remove the old logo os.remove(logo_path) # Save the new logo _save_encoded_image(path, data['image']['name'], data['image']['data']) offering.image_url = settings.MEDIA_URL + dir_name + '/' + data['image']['name'] # Update the related images if 'related_images' in data: # Delete old related images for img in offering.related_images: old_image = os.path.join(settings.BASEDIR, img[1:]) os.remove(old_image) offering.related_images = [] # Create new images for img in data['related_images']: _save_encoded_image(path, img['name'], img['data']) offering.related_images.append(settings.MEDIA_URL + dir_name + '/' + img['name']) if 'offering_description' in data: # Create offering USDL offering_info = deepcopy(data['offering_description']) offering_info['image_url'] = offering.image_url offering_info['name'] = offering.name offering_info['version'] = offering.version offering_info['organization'] = offering.owner_organization.name offering_info['base_id'] = offering.pk offering_info['created'] = unicode(offering.creation_date) mod = unicode(datetime.now()) offering_info['modified'] = mod usdl_generator = USDLGenerator() usdl_generator.validate_info(offering_info, offering.owner_organization, open_=offering.open) data['offering_description']['modified'] = mod offering.offering_description = data['offering_description'] if offering.open and offering.state == 'published' and len(offering.description_url): repository_adaptor = unreg_repository_adaptor_factory(offering.description_url) if settings.OILAUTH: repository_adaptor.set_credentials(user.userprofile.access_token) repository_adaptor.upload( 'application/rdf+xml', usdl_generator.generate_offering_usdl(offering)[0] ) offering.save() # Update offering indexes index_path = os.path.join(settings.BASEDIR, 'wstore') index_path = os.path.join(index_path, 'search') index_path = os.path.join(index_path, 'indexes') se = SearchEngine(index_path) se.update_index(offering)
def _update_usdl(resource, user): if len(resource.resource_usdl) > 0: usdl, resource_uri = _build_usdl(resource) repository_adaptor = unreg_repository_adaptor_factory(resource.resource_usdl) repository_adaptor.set_credentials(user.userprofile.access_token) repository_adaptor.upload('application/rdf+xml', usdl)