Beispiel #1
0
def program_entry(request, program_id):
    """
    Process requests for a single `ProgramModel`s.

    HTTP Methods
    ------------
        DELETE:
            Removes the specified entry (in the URL) from the database.
        PUT:
            Updates the specified entry (in the URL) in the database.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """
    if request.method == 'DELETE':
        try:
            ProgramModel.objects.get(id=program_id).delete()
            return StatusResponse.ok('')
        except ProgramModel.DoesNotExist as err:
            return StatusResponse(ProgramNotExistError(err, program_id))
    elif request.method == 'PUT':
        # create form from a new QueryDict made from the request body
        # (request.PUT is unsupported) as an update (instance) of the
        # existing slave
        try:
            model = ProgramModel.objects.get(id=program_id)

            form = ProgramForm(QueryDict(request.body), instance=model)
            if form.is_valid():
                program = form.save(commit=False)
                try:
                    program.full_clean()
                    form.save()
                    return StatusResponse.ok('')
                except ValidationError as _:
                    error_dict = {
                        'name': [
                            'Program with this Name already exists on this Client.'
                        ]
                    }
                    return StatusResponse.err(error_dict)
            else:
                return StatusResponse.err(form.errors)
        except ProgramModel.DoesNotExist as err:
            return StatusResponse(ProgramNotExistError(err, program_id))
    else:
        return HttpResponseForbidden()
Beispiel #2
0
def script_put_post(data, script_id):
    """
    This functions removes code duplication for `script_entry` and
    `script_set`. The logic for the PUT and POST method inside these functions
    are identical. For more information take a look at `script_entry` or
    `script_set`
    """
    try:
        script = Script.from_json(data)
        if script_id is None:
            script.save()
        else:
            (new_model, _) = ScriptModel.objects.update_or_create(
                id=script_id,
                defaults={"name": script.name},
            )

            SGFModel.objects.filter(script_id=script_id).delete()
            SGPModel.objects.filter(script_id=script_id).delete()

            for program in script.programs:
                program.save(new_model)
            for filesystem in script.filesystems:
                filesystem.save(new_model)

        return StatusResponse.ok('')
    except FsimError as err:
        return StatusResponse(err)
    except KeyError as err:
        return StatusResponse.err("Could not find required key {}".format(
            err.args[0]))
    except TypeError as err:
        return StatusResponse.err(str(err))
    except ValueError as err:
        return StatusResponse.err(str(err))
    except ValidationError as err:
        return StatusResponse.err('; '.join(err.messages))
    except IntegrityError as err:
        return StatusResponse.err(str(err))
Beispiel #3
0
def slave_entry(request, slave_id):
    """
    Process requests for a single `SlaveModel`s.

    HTTP Methods
    ------------
        DELETE:
            Removes the specified entry (in the URL) from the database.
        PUT:
            Updates the specified entry (in the URL) in the database.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """
    if request.method == 'DELETE':
        try:
            SlaveModel.objects.get(id=slave_id).delete()
            return StatusResponse.ok('')
        except SlaveModel.DoesNotExist as err:
            return StatusResponse(SlaveNotExistError(err, slave_id))

    elif request.method == 'PUT':
        try:
            # create form from a new QueryDict made from the request body
            # (request.PUT is unsupported) as an update (instance) of the
            # existing slave
            model = SlaveModel.objects.get(id=slave_id)
            form = SlaveForm(QueryDict(request.body), instance=model)

            if form.is_valid():
                form.save()
                return StatusResponse.ok('')
            else:
                return StatusResponse.err(form.errors)
        except SlaveModel.DoesNotExist as err:
            return StatusResponse(SlaveNotExistError(err, slave_id))
    else:
        return HttpResponseForbidden()
Beispiel #4
0
def filesystem_set(request):
    """
    Process requests on a set of `FilesystemModel`s.

    HTTP Methods
    ------------
        POST:
            Adds a new `FilesystemModel` to the database.
        GET: query with (?q=None)
            Searches for the name which is like ".*q.*"
        GET: query with (?slave=None&is_string=False)
            Searches for all `FilesystemModel`s which belong to the given `slave`.
            Where `is_string` specifies if the given `slave` is an unique name or
            and unique index.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """
    if request.method == 'POST':
        form = FilesystemForm(request.POST or None)

        if form.is_valid():
            filesystem = form.save(commit=False)
            filesystem.slave = form.cleaned_data['slave']

            try:
                filesystem.full_clean()
                # IMPORTANT: remove trailing path seperator (if not the query
                # will not work [the query in filesystem_move])
                filesystem.destination_path = up.remove_trailing_path_seperator(
                    filesystem.destination_path)
                filesystem.source_path = up.remove_trailing_path_seperator(
                    filesystem.source_path)
                form.save()

                return StatusResponse.ok("")
            except ValidationError as err:
                LOGGER.warning(
                    "Error while adding filesystem `%s`: %s",
                    filesystem.name,
                    err,
                )

                string = err.message_dict['__all__'][0]
                if 'Source path' in string and 'Destination path' in string and 'Slave' in string:

                    error_msg = 'Filesystem with this source path and destination path already exists on this Client.'
                    error_dict = {
                        'source_path': [error_msg],
                        'destination_path': [error_msg],
                    }
                elif 'Name' in err.message_dict['__all__'][
                        0] and 'Slave' in err.message_dict['__all__'][0]:
                    error_dict = {
                        'name': [
                            'Filesystem with this Name already exists on this Client.'
                        ]
                    }
                return StatusResponse.err(error_dict)
        else:
            return StatusResponse.err(form.errors)

    elif request.method == 'GET':
        query = request.GET.get('q', None)

        slave = request.GET.get('slave', None)
        slave_str = request.GET.get('is_string', False)

        if query is not None:
            filesystems = FilesystemModel.objects.filter(
                name__contains=query).values_list(
                    "name",
                    flat=True,
                )
        elif slave is not None:
            if slave_str:
                slave_str = convert_str_to_bool(slave_str)

            try:
                slave = SlaveModel.from_identifier(slave, slave_str)
            except FsimError as err:
                return StatusResponse(err)
            except SlaveModel.DoesNotExist as err:
                return StatusResponse(SlaveNotExistError(err, slave))

            filesystems = FilesystemModel.objects.filter(
                slave=slave).values_list(
                    "name",
                    flat=True,
                )

        else:
            filesystems = FilesystemModel.objects.all().values_list(
                "name",
                flat=True,
            )

        return StatusResponse.ok(list(filesystems))
    else:
        return HttpResponseForbidden()
Beispiel #5
0
def program_set(request):
    """
    Process requests on a set of `ProgramModel`s.

    HTTP Methods
    ------------
        POST:
            Adds a new `ProgramModel` to the database.
        GET: query with (?q=None)
            Searches for the name which is like ".*q.*"
        GET: query with (?slave=None&is_string=False)
            Searches for all `ProgramModel`s which belong to the given `slave`.
            Where `is_string` specifies if the given `slave` is an unique name
            or and unique index.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """
    if request.method == 'POST':
        form = ProgramForm(request.POST or None)

        if form.is_valid():
            program = form.save(commit=False)
            program.slave = form.cleaned_data['slave']
            try:
                program.full_clean()
                form.save()
                return StatusResponse.ok('')
            except ValidationError as _:
                error_dict = {
                    'name':
                    ["Program with this Name already exists on this Client."]
                }
                return StatusResponse.err(error_dict)
        else:
            return StatusResponse.err(form.errors)
    elif request.method == 'GET':
        query = request.GET.get('q', None)

        slave = request.GET.get('slave', None)
        slave_str = request.GET.get('is_string', False)

        if query is not None:
            progs = ProgramModel.objects.filter(
                name__contains=query).values_list(
                    "name",
                    flat=True,
                )
        elif slave is not None:
            if slave_str:
                slave_str = convert_str_to_bool(slave_str)

            try:
                slave = SlaveModel.from_identifier(slave, slave_str)
            except FsimError as err:
                return StatusResponse(err)
            except SlaveModel.DoesNotExist as err:
                return StatusResponse(SlaveNotExistError(err, slave))

            progs = ProgramModel.objects.filter(slave=slave).values_list(
                "name",
                flat=True,
            )

        else:
            progs = ProgramModel.objects.all().values_list(
                "name",
                flat=True,
            )

        return StatusResponse.ok(list(progs))
    else:
        return HttpResponseForbidden()
Beispiel #6
0
def slave_set(request):
    """
    Process requests on a set of `SlaveModel`s.

    HTTP Methods
    ------------
        POST:
            Adds a new `SlaveModel` to the database.
        GET: query with (?q=None)
            Searches for the name which is like ".*q.*"
        GET: query with (?programs=False)
            If this is True, then all `SlaveModel`s are returned which have a
            `ProgramModel`.
        GET: query with (?filesystems=False)
            If this is True, then all `SlaveModel`s are returned which have a
            `FilesystemModel`.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """
    if request.method == 'POST':
        form = SlaveForm(request.POST)
        if form.is_valid():
            form.save()
            return StatusResponse.ok('')
        return StatusResponse.err(form.errors)
    elif request.method == 'GET':
        query = request.GET.get('q', None)

        programs = request.GET.get('programs', '')
        programs = convert_str_to_bool(programs)

        filesystems = request.GET.get('filesystems', '')
        filesystems = convert_str_to_bool(filesystems)

        if query is not None:
            slaves = SlaveModel.objects.filter(
                name__contains=query).values_list(
                    "name",
                    flat=True,
                )
        elif programs or filesystems:
            if programs and filesystems:
                return StatusResponse(
                    SimultaneousQueryError('filesystems', 'programs'))
            elif programs:
                slaves = SlaveModel.with_programs()
            elif filesystems:
                slaves = SlaveModel.with_filesystems()
        else:
            slaves = SlaveModel.objects.all().values_list(
                'name',
                flat=True,
            )
        return StatusResponse.ok(list(slaves))
    else:
        return HttpResponseForbidden()