Skip to content

Commit

Permalink
Set the main owner for the attribution credits in layer properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jan 18, 2021
1 parent 66c1ce9 commit 75f9bdd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
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])

# Metadata panel
metadata = layer.metadata()
metadata.setRights([data[0][0]])
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;

0 comments on commit 75f9bdd

Please sign in to comment.