Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the main owner for the attribution credits in layer properties #55

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions pg_metadata/dock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from qgis.core import (
NULL,
QgsAbstractDatabaseProviderConnection,
QgsApplication,
QgsProviderConnectionException,
QgsProviderRegistry,
Expand Down Expand Up @@ -269,6 +270,7 @@ def layer_changed(self, layer):

self.set_html_content(body=data[0][0])
self.save_button.setEnabled(True)
self.set_layer_metadata(layer, connection, uri.schema(), uri.table())
self.current_datasource_uri = uri
self.current_connection = connection

Expand Down Expand Up @@ -314,3 +316,39 @@ def set_html_content(self, title=None, body=None):
def default_html_content(self):
self.set_html_content(
'PgMetadata', tr('You should click on a layer in the legend which is stored in PostgreSQL.'))

@staticmethod
def set_layer_metadata(
layer: QgsVectorLayer,
connection: QgsAbstractDatabaseProviderConnection,
schema: str,
table: str,
):
""" Set the main owner contact for the attribution. """
sql = (
"SELECT name FROM pgmetadata.v_contact "
f"WHERE table_name = '{table}' "
f"AND schema_name = '{schema}' "
"ORDER BY CASE "
"WHEN contact_role_code = 'OW' then 1 "
"WHEN contact_role_code = 'DI' then 2 "
"WHEN contact_role_code = 'CU' then 3 "
"ELSE 10 "
"END ASC "
"LIMIT 1;"
)

data = connection.executeSql(sql)
if not data:
return

if data[0] == NULL or data[0][0] == NULL:
return

# QGIS Server panel
layer.setAttribution(data[0][0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add title and abstract to the QGIS Server corresponding properties ? Related to Lizmap plugin too

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which code can we use to add title and abstract to the QGIS server corresponding properties?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mwekezi I think you should raise your topic in an issue, but not in a pull request. This one was a WIP, work in progress.
You need to use some Python to make it automatic for now. (semi-automatic to synchronize database metadata into the layer properties)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I tried running this code in the python console of QGIS but it didn't work. It mentioned an indentation error. Could you help with how I could successfully run the code above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
"Indentation error" is the most common Python error. Before digging in this project, I suggest you to read about programming with Python, follow a tutorial about Python, you will be able to understand why you couldn't run the code.


# Metadata panel
metadata = layer.metadata()
metadata.setRights([data[0][0]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we always override existing metadata, or only if a new checkbox is checked in options ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you successfully run the code above?

layer.setMetadata(metadata)
1 change: 1 addition & 0 deletions pg_metadata/install/sql/pgmetadata/30_VIEW.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CREATE VIEW pgmetadata.v_contact AS
c.organisation_name,
c.organisation_unit,
((((glossary.dict -> 'contact.contact_role'::text) -> dc.contact_role) -> 'label'::text) ->> glossary.locale) AS contact_role,
dc.contact_role AS contact_role_code,
c.email
FROM glossary,
((pgmetadata.dataset_contact dc
Expand Down
29 changes: 29 additions & 0 deletions pg_metadata/install/sql/upgrade/upgrade_to_0.4.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,33 @@ COMMENT ON FUNCTION pgmetadata.get_datasets_as_dcat_xml(_locale text) IS

-- End DCAT

-- Start contact role

DROP VIEW IF EXISTS pgmetadata.v_contact;
CREATE VIEW pgmetadata.v_contact AS
WITH glossary AS (
SELECT COALESCE(current_setting('pgmetadata.locale'::text, true), 'en'::text) AS locale,
v_glossary.dict
FROM pgmetadata.v_glossary
)
SELECT d.table_name,
d.schema_name,
c.name,
c.organisation_name,
c.organisation_unit,
((((glossary.dict -> 'contact.contact_role'::text) -> dc.contact_role) -> 'label'::text) ->> glossary.locale) AS contact_role,
dc.contact_role AS contact_role_code,
c.email
FROM glossary,
((pgmetadata.dataset_contact dc
JOIN pgmetadata.dataset d ON ((d.id = dc.fk_id_dataset)))
JOIN pgmetadata.contact c ON ((dc.fk_id_contact = c.id)))
WHERE true
ORDER BY dc.id;

COMMENT ON VIEW pgmetadata.v_contact IS
'Formatted version of contact data, with all the codes replaced by corresponding labels taken from pgmetadata.glossary. Used in the function in charge of building the HTML metadata content. The localized version of labels and descriptions are taken considering the session setting ''pgmetadata.locale''. For example with: SET SESSION "pgmetadata.locale" = ''fr''; ';

-- End contact role

COMMIT;