Ejemplo n.º 1
0
def new_collection(request, parent):
    parent_collection = Collection.find(parent)

    if not parent_collection.user_can(request.user, "write"):
        raise PermissionDenied

    keys = get_collection_keys()
    mdata = collections.OrderedDict()
    for k in keys:
        mdata[k] = ""
    if not mdata:
        mdata[""] = ""

    read_access, write_access = parent_collection.get_acl_list()
    initial = {
        'metadata': json.dumps(mdata),
        "read_access": read_access,
        "write_access": write_access
    }
    form = CollectionNewForm(request.POST or None, initial=initial)
    if request.method == 'POST':
        if form.is_valid():
            data = form.cleaned_data
            try:
                name = data['name']
                parent = parent_collection.path
                metadata = {}

                for k, v in json.loads(data['metadata']):
                    if k in metadata:
                        if isinstance(metadata[k], list):
                            metadata[k].append(v)
                        else:
                            metadata[k] = [metadata[k], v]
                    else:
                        metadata[k] = v

                collection = Collection.create(name=name,
                                               container=parent,
                                               metadata=metadata,
                                               username=request.user.name)
                collection.create_acl_list(data['read_access'], data['write_access'])
                messages.add_message(request, messages.INFO,
                                     u"New collection '{}' created" .format(collection.name))
                return redirect('archive:view', path=collection.path)
            except CollectionConflictError:
                messages.add_message(request, messages.ERROR,
                                     "That name is in use in the current collection")
            except ResourceConflictError:
                messages.add_message(request, messages.ERROR,
                                     "That name is in use in the current collection")

    groups = Group.objects.all()
    return render(request, 'archive/new.html', {'form': form, "parent": parent_collection, "groups": groups})
Ejemplo n.º 2
0
 def create_collection(self, parent_path, name, path):
     try:
         c = Collection.create(name,
                               container=parent_path,
                               metadata=None,
                               username=self.user.name)
         c.create_acl_list(self.groups, self.groups)
     except CollectionConflictError:
         # Collection already exists
         c = Collection.find(path)
     self.collection_cache[path] = c
     return c
Ejemplo n.º 3
0
 def create_resource(self, path):
     container, name = split(path)
     Collection.create(name, container)
Ejemplo n.º 4
0
    def put_container(self, path):
        # Check if the container already exists
        collection = Collection.find(path)
        if collection:
            # Update
            if not collection.user_can(self.user, "edit"):
                self.logger.warning(u"User {} tried to modify collection at '{}'".format(self.user, path))
                return Response(status=HTTP_403_FORBIDDEN)
            if self.http_mode:
                # HTTP Request, unsupported
                self.logger.warning(u"Update collection '{}' using HTTP is undefined".format(path))
                return Response(status=HTTP_406_NOT_ACCEPTABLE)
            res = self.put_container_metadata(collection)
            return Response(status=res)

        # Create Collection
        parent, name = split(path)
        if name.startswith("cdmi_"):
            return Response("cdmi_ prefix is not a valid name for a container",
                            status=HTTP_400_BAD_REQUEST)
        parent_collection = Collection.find(parent)
        if not parent_collection:
            self.logger.info(u"Fail to create a collection at '{}', parent collection doesn't exist".format(path))
            return Response(status=HTTP_404_NOT_FOUND)
        # Check if user can create a new collection in the collection
        if not parent_collection.user_can(self.user, "write"):
            self.logger.warning(u"User {} tried to create new collection at '{}'".format(self.user, path))
            return Response(status=HTTP_403_FORBIDDEN)

        body = OrderedDict()
        try:
            collection = Collection.create(name=name,
                                           container=parent)
        except ResourceConflictError:
            return Response(status=HTTP_409_CONFLICT)
        cdmi_container = CDMIContainer(collection, self.api_root)
        delayed = False
        res = self.put_container_metadata(collection)
        if res != HTTP_204_NO_CONTENT:
            return Response(status=res)
        if self.http_mode:
            # Specification states that:
            #
            #     A response message body may be provided as per RFC 2616.
            #
            # Send the CDMI response but with Content-Type = application/json
            content_type = 'application/json'
            # Mandatory completionStatus
            # Does not accept CDMI - cannot return "202 Accepted"
            # Try to wait until complete
            while not path_exists(path):
                # Wait for 5 seconds
                time.sleep(5)
            response_status = "201 Created"
            body['completionStatus'] = "Complete"
        else:
            # CDMI mode
            for field, value in FIELDS_CONTAINER.items():
                get_field = getattr(cdmi_container, 'get_{}'.format(field))
                body[field] = get_field()

            if delayed:
                response_status = HTTP_202_ACCEPTED
                body['completionStatus'] = "Processing"
            else:
                response_status = HTTP_201_CREATED
                body['completionStatus'] = "Complete"
        return JsonResponse(body,
                            content_type=body['objectType'],
                            status=response_status)