Skip to content

Commit

Permalink
shotgrid manager batch interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jlorrain authored and ClementHector committed Feb 7, 2022
1 parent a8a10bb commit 53e48ab
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 54 deletions.
22 changes: 10 additions & 12 deletions openpype/modules/default_modules/shotgrid/lib/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import traceback

import requests
from typing import Dict, Any
from openpype.modules.default_modules.shotgrid.lib import (
Expand All @@ -22,10 +24,11 @@ def poll_server() -> int:


def check_batch_settings(
project: str, url: str, script_name: str, api_key: str, project_id: int
url: str, script_name: str, api_key: str, project_id: int
) -> bool:
module_url = _format_url(settings_lib.get_module_server_url())
api_url = "/".join([module_url, "batch", project, "check"])
api_url = "/".join([module_url, "batch", "check"])
print(api_url)
params = {
"shotgrid_url": url,
"shotgrid_project_id": project_id,
Expand All @@ -35,13 +38,9 @@ def check_batch_settings(

try:
res = requests.get(api_url, params=params)
return {"status_code": res.status_code, "payload": res.json()}
except requests.exceptions.RequestException:
return False

if res.status_code == 200:
return res.json().get("status", "KO") == "OK"
else:
return False
traceback.print_stack()


def send_batch_request(
Expand All @@ -67,7 +66,6 @@ def send_batch_request(

try:
res = requests.post(api_url, json=payload)
except requests.exceptions.RequestException:
return 404

return res.status_code
return {"status_code": res.status_code, "payload": res.json()}
except requests.exceptions.RequestException as e:
traceback.print_stack()
8 changes: 5 additions & 3 deletions openpype/modules/default_modules/shotgrid/manager/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap.min.css">

<link rel="icon" href="../../../../resources/app_icons/favicon.png">

<title>Shotgrid Manager</title>
</head>
Expand All @@ -28,18 +29,19 @@
<!-- Batch Tab -->
<div class="tab-pane fade show active" id="nav-batch" role="tabpanel" aria-labelledby="nav-batch-tab">
<div class="container">
<form class="" id="batchForm">

<div class="mb-3 row">
<label for="selectOpenPypeProject" class="col-sm-3 col-form-label">OpenPype project</label>
<div class="col-sm-4">
<select class="form-control form-control-sm" id="selectOpenPypeProject">
<option value="#new">new project</option>
<option value="newproject">new project</option>
</select>
</div>
<button type="button" class="col-sm-1 btn btn-sm btn-primary" id="reloadBatchConf">
reload
</button>
</div>
<form action="#" class="needs-validation" id="batchForm" novalidate>
<div class="form-group row">
<label for="shotgridUrl" class="col-sm-2 col-form-label">url</label>
<div class="col-sm-6">
Expand Down Expand Up @@ -81,7 +83,7 @@
<div class="footer">
<div class="float-left" id="infoPanel"></div>
<div class="float-right">
<button type="button" class="btn btn-primary" id="batchBtn">Batch</button>
<button type="submit" class="btn btn-primary" id="batchBtn">Batch</button>
</div>
</div>
</form>
Expand Down
79 changes: 55 additions & 24 deletions openpype/modules/default_modules/shotgrid/manager/js/manager.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
window.addEventListener('pywebviewready', () => {
console.log('pywebview ready');
window.pywebview.api.getProjectList().then(fillProjectsSelector);
window.pywebview.api.checkServerStatus().then((result) => {
if (!result) {
printError("Shotgrid module API unreachable");
// Check Form validity before Submit
(function () {
'use strict'

// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')

// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
});
})
onBatchSubmit(event);
form.classList.add('was-validated')
}, false)
})
})()

window.addEventListener('pywebviewready', initializeInterface)

window.addEventListener('load', (event) => {
batchSelector = document.getElementById('selectOpenPypeProject');
batchSelector.addEventListener("change", onBatchSelectorChange);

$("#batchBtn").click(onBatchSubmit);
});

/* Event functions */
Expand All @@ -22,7 +33,7 @@ function onBatchSelectorChange(event) {
project = event.target.options[event.target.selectedIndex].value;
$("#shotgridUrl, #shotgridScriptName, #shotgridApiKey, #shotgridProjectId, #shotgridFieldMapping").val("");

if (project != "#new") {
if (project != "newproject") {
batchWorking();
window.pywebview.api.getProjectBatchInfos(project).then((infos) => {
$("#shotgridUrl").val(infos.url);
Expand All @@ -36,27 +47,40 @@ function onBatchSelectorChange(event) {

function onBatchSubmit(event) {
batchWorking();

checkBatchValues().then(() => {
SendBatch().then(() => {
checkBatchValues().then((project_name) => {
SendBatch(project_name).then(() => {
printInfo("Batch sent successfully");
batchEndWorking();
event.stopPropagation()
}).catch((error) => {
printWarning(error);
batchEndWorking();
event.stopPropagation()
});
}).catch(error => {
printWarning(error);
batchEndWorking();
event.stopPropagation()
});
}

/* Functions */

function initializeInterface() {
console.log('Initialize interface');
window.pywebview.api.getProjectList().then(fillProjectsSelector);
window.pywebview.api.checkServerStatus().then((result) => {
if (!result) {
printError("Shotgrid module API unreachable");
}
});
window.removeEventListener('pywebviewready', initializeInterface);
}

function fillProjectsSelector(projectList) {
batchSelector = document.getElementById('selectOpenPypeProject');
ScheduleSelector = document.getElementById('selectOpenPypeProjectSchedule');

batchSelector.innerHTML = "<option value=\"newproject\">new project</option>";
projectList.forEach(function(project){
var el = document.createElement("option");
el.innerHTML = project;
Expand All @@ -71,42 +95,49 @@ function checkBatchValues() {
infos = getBatchInfos();
return new Promise((success, failure) => {
window.pywebview.api.checkProjectSettings(
infos['project'],
infos['url'],
infos['script_name'],
infos['api_key'],
infos['project_id'],
).then((result) => {
if (result){
success();
console.log(result);
if (result["status_code"] == "200"){
success(result["payload"]["status"]);
} else {
failure("Could not run batch with those settings");
}
});
})
}

function SendBatch() {
function SendBatch(projectName) {
infos = getBatchInfos();
newProject = (infos['project'] == "newproject")
if (!newProject) {
projectName = infos['project']
}
return new Promise((success, failure) => {
var fieldsMapping;
try {
fieldsMapping = JSON.parse(infos['fields_mapping']);
} catch(e) {
failure("fields_mapping field contained malformed json");
return;
}
window.pywebview.api.sendBatch(
infos['project'],
newProject,
projectName,
infos['url'],
infos['script_name'],
infos['api_key'],
infos['project_id'],
fieldsMapping
).then((result) => {
if (result){
console.log(result);
if (result['status_code'] == 200){
success();
} else {
failure();
failure(result['payload']['detail']);
}
});
})
Expand All @@ -130,12 +161,12 @@ function batchWorking() {
spinner.setAttribute('class', "spinner-border spinner-border-sm");
spinner.setAttribute('role', "status");
spinner.setAttribute('aria-hidden', "true");
$("#batchBtn").attr("disabled");
$("#batchBtn").addClass("disabled");
$("#batchBtn").html(spinner);
}

function batchEndWorking() {
$("#batchBtn").attr("enabled");
$("#batchBtn").removeClass("disabled");
$("#batchBtn").html("Batch");
}

Expand Down
25 changes: 15 additions & 10 deletions openpype/modules/default_modules/shotgrid/tray/manager_api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Dict, Any
from openpype.modules.default_modules.shotgrid.lib import settings, server
from openpype.lib import create_project


class ManagerApi():
class ManagerApi:

def getProjectList(self):
print("get project list")
return settings.get_project_list()

def getProjectBatchInfos(self, project):
Expand All @@ -28,29 +28,34 @@ def checkServerStatus(self):

def checkProjectSettings(
self,
project: str,
url: str,
script_name: str,
api_key: str,
project_id: int,
):
print("Check project ", project)
if not server.check_batch_settings(
project, url, script_name, api_key, project_id
):
return False
return True
print("Check project ", url, script_name, api_key, project_id)
res = server.check_batch_settings(
url, script_name, api_key, project_id
)
return res

def sendBatch(
self,
new_project: bool,
project: str,
url: str,
script_name: str,
api_key: str,
project_id: int,
fields_mapping: Dict[str, Any],
):
print("Send Batch ", project, url, script_name, api_key, project_id)

if new_project:
print("Create project")
create_project(project, project)

res = server.send_batch_request(
project, url, script_name, api_key, project_id, fields_mapping
)
return res == 200
return res
18 changes: 13 additions & 5 deletions openpype/modules/default_modules/shotgrid/tray/shotgrid_tray.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def show_batch_dialog(self):
manager_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
"manager/index.html")
webview.create_window('Shotgrid Manager', url=manager_path,
js_api=api, min_size=(790, 500))
webview.start(gui='cef', debug=False)
js_api=api, min_size=(785, 500),
width=785, height=480)
webview.start(gui='cef')


def show_connect_dialog(self):
Expand All @@ -45,15 +46,22 @@ def show_credential_dialog(self):
self.credentials_dialog.raise_()

def tray_menu(self, tray_menu):
# Add login to user menu
menu = QtWidgets.QMenu("Shotgrid", tray_menu)
show_connect_action = QtWidgets.QAction("Connect to Shotgrid", menu)
show_connect_action.triggered.connect(self.show_connect_dialog)
show_batch_action = QtWidgets.QAction("Launch Batch", menu)
show_batch_action.triggered.connect(self.show_batch_dialog)
menu.addAction(show_connect_action)
menu.addAction(show_batch_action)
tray_menu.addMenu(menu)

# Add manager to Admin menu
for m in tray_menu.findChildren(QtWidgets.QMenu):
if m.title() == "Admin":
shotgrid_manager_action = QtWidgets.QAction("Shotgrid manager",
menu)
shotgrid_manager_action.triggered.connect(
self.show_batch_dialog)
m.addAction(shotgrid_manager_action)

def validate(self) -> bool:
shotgrid_url = settings.get_shotgrid_url()

Expand Down

0 comments on commit 53e48ab

Please sign in to comment.