def _manage_digital_asset(request, manager):
    user = request.user
    profile = user.userprofile
    content_type = get_content_type(request)[0]

    if 'provider' not in profile.get_current_roles() and not user.is_staff:
        return build_response(request, 403, "You don't have the seller role")

    try:
        resource, data = manager(request, user, content_type)
    except ValueError as e:
        return build_response(request, 422, unicode(e))
    except ConflictError as e:
        return build_response(request, 409, unicode(e))
    except ObjectDoesNotExist as e:
        return build_response(request, 404, unicode(e))
    except PermissionDenied as e:
        return build_response(request, 403, unicode(e))
    except Exception as e:
        return build_response(request, 400, unicode(e))

    location = resource.get_url()

    # Fill location header with the URL of the uploaded digital asset
    response = HttpResponse(json.dumps({
        'content': location,
        'contentType': data['contentType'],
        'id': resource.pk,
        'href': resource.get_uri()
    }),
                            status=200,
                            mimetype='application/json; charset=utf-8')

    response['Location'] = location
    return response
Beispiel #2
0
    def create(self, request):

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if 'provider' in profile.get_current_roles():

            try:
                if content_type == 'application/json':
                    data = json.loads(request.raw_post_data)
                    register_resource(user, data)
                else:
                    data = json.loads(request.POST['json'])
                    f = request.FILES['file']
                    register_resource(user, data, file_=f)

            except ConflictError as e:
                return build_response(request, 409, unicode(e))
            except Exception as e:
                return build_response(request, 400, unicode(e))
        else:
            return build_response(request, 403, "You don't have the provider role")

        return build_response(request, 201, 'Created')
    def create(self, request):
        """
        Uploads a new downloadable digital asset
        :param request:
        :return: 201 Created, including the new URL of the asset in the location header
        """

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if 'provider' not in profile.get_current_roles() and not user.is_staff:
            return build_response(request, 403,
                                  "You don't have the seller role")

        asset_manager = AssetManager()
        try:
            if content_type == 'application/json':
                data = json.loads(request.body)
                resource = asset_manager.upload_asset(user, data)
            else:
                data = json.loads(request.POST['json'])
                f = request.FILES['file']
                resource = asset_manager.upload_asset(user, data, file_=f)

        except ConflictError as e:
            return build_response(request, 409, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        location = resource.get_url()

        # Fill location header with the URL of the uploaded digital asset
        response = HttpResponse(json.dumps({
            'content': location,
            'contentType': data['contentType'],
            'id': resource.pk,
            'href': resource.get_uri()
        }),
                                status=200,
                                mimetype='application/json; charset=utf-8')

        response['Location'] = location
        return response
Beispiel #4
0
    def create(self, request, provider, name, version):

        content_type = get_content_type(request)[0]

        try:
            # Extract the data depending on the content type
            if content_type ==  'application/json':
                data = json.loads(request.raw_post_data)
                params = (data, )
            else:
                data = json.loads(request.POST['json'])
                file_ = request.FILES['file']
                params = (data, file_)
        except:
            return build_response(request, 400, 'Invalid content')

        return _call_resource_entry_method(request, {
            'provider': provider,
            'name': name,
            'version': version
        }, upgrade_resource, params)
Beispiel #5
0
    def create(self, request):

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if 'provider' in profile.get_current_roles():

            if content_type == 'application/json':
                try:
                    data = json.loads(request.raw_post_data)
                    register_resource(user, data)
                except Exception, e:
                    return build_response(request, 400, e.message)
            else:
                try:
                    data = json.loads(request.POST['json'])
                    f = request.FILES['file']
                    register_resource(user, data, file_=f)
                except Exception, e:
                    return build_response(request, 400, e.message)
Beispiel #6
0
    def create(self, request, provider, name, version):

        content_type = get_content_type(request)[0]

        try:
            # Extract the data depending on the content type
            if content_type == 'application/json':
                data = json.loads(request.raw_post_data)
                params = (request.user, data, )
            else:
                data = json.loads(request.POST['json'])
                file_ = request.FILES['file']
                params = (request.user, data, file_)
        except:
            return build_response(request, 400, 'Invalid content')

        return _call_resource_entry_method(request, {
            'provider': provider,
            'name': name,
            'version': version
        }, upgrade_resource, params)
Beispiel #7
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        content_type = get_content_type(request)[0]
        try:
            org = Organization.objects.get(name=organization)
            offering = Offering.objects.get(name=name, owner_organization=org, version=version)
        except:
            return build_response(request, 404, 'Not found')

        # Check that the user can bind resources to the offering
        if not offering.is_owner(request.user) and request.user.pk not in org.managers:
            return build_response(request, 403, 'Forbidden')

        if content_type == 'application/json':
            try:
                data = json.loads(request.raw_post_data)
                bind_resources(offering, data, request.user)
            except:
                build_response(request, 400, 'Invalid JSON content')

        return build_response(request, 200, 'OK')
Beispiel #8
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        content_type = get_content_type(request)[0]
        try:
            org = Organization.objects.get(name=organization)
            offering = Offering.objects.get(name=name, owner_organization=org, version=version)
        except:
            return build_response(request, 404, 'Not found')

        # Check that the user can publish the offering
        if not offering.is_owner(request.user) and request.user.pk not in org.managers:
            return build_response(request, 403, 'Forbidden')

        if content_type == 'application/json':
            try:
                data = json.loads(request.raw_post_data)
                publish_offering(offering, data)
            except HTTPError:
                return build_response(request, 502, 'Bad gateway')
            except Exception, e:
                return build_response(request, 400, e.message)
    def create(self, request):
        """
        Uploads a new downloadable digital asset
        :param request:
        :return: 201 Created, including the new URL of the asset in the location header
        """

        user = request.user
        profile = user.userprofile
        content_type = get_content_type(request)[0]

        if "provider" not in profile.get_current_roles() and not user.is_staff:
            return build_response(request, 403, "You don't have the seller role")

        asset_manager = AssetManager()
        try:
            if content_type == "application/json":
                data = json.loads(request.body)
                location = asset_manager.upload_asset(user, data)
            else:
                data = json.loads(request.POST["json"])
                f = request.FILES["file"]
                location = asset_manager.upload_asset(user, data, file_=f)

        except ConflictError as e:
            return build_response(request, 409, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        # Fill location header with the URL of the uploaded digital asset
        response = HttpResponse(
            json.dumps({"content": location, "contentType": data["contentType"]}),
            status=200,
            mimetype="application/json; charset=utf-8",
        )

        response["Location"] = location
        return response
Beispiel #10
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        content_type = get_content_type(request)[0]
        try:
            offering, org = _get_offering(organization, name, version)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        # Check that the user can publish the offering
        if request.user.userprofile.current_organization != org\
        or (not offering.is_owner(request.user) and not request.user.pk in org.managers):
            return build_response(request, 403, 'Forbidden')

        if content_type == 'application/json':
            try:
                data = json.loads(request.raw_post_data)
                publish_offering(offering, data)
            except HTTPError:
                return build_response(request, 502, 'Bad gateway')
            except Exception, e:
                return build_response(request, 400, unicode(e))
Beispiel #11
0
    def create(self, request, organization, name, version):
        # Obtains the offering
        offering = None
        content_type = get_content_type(request)[0]
        try:
            offering, org = _get_offering(organization, name, version)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except Exception as e:
            return build_response(request, 400, unicode(e))

        # Check that the user can publish the offering
        if request.user.userprofile.current_organization != org\
        or (not offering.is_owner(request.user) and not request.user.pk in org.managers):
            return build_response(request, 403, 'Forbidden')

        if content_type == 'application/json':
            try:
                data = json.loads(request.raw_post_data)
                publish_offering(request.user, offering, data)
            except HTTPError:
                return build_response(request, 502, 'The Marketplace has failed publishing the offering')
            except Exception, e:
                return build_response(request, 400, unicode(e))
Beispiel #12
0
    def create(self, request):

        user = request.user
        content_type = get_content_type(request)[0]

        try:
            data = json.loads(request.raw_post_data)
            payment_info = {}

            if isinstance(data['offering'], dict):
                id_ = data['offering']
                org = Organization.objects.get(name=id_['organization'])
                offering = Offering.objects.get(owner_organization=org, name=id_['name'], version=id_['version'])
            else:
                offering = Offering.objects.get(description_url=data['offering'])

            if 'tax_address' in data:
                payment_info['tax_address'] = data['tax_address']

            payment_info['payment_method'] = data['payment']['method']

            if 'credit_card' in data['payment']:
                payment_info['credit_card'] = data['payment']['credit_card']

            # Get terms and conditions flag
            payment_info['accepted'] = data.get('conditions_accepted', False)

            # Check the selected price plan
            update_plan = False
            developer_plan = False
            if 'plan_label' in data:
                payment_info['plan'] = data['plan_label']
                # Classify the plan
                # Check if the plan is an update
                if data['plan_label'].lower() == 'update':
                    update_plan = True

                # Check if the plan is a developer purchase
                elif data['plan_label'].lower() == 'developer':
                    developer_plan = True

            # Check if the user is purchasing for an organization

            org_owned = True
            if user.userprofile.is_user_org():
                org_owned = False

            # Check if the user has the customer role for the organization
            roles = user.userprofile.get_current_roles()
            if org_owned:

                if not 'customer' in roles and not developer_plan:
                    return build_response(request, 403, 'Forbidden')

                # Load the purchased offerings if it is an update in
                # order to check versions later
                if update_plan:
                    purchased_offerings = user.userprofile.current_organization.offerings_purchased

            elif update_plan:
                purchased_offerings = user.userprofile.offerings_purchased
                    
            # Check if the plan is an update
            if update_plan:
                # Check if the user has purchased a previous version
                found = False
                offerings = Offering.objects.filter(owner_organization=offering.owner_organization, name=offering.name)

                for off in offerings:
                    if off.pk in purchased_offerings and is_lower_version(off.version, offering.version):
                        found = True
                        break

                if not found:
                    return build_response(request, 403, 'Forbidden')

            if developer_plan and not 'developer' in roles:
                    return build_response(request, 403, 'Forbidden')

            response_info = create_purchase(user, offering, org_owned=org_owned, payment_info=payment_info)
        except Exception as e:
            # Check if the offering has been paid before the exception has been raised
            paid = False

            if org_owned:
                if offering.pk in request.user.userprofile.current_organization.offerings_purchased:
                    paid = True
                    response_info = Purchase.objects.get(owner_organization=request.user.userprofile.current_organization, offering=offering)
            else:
                if offering.pk in request.user.userprofile.offerings_purchased:
                    paid = True
                    response_info = Purchase.objects.get(customer=request.user, offering=offering, organization_owned=False)

            if not paid:
                return build_response(request, 400, unicode(e))

        response = {}
        # If the value returned by the create_purchase method is a string means that
        # the purchase is not ended and need user confirmation. response_info contains
        # the URL where redirect the user
        if isinstance(response_info, str) or isinstance(response_info, unicode):
            response['redirection_link'] = response_info
            status = 200
        else:  # The purchase is finished so the download links are returned
            # Load download resources URL

            response['resources'] = []

            for res in offering.resources:
                r = store_resource.objects.get(pk=res)

                # Check if the resource has been uploaded to the store or if is
                # in an external applications server
                if r.resource_path != '':
                    response['resources'].append(r.resource_path)
                elif r.download_link != '':
                    response['resources'].append(r.download_link)

            # Load bill URL
            response['bill'] = response_info.bill
            status = 201

        # Check if it is needed to redirect the user
        token = offering.pk + user.pk

        site = get_current_site(request)
        context = Context.objects.get(site=site)

        if token in context.user_refs:
            redirect_uri = context.user_refs[token]['redirect_uri']
            del(context.user_refs[token])
            context.save()
            response['client_redirection_uri'] = redirect_uri

        return HttpResponse(json.dumps(response), status=status, mimetype=content_type)
Beispiel #13
0
    def create(self, request):

        user = request.user
        content_type = get_content_type(request)[0]

        try:
            data = json.loads(request.raw_post_data)
            payment_info = {}

            if isinstance(data['offering'], dict):
                id_ = data['offering']
                org = Organization.objects.get(name=id_['organization'])
                offering = Offering.objects.get(owner_organization=org, name=id_['name'], version=id_['version'])
            else:
                offering = Offering.objects.get(description_url=data['offering'])

            if 'tax_address' in data:
                payment_info['tax_address'] = data['tax_address']

            payment_info['payment_method'] = data['payment']['method']

            if 'credit_card' in data['payment']:
                payment_info['credit_card'] = data['payment']['credit_card']

            # Get terms and conditions flag
            payment_info['accepted'] = data.get('conditions_accepted', False)

            # Check the selected price plan
            update_plan = False
            developer_plan = False
            if 'plan_label' in data:
                payment_info['plan'] = data['plan_label']
                # Classify the plan
                # Check if the plan is an update
                if data['plan_label'].lower() == 'update':
                    update_plan = True

                # Check if the plan is a developer purchase
                elif data['plan_label'].lower() == 'developer':
                    developer_plan = True

            # Check if the user is purchasing for an organization

            org_owned = True
            if user.userprofile.is_user_org():
                org_owned = False

            # Check if the user has the customer role for the organization
            roles = user.userprofile.get_current_roles()
            if org_owned:

                if not 'customer' in roles and not developer_plan:
                    return build_response(request, 403, 'Forbidden')

                # Load the purchased offerings if it is an update in
                # order to check versions later
                if update_plan:
                    purchased_offerings = user.userprofile.current_organization.offerings_purchased

            elif update_plan:
                purchased_offerings = user.userprofile.offerings_purchased
                    
            # Check if the plan is an update
            if update_plan:
                # Check if the user has purchased a previous version
                found = False
                offerings = Offering.objects.filter(owner_organization=offering.owner_organization, name=offering.name)

                for off in offerings:
                    if off.pk in purchased_offerings and is_lower_version(off.version, offering.version):
                        found = True
                        break

                if not found:
                    return build_response(request, 403, 'Forbidden')

            if developer_plan and not 'developer' in roles:
                    return build_response(request, 403, 'Forbidden')

            response_info = create_purchase(user, offering, org_owned=org_owned, payment_info=payment_info)
        except Exception as e:
            # Check if the offering has been paid before the exception has been raised
            paid = False

            if org_owned:
                if offering.pk in request.user.userprofile.current_organization.offerings_purchased:
                    paid = True
                    response_info = Purchase.objects.get(owner_organization=request.user.userprofile.current_organization, offering=offering)
            else:
                if offering.pk in request.user.userprofile.offerings_purchased:
                    paid = True
                    response_info = Purchase.objects.get(customer=request.user, offering=offering, organization_owned=False)

            if not paid:
                return build_response(request, 400, unicode(e))

        response = {}
        # If the value returned by the create_purchase method is a string means that
        # the purchase is not ended and need user confirmation. response_info contains
        # the URL where redirect the user
        if isinstance(response_info, str) or isinstance(response_info, unicode):
            response['redirection_link'] = response_info
            status = 200
        else:  # The purchase is finished so the download links are returned
            # Load download resources URL

            response['resources'] = []

            for res in offering.resources:
                r = store_resource.objects.get(pk=res)

                # Check if the resource has been uploaded to the store or if is
                # in an external applications server
                if r.resource_path != '':
                    response['resources'].append(r.resource_path)
                elif r.download_link != '':
                    response['resources'].append(r.download_link)

            # Load bill URL
            response['bill'] = response_info.bill
            status = 201

        # Check if it is needed to redirect the user
        token = offering.pk + user.pk

        site = get_current_site(request)
        context = Context.objects.get(site=site)

        if token in context.user_refs:
            redirect_uri = context.user_refs[token]['redirect_uri']
            del(context.user_refs[token])
            context.save()
            response['client_redirection_uri'] = redirect_uri

        return HttpResponse(json.dumps(response), status=status, mimetype=content_type)
Beispiel #14
0
    def create(self, request):

        user = request.user
        content_type = get_content_type(request)[0]

        if content_type == 'application/json':

            try:
                data = json.loads(request.raw_post_data)
                payment_info = {}

                if isinstance(data['offering'], dict):
                    id_ = data['offering']
                    org = Organization.objects.get(name=id_['organization'])
                    offering = Offering.objects.get(owner_organization=org, name=id_['name'], version=id_['version'])
                else:
                    offering = Offering.objects.get(description_url=data['offering'])

                if 'tax_address' in data:
                    payment_info['tax_address'] = data['tax_address']

                payment_info['payment_method'] = data['payment']['method']

                if 'credit_card' in data['payment']:
                    payment_info['credit_card'] = data['payment']['credit_card']

                # Check the selected price plan
                update_plan = False
                developer_plan = False
                if 'plan_label' in data:
                    payment_info['plan'] = data['plan_label']
                    # Classify the plan
                    # Check if the plan is an update
                    if data['plan_label'].lower() == 'update':
                        update_plan = True

                    # Check if the plan is a developer purchase
                    elif data['plan_label'].lower() == 'developer':
                        developer_plan = True

                # Check if the user is purchasing for an organization

                org_owned = True
                if user.userprofile.is_user_org():
                    org_owned = False

                # Check if the user has the customer role for the organization
                roles = user.userprofile.get_current_roles()
                if org_owned:

                    if not 'customer' in roles and not developer_plan:
                        return build_response(request, 403, 'Forbidden')

                    # Load the purchased offerings if it is an update in
                    # order to check versions later
                    if update_plan:
                        purchased_offerings = user.userprofile.current_organization.offerings_purchased
                    
                elif update_plan:
                    purchased_offerings = user.userprofile.offerings_purchased
                    
                # Check if the plan is an update
                if update_plan:
                    # Check if the user has purchased a previous version
                    found = False
                    offerings = Offering.objects.filter(owner_organization=offering.owner_organization, name=offering.name)

                    for off in offerings:
                        if off.pk in purchased_offerings and is_lower_version(off.version, offering.version):
                            found = True
                            break

                    if not found:
                        return build_response(request, 403, 'Forbidden')

                if developer_plan and not 'developer' in roles:
                        return build_response(request, 403, 'Forbidden')

                response_info = create_purchase(user, offering, org_owned=org_owned, payment_info=payment_info)
            except Exception, e:
                # Check if the offering has been paid before the exception has been raised
                paid = False

                if org_owned:
                    if offering.pk in request.user.userprofile.current_organization.offerings_purchased:
                        paid = True
                        response_info = Purchase.objects.get(owner_organization=request.user.userprofile.current_organization, offering=offering)
                else:
                    if offering.pk in request.user.userprofile.offerings_purchased:
                        paid = True
                        response_info = Purchase.objects.get(customer=request.user, offering=offering, organization_owned=False)

                if not paid:
                    return build_response(request, 400, e.message)