|
17 | 17 | # limitations under the License. |
18 | 18 | """Datasets JSON-LD schemes.""" |
19 | 19 |
|
20 | | -from marshmallow import EXCLUDE |
| 20 | +from marshmallow import EXCLUDE, pre_dump |
21 | 21 |
|
22 | 22 | from renku.command.schema.agent import PersonSchema |
23 | 23 | from renku.command.schema.annotation import AnnotationSchema |
@@ -69,6 +69,27 @@ class Meta: |
69 | 69 | id = fields.Id() |
70 | 70 | name = fields.String(schema.name) |
71 | 71 |
|
| 72 | + @pre_dump(pass_many=True) |
| 73 | + def removes_ms(self, objs, many, **kwargs): |
| 74 | + """Remove milliseconds from datetimes. |
| 75 | +
|
| 76 | + Note: since DateField uses `strftime` as format, which only supports timezone info without a colon |
| 77 | + e.g. `+0100` instead of `+01:00`, we have to deal with milliseconds manually instead of using a format string. |
| 78 | + """ |
| 79 | + |
| 80 | + def _replace_times(obj): |
| 81 | + obj.unfreeze() |
| 82 | + obj.date_created = obj.date_created.replace(microsecond=0) |
| 83 | + obj.freeze() |
| 84 | + |
| 85 | + if many: |
| 86 | + for obj in objs: |
| 87 | + _replace_times(obj) |
| 88 | + return objs |
| 89 | + |
| 90 | + _replace_times(objs) |
| 91 | + return objs |
| 92 | + |
72 | 93 |
|
73 | 94 | class LanguageSchema(JsonLDSchema): |
74 | 95 | """Language schema.""" |
@@ -134,6 +155,27 @@ class Meta: |
134 | 155 | is_external = fields.Boolean(renku.external, load_default=False) |
135 | 156 | source = fields.String(renku.source, load_default=None) |
136 | 157 |
|
| 158 | + @pre_dump(pass_many=True) |
| 159 | + def removes_ms(self, objs, many, **kwargs): |
| 160 | + """Remove milliseconds from datetimes. |
| 161 | +
|
| 162 | + Note: since DateField uses `strftime` as format, which only supports timezone info without a colon |
| 163 | + e.g. `+0100` instead of `+01:00`, we have to deal with milliseconds manually instead of using a format string. |
| 164 | + """ |
| 165 | + |
| 166 | + def _replace_times(obj): |
| 167 | + obj.date_added = obj.date_added.replace(microsecond=0) |
| 168 | + if obj.date_removed: |
| 169 | + obj.date_removed = obj.date_removed.replace(microsecond=0) |
| 170 | + |
| 171 | + if many: |
| 172 | + for obj in objs: |
| 173 | + _replace_times(obj) |
| 174 | + return objs |
| 175 | + |
| 176 | + _replace_times(objs) |
| 177 | + return objs |
| 178 | + |
137 | 179 |
|
138 | 180 | class DatasetSchema(JsonLDSchema): |
139 | 181 | """Dataset schema.""" |
@@ -168,3 +210,30 @@ class Meta: |
168 | 210 | same_as = Nested(schema.sameAs, UrlSchema, load_default=None) |
169 | 211 | title = fields.String(schema.name) |
170 | 212 | version = fields.String(schema.version, load_default=None) |
| 213 | + |
| 214 | + @pre_dump(pass_many=True) |
| 215 | + def removes_ms(self, objs, many, **kwargs): |
| 216 | + """Remove milliseconds from datetimes. |
| 217 | +
|
| 218 | + Note: since DateField uses `strftime` as format, which only supports timezone info without a colon |
| 219 | + e.g. `+0100` instead of `+01:00`, we have to deal with milliseconds manually instead of using a format string. |
| 220 | + """ |
| 221 | + |
| 222 | + def _replace_times(obj): |
| 223 | + obj.unfreeze() |
| 224 | + if obj.date_created: |
| 225 | + obj.date_created = obj.date_created.replace(microsecond=0) |
| 226 | + if obj.date_removed: |
| 227 | + obj.date_removed = obj.date_removed.replace(microsecond=0) |
| 228 | + if obj.date_published: |
| 229 | + obj.date_published = obj.date_published.replace(microsecond=0) |
| 230 | + obj.date_modified = obj.date_modified.replace(microsecond=0) |
| 231 | + obj.freeze() |
| 232 | + |
| 233 | + if many: |
| 234 | + for obj in objs: |
| 235 | + _replace_times(obj) |
| 236 | + return objs |
| 237 | + |
| 238 | + _replace_times(objs) |
| 239 | + return objs |
0 commit comments