11
11
log = logging .getLogger (__file__ )
12
12
13
13
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"
20
20
]
21
21
22
22
def get_dcatapit_package_schema ():
@@ -36,110 +36,110 @@ def get_dcatapit_resource_schema():
36
36
return dcatapit_schema .get_custom_resource_schema ()
37
37
38
38
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 []
57
57
58
58
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 )
61
61
62
62
def list_to_string (_list , _format = None ):
63
- if _list :
64
- _string = ''
63
+ if _list :
64
+ _string = ''
65
65
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
71
71
72
- if _format :
73
- element = format (element , _format )
72
+ if _format :
73
+ element = format (element , _format )
74
74
75
- _string = _string + element
76
- else :
77
- element = item
75
+ _string = _string + element
76
+ else :
77
+ element = item
78
78
79
- if _format :
80
- element = format (element , _format )
79
+ if _format :
80
+ element = format (element , _format )
81
81
82
- _string = _string + ', ' + item
82
+ _string = _string + ', ' + item
83
83
84
- return _string
84
+ return _string
85
85
86
86
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
94
94
95
- return _string
96
- return None
95
+ return _string
96
+ return None
97
97
98
98
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 )
103
103
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 ]
106
106
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 )
111
111
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 )))
115
115
116
- return html_elements if len (html_elements ) > 0 else []
117
- return []
116
+ return html_elements if len (html_elements ) > 0 else []
117
+ return []
118
118
119
119
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
137
137
138
138
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
145
145
0 commit comments