Ejemplo n.º 1
0
 def put(self, resource_id):
     """Update a deposition."""
     d = Deposition.get(resource_id, user=current_user)
     self.validate_input(d)
     self.process_input(d)
     d.save()
     return d.marshal()
Ejemplo n.º 2
0
 def get(self, resource_id, file_id):
     """Get a deposition file."""
     d = Deposition.get(resource_id, user=current_user)
     df = d.get_file(file_id)
     if df is None:
         abort(404, message="File does not exist", status=404)
     return d.type.marshal_file(df)
Ejemplo n.º 3
0
    def post(self, resource_id):
        """Upload a file."""
        d = Deposition.get(resource_id, user=current_user)

        # Bail-out early if not permitted (add_file will also check, but then
        # we already uploaded the file)
        if not d.authorize('add_file'):
            raise ForbiddenAction('add_file', d)

        uploaded_file = request.files['file']
        filename = secure_filename(
            request.form.get('filename') or uploaded_file.filename
        )

        description = request.form.get('description')
        df = DepositionFile(backend=DepositionStorage(d.id))

        if df.save(uploaded_file, filename=filename, description=description):
            try:
                d.add_file(df)
                d.save()
            except FilenameAlreadyExists as e:
                df.delete()
                raise e

        return d.type.marshal_file(df), 201
Ejemplo n.º 4
0
    def delete(self, resource_id, file_id):
        """Delete existing deposition file."""
        d = Deposition.get(resource_id, user=current_user)

        # Sort files raise ForbiddenAction if not authorized
        df = d.remove_file(file_id)
        if df is None:
            abort(404, message="File does not exist", status=404)
        df.delete()
        d.save()
        return "", 204
Ejemplo n.º 5
0
    def put(self, resource_id, file_id):
        """Update a deposition file - i.e. rename it."""
        v = APIValidator()
        if not v.validate(request.json, file_schema):
            abort(
                400,
                message="Bad request",
                status=400,
                errors=map(lambda x: dict(
                    message=x,
                    code=error_codes["validation_error"]
                ), v.errors),
            )

        d = Deposition.get(resource_id, user=current_user)
        df = d.get_file(file_id)

        if not d.type.authorize_file(d, df, 'update_metadata'):
            raise ForbiddenAction('update_metadata', df)

        new_name = secure_filename(request.json['filename'])
        if new_name != request.json['filename']:
            abort(
                400,
                message="Bad request",
                status=400,
                errors=[dict(
                    message="Not a valid filename",
                    code=error_codes["validation_error"]
                )],
            )

        df.name = new_name
        d.save()

        return d.type.marshal_file(df)
Ejemplo n.º 6
0
    def put(self, resource_id):
        """Sort files in collection."""
        if not isinstance(request.json, list):
            abort(
                400,
                message="Bad request",
                status=400,
                errors=[dict(
                    message="Expected a list",
                    code=error_codes["validation_error"],
                )],
            )

        v = APIValidator()
        for file_item in request.json:
            if not v.validate(file_item, file_schema_list):
                abort(
                    400,
                    message="Bad request",
                    status=400,
                    errors=map(lambda x: dict(
                        message=x,
                        code=error_codes["validation_error"]
                    ), v.errors),
                )

        d = Deposition.get(resource_id, user=current_user)

        for file_item in request.json:
            if not d.get_file(file_item['id']):
                raise FileDoesNotExists(file_item['id'])

        # Sort files raise ForbiddenAction if not authorized
        d.sort_files(map(lambda x: x['id'], request.json))
        d.save()
        return map(lambda f: d.type.marshal_file(f), d.files)
Ejemplo n.º 7
0
 def get(self, resource_id):
     """Get deposition list of files."""
     d = Deposition.get(resource_id, user=current_user)
     return map(lambda f: d.type.marshal_file(f), d.files)
Ejemplo n.º 8
0
 def post(self, resource_id, action_id):
     """Run an action."""
     d = Deposition.get(resource_id, user=current_user)
     return d.type.api_action(d, action_id)
Ejemplo n.º 9
0
 def put(self, resource_id, draft_id):
     """Update a deposition draft."""
     d = Deposition.get(resource_id, user=current_user)
     self.validate_input(d, draft_id)
     self.process_input(d, draft_id)
     d.save()
Ejemplo n.º 10
0
 def get(self, oauth, resource_id, draft_id):
     """Get a deposition draft."""
     d = Deposition.get(resource_id, user=current_user)
     return d.type.marshal_draft(d.get_draft(draft_id))
Ejemplo n.º 11
0
 def get(self, resource_id):
     """List all drafts."""
     d = Deposition.get(resource_id, user=current_user)
     return map(lambda x: d.type.marshal_draft(x), d.drafts_list)
Ejemplo n.º 12
0
 def delete(self, resource_id):
     """Delete existing deposition."""
     d = Deposition.get(resource_id, user=current_user)
     d.delete()
     return "", 204
Ejemplo n.º 13
0
 def get(self, resource_id):
     """Get a deposition."""
     return Deposition.get(resource_id, user=current_user).marshal()