Beispiel #1
0
    def read(self, request):
        """
        Retrives the existing digital assets associated with a given seller
        :param request:
        :return: JSON List containing the existing assets
        """

        pagination = {
            'start': request.GET.get('start', None),
            'limit': request.GET.get('limit', None)
        }
        if pagination['start'] is None or pagination['limit'] is None:
            pagination = None

        profile = request.user.userprofile

        if 'provider' not in profile.get_current_roles():
            return build_response(request, 403, 'You are not authorized to retrieve digital asset information')

        try:
            asset_manager = AssetManager()
            response = asset_manager.get_provider_assets_info(request.user, pagination=pagination)
        except Exception as e:
            return build_response(request, 400, unicode(e))

        return HttpResponse(json.dumps(response), status=200, mimetype='application/json; charset=utf-8')
        def upload_asset(req, user, content_type):
            asset_manager = AssetManager()

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

            return resource, data
    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
    def read(self, request, product_id):
        """
        Retrieves the assets from a product
        :param request:
        :param id:
        :return:
        """

        try:
            asset_manager = AssetManager()
            response = asset_manager.get_product_assets(product_id)
        except PermissionDenied as e:
            return build_response(request, 403, unicode(e))
        except:
            return build_response(request, 500, 'An unexpected error occurred')

        return HttpResponse(json.dumps(response),
                            status=200,
                            mimetype='application/json; charset=utf-8')
    def read(self, request, asset_id):
        """
        Retrieves the information associated to a given digital asset
        :param request:
        :param id:
        :return:
        """

        try:
            asset_manager = AssetManager()
            response = asset_manager.get_asset_info(asset_id)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except PermissionDenied as e:
            return build_response(request, 403, unicode(e))
        except:
            return build_response(request, 500, 'An unexpected error occurred')

        return HttpResponse(json.dumps(response),
                            status=200,
                            mimetype='application/json; charset=utf-8')
    def read(self, request):
        """
        Retrieves the existing digital assets associated with a given seller
        :param request:
        :return: JSON List containing the existing assets
        """

        user = request.GET.get('user', None)

        pagination = {
            'offset': request.GET.get('offset', None),
            'size': request.GET.get('size', None)
        }
        if pagination['offset'] is None or pagination['size'] is None:
            pagination = None

        if user is None:
            if request.user.is_anonymous():
                return build_response(request, 401, 'Authentication required')
            user = request.user.userprofile
        else:
            try:
                user_search = User.objects.get(username=user)
                user = UserProfile.objects.get(user=user_search)
            except Exception as e:
                return build_response(
                    request, 404, "User {} does not exist, error: {}".format(
                        user, unicode(e)))

        try:
            asset_manager = AssetManager()
            response = asset_manager.get_provider_assets_info(
                user, pagination=pagination)
        except Exception as e:
            return build_response(request, 400, unicode(e))

        return HttpResponse(json.dumps(response),
                            status=200,
                            mimetype='application/json; charset=utf-8')
    def read(self, request, asset_id):
        """
        Retrieves the information associated to a given digital asset
        :param request:
        :param id:
        :return:
        """

        if "provider" not in request.user.userprofile.get_current_roles():
            return build_response(request, 403, "You are not authorized to retrieve digital asset information")

        try:
            asset_manager = AssetManager()
            response = asset_manager.get_provider_asset_info(request.user, asset_id)
        except ObjectDoesNotExist as e:
            return build_response(request, 404, unicode(e))
        except PermissionDenied as e:
            return build_response(request, 403, unicode(e))
        except:
            return build_response(request, 500, "An unexpected error occurred")

        return HttpResponse(json.dumps(response), status=200, mimetype="application/json; charset=utf-8")
    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