Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Added create,edit and schedule options #38

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 108 additions & 28 deletions invenio_jobs/administration/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,70 @@
# details.

"""Invenio administration view module."""

from flask import current_app
from invenio_administration.views.base import AdminResourceListView
from invenio_administration.views.base import (
AdminResourceCreateView,
AdminResourceEditView,
AdminResourceListView,
)
from invenio_i18n import lazy_gettext as _

from invenio_jobs.config import JOBS_QUEUES
from invenio_jobs.models import Task
from invenio_jobs.services.ui_schema import ScheduleUISchema

class JobsListView(AdminResourceListView):
"""Configuration for Jobs list view."""

class JobsAdminMixin:
"""Common admin properties."""

api_endpoint = "/jobs"
name = "jobs"
resource_config = "jobs_resource"
pid_path = "id"

display_search = False
display_delete = False
display_create = True
display_edit = True

search_config_name = "JOBS_SEARCH"
search_sort_config_name = "JOBS_SORT_OPTIONS"
search_facets_config_name = "JOBS_FACETS"


class JobsListView(JobsAdminMixin, AdminResourceListView):
"""Configuration for Jobs list view."""

name = "jobs"
search_request_headers = {"Accept": "application/vnd.inveniordm.v1+json"}
title = "Jobs"
menu_label = "Jobs"
category = "System"
pid_path = "id"
icon = "settings"
template = "invenio_jobs/system/jobs/jobs-search.html"

display_search = False
display_delete = False
display_create = False
display_edit = False
create_view_name = "jobs-create"

item_field_list = {
"job": {"text": _("Jobs"), "order": 1, "width": 3},
"last_run_start_time": {"text": _("Last run"), "order": 2, "width": 3},
"user": {"text": _("Started by"), "order": 3, "width": 3},
"next_run": {"text": _("Next run"), "order": 4, "width": 3},
"action": {"text": _("Action"), "order": 5, "width": 2},
"active": {"text": _("Status"), "order": 2, "width": 2},
"last_run_start_time": {"text": _("Last run"), "order": 3, "width": 3},
"user": {"text": _("Started by"), "order": 4, "width": 3},
"next_run": {"text": _("Next run"), "order": 5, "width": 3},
}
actions = {
"schedule": {
"text": "Schedule",
"payload_schema": ScheduleUISchema,
"order": 1,
}
}

search_config_name = "JOBS_SEARCH"
search_sort_config_name = "JOBS_SORT_OPTIONS"
search_facets_config_name = "JOBS_FACETS"

@staticmethod
def disabled():
"""Disable the view on demand."""
return not current_app.config["JOBS_ADMINISTRATION_ENABLED"]


class JobsDetailsView(AdminResourceListView):
class JobsDetailsView(JobsAdminMixin, AdminResourceListView):
"""Configuration for Jobs detail view which shows runs."""

def get_api_endpoint(self, pid_value=None):
Expand All @@ -65,13 +85,8 @@ def get_api_endpoint(self, pid_value=None):
disabled = lambda _: True

template = "invenio_jobs/system/jobs/jobs-details.html"
display_delete = False
display_edit = False
display_search = False
display_create = False

list_view_name = "jobs"
pid_path = "id"
pid_value = "<pid_value>"

item_field_list = {
Expand All @@ -82,6 +97,71 @@ def get_api_endpoint(self, pid_value=None):
"action": {"text": _("Action"), "order": 5, "width": 2},
}

search_config_name = "JOBS_SEARCH"
search_sort_config_name = "JOBS_SORT_OPTIONS"
search_facets_config_name = "JOBS_FACETS"

class JobsFormMixin:
"""Mixin class for form fields."""

@property
def form_fields(self):
"""Initializing form fields."""
jobs_queues = [
{"title_l10n": str(queue["title"]), "id": queue["name"]}
for queue in JOBS_QUEUES.values()
]
tasks = [{"title_l10n": name, "id": name} for name, t in Task.all().items()]
return {
"title": {
"order": 1,
"text": _("Title"),
"description": _("A title of the job."),
},
"description": {
"order": 2,
"text": _("Description"),
"description": _("A short description about the job."),
},
"default_queue": {
"order": 3,
"text": _("Queue"),
"description": _("A queue for the job run."),
"placeholder": "Select the queue",
"options": jobs_queues,
},
"task": {
"order": 4,
"text": _("Task"),
"description": _("A task for the job run."),
"placeholder": "Select the task",
"options": tasks,
},
"active": {
"order": 5,
"text": _("Active"),
},
"default_args": {
"order": 6,
"text": _("Default Arguments"),
"description": _("A task for the job run."),
},
"created": {"order": 7},
"updated": {"order": 8},
}


class JobsEditView(JobsAdminMixin, JobsFormMixin, AdminResourceEditView):
"""Configuration for job edit view."""

name = "jobs-edit"
url = "/jobs/<pid_value>/edit"
title = "Job Edit"
list_view_name = "jobs"


class JobsCreateView(JobsAdminMixin, JobsFormMixin, AdminResourceCreateView):
"""Configuration for Jobs create view."""

name = "jobs-create"
url = "/jobs/create"
api_endpoint = "/jobs"
title = "Create Job"
list_view_name = "jobs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Button, Modal, Icon } from "semantic-ui-react";
import { ActionModal, ActionForm } from "@js/invenio_administration";
import _isEmpty from "lodash/isEmpty";
import { i18next } from "@translations/invenio_app_rdm/i18next";
import ScheduleJobModal from "./ScheduleJobModal";

export class JobActions extends Component {
constructor(props) {
super(props);
this.state = {
modalOpen: false,
modalHeader: undefined,
modalBody: undefined,
};
}

onModalTriggerClick = (e, { payloadSchema, dataName, dataActionKey }) => {
const { resource } = this.props;
const { modalOpen } = this.state;

if (dataActionKey === "schedule") {
this.setState({
modalOpen: true,
modalHeader: i18next.t("Schedule Job"),
modalBody: (
<ScheduleJobModal
actionSuccessCallback={this.handleSuccess}
actionCancelCallback={this.closeModal}
modalOpen={modalOpen}
data={resource}
payloadSchema={payloadSchema}
apiUrl={`/api/jobs/${resource.id}`}
/>
),
});
} else {
this.setState({
modalOpen: true,
modalHeader: dataName,
modalBody: (
<ActionForm
actionKey={dataActionKey}
actionSchema={payloadSchema}
actionSuccessCallback={this.handleSuccess}
actionCancelCallback={this.closeModal}
resource={resource}
/>
),
});
}
};

closeModal = () => {
this.setState({
modalOpen: false,
modalHeader: undefined,
modalBody: undefined,
});
};

handleSuccess = () => {
this.setState({
modalOpen: false,
modalHeader: undefined,
modalBody: undefined,
});
setTimeout(() => {
window.location.reload();
}, 1000);
};

render() {
const { actions, Element, resource } = this.props;
const { modalOpen, modalHeader, modalBody } = this.state;
return (
<>
{Object.entries(actions).map(([actionKey, actionConfig]) => {
if (actionKey === "schedule") {
return (
<Element
key={actionKey}
onClick={this.onModalTriggerClick}
payloadSchema={actionConfig.payload_schema}
dataName={actionConfig.text}
dataActionKey={actionKey}
icon
labelPosition="left"
>
<Icon name="calendar" />
{actionConfig.text}
</Element>
);
} else {
return (
<Element
key={actionKey}
onClick={this.onModalTriggerClick}
payloadSchema={actionConfig.payload_schema}
dataName={actionConfig.text}
dataActionKey={actionKey}
>
{actionConfig.text}
</Element>
);
}
})}
<ActionModal modalOpen={modalOpen} resource={resource}>
{modalHeader && <Modal.Header>{modalHeader}</Modal.Header>}
{!_isEmpty(modalBody) && modalBody}
</ActionModal>
</>
);
}
}

JobActions.propTypes = {
resource: PropTypes.object.isRequired,
actions: PropTypes.shape({
text: PropTypes.string.isRequired,
payload_schema: PropTypes.object.isRequired,
order: PropTypes.number.isRequired,
}),
Element: PropTypes.node,
};

JobActions.defaultProps = {
Element: Button,
actions: undefined,
};
Loading