Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 4a40120

Browse files
author
Tobia Di Pisa
authored
More test cases (geosolutions-it#42)
* Adding more test cases * Removing some tabs * Removing some tabs
1 parent d313843 commit 4a40120

14 files changed

+129814
-189
lines changed

ckanext/dcatapit/commands/dcatapit.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DCATAPITCommands(CkanCommand):
3636
eu_themes -> http://publications.europa.eu/mdr/resource/authority/data-theme/skos/data-theme-skos.rdf
3737
places -> http://publications.europa.eu/mdr/resource/authority/place/skos/places-skos.rdf
3838
frequencies -> http://publications.europa.eu/mdr/resource/authority/frequency/skos/frequencies-skos.rdf
39-
filetype -> http://publications.europa.eu/mdr/resource/authority/file-type/skos/filetypes-skos.rdf
39+
filetype -> http://publications.europa.eu/mdr/resource/authority/file-type/skos/filetypes-skos.rdf
4040
PATH_TO_INI_FILE is the path to the Ckan configuration file
4141
'''
4242

@@ -50,7 +50,7 @@ class DCATAPITCommands(CkanCommand):
5050
'de': 'de',
5151
'en': 'en_GB',
5252
'fr': 'fr',
53-
'es': 'es'
53+
'es': 'es'
5454
}
5555

5656
_ckan_language_theme_mapping = {
@@ -90,11 +90,11 @@ def command(self):
9090
return
9191

9292
def initdb(self):
93-
from ckanext.dcatapit.model import setup as db_setup
93+
from ckanext.dcatapit.model import setup as db_setup
9494
db_setup()
9595

9696
def load(self):
97-
##
97+
##
9898
# Checking command given options
9999
##
100100
url = self.options.url
@@ -113,7 +113,7 @@ def load(self):
113113
return
114114

115115
if vocab_name not in self._controlled_vocabularies_allowed:
116-
print "ERROR: Incorrect vocabulary name, only one of these values are allowed: {0}".format(self._controlled_vocabularies_allowed)
116+
print "ERROR: Incorrect vocabulary name, only one of these values are allowed: {0}".format(self._controlled_vocabularies_allowed)
117117
print self.usage
118118
return
119119

@@ -170,10 +170,10 @@ def load(self):
170170

171171
print u' Label {0}: {1}'.format(lang, label)
172172
pref_labels.append({
173-
'name': identifier,
174-
'lang': lang,
175-
'localized_text': label
176-
})
173+
'name': identifier,
174+
'lang': lang,
175+
'localized_text': label
176+
})
177177

178178
##
179179
# Creating the Tag Vocabulary using the given name
@@ -182,38 +182,38 @@ def load(self):
182182

183183
user = toolkit.get_action('get_site_user')({'ignore_auth': True}, {})
184184
context = {'user': user['name'], 'ignore_auth': True}
185-
185+
186186
print "Using site user '{0}'".format(user['name'])
187187

188188
try:
189-
data = {'id': vocab_name}
190-
toolkit.get_action('vocabulary_show')(context, data)
189+
data = {'id': vocab_name}
190+
toolkit.get_action('vocabulary_show')(context, data)
191191

192-
log.info("Vocabulary {0} already exists, skipping...".format(vocab_name))
192+
log.info("Vocabulary {0} already exists, skipping...".format(vocab_name))
193193
except toolkit.ObjectNotFound:
194-
log.info("Creating vocabulary '{0}'".format(vocab_name))
194+
log.info("Creating vocabulary '{0}'".format(vocab_name))
195195

196-
data = {'name': vocab_name}
197-
vocab = toolkit.get_action('vocabulary_create')(context, data)
196+
data = {'name': vocab_name}
197+
vocab = toolkit.get_action('vocabulary_create')(context, data)
198198

199-
for tag in concepts:
200-
log.info("Adding tag {0} to vocabulary '{1}'".format(tag, vocab_name))
199+
for tag in concepts:
200+
log.info("Adding tag {0} to vocabulary '{1}'".format(tag, vocab_name))
201201

202-
data = {'name': tag, 'vocabulary_id': vocab['id']}
203-
toolkit.get_action('tag_create')(context, data)
202+
data = {'name': tag, 'vocabulary_id': vocab['id']}
203+
toolkit.get_action('tag_create')(context, data)
204204

205205
##
206206
# Persisting Multilag Tags or updating existing
207207
##
208208
log.info('Creating the corresponding multilang tags for vocab: {0} ...'.format(vocab_name))
209209

210210
for pref_label in pref_labels:
211-
if pref_label['lang'] in self._locales_ckan_mapping:
212-
tag_name = pref_label['name']
213-
tag_lang = self._locales_ckan_mapping[pref_label['lang']]
214-
tag_localized_name = pref_label['localized_text']
211+
if pref_label['lang'] in self._locales_ckan_mapping:
212+
tag_name = pref_label['name']
213+
tag_lang = self._locales_ckan_mapping[pref_label['lang']]
214+
tag_localized_name = pref_label['localized_text']
215215

216-
interfaces.persist_tag_multilang(tag_name, tag_lang, tag_localized_name, vocab_name)
216+
interfaces.persist_tag_multilang(tag_name, tag_lang, tag_localized_name, vocab_name)
217217

218218
print 'Vocabulary successfully loaded ({0})'.format(vocab_name)
219219

ckanext/dcatapit/controllers/api.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ckan.logic as logic
77

88
from ckan.controllers.api import ApiController
9-
from ckan.common import _, c, request, response
9+
from ckan.common import c, request
1010

1111
log = logging.getLogger(__file__)
1212

@@ -15,27 +15,28 @@
1515

1616
class DCATAPITApiController(ApiController):
1717

18-
def vocabulary_autocomplete(self):
19-
q = request.str_params.get('incomplete', '')
20-
q = unicode(urllib.unquote(q), 'utf-8')
18+
def vocabulary_autocomplete(self):
19+
q = request.str_params.get('incomplete', '')
20+
q = unicode(urllib.unquote(q), 'utf-8')
2121

22-
vocab = request.params.get('vocabulary_id', None)
23-
vocab = str(vocab)
22+
vocab = request.params.get('vocabulary_id', None)
2423

25-
log.debug('Looking for Vocab %r', vocab)
24+
vocab = str(vocab)
2625

27-
limit = request.params.get('limit', 10)
28-
29-
tag_names = []
30-
if q:
31-
context = {'model': model, 'session': model.Session, 'user': c.user, 'auth_user_obj': c.userobj}
32-
data_dict = {'q': q, 'limit': limit, 'vocabulary_id': vocab}
33-
tag_names = get_action('tag_autocomplete')(context, data_dict)
26+
log.debug('Looking for Vocab %r', vocab)
3427

35-
resultSet = {
36-
'ResultSet': {
37-
'Result': [{'Name': tag} for tag in tag_names]
38-
}
39-
}
28+
limit = request.params.get('limit', 10)
4029

41-
return super(DCATAPITApiController, self)._finish_ok(resultSet)
30+
tag_names = []
31+
if q:
32+
context = {'model': model, 'session': model.Session, 'user': c.user, 'auth_user_obj': c.userobj}
33+
data_dict = {'q': q, 'limit': limit, 'vocabulary_id': vocab}
34+
tag_names = get_action('tag_autocomplete')(context, data_dict)
35+
36+
resultSet = {
37+
'ResultSet': {
38+
'Result': [{'Name': tag} for tag in tag_names]
39+
}
40+
}
41+
42+
return super(DCATAPITApiController, self)._finish_ok(resultSet)

ckanext/dcatapit/helpers.py

+89-89
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
log = logging.getLogger(__file__)
1212

1313
dateformats = [
14-
"%d-%m-%Y",
15-
"%Y-%m-%d",
16-
"%d-%m-%y",
17-
"%Y-%m-%d %H:%M:%S",
18-
"%d-%m-%Y %H:%M:%S",
19-
"%Y-%m-%dT%H:%M:%S"
14+
"%d-%m-%Y",
15+
"%Y-%m-%d",
16+
"%d-%m-%y",
17+
"%Y-%m-%d %H:%M:%S",
18+
"%d-%m-%Y %H:%M:%S",
19+
"%Y-%m-%dT%H:%M:%S"
2020
]
2121

2222
def get_dcatapit_package_schema():
@@ -36,110 +36,110 @@ def get_dcatapit_resource_schema():
3636
return dcatapit_schema.get_custom_resource_schema()
3737

3838
def get_vocabulary_items(vocabulary_name, keys=None):
39-
try:
40-
tag_list = toolkit.get_action('tag_list')
41-
items = tag_list(data_dict={'vocabulary_id': vocabulary_name})
42-
43-
tag_list = []
44-
for item in items:
45-
if keys:
46-
for key in keys:
47-
if key == item:
48-
localized_tag_name = interfaces.get_localized_tag_name(item)
49-
tag_list.append(localized_tag_name)
50-
else:
51-
localized_tag_name = interfaces.get_localized_tag_name(item)
52-
tag_list.append({'text': localized_tag_name, 'value': item})
53-
54-
return tag_list
55-
except toolkit.ObjectNotFound:
56-
return []
39+
try:
40+
tag_list = toolkit.get_action('tag_list')
41+
items = tag_list(data_dict={'vocabulary_id': vocabulary_name})
42+
43+
tag_list = []
44+
for item in items:
45+
if keys:
46+
for key in keys:
47+
if key == item:
48+
localized_tag_name = interfaces.get_localized_tag_name(item)
49+
tag_list.append(localized_tag_name)
50+
else:
51+
localized_tag_name = interfaces.get_localized_tag_name(item)
52+
tag_list.append({'text': localized_tag_name, 'value': item})
53+
54+
return tag_list
55+
except toolkit.ObjectNotFound:
56+
return []
5757

5858
def get_localized_field_value(field=None, pkg_id=None, field_type='extra'):
59-
log.debug('Retrieving localized package field...')
60-
return interfaces.get_localized_field_value(field, pkg_id, field_type)
59+
log.debug('Retrieving localized package field...')
60+
return interfaces.get_localized_field_value(field, pkg_id, field_type)
6161

6262
def list_to_string(_list, _format=None):
63-
if _list:
64-
_string = ''
63+
if _list:
64+
_string = ''
6565

66-
first_item = True
67-
for item in _list:
68-
if first_item:
69-
first_item = False
70-
element = item
66+
first_item = True
67+
for item in _list:
68+
if first_item:
69+
first_item = False
70+
element = item
7171

72-
if _format:
73-
element = format(element, _format)
72+
if _format:
73+
element = format(element, _format)
7474

75-
_string = _string + element
76-
else:
77-
element = item
75+
_string = _string + element
76+
else:
77+
element = item
7878

79-
if _format:
80-
element = format(element, _format)
79+
if _format:
80+
element = format(element, _format)
8181

82-
_string = _string + ', ' + item
82+
_string = _string + ', ' + item
8383

84-
return _string
84+
return _string
8585

8686
def couple_to_string(field_couples, pkg_dict):
87-
if field_couples and pkg_dict:
88-
_string = ''
89-
for couple in field_couples:
90-
if couple['name'] in pkg_dict:
91-
field_value = pkg_dict[couple['name']]
92-
if field_value and couple['label']:
93-
_string = _string + ' ' + couple['label'] + ': ' + field_value
87+
if field_couples and pkg_dict:
88+
_string = ''
89+
for couple in field_couples:
90+
if couple['name'] in pkg_dict:
91+
field_value = pkg_dict[couple['name']]
92+
if field_value and couple['label']:
93+
_string = _string + ' ' + couple['label'] + ': ' + field_value
9494

95-
return _string
96-
return None
95+
return _string
96+
return None
9797

9898
def couple_to_html(field_couples, pkg_dict):
99-
if field_couples and pkg_dict:
100-
html_elements = []
101-
for couple in field_couples:
102-
couple_name = couple.get('name', None)
99+
if field_couples and pkg_dict:
100+
html_elements = []
101+
for couple in field_couples:
102+
couple_name = couple.get('name', None)
103103

104-
if couple_name in pkg_dict:
105-
field_value = pkg_dict[couple_name]
104+
if couple_name in pkg_dict:
105+
field_value = pkg_dict[couple_name]
106106

107-
couple_format = couple.get('format', None)
108-
if couple_format:
109-
couple_type = couple.get('type', None)
110-
field_value = format(field_value, couple_format, couple_type)
107+
couple_format = couple.get('format', None)
108+
if couple_format:
109+
couple_type = couple.get('type', None)
110+
field_value = format(field_value, couple_format, couple_type)
111111

112-
couple_label = couple.get('label', None)
113-
if field_value and couple_label:
114-
html_elements.append(literal(('<span style="font-weight:bold">%s: </span><span>%s</span>') % (couple_label, field_value)))
112+
couple_label = couple.get('label', None)
113+
if field_value and couple_label:
114+
html_elements.append(literal(('<span style="font-weight:bold">%s: </span><span>%s</span>') % (couple_label, field_value)))
115115

116-
return html_elements if len(html_elements) > 0 else []
117-
return []
116+
return html_elements if len(html_elements) > 0 else []
117+
return []
118118

119119
def format(value, _format='%d-%m-%Y', _type=None):
120-
# #################################################
121-
# TODO: manage here other formats if needed
122-
# (ie. for type text, other date formats etc)
123-
# #################################################
124-
if _format and _type:
125-
if _type == 'date':
126-
date = None
127-
for dateformat in dateformats:
128-
date = validate_dateformat(value, dateformat)
129-
130-
if date and isinstance(date, datetime.date):
131-
date = date.strftime(_format)
132-
return date
133-
if _type == 'text':
134-
return value
135-
136-
return value
120+
# #################################################
121+
# TODO: manage here other formats if needed
122+
# (ie. for type text, other date formats etc)
123+
# #################################################
124+
if _format and _type:
125+
if _type == 'date':
126+
date = None
127+
for dateformat in dateformats:
128+
date = validate_dateformat(value, dateformat)
129+
130+
if date and isinstance(date, datetime.date):
131+
date = date.strftime(_format)
132+
return date
133+
if _type == 'text':
134+
return value
135+
136+
return value
137137

138138
def validate_dateformat(date_string, date_format):
139-
try:
140-
date = datetime.datetime.strptime(date_string, date_format)
141-
return date
142-
except ValueError:
143-
log.debug(u'Incorrect date format {0} for date string {1}'.format(date_format, date_string))
144-
return None
139+
try:
140+
date = datetime.datetime.strptime(date_string, date_format)
141+
return date
142+
except ValueError:
143+
log.debug(u'Incorrect date format {0} for date string {1}'.format(date_format, date_string))
144+
return None
145145

0 commit comments

Comments
 (0)