Ejemplo n.º 1
0
def unregister_from_market(user, market):

    # Get the Marketplace object
    marketplace = None
    try:
        marketplace = Marketplace.objects.get(name=market)
    except:
        raise Exception('Not found')

    # Unregister WStore from the Marketplace
    marketadaptor = marketadaptor_factory(marketplace, user)

    try:
        marketadaptor.delete_store()
    except HTTPError:
        raise Exception('Bad Gateway')

    # Remove the Marketplace from Offerings
    for o in Offering.objects.all():
        if len(o.marketplaces) > 0:
            old = []

            for m in o.marketplaces:
                if m.marketplace != marketplace:
                    old.append(m)

            if len(old) < len(o.marketplaces):
                o.marketplaces = old
                o.save()

    marketplace.delete()
Ejemplo n.º 2
0
def unregister_from_market(user, market):

    # Get the Marketplace object
    marketplace = None
    try:
        marketplace = Marketplace.objects.get(name=market)
    except:
        raise Exception("Not found")

    # Unregister WStore from the Marketplace
    marketadaptor = marketadaptor_factory(marketplace, user)

    try:
        marketadaptor.delete_store()
    except HTTPError:
        raise Exception("Bad Gateway")

    # Remove the Marketplace from Offerings
    for o in Offering.objects.all():
        if len(o.marketplaces) > 0:
            old = []

            for m in o.marketplaces:
                if m.marketplace != marketplace:
                    old.append(m)

            if len(old) < len(o.marketplaces):
                o.marketplaces = old
                o.save()

    marketplace.delete()
Ejemplo n.º 3
0
def register_on_market(user, name, host, api_version, credentials, site):

    if host[-1] != '/':
        host += '/'

    # Check if the marketplace already exists
    if len(
            Marketplace.objects.filter(name=name)
            | Marketplace.objects.filter(host=host)) > 0:
        raise ConflictError('Marketplace already registered')

    store_name = settings.STORE_NAME

    username = None
    passwd = None

    if credentials:
        username = credentials['username']
        passwd = credentials['passwd']

    cred = MarketCredentials(username=username, passwd=passwd)

    marketplace = Marketplace(name=name,
                              host=host,
                              api_version=api_version,
                              credentials=cred)

    marketadaptor = marketadaptor_factory(marketplace, user)

    store_info = {
        'store_name': store_name,
        'store_uri': site,
    }

    store_id = marketadaptor.add_store(store_info)

    try:
        marketplace.store_id = store_id
        marketplace.save()
    except Exception as e:
        # If the marketplace model creation fails it is necesary to unregister the store
        # in order to avoid an inconsistent state
        marketadaptor.delete_store()
        raise e
Ejemplo n.º 4
0
def register_on_market(user, name, host, api_version, credentials, site):

    if host[-1] != "/":
        host += "/"

    # Check if the marketplace already exists
    if len(Marketplace.objects.filter(name=name) | Marketplace.objects.filter(host=host)) > 0:
        raise ConflictError("Marketplace already registered")

    store_name = settings.STORE_NAME

    username = None
    passwd = None

    if credentials:
        username = credentials["username"]
        passwd = credentials["passwd"]

    cred = MarketCredentials(username=username, passwd=passwd)

    marketplace = Marketplace(name=name, host=host, api_version=api_version, credentials=cred)

    marketadaptor = marketadaptor_factory(marketplace, user)

    store_info = {"store_name": store_name, "store_uri": site}

    store_id = marketadaptor.add_store(store_info)

    try:
        marketplace.store_id = store_id
        marketplace.save()
    except Exception as e:
        # If the marketplace model creation fails it is necesary to unregister the store
        # in order to avoid an inconsistent state
        marketadaptor.delete_store()
        raise e
    def _get_marketadaptor(self, host):
        self.marketplace.host = host
        market_adaptor = marketadaptor.marketadaptor_factory(self.marketplace)
        market_adaptor._session_id = '1111'

        return market_adaptor
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
def publish_offering(user, offering, data):

    # Validate data
    if 'marketplaces' not in data:
        raise ValueError('Publication error: missing required field, marketplaces')

    # Validate the state of the offering
    if not offering.state == 'uploaded':
        raise PermissionDenied('Publication error: The offering ' + offering.name + ' ' + offering.version + ' cannot be published')

    # Validate the offering has enough content to be published
    # Open offerings cannot be published in they do not contain
    # digital assets (applications or resources)
    if offering.open and not len(offering.resources) and not len(offering.applications):
        raise PermissionDenied('Publication error: Open offerings cannot be published if they do not contain at least a digital asset (resource or application)')

    # Check if it possible to publish the offering in a Marketplace
    if not len(Repository.objects.all()) > 0 and len(data['marketplaces']) > 0:
        raise PermissionDenied('Publication error: It is not possible to publish an offering in a Markteplace if no Repositories has been registered')

    # Upload the USDL description of the offering to the repository
    if len(Repository.objects.all()) > 0:
        repository = Repository.objects.get(is_default=True)

        # Generate the USDL of the offering
        generator = USDLGenerator()
        usdl, offering_uri = generator.generate_offering_usdl(offering)

        repository_adaptor = repository_adaptor_factory(repository)
        offering_id = offering.owner_organization.name + '__' + offering.name + '__' + offering.version

        repository_adaptor.set_uri(offering_uri)

        if settings.OILAUTH:
            repository_adaptor.set_credentials(user.userprofile.access_token)

        offering.description_url = repository_adaptor.upload('application/rdf+xml', usdl, name=offering_id)

    # Publish the offering in the selected marketplaces
    for market in data['marketplaces']:
        try:
            m = Marketplace.objects.get(name=market)
        except:
            raise ValueError('Publication error: The marketplace ' + market + ' does not exist')

        market_adaptor = marketadaptor_factory(m, user)

        off_market_name = market_adaptor.add_service(offering)
        offering.marketplaces.append(MarketOffering(
            marketplace=m,
            offering_name=off_market_name
        ))

    # Create revenue sharing models if needed
    build_rs_model(offering)

    offering.state = 'published'
    offering.publication_date = datetime.now()
    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 _get_marketadaptor(self, host):
        self.marketplace.host = host
        market_adaptor = marketadaptor.marketadaptor_factory(self.marketplace)
        market_adaptor._session_id = '1111'

        return market_adaptor