Browse Source

Add import API

Gautier P 7 months ago
parent
commit
711590b809
2 changed files with 23 additions and 0 deletions
  1. 1
    0
      flatisfy/web/app.py
  2. 22
    0
      flatisfy/web/routes/api.py

+ 1
- 0
flatisfy/web/app.py View File

@@ -96,6 +96,7 @@ def get_app(config):
96 96
     )
97 97
 
98 98
     app.route("/api/v1/metadata", ["GET", "OPTIONS"], api_routes.metadata_v1)
99
+    app.route("/api/v1/import", ["GET", "OPTIONS"], api_routes.import_v1)
99 100
 
100 101
     # Index
101 102
     app.route("/", "GET", lambda: _serve_static_file("index.html"))

+ 22
- 0
flatisfy/web/routes/api.py View File

@@ -16,6 +16,7 @@ import vobject
16 16
 import flatisfy.data
17 17
 from flatisfy.models import flat as flat_model
18 18
 from flatisfy.models.postal_code import PostalCode
19
+from flatisfy import cmds
19 20
 
20 21
 FILTER_RE = re.compile(r"filter\[([A-z0-9_]+)\]")
21 22
 
@@ -496,3 +497,24 @@ def metadata_v1(config):
496 497
         return {"data": {"last_update": last_update}}
497 498
     except Exception as exc:  # pylint: disable= broad-except
498 499
         return JSONError(500, str(exc))
500
+
501
+
502
+def import_v1(config):
503
+    """
504
+    API v1 import new flats.
505
+
506
+    Example::
507
+
508
+        GET /api/v1/import
509
+
510
+    :return: The new flats.
511
+    """
512
+    if bottle.request.method == "OPTIONS":
513
+        # CORS
514
+        return {}
515
+
516
+    try:
517
+        flats_id = cmds.import_and_filter(config, False, True)
518
+        return {"flats": flats_id}
519
+    except Exception as exc:  # pylint: disable= broad-except
520
+        return JSONError(500, str(exc))