예제 #1
0
    def remove_review(self, user, review):
        """
        Removes a given review
        """
        rev = self._get_and_validate_review(user, review)

        # Remove review from offering
        rev.offering.comments.remove(review)

        # Update offering rating
        if len(rev.offering.comments):
            rev.offering.rating = ((rev.offering.rating *
                                    (len(rev.offering.comments) + 1)) -
                                   rev.rating) / len(rev.offering.comments)
        else:
            rev.offering.rating = 0

        rev.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(rev.offering)

        # Update top rated offerings
        self._update_top_rated()

        # Update user info to allow her to create a new review
        if rev.user == user:
            # Check if is a private review or an organization review
            if user.userprofile.is_user_org():
                user.userprofile.rated_offerings.remove(rev.offering.pk)
                user.userprofile.save()
            else:
                self._remove_review_from_org(
                    user, rev.offering, user.userprofile.current_organization)
        else:
            self._remove_review_from_org(rev.user, rev.offering,
                                         rev.organization)

        rev.delete()
예제 #2
0
    def remove_review(self, user, review):
        """
        Removes a given review
        """
        rev = self._get_and_validate_review(user, review)

        # Remove review from offering
        rev.offering.comments.remove(review)

        # Update offering rating
        if len(rev.offering.comments):
            rev.offering.rating = ((rev.offering.rating * (len(rev.offering.comments) + 1)) - rev.rating) / len(
                rev.offering.comments
            )
        else:
            rev.offering.rating = 0

        rev.offering.save()

        # Update offering indexes
        index_path = settings.DATADIR
        index_path = os.path.join(index_path, "search")
        index_path = os.path.join(index_path, "indexes")

        se = SearchEngine(index_path)
        se.update_index(rev.offering)

        # Update top rated offerings
        self._update_top_rated()

        # Update user info to allow her to create a new review
        if rev.user == user:
            # Check if is a private review or an organization review
            if user.userprofile.is_user_org():
                user.userprofile.rated_offerings.remove(rev.offering.pk)
                user.userprofile.save()
            else:
                self._remove_review_from_org(user, rev.offering, user.userprofile.current_organization)
        else:
            self._remove_review_from_org(rev.user, rev.offering, rev.organization)

        rev.delete()
예제 #3
0
def publish_offering(offering, data):

    # Validate data
    if not 'marketplaces' 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)')

    # 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(m.host)
        info = {
            'name': offering.name,
            'url': offering.description_url
        }
        market_adaptor.add_service(settings.STORE_NAME, info)
        offering.marketplaces.append(m.pk)

    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)
예제 #4
0
    def update_review(self, user, review, review_data):
        """
        Updates a given review
        """

        # Check data
        validation = self._validate_content(review_data)
        if validation:
            raise validation

        rev = self._get_and_validate_review(user, review)

        # Calculate new rating
        rate = ((rev.offering.rating * len(rev.offering.comments)) - rev.rating + review_data["rating"]) / len(
            rev.offering.comments
        )

        # update review
        rev.title = review_data["title"]
        rev.comment = review_data["comment"]
        rev.rating = review_data["rating"]
        rev.timestamp = datetime.now()

        rev.save()

        # Update offering rating
        rev.offering.rating = rate
        rev.offering.save()

        # Update offering indexes
        index_path = settings.DATADIR
        index_path = os.path.join(index_path, "search")
        index_path = os.path.join(index_path, "indexes")

        se = SearchEngine(index_path)
        se.update_index(rev.offering)

        # Update top rated offerings
        self._update_top_rated()
예제 #5
0
    def update_review(self, user, review, review_data):
        """
        Updates a given review
        """

        # Check data
        validation = self._validate_content(review_data)
        if validation:
            raise validation

        rev = self._get_and_validate_review(user, review)

        # Calculate new rating
        rate = (
            (rev.offering.rating * len(rev.offering.comments)) - rev.rating +
            review_data['rating']) / len(rev.offering.comments)

        # update review
        rev.title = review_data['title']
        rev.comment = review_data['comment']
        rev.rating = review_data['rating']
        rev.timestamp = datetime.now()

        rev.save()

        # Update offering rating
        rev.offering.rating = rate
        rev.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(rev.offering)

        # Update top rated offerings
        self._update_top_rated()
예제 #6
0
    def update_review(self, user, review, review_data):
        """
        Updates a given review
        """

        # Check data
        validation = self._validate_content(review_data)
        if validation:
            raise validation

        rev = self._get_and_validate_review(user, review)

        # Calculate new rating
        rate = ((rev.offering.rating * len(rev.offering.comments)) - rev.rating + review_data['rating']) / len(rev.offering.comments)

        # update review
        rev.title = review_data['title']
        rev.comment = review_data['comment']
        rev.rating = review_data['rating']
        rev.timestamp = datetime.now()

        rev.save()

        # Update offering rating
        rev.offering.rating = rate
        rev.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(rev.offering)

        # Update top rated offerings
        self._update_top_rated()
예제 #7
0
파일: tests.py 프로젝트: jartieda/wstore
    def test_update_index(self, update_method, offering, query_, owned=False, err_type=None, err_msg=None):

        # Get the offering
        off = None
        if not err_type:
            off = Offering.objects.get(pk=offering)

        # Create the search engine
        se = SearchEngine(settings.BASEDIR + '/wstore/test/test_index')

        # Call the update method
        update_method(self, off, sa=se)

        # Call full text search
        error = None
        try:
            se.update_index(off)
        except Exception as e:
            error = e

        if not err_type:
            self.assertEquals(error, None)
            index = open_dir(settings.BASEDIR + '/wstore/test/test_index')

            with index.searcher() as searcher:
                if owned:
                    user = User.objects.get(pk="51000aba8e05ac2115f022f9")
                    pk = user.userprofile.current_organization.pk
                    query_ = query_ & query.Term('purchaser', pk)

                search_result = searcher.search(query_)

                self.assertEquals(len(search_result), 1)
        else:
            self.assertTrue(isinstance(error, err_type))
            self.assertEquals(unicode(error), err_msg)
예제 #8
0
def rollback(purchase):
    # If the purchase state is paid means that the purchase has been made
    # so the models must not be deleted
    offering = purchase.offering

    if purchase.state != 'paid':

        # Check that the payment has been made
        contract = True
        try:
            contr = purchase.contract
        except:
            contract = False

        to_del = True
        if contract:
            # If the charges field contains any charge means that it is not
            # the first charge so the models cannot be deleted
            if len(contr.charges) > 0:
                purchase.state = 'paid'
                purchase.save()
                to_del = False

        if to_del:
            # Check organization owned
            if purchase.organization_owned:
                org = purchase.owner_organization
                if purchase.offering.pk in org.offerings_purchased:
                    org.offerings_purchased.remove(purchase.offering.pk)
                    org.save()

            # Delete the offering from the user profile
            user_profile = UserProfile.objects.get(user=purchase.customer)
            if purchase.offering.pk in user_profile.offerings_purchased:
                user_profile.offerings_purchased.remove(purchase.offering.pk)
                user_profile.save()

            # Delete the contract
            if contract:
                purchase.contract.delete()
            # Delete the Purchase
            purchase.delete()

    # If the purchase is paid the offering must be included in the customer
    # offerings purchased list
    else:
        if purchase.organization_owned:
            org = purchase.owner_organization
            if not purchase.offering.pk in org.offerings_purchased:
                org.offerings_purchased.append(purchase.offering.pk)
                org.save()
        else:
            profile = purchase.customer.userprofile
            if not purchase.offering.pk in profile.offerings_purchased:
                profile.offerings_purchased.append(purchase.offering.pk)
                profile.save()

    # Update offering indexes: Offering index must be updated in any case
    index_path = settings.DATADIR
    index_path = os.path.join(index_path, 'search')
    index_path = os.path.join(index_path, 'indexes')

    se = SearchEngine(index_path)
    se.update_index(offering)
예제 #9
0
    def create_review(self, user, offering, review):
        """
        Creates a new review for a given offering
        """

        # Check if the user has purchased the offering (Not if the offering is open)
        if not offering.open:
            try:
                purchase = Purchase.objects.get(
                    offering=offering, owner_organization=user.userprofile.current_organization
                )
            except:
                raise PermissionDenied("You cannot review this offering since you has not acquire it")

        # Check if the user has already review the offering.
        if (user.userprofile.is_user_org() and offering.pk in user.userprofile.rated_offerings) or (
            not user.userprofile.is_user_org()
            and user.userprofile.current_organization.has_rated_offering(user, offering)
        ):
            raise PermissionDenied(
                "You cannot review this offering again. Please update your review to provide new comments"
            )

        # Validate review data
        validation = self._validate_content(review)
        if validation:
            raise validation

        # Create the review
        rev = Review.objects.create(
            user=user,
            organization=user.userprofile.current_organization,
            offering=offering,
            timestamp=datetime.now(),
            title=review["title"],
            comment=review["comment"],
            rating=review["rating"],
        )

        offering.comments.insert(0, rev.pk)

        # Calculate new offering rate
        old_rate = offering.rating

        if old_rate == 0:
            offering.rating = review["rating"]
        else:
            offering.rating = ((old_rate * (len(offering.comments) - 1)) + review["rating"]) / len(offering.comments)

        offering.save()

        # Update offering indexes
        index_path = settings.DATADIR
        index_path = os.path.join(index_path, "search")
        index_path = os.path.join(index_path, "indexes")

        se = SearchEngine(index_path)
        se.update_index(offering)

        # Save the offering as rated
        if user.userprofile.is_user_org():
            user.userprofile.rated_offerings.append(offering.pk)
            user.userprofile.save()
        else:
            user.userprofile.current_organization.rated_offerings.append({"user": user.pk, "offering": offering.pk})
            user.userprofile.current_organization.save()

        # Update top rated list
        self._update_top_rated()
예제 #10
0
def delete_offering(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')

    parsed_url = urlparse(offering.description_url)
    path = parsed_url.path
    host = parsed_url.scheme + '://' + parsed_url.netloc
    path = path.split('/')
    host += '/' + path[1] + '/' + path[2]
    collection = path[3]

    repository_adaptor = RepositoryAdaptor(host, collection)
    repository_adaptor.delete(path[4])

    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:
            m = Marketplace.objects.get(pk=market)
            market_adaptor = MarketAdaptor(m.host)
            market_adaptor.delete_service(settings.STORE_NAME, 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
                connection = MongoClient()
                db = connection[settings.DATABASES['default']['NAME']]
                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
                connection = MongoClient()
                db = connection[settings.DATABASES['default']['NAME']]
                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)
예제 #11
0
def update_offering(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
        f = open(os.path.join(path, data['image']['name']), "wb")
        dec = base64.b64decode(data['image']['data'])
        f.write(dec)
        f.close()
        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']:
            f = open(os.path.join(path, img['name']), "wb")
            dec = base64.b64decode(img['data'])
            f.write(dec)
            f.close()
            offering.related_images.append(settings.MEDIA_URL + dir_name + '/' + img['name'])

    new_usdl = False
    # Update the USDL description
    if 'offering_description' in data:
        usdl_info = data['offering_description']

        repository_adaptor = RepositoryAdaptor(offering.description_url)

        usdl = usdl_info['data']
        repository_adaptor.upload(usdl_info['content_type'], usdl)
        new_usdl = True

    # The USDL document has changed in the repository
    elif 'description_url' in data:
        usdl_info = {}
        usdl_url = data['description_url']

        # Check the link
        if usdl_url != offering.description_url:
            raise ValueError('The provided USDL URL is not valid')

        # Download new content
        repository_adaptor = RepositoryAdaptor(usdl_url)
        accept = "text/plain; application/rdf+xml; text/turtle; text/n3"
        usdl = repository_adaptor.download(content_type=accept)

        usdl_info['content_type'] = usdl['content_type']
        usdl = usdl['data']

        new_usdl = True
    elif 'offering_info' in data:
        usdl_info = {
            'content_type': 'application/rdf+xml'
        }
        # Validate USDL info
        if not 'description' in data['offering_info'] or not 'pricing' in data['offering_info']:
            raise ValueError('Invalid USDL info')

        offering_info = data['offering_info']
        offering_info['image_url'] = offering.image_url

        offering_info['name'] = offering.name

        splited_desc_url = offering.description_url.split('/')

        base_uri = splited_desc_url[0] + '//'
        splited_desc_url.remove(splited_desc_url[0])
        splited_desc_url.remove(splited_desc_url[0])
        splited_desc_url.remove(splited_desc_url[-1])
        splited_desc_url.remove(splited_desc_url[-1])

        for p in splited_desc_url:
            base_uri += (p + '/')

        offering_info['base_uri'] = base_uri

        usdl = _create_basic_usdl(offering_info)
        usdl_info = {
            'content_type': 'application/rdf+xml'
        }

        repository_adaptor = RepositoryAdaptor(offering.description_url)
        repository_adaptor.upload(usdl_info['content_type'], usdl)
        new_usdl = True

    # If the USDL has changed store the new description
    # in the offering model
    if new_usdl:
        # Validate the USDL
        valid = validate_usdl(usdl, usdl_info['content_type'], {
            'name': offering.name,
            'organization': offering.owner_organization
        })

        if not valid[0]:
            raise ValueError(valid[1])

        # Serialize and store USDL info in json-ld format
        graph = rdflib.Graph()

        rdf_format = usdl_info['content_type']

        if usdl_info['content_type'] == 'text/turtle' or usdl_info['content_type'] == 'text/plain':
            rdf_format = 'n3'
        elif usdl_info['content_type'] == 'application/json':
            rdf_format = 'json-ld'

        off_description = usdl
        if rdf_format != 'json-ld':
            graph.parse(data=usdl, format=rdf_format)
            off_description = graph.serialize(format='json-ld', auto_compact=True)

        offering.offering_description = json.loads(off_description)

    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)
예제 #12
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)
예제 #13
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)
예제 #14
0
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)
예제 #15
0
def create_purchase(user, offering, org_owned=False, payment_info=None):

    if offering.state != 'published':
        raise PermissionDenied("This offering can't be purchased")

    if offering.open:
        raise PermissionDenied('Open offerings cannot be purchased')

    if accepted_needed(offering) and not payment_info['accepted']:
        raise PermissionDenied('You must accept the terms and conditions of the offering to acquire it')
 
    profile = UserProfile.objects.get(user=user)

    # Check if the offering is already purchased
    if (org_owned and offering.pk in profile.current_organization.offerings_purchased) \
    or (not org_owned and offering.pk in profile.offerings_purchased):
            raise PermissionDenied('The offering has been already purchased')

    organization = profile.current_organization

    plan = None
    # Check the selected plan
    if payment_info and 'plan' in payment_info:
        plan = payment_info['plan']

    # Get the effective tax address
    if not 'tax_address' in payment_info:
        if org_owned:
            tax = organization.tax_address
        else:
            tax = profile.tax_address

        # Check that the customer has a tax address
        if not 'street' in tax:
            raise ValueError('The customer does not have a tax address')
    else:
        tax = payment_info['tax_address']

        # Check tax_address fields
        if (not 'street' in tax) or (not 'postal' in tax) or (not 'city' in tax) or (not 'country' in tax):
            raise ValueError('The tax address is not valid')

    # Check the payment method before purchase creation in order to avoid
    # an inconsistent state in the database
    credit_card_info = None
    if payment_info['payment_method'] == 'credit_card':
        if 'credit_card' in payment_info:
            # Check credit card info
            if (not ('number' in payment_info['credit_card'])) or (not ('type' in payment_info['credit_card']))\
            or (not ('expire_year' in payment_info['credit_card'])) or (not ('expire_month' in payment_info['credit_card']))\
            or (not ('cvv2' in payment_info['credit_card'])):
                raise ValueError('Invalid credit card info')

            credit_card_info = payment_info['credit_card']
        else:
            if org_owned:
                credit_card_info = organization.payment_info
            else:
                credit_card_info = profile.payment_info

            # Check the credit card info
            if not 'number' in credit_card_info:
                raise Exception('The customer does not have payment info')

    elif payment_info['payment_method'] != 'paypal':
        raise ValueError('Invalid payment method')

    # Create the purchase
    purchase = Purchase.objects.create(
        customer=user,
        date=datetime.now(),
        offering=offering,
        organization_owned=org_owned,
        state='pending',
        tax_address=tax,
        owner_organization = organization
    )

    # Load ref
    purchase.ref = purchase.pk
    purchase.save()

    if credit_card_info != None:
        charging_engine = ChargingEngine(purchase, payment_method='credit_card', credit_card=credit_card_info, plan=plan)
    else:
        charging_engine = ChargingEngine(purchase, payment_method='paypal', plan=plan)

    redirect_url = charging_engine.resolve_charging(new_purchase=True)

    if redirect_url == None:
        result = purchase

        # If no redirect URL is provided the purchase has ended so the user profile
        # info is updated
        if org_owned:
            organization.offerings_purchased.append(offering.pk)
            organization.save()
        else:
            profile.offerings_purchased.append(offering.pk)
            profile.save()

        notify_provider(purchase)

    else:
        result = redirect_url

    # Update offering indexes
    index_path = settings.DATADIR
    index_path = os.path.join(index_path, 'search')
    index_path = os.path.join(index_path, 'indexes')

    se = SearchEngine(index_path)
    se.update_index(offering)

    return result
예제 #16
0
    def create_review(self, user, offering, review):
        """
        Creates a new review for a given offering
        """

        # Check if the user has purchased the offering (Not if the offering is open)
        if not offering.open:
            try:
                purchase = Purchase.objects.get(
                    offering=offering,
                    owner_organization=user.userprofile.current_organization)
            except:
                raise PermissionDenied(
                    'You cannot review this offering since you has not acquire it'
                )
        elif offering.is_owner(user):
            # If the offering is open, check that the user is not owner of the offering
            raise PermissionDenied('You cannot review your own offering')

        # Check if the user has already review the offering.
        if (user.userprofile.is_user_org() and offering.pk in user.userprofile.rated_offerings)\
        or (not user.userprofile.is_user_org() and user.userprofile.current_organization.has_rated_offering(user, offering)):
            raise PermissionDenied(
                'You cannot review this offering again. Please update your review to provide new comments'
            )

        # Validate review data
        validation = self._validate_content(review)
        if validation:
            raise validation

        # Create the review
        rev = Review.objects.create(
            user=user,
            organization=user.userprofile.current_organization,
            offering=offering,
            timestamp=datetime.now(),
            title=review['title'],
            comment=review['comment'],
            rating=review['rating'])

        offering.comments.insert(0, rev.pk)

        # Calculate new offering rate
        old_rate = offering.rating

        if old_rate == 0:
            offering.rating = review['rating']
        else:
            offering.rating = ((old_rate * (len(offering.comments) - 1)) +
                               review['rating']) / len(offering.comments)

        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)

        # Save the offering as rated
        if user.userprofile.is_user_org():
            user.userprofile.rated_offerings.append(offering.pk)
            user.userprofile.save()
        else:
            user.userprofile.current_organization.rated_offerings.append({
                'user':
                user.pk,
                'offering':
                offering.pk
            })
            user.userprofile.current_organization.save()

        # Update top rated list
        self._update_top_rated()