udata 14.0.0__py3-none-any.whl → 14.4.1.dev7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of udata might be problematic. Click here for more details.

Files changed (130) hide show
  1. udata/api_fields.py +35 -4
  2. udata/app.py +18 -20
  3. udata/auth/__init__.py +29 -6
  4. udata/auth/forms.py +2 -2
  5. udata/auth/views.py +6 -3
  6. udata/commands/serve.py +3 -11
  7. udata/commands/tests/test_fixtures.py +9 -9
  8. udata/core/access_type/api.py +1 -1
  9. udata/core/access_type/constants.py +12 -8
  10. udata/core/activity/api.py +5 -6
  11. udata/core/badges/tests/test_commands.py +6 -6
  12. udata/core/csv.py +5 -0
  13. udata/core/dataservices/models.py +1 -1
  14. udata/core/dataservices/tasks.py +7 -0
  15. udata/core/dataset/api.py +2 -0
  16. udata/core/dataset/models.py +2 -2
  17. udata/core/dataset/permissions.py +31 -0
  18. udata/core/dataset/tasks.py +17 -5
  19. udata/core/discussions/models.py +1 -0
  20. udata/core/organization/api.py +8 -5
  21. udata/core/organization/mails.py +1 -1
  22. udata/core/organization/models.py +9 -1
  23. udata/core/organization/notifications.py +84 -0
  24. udata/core/organization/permissions.py +1 -1
  25. udata/core/organization/tasks.py +3 -0
  26. udata/core/pages/tests/test_api.py +32 -0
  27. udata/core/post/api.py +24 -69
  28. udata/core/post/models.py +84 -16
  29. udata/core/post/tests/test_api.py +24 -1
  30. udata/core/reports/api.py +18 -0
  31. udata/core/reports/models.py +42 -2
  32. udata/core/reuse/models.py +1 -1
  33. udata/core/reuse/tasks.py +7 -0
  34. udata/core/spatial/forms.py +2 -2
  35. udata/core/user/models.py +5 -1
  36. udata/features/notifications/api.py +7 -18
  37. udata/features/notifications/models.py +56 -0
  38. udata/features/notifications/tasks.py +25 -0
  39. udata/flask_mongoengine/engine.py +0 -4
  40. udata/frontend/markdown.py +2 -1
  41. udata/harvest/actions.py +21 -1
  42. udata/harvest/api.py +25 -8
  43. udata/harvest/backends/base.py +27 -1
  44. udata/harvest/backends/ckan/harvesters.py +11 -2
  45. udata/harvest/commands.py +33 -0
  46. udata/harvest/filters.py +17 -6
  47. udata/harvest/models.py +16 -0
  48. udata/harvest/permissions.py +27 -0
  49. udata/harvest/tests/ckan/test_ckan_backend.py +33 -0
  50. udata/harvest/tests/test_actions.py +58 -5
  51. udata/harvest/tests/test_api.py +276 -122
  52. udata/harvest/tests/test_base_backend.py +86 -1
  53. udata/harvest/tests/test_dcat_backend.py +57 -10
  54. udata/harvest/tests/test_filters.py +6 -0
  55. udata/i18n.py +1 -4
  56. udata/mail.py +5 -1
  57. udata/migrations/2025-10-31-create-membership-request-notifications.py +55 -0
  58. udata/migrations/2025-12-04-add-uuid-to-discussion-messages.py +28 -0
  59. udata/mongo/slug_fields.py +1 -1
  60. udata/rdf.py +45 -6
  61. udata/routing.py +2 -2
  62. udata/settings.py +7 -0
  63. udata/tasks.py +1 -0
  64. udata/templates/mail/message.html +5 -31
  65. udata/tests/__init__.py +27 -2
  66. udata/tests/api/__init__.py +108 -21
  67. udata/tests/api/test_activities_api.py +36 -0
  68. udata/tests/api/test_auth_api.py +121 -95
  69. udata/tests/api/test_base_api.py +7 -4
  70. udata/tests/api/test_datasets_api.py +44 -19
  71. udata/tests/api/test_organizations_api.py +192 -197
  72. udata/tests/api/test_reports_api.py +157 -0
  73. udata/tests/api/test_reuses_api.py +147 -147
  74. udata/tests/api/test_security_api.py +12 -12
  75. udata/tests/api/test_swagger.py +4 -4
  76. udata/tests/api/test_tags_api.py +8 -8
  77. udata/tests/api/test_user_api.py +1 -1
  78. udata/tests/apiv2/test_swagger.py +4 -4
  79. udata/tests/cli/test_cli_base.py +8 -9
  80. udata/tests/dataset/test_dataset_commands.py +4 -4
  81. udata/tests/dataset/test_dataset_model.py +66 -26
  82. udata/tests/dataset/test_dataset_rdf.py +99 -5
  83. udata/tests/frontend/test_auth.py +24 -1
  84. udata/tests/frontend/test_csv.py +0 -3
  85. udata/tests/helpers.py +25 -27
  86. udata/tests/organization/test_notifications.py +67 -2
  87. udata/tests/plugin.py +6 -261
  88. udata/tests/site/test_site_csv_exports.py +22 -10
  89. udata/tests/test_activity.py +9 -9
  90. udata/tests/test_dcat_commands.py +2 -2
  91. udata/tests/test_discussions.py +5 -5
  92. udata/tests/test_migrations.py +21 -21
  93. udata/tests/test_notifications.py +15 -57
  94. udata/tests/test_notifications_task.py +43 -0
  95. udata/tests/test_owned.py +81 -1
  96. udata/tests/test_storages.py +25 -19
  97. udata/tests/test_topics.py +77 -61
  98. udata/tests/test_uris.py +33 -0
  99. udata/tests/workers/test_jobs_commands.py +23 -23
  100. udata/translations/ar/LC_MESSAGES/udata.mo +0 -0
  101. udata/translations/ar/LC_MESSAGES/udata.po +187 -108
  102. udata/translations/de/LC_MESSAGES/udata.mo +0 -0
  103. udata/translations/de/LC_MESSAGES/udata.po +187 -108
  104. udata/translations/es/LC_MESSAGES/udata.mo +0 -0
  105. udata/translations/es/LC_MESSAGES/udata.po +187 -108
  106. udata/translations/fr/LC_MESSAGES/udata.mo +0 -0
  107. udata/translations/fr/LC_MESSAGES/udata.po +188 -109
  108. udata/translations/it/LC_MESSAGES/udata.mo +0 -0
  109. udata/translations/it/LC_MESSAGES/udata.po +187 -108
  110. udata/translations/pt/LC_MESSAGES/udata.mo +0 -0
  111. udata/translations/pt/LC_MESSAGES/udata.po +187 -108
  112. udata/translations/sr/LC_MESSAGES/udata.mo +0 -0
  113. udata/translations/sr/LC_MESSAGES/udata.po +187 -108
  114. udata/translations/udata.pot +215 -106
  115. udata/uris.py +0 -2
  116. udata-14.4.1.dev7.dist-info/METADATA +109 -0
  117. {udata-14.0.0.dist-info → udata-14.4.1.dev7.dist-info}/RECORD +121 -123
  118. udata/core/post/forms.py +0 -30
  119. udata/flask_mongoengine/json.py +0 -38
  120. udata/templates/mail/base.html +0 -105
  121. udata/templates/mail/base.txt +0 -6
  122. udata/templates/mail/button.html +0 -3
  123. udata/templates/mail/layouts/1-column.html +0 -19
  124. udata/templates/mail/layouts/2-columns.html +0 -20
  125. udata/templates/mail/layouts/center-panel.html +0 -16
  126. udata-14.0.0.dist-info/METADATA +0 -132
  127. {udata-14.0.0.dist-info → udata-14.4.1.dev7.dist-info}/WHEEL +0 -0
  128. {udata-14.0.0.dist-info → udata-14.4.1.dev7.dist-info}/entry_points.txt +0 -0
  129. {udata-14.0.0.dist-info → udata-14.4.1.dev7.dist-info}/licenses/LICENSE +0 -0
  130. {udata-14.0.0.dist-info → udata-14.4.1.dev7.dist-info}/top_level.txt +0 -0
@@ -2,7 +2,8 @@ from datetime import datetime
2
2
 
3
3
  from bson import DBRef
4
4
  from flask import url_for
5
- from mongoengine import DO_NOTHING, NULLIFY, signals
5
+ from flask_restx import inputs
6
+ from mongoengine import DO_NOTHING, NULLIFY, Q, signals
6
7
 
7
8
  from udata.api_fields import field, generate_fields
8
9
  from udata.core.user.api_fields import user_ref_fields
@@ -12,7 +13,32 @@ from udata.mongo import db
12
13
  from .constants import REPORT_REASONS_CHOICES, REPORTABLE_MODELS
13
14
 
14
15
 
15
- @generate_fields()
16
+ class ReportQuerySet(db.BaseQuerySet):
17
+ def unhandled(self):
18
+ return self.filter(dismissed_at=None, subject_deleted_at=None)
19
+
20
+ def handled(self):
21
+ return self.filter(Q(dismissed_at__ne=None) | Q(subject_deleted_at__ne=None))
22
+
23
+
24
+ def filter_by_handled(base_query, filter_value):
25
+ if filter_value is True:
26
+ return base_query.handled()
27
+ elif filter_value is False:
28
+ return base_query.unhandled()
29
+ else:
30
+ return base_query
31
+
32
+
33
+ @generate_fields(
34
+ standalone_filters=[
35
+ {
36
+ "key": "handled",
37
+ "query": filter_by_handled,
38
+ "type": inputs.boolean,
39
+ },
40
+ ],
41
+ )
16
42
  class Report(db.Document):
17
43
  by = field(
18
44
  db.ReferenceField(User, reverse_delete_rule=NULLIFY),
@@ -44,8 +70,22 @@ class Report(db.Document):
44
70
  reported_at = field(
45
71
  db.DateTimeField(default=datetime.utcnow, required=True),
46
72
  readonly=True,
73
+ sortable=True,
47
74
  )
48
75
 
76
+ dismissed_at = field(
77
+ db.DateTimeField(),
78
+ )
79
+ dismissed_by = field(
80
+ db.ReferenceField(User, reverse_delete_rule=NULLIFY),
81
+ nested_fields=user_ref_fields,
82
+ allow_null=True,
83
+ )
84
+
85
+ meta = {
86
+ "queryset_class": ReportQuerySet,
87
+ }
88
+
49
89
  @field(description="Link to the API endpoint for this report")
50
90
  def self_api_url(self):
51
91
  return url_for("api.report", report=self, _external=True)
@@ -199,7 +199,7 @@ class Reuse(db.Datetimed, Auditable, WithMetrics, ReuseBadgeMixin, Linkable, Own
199
199
  cls.before_save.send(document)
200
200
 
201
201
  def self_web_url(self, **kwargs):
202
- return cdata_url(f"/reuses/{self._link_id(**kwargs)}/", **kwargs)
202
+ return cdata_url(f"/reuses/{self._link_id(**kwargs)}", **kwargs)
203
203
 
204
204
  def self_api_url(self, **kwargs):
205
205
  return url_for(
udata/core/reuse/tasks.py CHANGED
@@ -1,4 +1,5 @@
1
1
  from udata.core import storages
2
+ from udata.core.pages.models import Page
2
3
  from udata.core.topic.models import TopicElement
3
4
  from udata.models import Activity, Discussion, Follow, Transfer
4
5
  from udata.tasks import get_logger, job, task
@@ -23,6 +24,12 @@ def purge_reuses(self) -> None:
23
24
  Transfer.objects(subject=reuse).delete()
24
25
  # Remove reuses references in Topics
25
26
  TopicElement.objects(element=reuse).update(element=None)
27
+ # Remove reuses in pages (mongoengine doesn't support updating a field in a generic embed)
28
+ Page._get_collection().update_many(
29
+ {"blocs.reuses": reuse.id},
30
+ {"$pull": {"blocs.$[b].reuses": reuse.id}},
31
+ array_filters=[{"b.reuses": reuse.id}],
32
+ )
26
33
  # Remove reuse's logo in all sizes
27
34
  if reuse.image.filename is not None:
28
35
  storage = storages.images
@@ -64,8 +64,8 @@ class GeomField(Field):
64
64
  self.data = geojson.GeoJSON.to_instance(value)
65
65
  except Exception:
66
66
  self.data = None
67
- log.exception("Unable to parse GeoJSON")
68
- raise ValueError(self.gettext("Not a valid GeoJSON"))
67
+ log.warning(f"Unable to parse GeoJSON: {value}")
68
+ raise validators.ValidationError(self.gettext("Not a valid GeoJSON"))
69
69
 
70
70
  def pre_validate(self, form):
71
71
  if self.data:
udata/core/user/models.py CHANGED
@@ -142,7 +142,7 @@ class User(WithMetrics, UserMixin, Linkable, db.Document):
142
142
  return self.has_role("admin")
143
143
 
144
144
  def self_web_url(self, **kwargs):
145
- return cdata_url(f"/users/{self._link_id(**kwargs)}/", **kwargs)
145
+ return cdata_url(f"/users/{self._link_id(**kwargs)}", **kwargs)
146
146
 
147
147
  def self_api_url(self, **kwargs):
148
148
  return url_for(
@@ -297,6 +297,10 @@ class User(WithMetrics, UserMixin, Linkable, db.Document):
297
297
  discussion.save()
298
298
  Follow.objects(follower=self).delete()
299
299
  Follow.objects(following=self).delete()
300
+ # Remove related notifications
301
+ from udata.features.notifications.models import Notification
302
+
303
+ Notification.objects.with_user_in_details(self).delete()
300
304
 
301
305
  from udata.models import ContactPoint
302
306
 
@@ -1,30 +1,19 @@
1
- from udata.api import API, api, fields
1
+ from udata.api import API, api
2
2
  from udata.auth import current_user
3
3
 
4
- from .actions import get_notifications
4
+ from .models import Notification
5
5
 
6
6
  notifs = api.namespace("notifications", "Notifications API")
7
7
 
8
- notifications_fields = api.model(
9
- "Notification",
10
- {
11
- "type": fields.String(description="The notification type", readonly=True),
12
- "created_on": fields.ISODateTime(
13
- description="The notification creation datetime", readonly=True
14
- ),
15
- "details": fields.Raw(
16
- description="Key-Value details depending on notification type", readonly=True
17
- ),
18
- },
19
- )
20
-
21
8
 
22
9
  @notifs.route("/", endpoint="notifications")
23
10
  class NotificationsAPI(API):
24
11
  @api.secure
25
- @api.doc("get_notifications")
26
- @api.marshal_list_with(notifications_fields)
12
+ @api.doc("list_notifications")
13
+ @api.expect(Notification.__index_parser__)
14
+ @api.marshal_with(Notification.__page_fields__)
27
15
  def get(self):
28
16
  """List all current user pending notifications"""
29
17
  user = current_user._get_current_object()
30
- return get_notifications(user)
18
+ notifications = Notification.objects(user=user)
19
+ return Notification.apply_pagination(Notification.apply_sort_filters(notifications))
@@ -0,0 +1,56 @@
1
+ from flask_restx.inputs import boolean
2
+ from mongoengine import NULLIFY
3
+
4
+ from udata.api_fields import field, generate_fields
5
+ from udata.core.organization.notifications import MembershipRequestNotificationDetails
6
+ from udata.core.user.api_fields import user_ref_fields
7
+ from udata.core.user.models import User
8
+ from udata.models import db
9
+ from udata.mongo.datetime_fields import Datetimed
10
+ from udata.mongo.queryset import UDataQuerySet
11
+
12
+
13
+ class NotificationQuerySet(UDataQuerySet):
14
+ def with_organization_in_details(self, organization):
15
+ """This function must be updated to handle new details cases"""
16
+ return self(details__request_organization=organization)
17
+
18
+ def with_user_in_details(self, user):
19
+ """This function must be updated to handle new details cases"""
20
+ return self(details__request_user=user)
21
+
22
+
23
+ def is_handled(base_query, filter_value):
24
+ if filter_value is None:
25
+ return base_query
26
+ if filter_value is True:
27
+ return base_query.filter(handled_at__ne=None)
28
+ return base_query.filter(handled_at=None)
29
+
30
+
31
+ @generate_fields()
32
+ class Notification(Datetimed, db.Document):
33
+ meta = {
34
+ "ordering": ["-created_at"],
35
+ "queryset_class": NotificationQuerySet,
36
+ }
37
+
38
+ id = field(db.AutoUUIDField(primary_key=True))
39
+ handled_at = field(
40
+ db.DateTimeField(),
41
+ sortable=True,
42
+ auditable=False,
43
+ filterable={"key": "handled", "query": is_handled, "type": boolean},
44
+ )
45
+ user = field(
46
+ db.ReferenceField(User, reverse_delete_rule=NULLIFY),
47
+ nested_fields=user_ref_fields,
48
+ readonly=True,
49
+ allow_null=True,
50
+ auditable=False,
51
+ filterable={},
52
+ )
53
+ details = field(
54
+ db.GenericEmbeddedDocumentField(choices=(MembershipRequestNotificationDetails,)),
55
+ generic=True,
56
+ )
@@ -0,0 +1,25 @@
1
+ import logging
2
+ from datetime import datetime, timedelta
3
+
4
+ from flask import current_app
5
+
6
+ from udata.features.notifications.models import Notification
7
+ from udata.tasks import job
8
+
9
+ log = logging.getLogger(__name__)
10
+
11
+
12
+ @job("delete-expired-notifications")
13
+ def delete_expired_notifications(self):
14
+ # Delete expired notifications
15
+ handled_at = datetime.utcnow() - timedelta(
16
+ days=current_app.config["DAYS_AFTER_NOTIFICATION_EXPIRED"]
17
+ )
18
+ notifications_to_delete = Notification.objects(
19
+ handled_at__lte=handled_at,
20
+ )
21
+ count = notifications_to_delete.count()
22
+ for notification in notifications_to_delete:
23
+ notification.delete()
24
+
25
+ log.info(f"Deleted {count} expired notifications")
@@ -7,7 +7,6 @@ from mongoengine.errors import DoesNotExist
7
7
  from mongoengine.queryset import QuerySet
8
8
 
9
9
  from .connection import create_connections
10
- from .json import override_json_encoder
11
10
  from .pagination import ListFieldPagination, Pagination
12
11
  from .wtf import WtfBaseField
13
12
 
@@ -108,9 +107,6 @@ class MongoEngine(object):
108
107
 
109
108
  app.extensions = getattr(app, "extensions", {})
110
109
 
111
- # Make documents JSON serializable
112
- override_json_encoder(app)
113
-
114
110
  if "mongoengine" not in app.extensions:
115
111
  app.extensions["mongoengine"] = {}
116
112
 
@@ -7,8 +7,9 @@ import html2text
7
7
  import mistune
8
8
  from bleach.css_sanitizer import CSSSanitizer
9
9
  from bleach.linkifier import LinkifyFilter
10
- from flask import Markup, current_app, request
10
+ from flask import current_app, request
11
11
  from jinja2.filters import do_striptags, do_truncate
12
+ from markupsafe import Markup
12
13
  from werkzeug.local import LocalProxy
13
14
 
14
15
  from udata.i18n import _
udata/harvest/actions.py CHANGED
@@ -254,7 +254,7 @@ def schedule(
254
254
  source.modify(
255
255
  periodic_task=PeriodicTask.objects.create(
256
256
  task="harvest",
257
- name="Harvest {0}".format(source.name),
257
+ name=f"Harvest {source.name} ({source.id})",
258
258
  description="Periodic Harvesting",
259
259
  enabled=True,
260
260
  args=[str(source.id)],
@@ -317,3 +317,23 @@ def attach(domain, filename):
317
317
  count += 1
318
318
 
319
319
  return AttachResult(count, errors)
320
+
321
+
322
+ def detach(dataset: Dataset):
323
+ """Detach a dataset from its harvest source
324
+
325
+ The dataset will be cleaned from harvested information
326
+ and will no longer be updated or archived by harvesting.
327
+ """
328
+ dataset.harvest = None
329
+ for resource in dataset.resources:
330
+ resource.harvest = None
331
+ dataset.save()
332
+
333
+
334
+ def detach_all_from_source(source: HarvestSource):
335
+ """Detach all datasets linked to a harvest source"""
336
+ datasets = Dataset.objects.filter(harvest__source_id=str(source.id))
337
+ for dataset in datasets:
338
+ detach(dataset)
339
+ return len(datasets)
udata/harvest/api.py CHANGED
@@ -6,7 +6,6 @@ from udata.api import API, api, fields
6
6
  from udata.auth import admin_permission
7
7
  from udata.core.dataservices.models import Dataservice
8
8
  from udata.core.dataset.api_fields import dataset_fields, dataset_ref_fields
9
- from udata.core.dataset.permissions import OwnablePermission
10
9
  from udata.core.organization.api_fields import org_ref_fields
11
10
  from udata.core.organization.permissions import EditOrganizationPermission
12
11
  from udata.core.user.api_fields import user_ref_fields
@@ -55,6 +54,7 @@ item_fields = api.model(
55
54
  "HarvestItem",
56
55
  {
57
56
  "remote_id": fields.String(description="The item remote ID to process", required=True),
57
+ "remote_url": fields.String(description="The item remote url (if available)"),
58
58
  "dataset": fields.Nested(
59
59
  dataset_ref_fields, description="The processed dataset", allow_null=True
60
60
  ),
@@ -115,6 +115,18 @@ validation_fields = api.model(
115
115
  },
116
116
  )
117
117
 
118
+ source_permissions_fields = api.model(
119
+ "HarvestSourcePermissions",
120
+ {
121
+ "edit": fields.Permission(),
122
+ "delete": fields.Permission(),
123
+ "run": fields.Permission(),
124
+ "preview": fields.Permission(),
125
+ "validate": fields.Permission(),
126
+ "schedule": fields.Permission(),
127
+ },
128
+ )
129
+
118
130
  source_fields = api.model(
119
131
  "HarvestSource",
120
132
  {
@@ -153,6 +165,7 @@ source_fields = api.model(
153
165
  "schedule": fields.String(
154
166
  description="The source schedule (interval or cron expression)", readonly=True
155
167
  ),
168
+ "permissions": fields.Nested(source_permissions_fields, readonly=True),
156
169
  },
157
170
  )
158
171
 
@@ -313,7 +326,7 @@ class SourceAPI(API):
313
326
  @api.marshal_with(source_fields)
314
327
  def put(self, source: HarvestSource):
315
328
  """Update a harvest source"""
316
- OwnablePermission(source).test()
329
+ source.permissions["edit"].test()
317
330
  form = api.validate(HarvestSourceForm, source)
318
331
  source = actions.update_source(source, form.data)
319
332
  return source
@@ -322,18 +335,19 @@ class SourceAPI(API):
322
335
  @api.doc("delete_harvest_source")
323
336
  @api.marshal_with(source_fields)
324
337
  def delete(self, source: HarvestSource):
325
- OwnablePermission(source).test()
338
+ source.permissions["delete"].test()
326
339
  return actions.delete_source(source), 204
327
340
 
328
341
 
329
342
  @ns.route("/source/<harvest_source:source>/validate/", endpoint="validate_harvest_source")
330
343
  class ValidateSourceAPI(API):
331
344
  @api.doc("validate_harvest_source")
332
- @api.secure(admin_permission)
345
+ @api.secure
333
346
  @api.expect(validation_fields)
334
347
  @api.marshal_with(source_fields)
335
348
  def post(self, source: HarvestSource):
336
349
  """Validate or reject an harvest source"""
350
+ source.permissions["validate"].test()
337
351
  form = api.validate(HarvestSourceValidationForm)
338
352
  if form.state.data == VALIDATION_ACCEPTED:
339
353
  return actions.validate_source(source, form.comment.data)
@@ -354,7 +368,7 @@ class RunSourceAPI(API):
354
368
  "Cannot run source manually. Please contact the platform if you need to reschedule the harvester.",
355
369
  )
356
370
 
357
- OwnablePermission(source).test()
371
+ source.permissions["run"].test()
358
372
 
359
373
  if source.validation.state != VALIDATION_ACCEPTED:
360
374
  api.abort(400, "Source is not validated. Please validate the source before running.")
@@ -367,11 +381,12 @@ class RunSourceAPI(API):
367
381
  @ns.route("/source/<harvest_source:source>/schedule/", endpoint="schedule_harvest_source")
368
382
  class ScheduleSourceAPI(API):
369
383
  @api.doc("schedule_harvest_source")
370
- @api.secure(admin_permission)
384
+ @api.secure
371
385
  @api.expect((str, "A cron expression"))
372
386
  @api.marshal_with(source_fields)
373
387
  def post(self, source: HarvestSource):
374
388
  """Schedule an harvest source"""
389
+ source.permissions["schedule"].test()
375
390
  # Handle both syntax: quoted and unquoted
376
391
  try:
377
392
  data = request.json
@@ -380,10 +395,11 @@ class ScheduleSourceAPI(API):
380
395
  return actions.schedule(source, data)
381
396
 
382
397
  @api.doc("unschedule_harvest_source")
383
- @api.secure(admin_permission)
398
+ @api.secure
384
399
  @api.marshal_with(source_fields)
385
400
  def delete(self, source: HarvestSource):
386
401
  """Unschedule an harvest source"""
402
+ source.permissions["schedule"].test()
387
403
  return actions.unschedule(source), 204
388
404
 
389
405
 
@@ -408,6 +424,7 @@ class PreviewSourceAPI(API):
408
424
  @api.marshal_with(preview_job_fields)
409
425
  def get(self, source: HarvestSource):
410
426
  """Preview a single harvest source given an ID or a slug"""
427
+ source.permissions["preview"].test()
411
428
  return actions.preview(source)
412
429
 
413
430
 
@@ -437,7 +454,7 @@ class JobAPI(API):
437
454
  @api.expect(parser)
438
455
  @api.marshal_with(job_fields)
439
456
  def get(self, ident):
440
- """List all jobs for a given source"""
457
+ """Get a single job given an ID"""
441
458
  return actions.get_job(ident)
442
459
 
443
460
 
@@ -166,6 +166,7 @@ class BaseBackend(object):
166
166
  log.debug(f"Starting harvesting {self.source.name} ({self.source.url})…")
167
167
  factory = HarvestJob if self.dryrun else HarvestJob.objects.create
168
168
  self.job = factory(status="initialized", started=datetime.utcnow(), source=self.source)
169
+ self.remote_ids = set()
169
170
 
170
171
  before_harvest_job.send(self)
171
172
  # Set harvest_activity_user on global context during the run
@@ -190,6 +191,7 @@ class BaseBackend(object):
190
191
 
191
192
  if any(i.status == "failed" for i in self.job.items):
192
193
  self.job.status += "-errors"
194
+
193
195
  except HarvestValidationError as e:
194
196
  log.exception(
195
197
  f'Harvesting validation failed for "{safe_unicode(self.source.name)}" ({self.source.backend})'
@@ -199,6 +201,15 @@ class BaseBackend(object):
199
201
 
200
202
  error = HarvestError(message=safe_unicode(e))
201
203
  self.job.errors.append(error)
204
+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
205
+ log.warning(
206
+ f'Harvesting connection error for "{safe_unicode(self.source.name)}" ({self.source.backend}): {e}'
207
+ )
208
+
209
+ self.job.status = "failed"
210
+
211
+ error = HarvestError(message=safe_unicode(e), details=traceback.format_exc())
212
+ self.job.errors.append(error)
202
213
  except Exception as e:
203
214
  log.exception(
204
215
  f'Harvesting failed for "{safe_unicode(self.source.name)}" ({self.source.backend})'
@@ -232,8 +243,13 @@ class BaseBackend(object):
232
243
 
233
244
  current_app.logger.addHandler(log_catcher)
234
245
  dataset = self.inner_process_dataset(item, **kwargs)
246
+ if dataset.harvest:
247
+ item.remote_url = dataset.harvest.remote_url
248
+
249
+ # Use `item.remote_id` from this point, because `inner_process_dataset` could have modified it.
250
+
251
+ self.ensure_unique_remote_id(item)
235
252
 
236
- # Use `item.remote_id` because `inner_process_dataset` could have modified it.
237
253
  dataset.harvest = self.update_dataset_harvest_info(dataset.harvest, item.remote_id)
238
254
  dataset.archived = None
239
255
 
@@ -291,6 +307,10 @@ class BaseBackend(object):
291
307
  raise HarvestSkipException("missing identifier")
292
308
 
293
309
  dataservice = self.inner_process_dataservice(item, **kwargs)
310
+ if dataservice.harvest:
311
+ item.remote_url = dataservice.harvest.remote_url
312
+
313
+ self.ensure_unique_remote_id(item)
294
314
 
295
315
  dataservice.harvest = self.update_dataservice_harvest_info(
296
316
  dataservice.harvest, remote_id
@@ -325,6 +345,12 @@ class BaseBackend(object):
325
345
  item.ended = datetime.utcnow()
326
346
  self.save_job()
327
347
 
348
+ def ensure_unique_remote_id(self, item):
349
+ if item.remote_id in self.remote_ids:
350
+ raise HarvestValidationError(f"Identifier '{item.remote_id}' already exists")
351
+
352
+ self.remote_ids.add(item.remote_id)
353
+
328
354
  def update_dataset_harvest_info(self, harvest: HarvestDatasetMetadata | None, remote_id: int):
329
355
  if not harvest:
330
356
  harvest = HarvestDatasetMetadata()
@@ -173,7 +173,10 @@ class CkanBackend(BaseBackend):
173
173
  continue
174
174
  elif key == "spatial":
175
175
  # GeoJSON representation (Polygon or Point)
176
- spatial_geom = json.loads(value)
176
+ if isinstance(value, dict):
177
+ spatial_geom = value
178
+ else:
179
+ spatial_geom = json.loads(value)
177
180
  elif key == "spatial-text":
178
181
  # Textual representation of the extent / location
179
182
  qs = GeoZone.objects(db.Q(name=value) | db.Q(slug=value))
@@ -213,12 +216,17 @@ class CkanBackend(BaseBackend):
213
216
  dataset.spatial.zones = [spatial_zone]
214
217
 
215
218
  if spatial_geom:
219
+ if "type" not in spatial_geom:
220
+ raise HarvestException(f"Spatial geometry {spatial_geom} without `type`")
221
+
216
222
  if spatial_geom["type"] == "Polygon":
217
223
  coordinates = [spatial_geom["coordinates"]]
218
224
  elif spatial_geom["type"] == "MultiPolygon":
219
225
  coordinates = spatial_geom["coordinates"]
220
226
  else:
221
- raise HarvestException("Unsupported spatial geometry")
227
+ raise HarvestException(
228
+ f"Unsupported spatial geometry {spatial_geom['type']} in {spatial_geom}. (Supported types are `Polygon` and `MultiPolygon`)"
229
+ )
222
230
  dataset.spatial.geom = {"type": "MultiPolygon", "coordinates": coordinates}
223
231
 
224
232
  if temporal_start and temporal_end:
@@ -267,5 +275,6 @@ class CkanBackend(BaseBackend):
267
275
 
268
276
  class DkanBackend(CkanBackend):
269
277
  name = "dkan"
278
+ display_name = "DKAN"
270
279
  schema = dkan_schema
271
280
  filters = []
udata/harvest/commands.py CHANGED
@@ -4,6 +4,7 @@ import click
4
4
 
5
5
  from udata.commands import KO, OK, cli, green, red
6
6
  from udata.harvest.backends import get_all_backends, is_backend_enabled
7
+ from udata.models import Dataset
7
8
 
8
9
  from . import actions
9
10
 
@@ -156,3 +157,35 @@ def attach(domain, filename):
156
157
  log.info("Attaching datasets for domain %s", domain)
157
158
  result = actions.attach(domain, filename)
158
159
  log.info("Attached %s datasets to %s", result.success, domain)
160
+
161
+
162
+ @grp.command()
163
+ @click.argument("dataset_id")
164
+ def detach(dataset_id):
165
+ """
166
+ Detach a dataset_id from its harvest source
167
+
168
+ The dataset will be cleaned from harvested information
169
+ """
170
+ log.info(f"Detaching dataset {dataset_id}")
171
+ dataset = Dataset.get(dataset_id)
172
+ actions.detach(dataset)
173
+ log.info("Done")
174
+
175
+
176
+ @grp.command()
177
+ @click.argument("identifier")
178
+ def detach_all_from_source(identifier):
179
+ """
180
+ Detach all datasets from a harvest source
181
+
182
+ All the datasets will be cleaned from harvested information.
183
+ Make sure the harvest source won't create new duplicate datasets,
184
+ either by deactivating it or filtering its scope, etc.
185
+ """
186
+ log.info(f"Detaching datasets from harvest source {identifier}")
187
+ count = actions.detach_all_from_source(actions.get_source(identifier))
188
+ log.info(f"Detached {count} datasets")
189
+ log.warning(
190
+ "Make sure the harvest source won't create new duplicate datasets, either by deactivating it or filtering its scope, etc."
191
+ )
udata/harvest/filters.py CHANGED
@@ -3,6 +3,9 @@ from voluptuous import Invalid
3
3
 
4
4
  from udata import tags, uris
5
5
 
6
+ TRUTHY_STRINGS = ("on", "t", "true", "y", "yes", "1")
7
+ FALSY_STRINGS = ("f", "false", "n", "no", "off", "0")
8
+
6
9
 
7
10
  def boolean(value):
8
11
  """
@@ -15,17 +18,25 @@ def boolean(value):
15
18
  if value is None or isinstance(value, bool):
16
19
  return value
17
20
 
18
- try:
19
- return bool(int(value))
20
- except ValueError:
21
+ if isinstance(value, int):
22
+ return bool(value)
23
+
24
+ if isinstance(value, str):
21
25
  lower_value = value.strip().lower()
26
+
22
27
  if not lower_value:
23
28
  return None
24
- if lower_value in ("f", "false", "n", "no", "off"):
29
+ if lower_value in FALSY_STRINGS:
25
30
  return False
26
- if lower_value in ("on", "t", "true", "y", "yes"):
31
+ if lower_value in TRUTHY_STRINGS:
27
32
  return True
28
- raise Invalid("Unable to parse boolean {0}".format(value))
33
+ raise Invalid(
34
+ f"Unable to parse string '{value}' as boolean. Supported values are {','.join(TRUTHY_STRINGS)} for `True` and {','.join(FALSY_STRINGS)} for `False`."
35
+ )
36
+
37
+ raise Invalid(
38
+ f"Cannot convert value {value} of type {type(value)} to boolean. Supported types are `bool`, `int` and `str`"
39
+ )
29
40
 
30
41
 
31
42
  def to_date(value):
udata/harvest/models.py CHANGED
@@ -66,6 +66,7 @@ class HarvestLog(db.EmbeddedDocument):
66
66
 
67
67
  class HarvestItem(db.EmbeddedDocument):
68
68
  remote_id = db.StringField()
69
+ remote_url = db.StringField()
69
70
  dataset = db.ReferenceField(Dataset)
70
71
  dataservice = db.ReferenceField(Dataservice)
71
72
  status = db.StringField(
@@ -172,6 +173,21 @@ class HarvestSource(Owned, db.Document):
172
173
  def __str__(self):
173
174
  return self.name or ""
174
175
 
176
+ @property
177
+ def permissions(self):
178
+ from udata.auth import admin_permission
179
+
180
+ from .permissions import HarvestSourceAdminPermission, HarvestSourcePermission
181
+
182
+ return {
183
+ "edit": HarvestSourceAdminPermission(self),
184
+ "delete": HarvestSourceAdminPermission(self),
185
+ "run": HarvestSourceAdminPermission(self),
186
+ "preview": HarvestSourcePermission(self),
187
+ "validate": admin_permission,
188
+ "schedule": admin_permission,
189
+ }
190
+
175
191
 
176
192
  class HarvestJob(db.Document):
177
193
  """Keep track of harvestings"""