Skip to content

Commit b2f52d8

Browse files
committed
Add license section to metadata
1 parent 5600395 commit b2f52d8

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

python/core/metadata/qgslayermetadata.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ class QgsLayerMetadata
341341
\see rights()
342342
%End
343343

344+
QStringList licenses() const;
345+
%Docstring
346+
Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
347+
\see setLicenses()
348+
:rtype: list of str
349+
%End
350+
351+
void setLicenses( const QStringList &licenses );
352+
%Docstring
353+
Sets a list of ``licenses`` associated with the resource.
354+
(examples: http://opendefinition.org/licenses/).
355+
\see licenses()
356+
%End
357+
344358
QString encoding() const;
345359
%Docstring
346360
Returns the character encoding of the data in the resource. An empty string will be returned if no encoding is set.

src/core/metadata/qgslayermetadata.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ void QgsLayerMetadata::setRights( const QStringList &rights )
9898
mRights = rights;
9999
}
100100

101+
QStringList QgsLayerMetadata::licenses() const
102+
{
103+
return mLicenses;
104+
}
105+
106+
void QgsLayerMetadata::setLicenses( const QStringList &licenses )
107+
{
108+
mLicenses = licenses;
109+
}
110+
101111
QString QgsLayerMetadata::encoding() const
102112
{
103113
return mEncoding;
@@ -193,6 +203,7 @@ void QgsLayerMetadata::saveToLayer( QgsMapLayer *layer ) const
193203
layer->setCustomProperty( QStringLiteral( "metadata/abstract" ), mAbstract );
194204
layer->setCustomProperty( QStringLiteral( "metadata/fees" ), mFees );
195205
layer->setCustomProperty( QStringLiteral( "metadata/rights" ), mRights );
206+
layer->setCustomProperty( QStringLiteral( "metadata/licenses" ), mLicenses );
196207
layer->setCustomProperty( QStringLiteral( "metadata/encoding" ), mEncoding );
197208
layer->setCustomProperty( QStringLiteral( "metadata/crs" ), mCrs.authid() );
198209
layer->setCustomProperty( QStringLiteral( "metadata/constraints" ), QVariant::fromValue( mConstraints ) );
@@ -211,6 +222,7 @@ void QgsLayerMetadata::readFromLayer( const QgsMapLayer *layer )
211222
mAbstract = layer->customProperty( QStringLiteral( "metadata/abstract" ) ).toString();
212223
mFees = layer->customProperty( QStringLiteral( "metadata/fees" ) ).toString();
213224
mRights = layer->customProperty( QStringLiteral( "metadata/rights" ) ).toStringList();
225+
mLicenses = layer->customProperty( QStringLiteral( "metadata/licenses" ) ).toStringList();
214226
mEncoding = layer->customProperty( QStringLiteral( "metadata/encoding" ) ).toString();
215227
QString crsAuthId = layer->customProperty( QStringLiteral( "metadata/crs" ) ).toString();
216228
mCrs = QgsCoordinateReferenceSystem::fromOgcWmsCrs( crsAuthId );

src/core/metadata/qgslayermetadata.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,19 @@ class CORE_EXPORT QgsLayerMetadata
382382
*/
383383
void setRights( const QStringList &rights );
384384

385+
/**
386+
* Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
387+
* \see setLicenses()
388+
*/
389+
QStringList licenses() const;
390+
391+
/**
392+
* Sets a list of \a licenses associated with the resource.
393+
* (examples: http://opendefinition.org/licenses/).
394+
* \see licenses()
395+
*/
396+
void setLicenses( const QStringList &licenses );
397+
385398
/**
386399
* Returns the character encoding of the data in the resource. An empty string will be returned if no encoding is set.
387400
* \see setEncoding()
@@ -558,6 +571,7 @@ class CORE_EXPORT QgsLayerMetadata
558571
QString mFees;
559572
ConstraintList mConstraints;
560573
QStringList mRights;
574+
QStringList mLicenses;
561575

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

src/core/metadata/qgslayermetadatavalidator.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QLi
5353
results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
5454
}
5555

56-
//result = result && !metadata.license().isEmpty();
56+
if ( metadata.licenses().isEmpty() )
57+
{
58+
result = false;
59+
results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
60+
}
5761

5862
if ( !metadata.crs().isValid() )
5963
{

tests/src/python/test_qgslayermetadata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def testGettersSetters(self):
5656
m.setRights(['right a', 'right b'])
5757
self.assertEqual(m.rights(), ['right a', 'right b'])
5858

59+
m.setLicenses(['l a', 'l b'])
60+
self.assertEqual(m.licenses(), ['l a', 'l b'])
61+
5962
m.setEncoding('encoding')
6063
self.assertEqual(m.encoding(), 'encoding')
6164

@@ -188,6 +191,7 @@ def createTestMetadata(self):
188191
m.setFees('None')
189192
m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')])
190193
m.setRights(['Copyright foo 2017'])
194+
m.setLicenses(['WTFPL'])
191195
m.setKeywords({'GEMET': ['kw1', 'kw2']})
192196
m.setEncoding('utf-8')
193197
m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326'))
@@ -249,6 +253,7 @@ def checkExpectedMetadata(self, m):
249253
self.assertEqual(m.constraints()[0].constraint, 'None')
250254
self.assertEqual(m.constraints()[0].type, 'access')
251255
self.assertEqual(m.rights(), ['Copyright foo 2017'])
256+
self.assertEqual(m.licenses(), ['WTFPL'])
252257
self.assertEqual(m.encoding(), 'utf-8')
253258
self.assertEqual(m.keywords(), {'GEMET': ['kw1', 'kw2']})
254259
self.assertEqual(m.crs().authid(), 'EPSG:4326')
@@ -344,6 +349,12 @@ def testValidateNative(self): # spellok
344349
self.assertFalse(res)
345350
self.assertEqual(list[0].section, 'abstract')
346351

352+
m = self.createTestMetadata()
353+
m.setLicenses([])
354+
res, list = v.validate(m)
355+
self.assertFalse(res)
356+
self.assertEqual(list[0].section, 'license')
357+
347358
m = self.createTestMetadata()
348359
m.setCrs(QgsCoordinateReferenceSystem())
349360
res, list = v.validate(m)

0 commit comments

Comments
 (0)