Skip to content

Commit cab975b

Browse files
committed
#349 Partial fixes
* New UrlListField introduced for lsit of urls * The UrlField is now only taking single URL value Signed-off-by: Chin Yeung Li <[email protected]>
1 parent acc6a1a commit cab975b

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

docs/CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2018-xx-xx
2+
3+
Release x.x.x
4+
5+
* New UrlListField introduced for list of urls
6+
* The UrlField is now only taking single URL value
7+
8+
19
2018-10-23
210

311
Release 3.2.2

src/attributecode/model.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,20 +344,19 @@ def __eq__(self, other):
344344
if sval == oval:
345345
return True
346346

347-
348-
class UrlField(ListField):
347+
class UrlListField(ListField):
349348
"""
350349
A URL field. The validated value is a list of URLs.
351350
"""
352351
def _validate(self, *args, **kwargs):
353352
"""
354353
Check that URLs are valid. Return a list of errors.
355354
"""
356-
errors = super(UrlField, self)._validate(*args, ** kwargs)
357-
for url in self.value:
355+
errors = super(UrlListField, self)._validate(*args, ** kwargs)
356+
name = self.name
357+
val = self.value
358+
for url in val:
358359
if not self.is_valid_url(url):
359-
name = self.name
360-
val = self.value
361360
msg = (u'Field %(name)s: Invalid URL: %(val)s' % locals())
362361
errors.append(Error(WARNING, msg))
363362
return errors
@@ -372,6 +371,32 @@ def is_valid_url(url):
372371
return valid
373372

374373

374+
class UrlField(StringField):
375+
"""
376+
A URL field. The validated value is a URL.
377+
"""
378+
def _validate(self, *args, **kwargs):
379+
"""
380+
Check that URL is valid. Return a list of errors.
381+
"""
382+
errors = super(UrlField, self)._validate(*args, ** kwargs)
383+
name = self.name
384+
val = self.value
385+
if not self.is_valid_url(val):
386+
msg = (u'Field %(name)s: Invalid URL: %(val)s' % locals())
387+
errors.append(Error(WARNING, msg))
388+
return errors
389+
390+
@staticmethod
391+
def is_valid_url(url):
392+
"""
393+
Return True if a URL is valid.
394+
"""
395+
scheme, netloc, _path, _p, _q, _frg = urlparse(url)
396+
valid = scheme in ('http', 'https', 'ftp') and netloc
397+
return valid
398+
399+
375400
class PathField(ListField):
376401
"""
377402
A field pointing to one or more paths relative to the ABOUT file location.
@@ -692,7 +717,7 @@ def create_fields(self):
692717
('license_key', ListField()),
693718
('license_name', ListField()),
694719
('license_file', FileTextField()),
695-
('license_url', UrlField()),
720+
('license_url', UrlListField()),
696721
('copyright', StringField()),
697722
('notice_file', FileTextField()),
698723
('notice_url', UrlField()),

tests/test_model.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,15 @@ def test_PathField_contains_dict_after_validate(self):
193193
]
194194
self.check_validate(field_class, value, expected, expected_errors)
195195

196+
"""
197+
UrlField no longer become a list.
198+
If a list is wanted, use UrlListField instead.
196199
def test_UrlField_contains_list_after_validate(self):
197200
value = 'http://some.com/url'
198201
field_class = model.UrlField
199202
expected = [value]
200203
self.check_validate(field_class, value, expected, expected_errors=[])
201-
204+
"""
202205
def test_SingleLineField_has_errors_if_multiline(self):
203206
value = '''line1
204207
line2'''
@@ -714,8 +717,7 @@ def test_About_dumps(self):
714717
AboutCode is a tool
715718
to process ABOUT files.
716719
An ABOUT file is a file.
717-
homepage_url:
718-
- http://dejacode.org
720+
homepage_url: http://dejacode.org
719721
license_expression: apache-2.0
720722
licenses:
721723
- file: apache-2.0.LICENSE
@@ -1002,7 +1004,7 @@ def test_load_dict_handles_field_validation_correctly(self):
10021004
u'author': [u'Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez'],
10031005
u'copyright': u'Copyright (c) 2013-2014 nexB Inc.',
10041006
u'description': u'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
1005-
u'homepage_url': [u'http://dejacode.org'],
1007+
u'homepage_url': u'http://dejacode.org',
10061008
u'license_expression': u'apache-2.0',
10071009
u'name': u'AboutCode',
10081010
u'owner': [u'nexB Inc.'],
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
about_resource: .
22
name: test
33
license_expression: mit or apache-2.0
4-
license_url: |
5-
https://enterprise.dejacode.com/urn/?urn=urn:dje:license:mit
6-
https://enterprise.dejacode.com/urn/?urn=urn:dje:license:apache-2.0
4+
licenses:
5+
- key: mit
6+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:mit
7+
- key: apache-2.0
8+
url: https://enterprise.dejacode.com/urn/?urn=urn:dje:license:apache-2.0

0 commit comments

Comments
 (0)