示例#1
0
 def _login_to_platform(self):
     self.hostname, self.username, self.password = get_credentials(
         'platform')
     platform_login(self.hostname, self.username, self.password,
                    self.session)
     sessionid = dict_from_cookiejar(self.session.cookies)['sessionid']
     sessionid_cookie = QNetworkCookie(b'sessionid',
                                       sessionid.encode('utf8'))
     self.cookie_jar.setCookiesFromUrl([sessionid_cookie],
                                       QUrl(self.hostname))
 def login(self):
     self.session = Session()
     self.hostname, username, password = get_credentials('engine')
     # try without authentication (if authentication is disabled server
     # side)
     # NOTE: check_is_lockdown() can raise exceptions,
     #       to be caught from outside
     is_lockdown = check_is_lockdown(self.hostname, self.session)
     if not is_lockdown:
         self.is_logged_in = True
         return
     with WaitCursorManager('Logging in...', self.message_bar):
         # it can raise exceptions, caught by self.attempt_login
         engine_login(self.hostname, username, password, self.session)
         # if no exception occurred
         self.is_logged_in = True
         return
     self.is_logged_in = False
示例#3
0
def get_loggedin_downloader(message_bar):
    """
    Attempt to login to the OpenQuake Platform

    :param message_bar: needed to display messages

    :returns: a :class:`svir.utilities.SvDownloader` instance
    """
    hostname, username, password = get_credentials('platform')
    sv_downloader = SvDownloader(hostname)

    try:
        msg = ("Connecting to the OpenQuake Platform...")
        with WaitCursorManager(msg, message_bar):
            sv_downloader.login(username, password)
        return sv_downloader
    except (SvNetworkError, ConnectionError, InvalidSchema, MissingSchema,
            ReadTimeout) as e:
        err_msg = str(e)
        if isinstance(e, InvalidSchema):
            err_msg += ' (you could try prepending http:// or https://)'
        log_msg(err_msg, level='C', message_bar=message_bar)
        return None
示例#4
0
    def __init__(self, iface, file_stem):
        self.iface = iface
        QDialog.__init__(self)

        # Set up the user interface from Designer.
        self.setupUi(self)
        self.message_bar = QgsMessageBar()
        self.message_bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.layout().insertWidget(0, self.message_bar)

        self.message_bar_item = None

        self.button_box = self.buttonBox

        self.hostname, self.username, self.password = get_credentials(
            'platform')

        self.web_view = self.web_view
        self.page = self.web_view.page()
        self.frame = self.page.mainFrame()

        self.session = Session()
        self.network_access_manager = self.page.networkAccessManager()
        self.cookie_jar = QNetworkCookieJar()
        self.network_access_manager.setCookieJar(self.cookie_jar)

        self._setup_context_menu()
        self.frame.javaScriptWindowObjectCleared.connect(self._setup_js)

        self.file_stem = file_stem

        self.layout().setContentsMargins(0, 0, 0, 0)

        self.web_view.loadFinished.connect(self.load_finished)

        self.layer_url = None
    def accept(self):
        self.suppl_info['title'] = self.title_le.text()
        if 'title' not in self.project_definition:
            self.project_definition['title'] = self.suppl_info['title']
        self.suppl_info['abstract'] = self.description_te.toPlainText()
        if 'description' not in self.project_definition:
            self.project_definition['description'] = self.suppl_info[
                'abstract']
        zone_label_field = self.zone_label_field_cbx.currentText()
        self.suppl_info['zone_label_field'] = zone_label_field

        license_name = self.license_cbx.currentText()
        license_idx = self.license_cbx.currentIndex()
        license_url = self.license_cbx.itemData(license_idx)
        license_txt = '%s (%s)' % (license_name, license_url)
        self.suppl_info['license'] = license_txt
        self.suppl_info['irmt_plugin_version'] = IRMT_PLUGIN_VERSION
        self.suppl_info['supplemental_information_version'] = \
            SUPPLEMENTAL_INFORMATION_VERSION
        self.suppl_info['vertices_count'] = self.vertices_count

        self.suppl_info['project_definitions'][self.selected_idx] = \
            self.project_definition
        active_layer_id = self.iface.activeLayer().id()
        write_layer_suppl_info_to_qgs(active_layer_id, self.suppl_info)

        if self.do_update:
            with WaitCursorManager(
                    'Updating project on the OpenQuake Platform',
                    self.iface.messageBar()):
                hostname, username, password = get_credentials('platform')
                session = Session()
                try:
                    platform_login(hostname, username, password, session)
                except SvNetworkError as e:
                    error_msg = (
                        'Unable to login to the platform: ' + e.message)
                    log_msg(error_msg, level='C',
                            message_bar=self.iface.messageBar(),
                            exception=e)
                    return
                if 'platform_layer_id' not in self.suppl_info:
                    error_msg = ('Unable to retrieve the id of'
                                 'the layer on the Platform')
                    log_msg(error_msg, level='C',
                            message_bar=self.iface.messageBar())
                    return
                response = update_platform_project(
                    hostname, session, self.project_definition,
                    self.suppl_info['platform_layer_id'])
                if response.ok:
                    log_msg(tr(response.text), level='S',
                            message_bar=self.iface.messageBar())
                else:
                    error_msg = response.text
                    # example of response text:
                    # "The title 'No incomplete data' was already assigned to
                    # another project definition. Please provide a new unique
                    # one."
                    # NOTE: if the api response message changes, this might
                    # not work properly
                    if 'Please provide a new unique' in response.text:
                        error_msg += (' Please consider using the "Manage'
                                      ' project definitions" functionality'
                                      ' to save the current project definition'
                                      ' as a new one having a unique title.')
                    log_msg(error_msg, level='C',
                            message_bar=self.iface.messageBar())
        else:
            if DEBUG:
                log_msg('xml_file: %s' % self.xml_file)
            # do not upload the selected_project_definition_idx
            self.suppl_info.pop('selected_project_definition_idx', None)
            write_iso_metadata_file(self.xml_file,
                                    self.suppl_info)
            metadata_dialog = UploadDialog(
                self.iface, self.file_stem)
            metadata_dialog.upload_successful.connect(
                lambda layer_url: insert_platform_layer_id(
                    layer_url,
                    active_layer_id,
                    self.suppl_info))
            if metadata_dialog.exec_():
                QDesktopServices.openUrl(QUrl(metadata_dialog.layer_url))
            elif DEBUG:
                log_msg("metadata_dialog cancelled")

        super(UploadSettingsDialog, self).accept()