Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add license section to metadata
  • Loading branch information
nyalldawson committed May 3, 2017
1 parent 5600395 commit b2f52d8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
14 changes: 14 additions & 0 deletions python/core/metadata/qgslayermetadata.sip
Expand Up @@ -341,6 +341,20 @@ class QgsLayerMetadata
\see rights()
%End

QStringList licenses() const;
%Docstring
Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
\see setLicenses()
:rtype: list of str
%End

void setLicenses( const QStringList &licenses );
%Docstring
Sets a list of ``licenses`` associated with the resource.
(examples: http://opendefinition.org/licenses/).
\see licenses()
%End

QString encoding() const;
%Docstring
Returns the character encoding of the data in the resource. An empty string will be returned if no encoding is set.
Expand Down
12 changes: 12 additions & 0 deletions src/core/metadata/qgslayermetadata.cpp
Expand Up @@ -98,6 +98,16 @@ void QgsLayerMetadata::setRights( const QStringList &rights )
mRights = rights;
}

QStringList QgsLayerMetadata::licenses() const
{
return mLicenses;
}

void QgsLayerMetadata::setLicenses( const QStringList &licenses )
{
mLicenses = licenses;
}

QString QgsLayerMetadata::encoding() const
{
return mEncoding;
Expand Down Expand Up @@ -193,6 +203,7 @@ void QgsLayerMetadata::saveToLayer( QgsMapLayer *layer ) const
layer->setCustomProperty( QStringLiteral( "metadata/abstract" ), mAbstract );
layer->setCustomProperty( QStringLiteral( "metadata/fees" ), mFees );
layer->setCustomProperty( QStringLiteral( "metadata/rights" ), mRights );
layer->setCustomProperty( QStringLiteral( "metadata/licenses" ), mLicenses );
layer->setCustomProperty( QStringLiteral( "metadata/encoding" ), mEncoding );
layer->setCustomProperty( QStringLiteral( "metadata/crs" ), mCrs.authid() );
layer->setCustomProperty( QStringLiteral( "metadata/constraints" ), QVariant::fromValue( mConstraints ) );
Expand All @@ -211,6 +222,7 @@ void QgsLayerMetadata::readFromLayer( const QgsMapLayer *layer )
mAbstract = layer->customProperty( QStringLiteral( "metadata/abstract" ) ).toString();
mFees = layer->customProperty( QStringLiteral( "metadata/fees" ) ).toString();
mRights = layer->customProperty( QStringLiteral( "metadata/rights" ) ).toStringList();
mLicenses = layer->customProperty( QStringLiteral( "metadata/licenses" ) ).toStringList();
mEncoding = layer->customProperty( QStringLiteral( "metadata/encoding" ) ).toString();
QString crsAuthId = layer->customProperty( QStringLiteral( "metadata/crs" ) ).toString();
mCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( crsAuthId );
Expand Down
14 changes: 14 additions & 0 deletions src/core/metadata/qgslayermetadata.h
Expand Up @@ -382,6 +382,19 @@ class CORE_EXPORT QgsLayerMetadata
*/
void setRights( const QStringList &rights );

/**
* Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
* \see setLicenses()
*/
QStringList licenses() const;

/**
* Sets a list of \a licenses associated with the resource.
* (examples: http://opendefinition.org/licenses/).
* \see licenses()
*/
void setLicenses( const QStringList &licenses );

/**
* Returns the character encoding of the data in the resource. An empty string will be returned if no encoding is set.
* \see setEncoding()
Expand Down Expand Up @@ -558,6 +571,7 @@ class CORE_EXPORT QgsLayerMetadata
QString mFees;
ConstraintList mConstraints;
QStringList mRights;
QStringList mLicenses;

// IMPORTANT - look up before adding anything here!!

Expand Down
6 changes: 5 additions & 1 deletion src/core/metadata/qgslayermetadatavalidator.cpp
Expand Up @@ -53,7 +53,11 @@ bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QLi
results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
}

//result = result && !metadata.license().isEmpty();
if ( metadata.licenses().isEmpty() )
{
result = false;
results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
}

if ( !metadata.crs().isValid() )
{
Expand Down
11 changes: 11 additions & 0 deletions tests/src/python/test_qgslayermetadata.py
Expand Up @@ -56,6 +56,9 @@ def testGettersSetters(self):
m.setRights(['right a', 'right b'])
self.assertEqual(m.rights(), ['right a', 'right b'])

m.setLicenses(['l a', 'l b'])
self.assertEqual(m.licenses(), ['l a', 'l b'])

m.setEncoding('encoding')
self.assertEqual(m.encoding(), 'encoding')

Expand Down Expand Up @@ -188,6 +191,7 @@ def createTestMetadata(self):
m.setFees('None')
m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
m.setRights(['Copyright foo 2017'])
m.setLicenses(['WTFPL'])
m.setKeywords({'GEMET': ['kw1', 'kw2']})
m.setEncoding('utf-8')
m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))
Expand Down Expand Up @@ -249,6 +253,7 @@ def checkExpectedMetadata(self, m):
self.assertEqual(m.constraints()[0].constraint, 'None')
self.assertEqual(m.constraints()[0].type, 'access')
self.assertEqual(m.rights(), ['Copyright foo 2017'])
self.assertEqual(m.licenses(), ['WTFPL'])
self.assertEqual(m.encoding(), 'utf-8')
self.assertEqual(m.keywords(), {'GEMET': ['kw1', 'kw2']})
self.assertEqual(m.crs().authid(), 'EPSG:4326')
Expand Down Expand Up @@ -344,6 +349,12 @@ def testValidateNative(self): # spellok
self.assertFalse(res)
self.assertEqual(list[0].section, 'abstract')

m = self.createTestMetadata()
m.setLicenses([])
res, list = v.validate(m)
self.assertFalse(res)
self.assertEqual(list[0].section, 'license')

m = self.createTestMetadata()
m.setCrs(QgsCoordinateReferenceSystem())
res, list = v.validate(m)
Expand Down

0 comments on commit b2f52d8

Please sign in to comment.