Switch to: V11V10V9V8V7V6V5

Project Deployment Help

When the work on the Valentina project is done it still contains data used during development, such as the datasource connection settings.

To prepare for deployment it may be necessary to change the datasource – for example, to remove the developer's database login or replace it with the user's credentials.

It can be done manually by the developer or the user, just establish a connection with the desired credentials and select it in the Edit Datasource dialog:

A tooltip may help to distinguish similar connections with different users.

Starting with version 10.5.6 the Valentina Scripts and Valentina Forms can help to automate this task in just 4 steps:

  • Change the datasource in the script – define a function that changes the datasource programmatically.
  • Design the database credentials form – create a form which gets info from the user and saves it to the datasource.
  • Add startup check – add the startup script which checks if the datasource needs to be updated.
  • Saving settings – save the login information entered by the user and use it to update the datasource on the project updates.

Change Datasource In Script

To clear the datasource from the developer credentials create a new script, e.g. clear_password.

The datasource object can be obtained in the following way:

ds = project.getItemByName( PROJECT_ITEM_TYPE.DATASOURCE, 'ds_name' );

Each datasource has the property data which contains definition of the database connection in the XML format, for example:

<?xml version="1.0" encoding="utf-8"?>
 <properties>
     <opts value=""/>
     <pass value="sa"/>
     <path value="cars_db"/>
     <port value="15432"/>
     <ssl value="0"/>
     <type value="server"/>
     <url value="localhost"/>
     <user value="sa"/>
</properties>

To get the definition for an existing datasource just print it to the console like that:

console.log( ds.data );

As a result, here's how the project datasource can be cleared from the developer connection data:

ds = project.getItemByName( PROJECT_ITEM_TYPE.DATASOURCE, 'cars_db' );
 
ds.data = 
	`<?xml version="1.0" encoding="utf-8"?>
	<properties>
		<opts value=""/>
		<pass value=""/>
		<path value="cars_db"/>
		<port value="15432"/>
		<ssl value="0"/>
		<type value="server"/>
		<url value="localhost"/>
		<user value=""/>
	</properties>`;
If the user credentials are known you can specify it right in this script.

This datasource XML structure will be used in different scripts, so there is a reason to create utils script with function which updates the datasource:

function update_datasource( user, password )
{
    ds = project.getItemByName( PROJECT_ITEM_TYPE.DATASOURCE, 'cars_db' );
 
    ds.data = 
        `<?xml version="1.0" encoding="utf-8"?>
        <properties>
	    <opts value=""/>
	    <pass value="${ password }"/>
            <path value="cars_db"/>
            <port value="15432"/>
            <ssl value="0"/>
            <type value="server"/>
            <user value="${ user }"/>
        </properties>`;
}

Parameters are inserted to the text as embedded expressions of the JavaScript template strings.

Modify the clear_password, now it is just two lines:

import '/utils'
 
update_datasource( '', '' );

Database Credentials Form

A form can be used to update the datasource with data entered by the user.

Create a login_form with the following layout:

textChanged signals of the User and Password fields are used to update the OK button enabled state:

this.pbOk.enabled = this.leUser.text.length > 0 &&
                    this.lePass.text.length > 0;

clicked signal of the OK button updates the datasource with values from the User and Password input fields:

import '/utils'
 
update_datasource( this.leUser.text, this.lePass.text );
 
this.accept();
You can add as many parameters as necessary to give the users an ability to specify, e.g. the hostname, port, SSL options.

Startup Check

The login_form can be presented automatically if the datasource wasn't adjusted yet using the startup script.

Create a new script, startup, with the following code:

import '/login_form' as dlg
 
ds = project.getItemByName( PROJECT_ITEM_TYPE.DATASOURCE, 'cars_db' );
 
if( ds.data.indexOf( '<user value=""/>' ) >= 0 )
    dlg.exec();

Using the contextual menu set it to be the startup script of the project.

Note, it is still possible to change the credentials executing the login_form manually.

Saving Settings

The user credentials can be saved not only to the project but also to the local user data. So the datasource will be updated using this data, without the need to enter credentials each time the project is updated.

Modify the clicked signal of the OK button in the login_form to save credentials to QSettings storage:

import '/utils'
 
update_datasource( this.leUser.text, this.lePass.text );
 
s = QSettings( 'Car Catalogs', 'Car Catalog' );
 
s.setValue( 'db_user', this.leUser.text );
s.setValue( 'db_pass', this.lePass.text );
 
this.accept();

Now define the code to assign saved settings to the controls for the created signal of this form:

s = QSettings( 'Car Catalogs', 'Car Catalog' );
 
user = s.value( 'db_user' );
pass = s.value( 'db_pass' );
 
if( user ) this.leUser.text = user.toString();
if( pass ) this.lePass.text = pass.toString();

If the credentials were previously saved – update the datasource with them in the startup script:

import '/utils'
import '/login_form' as dlg
 
ds = project.getItemByName( PROJECT_ITEM_TYPE.DATASOURCE, 'cars_db' );
 
if( ds.data.indexOf( '<user value=""/>' ) >= 0 ) // The datasource wasn't adjusted
{
    s = QSettings( 'Car Catalogs', 'Car Catalog' );
 
    user = s.value( 'db_user' );
    pass = s.value( 'db_pass' );
 
    if( user && pass ) // Use settings
        update_datasource( user, pass )
    else
        dlg.exec();
}