Skip to content

sg_login_dialog

SgLoginDialog

Bases: QDialog

A QDialog that allows the person to set a Shotgrid Username.

It also allows them to test the username against the API.

Source code in client/ayon_shotgrid/tray/sg_login_dialog.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class SgLoginDialog(QtWidgets.QDialog):
    """A QDialog that allows the person to set a Shotgrid Username.

    It also allows them to test the username against the API.
    """

    dialog_closed = QtCore.Signal()

    def __init__(self, addon, parent=None):
        super(SgLoginDialog, self).__init__(parent)
        self.addon = addon
        self.login_type = self.addon.get_client_login_type()

        self.setWindowTitle("AYON - Shotgrid Login")
        icon = QtGui.QIcon(resources.get_ayon_icon_filepath())
        self.setWindowIcon(icon)

        self.setWindowFlags(
            QtCore.Qt.WindowCloseButtonHint
            | QtCore.Qt.WindowMinimizeButtonHint
        )

        self.setStyleSheet(style.load_stylesheet())
        self.setContentsMargins(2, 2, 2, 2)

        self.setup_ui()

    def closeEvent(self, event):
        """Clear any message when closing the dialog."""
        self.sg_connection_message.setText("")
        self.dialog_closed.emit()
        super(SgLoginDialog, self).closeEvent(event)

    def setup_ui(self):
        server_url = self.addon.get_sg_url()

        if not server_url:
            server_url = "No Shotgrid Server set in AYON Settings."

        sg_server_url_label = QtWidgets.QLabel(
            "Please provide the credentials to log in into the "
            f"Shotgrid Server:\n{server_url}"
        )

        dialog_layout = QtWidgets.QVBoxLayout()
        dialog_layout.addWidget(sg_server_url_label)

        sg_username, sg_password = credentials.get_local_login()

        self.sg_username_input = QtWidgets.QLineEdit()

        if sg_username:
            self.sg_username_input.setText(sg_username)
        else:
            self.sg_username_input.setPlaceholderText("jane.doe@mycompany.com")
        self.sg_password_input = QtWidgets.QLineEdit()
        self.sg_password_input.setEchoMode(QtWidgets.QLineEdit.Password)


        if sg_password:
            self.sg_password_input.setText(sg_password)
        else:
            self.sg_password_input.setPlaceholderText("password1234")

        dialog_layout.addWidget(QtWidgets.QLabel("Shotgrid Username:"))
        dialog_layout.addWidget(self.sg_username_input)

        if self.login_type == "tray_pass":
            dialog_layout.addWidget(QtWidgets.QLabel("Shotgrid Password:"))
            dialog_layout.addWidget(self.sg_password_input)

        self.sg_check_login_button = QtWidgets.QPushButton(
            "Login into Shotgrid...")
        self.sg_check_login_button.clicked.connect(self.check_sg_credentials)
        self.sg_connection_message = QtWidgets.QLabel("")

        dialog_layout.addWidget(self.sg_check_login_button)
        dialog_layout.addWidget(self.sg_connection_message)

        self.setLayout(dialog_layout)

    def set_local_login(self):
        """Change Username label, save in local registry and set env var."""
        sg_username = self.sg_username_input.text()
        sg_password = self.sg_password_input.text()

        if self.login_type == "tray_pass":
            if sg_username and sg_password:
                credentials.save_local_login(sg_username, sg_password)
                os.environ["AYON_SG_USERNAME"] = sg_username
            else:
                credentials.clear_local_login()
                os.environ["AYON_SG_USERNAME"] = ""

        elif self.login_type == "tray_api_key":
            if sg_username:
                credentials.save_local_login(sg_username, None)
                os.environ["AYON_SG_USERNAME"] = sg_username
            else:
                credentials.clear_local_login()
                os.environ["AYON_SG_USERNAME"] = ""

    def check_sg_credentials(self):
        """Check if the provided username can login via the API."""
        sg_username = self.sg_username_input.text()
        sg_password = self.sg_password_input.text()

        kwargs = {
            "shotgrid_url": self.addon.get_sg_url(),
        }

        if self.login_type == "tray_pass":
            if not sg_username or not sg_password:
                self.sg_connection_message.setText(
                    "Please provide a valid username and password."
                )
                return
            kwargs.update({
                "username": sg_username,
                "password": sg_password
            })

        elif self.login_type == "tray_api_key":
            if not sg_username:
                self.sg_connection_message.setText(
                    "Please provide a valid username."
                )
                return
            kwargs.update({
                "username": sg_username,
                "api_key": self.addon.get_sg_api_key(),
                "script_name": self.addon.get_sg_script_name(),
            })

        login_result, login_message = credentials.check_user_permissions(
            **kwargs)

        self.set_local_login()

        if login_result:
            self.close()
        else:
            self.sg_connection_message.setText(login_message)

check_sg_credentials()

Check if the provided username can login via the API.

Source code in client/ayon_shotgrid/tray/sg_login_dialog.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def check_sg_credentials(self):
    """Check if the provided username can login via the API."""
    sg_username = self.sg_username_input.text()
    sg_password = self.sg_password_input.text()

    kwargs = {
        "shotgrid_url": self.addon.get_sg_url(),
    }

    if self.login_type == "tray_pass":
        if not sg_username or not sg_password:
            self.sg_connection_message.setText(
                "Please provide a valid username and password."
            )
            return
        kwargs.update({
            "username": sg_username,
            "password": sg_password
        })

    elif self.login_type == "tray_api_key":
        if not sg_username:
            self.sg_connection_message.setText(
                "Please provide a valid username."
            )
            return
        kwargs.update({
            "username": sg_username,
            "api_key": self.addon.get_sg_api_key(),
            "script_name": self.addon.get_sg_script_name(),
        })

    login_result, login_message = credentials.check_user_permissions(
        **kwargs)

    self.set_local_login()

    if login_result:
        self.close()
    else:
        self.sg_connection_message.setText(login_message)

closeEvent(event)

Clear any message when closing the dialog.

Source code in client/ayon_shotgrid/tray/sg_login_dialog.py
37
38
39
40
41
def closeEvent(self, event):
    """Clear any message when closing the dialog."""
    self.sg_connection_message.setText("")
    self.dialog_closed.emit()
    super(SgLoginDialog, self).closeEvent(event)

set_local_login()

Change Username label, save in local registry and set env var.

Source code in client/ayon_shotgrid/tray/sg_login_dialog.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def set_local_login(self):
    """Change Username label, save in local registry and set env var."""
    sg_username = self.sg_username_input.text()
    sg_password = self.sg_password_input.text()

    if self.login_type == "tray_pass":
        if sg_username and sg_password:
            credentials.save_local_login(sg_username, sg_password)
            os.environ["AYON_SG_USERNAME"] = sg_username
        else:
            credentials.clear_local_login()
            os.environ["AYON_SG_USERNAME"] = ""

    elif self.login_type == "tray_api_key":
        if sg_username:
            credentials.save_local_login(sg_username, None)
            os.environ["AYON_SG_USERNAME"] = sg_username
        else:
            credentials.clear_local_login()
            os.environ["AYON_SG_USERNAME"] = ""