Skip to content

Commit

Permalink
Store credential
Browse files Browse the repository at this point in the history
  • Loading branch information
clement.hector authored and ClementHector committed Feb 7, 2022
1 parent b60e484 commit 8dab062
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 50 deletions.
6 changes: 3 additions & 3 deletions openpype/modules/shotgrid/lib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@


def get_shotgrid_settings() -> Dict[AnyStr, Any]:
return get_system_settings()["modules"][MODULE_NAME]
return get_system_settings().get("modules", {}).get(MODULE_NAME, {})


def get_shotgrid_url_from_settings() -> AnyStr:
return get_shotgrid_settings()["shotgrid_url"]
def get_shotgrid_url() -> AnyStr:
return get_shotgrid_settings().get("shotgrid_url")


def get_shotgrid_event_mongo_info() -> Tuple[AnyStr, AnyStr]:
Expand Down
6 changes: 1 addition & 5 deletions openpype/modules/shotgrid/shotgrid_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ShotgridModule(PypeModule, ITrayModule, IPluginPaths, ILaunchHookPaths):
def initialize(self, modules_settings: Dict[AnyStr, Any]):
shotgrid_settings = modules_settings.get(self.name, dict())
self.enabled = shotgrid_settings.get("enabled", False)
shotgrid_url = shotgrid_settings.get("shotgrid_server").strip()
shotgrid_url = shotgrid_settings.get("shotgrid_url").strip()

self.shotgrid_url = shotgrid_url

Expand Down Expand Up @@ -60,10 +60,6 @@ def tray_exit(self, *args, **kwargs):
def tray_menu(self, tray_menu):
return self.tray_wrapper.tray_menu(tray_menu)

def set_credentials_to_env(self, username: AnyStr, api_key: AnyStr):
os.environ["SHOTGRID_LOGIN"] = username or ""
os.environ["SHOTGRID_PASSWORD"] = api_key or ""

def create_shotgrid_session(self) -> shotgun_api3.Shotgun:
credentials_ = credentials.get_credentials()
return shotgun_api3.Shotgun(
Expand Down
100 changes: 62 additions & 38 deletions openpype/modules/shotgrid/tray/credential_dialog.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from typing import Any
from typing import Any, AnyStr
from Qt import QtCore, QtWidgets, QtGui

from openpype import style
from openpype import resources
from openpype.api import get_system_settings

import shotgun_api3
from shotgun_api3.shotgun import AuthenticationFault
from openpype.modules.shotgrid.lib import credentials
from openpype.modules.shotgrid.lib import settings


class CredentialsDialog(QtWidgets.QDialog):
SIZE_W = 450
SIZE_H = 150
SIZE_H = 200

_module: Any = None
_is_logged: bool = False
Expand Down Expand Up @@ -47,7 +45,6 @@ def __init__(self, module, parent=None):
self.setStyleSheet(style.load_stylesheet())

self.ui_init()
self.fill_ftrack_url()

def ui_init(self):
self.url_label = QtWidgets.QLabel("Shotgrid URL:")
Expand All @@ -65,6 +62,7 @@ def ui_init(self):
self.password_input.setEchoMode(QtWidgets.QLineEdit.Password)

self.error_label = QtWidgets.QLabel("")
self.error_label.setStyleSheet("color: red;")
self.error_label.setWordWrap(True)
self.error_label.hide()

Expand All @@ -85,6 +83,7 @@ def ui_init(self):
self.logout_button.clicked.connect(self._on_shotgrid_logout_clicked)

self.buttons_layout = QtWidgets.QHBoxLayout()
self.buttons_layout.addWidget(self.logout_button)
self.buttons_layout.addWidget(self.login_button)

self.main_widget = QtWidgets.QVBoxLayout(self)
Expand All @@ -94,26 +93,42 @@ def ui_init(self):

def show(self, *args, **kwargs):
super(CredentialsDialog, self).show(*args, **kwargs)
self.fill_ftrack_url()

def fill_ftrack_url(self):
shotgrid_settings = (
get_system_settings().get("modules", {}).get("shotgrid", {})
)
self._fill_shotgrid_url()
self._fill_shotgrid_login()

url = shotgrid_settings.get("shotgrid_server")
def _fill_shotgrid_url(self):
url = settings.get_shotgrid_url()

if url:
self.url_input.setText(url)
self._valid_input(self.url_input)
self.login_button.show()
self.logout_button.show()
enabled = True
else:
self.url_input.setText("Ask your admin to add the shotgrid url")
self._invalid_input(self.url_input)
self.login_button.hide()
self.logout_button.hide()
enabled = False

self.login_input.setEnabled(enabled)
self.password_input.setEnabled(enabled)

def _fill_shotgrid_login(self):
cred = credentials.get_credentials(settings.get_shotgrid_url())
login = cred.get("login")
password = cred.get("login")

if login:
self.login_input.setText(login)
if password:
self.password_input.setText(password)

def _clear_shotgrid_login(self):
self.login_input.setText("")
self.password_input.setText("")

def _on_shotgrid_login_clicked(self):
login = self.login_input.text().strip()
password = self.password_input.text().strip()
Expand All @@ -127,50 +142,59 @@ def _on_shotgrid_login_clicked(self):
missing.append("password")
self._invalid_input(self.password_input)

url = settings.get_shotgrid_url()
if url == "":
missing.append("url")
self._invalid_input(self.url_input)

if len(missing) > 0:
self.set_error("You didn't enter {}".format(" and ".join(missing)))
return

if not self.login_with_credentials(login, password):
self._invalid_input(self.user_input)
self._invalid_input(self.api_input)
self.set_error(
"We're unable to sign in to Ftrack with these credentials"
if credentials.check_credentials(
login=login,
password=password,
shotgrid_url=url,
):
credentials.save_credentials(
login=login, password=password, shotgrid_url=url
)

try:
sg = shotgun_api3.Shotgun(
self.url_input.text(), login=login, password=password
)

sg.preferences_read()
self._on_login()

except AuthenticationFault as err:
raise AuthenticationFault(err)

except Exception as err:
print(err)
self.set_error("CANT LOGIN")

def _on_shotgrid_logout_clicked(self):
pass
credentials.clear_credentials(settings.get_shotgrid_url())
self._clear_shotgrid_login()
self._on_logout()

def set_error(self, msg):
def set_error(self, msg: AnyStr):
self.error_label.setText(msg)
self.error_label.show()

def _on_login(self):
print(
f"You are logged into shotgrid with user {self.login_input.text()}"
)
self._is_logged = True
self._close_widget()

def _on_logout(self):
self._is_logged = False

def _close_widget(self):
self.hide()

def _valid_input(self, input_widget):
def _valid_input(self, input_widget: QtWidgets.QLineEdit):
input_widget.setStyleSheet("")

def _invalid_input(self, input_widget):
def _invalid_input(self, input_widget: QtWidgets.QLineEdit):
input_widget.setStyleSheet("border: 1px solid red;")

def login_with_credentials(
self, url: AnyStr, login: AnyStr, password: AnyStr
) -> bool:
verification = credentials.check_credentials(url, login, password)
if verification:
credentials.save_credentials(login, password, False)
self._module.set_credentials_to_env(login, password)
self.set_credentials(login, password)
self.login_changed.emit()
return verification
19 changes: 17 additions & 2 deletions openpype/modules/shotgrid/tray/shotgrid_tray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from typing import Any

from openpype.modules.shotgrid.tray.credential_dialog import CredentialsDialog
from openpype.modules.shotgrid.lib import credentials
from openpype.modules.shotgrid.lib import settings

from Qt import QtWidgets


Expand Down Expand Up @@ -28,6 +31,18 @@ def tray_menu(self, tray_menu):

tray_menu.addMenu(menu)

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

if not shotgrid_url:
self.show_credential_dialog()
return True

cred = credentials.get_credentials(settings.get_shotgrid_url())
cred_login = cred.get("login")
cred_password = cred.get("password")

if not cred_login and not cred_password:
self.show_credential_dialog()

return True
2 changes: 1 addition & 1 deletion openpype/settings/defaults/system_settings/modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
},
"shotgrid": {
"enabled": true,
"shotgrid_server": ""
"shotgrid_url": ""
},
"timers_manager": {
"enabled": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
{
"type": "text",
"key": "shotgrid_server",
"key": "shotgrid_url",
"label": "Server URL"
}
]
Expand Down

0 comments on commit 8dab062

Please sign in to comment.