8
8
import json
9
9
import logging
10
10
11
- from ckanext .dcat .profiles import EuropeanDCATAP2Profile
11
+ from ckanext .dcat .profiles import EuropeanDCATAP3Profile
12
12
from ckan .plugins import toolkit
13
13
from ckan import model
14
14
import dateutil .parser as dateparser
21
21
22
22
VCARD = Namespace ("http://www.w3.org/2006/vcard/ns#" )
23
23
24
-
25
- def _convert_extras_to_declared_schema_fields (dataset_dict : Dict ) -> Dict :
26
- """
27
- Compares the extras dictionary with the declared schema.
28
- Updates the declared schema fields with the values that match from the extras.
29
- Remove the extras that are present on the declared schema.
30
- :param dataset_dict:
31
- :return: dataset_dict - Updated dataset_dict
32
- """
33
- # Use the correct dataset type, Defaults to 'dataset'
34
- dataset_type = dataset_dict .get ('type' , 'dataset' )
35
- # Gets the full Schema definition of the correct dataset type
36
- context = {'model' : model , 'session' : model .Session }
37
- data_dict = {'type' : dataset_type }
38
- full_schema_dict = toolkit .get_action ('scheming_dataset_schema_show' )(context , data_dict )
39
-
40
- dataset_fields = {x .get ('field_name' ): x .get ('preset' ) for x in full_schema_dict .get ('dataset_fields' , [])}
41
-
42
- # Populate the declared schema fields, if they are present in the extras
43
- for extra_dict in dataset_dict .get ('extras' , []):
44
- field_key = extra_dict .get ('key' )
45
- field_value = extra_dict .get ('value' )
46
- if field_key in dataset_fields :
47
- preset = dataset_fields [field_key ]
48
- if preset == 'multiple_text' and field_value :
49
- try :
50
- dataset_dict [field_key ] = json .loads (field_value )
51
- except JSONDecodeError :
52
- dataset_dict [field_key ] = field_value
53
- elif preset == 'date' and field_value :
54
- dataset_dict [field_key ] = convert_datetime_string (field_value )
55
- else :
56
- dataset_dict [field_key ] = field_value
57
-
58
- # Remove the extras that have been populated into the declared schema fields
59
- dataset_dict ['extras' ] = [d for d in dataset_dict ['extras' ] if d .get ('key' ) not in dataset_fields ]
60
-
61
- return dataset_dict
62
-
63
-
64
24
def validate_tags (values_list : List [Dict ]) -> List :
65
25
"""
66
26
Validates tags strings to contain allowed characters, replaces others with spaces
@@ -85,31 +45,18 @@ def validate_tags(values_list: List[Dict]) -> List:
85
45
return tags
86
46
87
47
88
- def convert_datetime_string (date_value : str ) -> datetime :
89
- """
90
- Converts datestrings (e.g. '2023-10-06T10:12:55.614000+00:00') to datetime class instance
91
- """
92
- try :
93
- date_value = dateparser .parse (date_value , yearfirst = True )
94
- if date_value .tzinfo is not None :
95
- date_value = date_value .astimezone (timezone .utc )
96
- except ParserError :
97
- log .error (f'A date field string value { date_value } can not be parsed to a date' )
98
- return date_value
99
-
100
-
101
- class FAIRDataPointDCATAPProfile (EuropeanDCATAP2Profile ):
48
+ class FAIRDataPointDCATAPProfile (EuropeanDCATAP3Profile ):
102
49
"""
103
50
An RDF profile for FAIR data points
104
51
"""
105
52
106
53
def parse_dataset (self , dataset_dict : Dict , dataset_ref : URIRef ) -> Dict :
107
54
super (FAIRDataPointDCATAPProfile , self ).parse_dataset (dataset_dict , dataset_ref )
108
- dataset_dict = self ._parse_contact_point (dataset_dict , dataset_ref )
109
55
56
+ #dataset_dict = self._parse_contact_point(dataset_dict, dataset_ref)
110
57
dataset_dict = self ._parse_creator (dataset_dict , dataset_ref )
111
58
112
- dataset_dict = _convert_extras_to_declared_schema_fields (dataset_dict )
59
+ ## dataset_dict = _convert_extras_to_declared_schema_fields(dataset_dict)
113
60
114
61
dataset_dict ['tags' ] = validate_tags (dataset_dict ['tags' ])
115
62
@@ -123,10 +70,10 @@ def _contact_point_details(self, subject, predicate) -> List:
123
70
124
71
for agent in self .g .objects (subject , predicate ):
125
72
contact = {
126
- 'contact_uri ' : (str (agent ) if isinstance (agent , URIRef )
73
+ 'uri ' : (str (agent ) if isinstance (agent , URIRef )
127
74
else self ._get_vcard_property_value (agent , VCARD .hasUID )),
128
- 'contact_name ' : self ._get_vcard_property_value (agent , VCARD .hasFN , VCARD .fn ),
129
- 'contact_email ' : self ._without_mailto (self ._get_vcard_property_value (agent , VCARD .hasEmail ))}
75
+ 'name ' : self ._get_vcard_property_value (agent , VCARD .hasFN , VCARD .fn ),
76
+ 'email ' : self ._without_mailto (self ._get_vcard_property_value (agent , VCARD .hasEmail ))}
130
77
131
78
contact_list .append (contact )
132
79
@@ -156,16 +103,16 @@ def _parse_creator(self, dataset_dict: Dict, dataset_ref: URIRef) -> Dict:
156
103
creator_name = graph .value (creator_ref , FOAF .name )
157
104
158
105
if creator_identifier :
159
- creator ['creator_identifier ' ] = str (creator_identifier )
106
+ creator ['identifier ' ] = str (creator_identifier )
160
107
if creator_name :
161
- creator ['creator_name ' ] = str (creator_name )
108
+ creator ['name ' ] = str (creator_name )
162
109
else :
163
110
# If the creator is a URI, use it as the identifier
164
111
if isinstance (creator_ref , URIRef ):
165
- creator ['creator_identifier ' ] = str (creator_ref )
166
- creator ['creator_name ' ] = str (creator_ref )
112
+ creator ['identifier ' ] = str (creator_ref )
113
+ creator ['name ' ] = str (creator_ref )
167
114
else :
168
- creator ['creator_name ' ] = str (creator_ref )
115
+ creator ['name ' ] = str (creator_ref )
169
116
170
117
creators .append (creator )
171
118
0 commit comments