udata 11.1.2.dev3__py3-none-any.whl → 11.1.2.dev5__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 (27) hide show
  1. udata/app.py +50 -1
  2. udata/core/dataset/api.py +1 -1
  3. udata/core/site/models.py +9 -0
  4. udata/static/chunks/{10.471164b2a9fe15614797.js → 10.8ca60413647062717b1e.js} +3 -3
  5. udata/static/chunks/{10.471164b2a9fe15614797.js.map → 10.8ca60413647062717b1e.js.map} +1 -1
  6. udata/static/chunks/{11.55ab79044cda0271b595.js → 11.0f04e49a40a0a381bcce.js} +3 -3
  7. udata/static/chunks/{11.55ab79044cda0271b595.js.map → 11.0f04e49a40a0a381bcce.js.map} +1 -1
  8. udata/static/chunks/{19.f03a102365af4315f9db.js → 19.8da42e8359d72afc2618.js} +3 -3
  9. udata/static/chunks/{19.f03a102365af4315f9db.js.map → 19.8da42e8359d72afc2618.js.map} +1 -1
  10. udata/static/chunks/{8.b58fcd977fcaf3415571.js → 8.494b003a94383b142c18.js} +2 -2
  11. udata/static/chunks/{8.b58fcd977fcaf3415571.js.map → 8.494b003a94383b142c18.js.map} +1 -1
  12. udata/static/chunks/{9.07515e5187f475bce828.js → 9.033d7e190ca9e226a5d0.js} +3 -3
  13. udata/static/chunks/{9.07515e5187f475bce828.js.map → 9.033d7e190ca9e226a5d0.js.map} +1 -1
  14. udata/static/common.js +1 -1
  15. udata/static/common.js.map +1 -1
  16. udata/templates/404.html +43 -0
  17. udata/tests/api/test_datasets_api.py +11 -0
  18. udata/tests/frontend/test_error_handlers.py +77 -0
  19. udata/tests/site/test_site_api.py +1 -0
  20. udata/translations/udata.pot +133 -77
  21. udata/uris.py +7 -1
  22. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/METADATA +1 -1
  23. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/RECORD +27 -25
  24. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/WHEEL +0 -0
  25. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/entry_points.txt +0 -0
  26. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/licenses/LICENSE +0 -0
  27. {udata-11.1.2.dev3.dist-info → udata-11.1.2.dev5.dist-info}/top_level.txt +0 -0
udata/app.py CHANGED
@@ -7,10 +7,21 @@ from os.path import abspath, dirname, exists, isfile, join
7
7
 
8
8
  import bson
9
9
  from flask import Blueprint as BaseBlueprint
10
- from flask import Flask, abort, g, json, make_response, send_from_directory
10
+ from flask import (
11
+ Flask,
12
+ abort,
13
+ g,
14
+ json,
15
+ jsonify,
16
+ make_response,
17
+ render_template,
18
+ request,
19
+ send_from_directory,
20
+ )
11
21
  from flask_caching import Cache
12
22
  from flask_wtf.csrf import CSRFProtect
13
23
  from speaklater import is_lazy_string
24
+ from werkzeug.exceptions import NotFound
14
25
  from werkzeug.middleware.proxy_fix import ProxyFix
15
26
 
16
27
  from udata import cors, entrypoints
@@ -225,9 +236,47 @@ def register_extensions(app):
225
236
  mail.init_app(app)
226
237
  search.init_app(app)
227
238
  sentry.init_app(app)
239
+ app.after_request(return_404_html_if_requested)
240
+ app.register_error_handler(NotFound, page_not_found)
228
241
  return app
229
242
 
230
243
 
244
+ def return_404_html_if_requested(response):
245
+ # It's impossible to return an HTML page from an error handler of
246
+ # the API with Fask-RestX because the error handler is expected to
247
+ # return a JSON `dict` and not an HTML string.
248
+ # The only way to return the correct HTML response from a client
249
+ # that require HTML (for exemple by requesting /api/1/datasets/r/some-uuid with
250
+ # a non-existant resource) is to hook an `after_request` middleware.
251
+
252
+ if response.status_code != 404:
253
+ return response
254
+
255
+ # Return an HTML response if the user prefers HTML over JSON
256
+ if request.accept_mimetypes.best_match(["application/json", "text/html"]) == "text/html":
257
+ from udata.uris import homepage_url
258
+
259
+ html_content = render_template("404.html", homepage_url=homepage_url())
260
+ response = make_response(html_content, 404)
261
+ response.headers["Content-Type"] = "text/html; charset=utf-8"
262
+
263
+ return response
264
+
265
+
266
+ def page_not_found(e: NotFound):
267
+ """
268
+ This handler is only called for non existing pages,
269
+ for example "/api/1/oups", see `return_404_html_if_requested`
270
+ for calling `abort(404)` inside the API.
271
+ """
272
+ from udata.uris import homepage_url
273
+
274
+ if request.accept_mimetypes.best_match(["application/json", "text/html"]) == "text/html":
275
+ return render_template("404.html", homepage_url=homepage_url()), 404
276
+
277
+ return jsonify({"error": e.description, "status": 404}), 404
278
+
279
+
231
280
  def register_features(app):
232
281
  from udata.features import notifications
233
282
 
udata/core/dataset/api.py CHANGED
@@ -492,7 +492,7 @@ class ResourceRedirectAPI(API):
492
492
  Redirect to the latest version of a resource given its identifier.
493
493
  """
494
494
  resource = get_resource(id)
495
- return redirect(resource.url.strip()) if resource else abort(404)
495
+ return redirect(resource.url.strip()) if resource else abort(404, "Resource not found")
496
496
 
497
497
 
498
498
  @ns.route("/<dataset:dataset>/resources/", endpoint="resources")
udata/core/site/models.py CHANGED
@@ -64,6 +64,15 @@ class Site(WithMetrics, db.Document):
64
64
  def __str__(self):
65
65
  return self.title or ""
66
66
 
67
+ @field(description="The current version of udata")
68
+ def version(self):
69
+ try:
70
+ from importlib.metadata import version
71
+
72
+ return version("udata")
73
+ except Exception:
74
+ return None
75
+
67
76
  def count_users(self):
68
77
  from udata.models import User
69
78