from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from flask import Blueprint
from flask import jsonify
from webargs import Arg
from webargs.flaskparser import use_args

from mishapp_api.database import Disaster

disaster_api = Blueprint("disaster", __name__)


@disaster_api.errorhandler(400)
def handle_bad_request(err):
    data = getattr(err, "data")
    if data:
        err_message = data["message"]
    else:
        err_message = "Bad request"
    return jsonify({"message": err_message}), 400


@disaster_api.errorhandler(404)
def handle_not_found(err):
    return jsonify({"message": "Not found"}), 404


@disaster_api.route("/disasters")
@use_args({
    "page": Arg(int, default=1),
    "per_page": Arg(int, default=20),
})
def index(args):
    docs = Disaster.objects.paginate(args["page"], min(args["per_page"], 20))

    return jsonify({
        "meta": {
            "total": docs.total,
            "page": docs.page,
            "per_page": docs.per_page,
        },
        "items": [doc.asdict() for doc in docs.items]
    })


@disaster_api.route("/disasters/nearby")
@use_args({
    "lat": Arg(float, required=True),
    "lon": Arg(float, required=True),
    "radius": Arg(float, required=True),
    "page": Arg(int, default=1),
    "per_page": Arg(int, default=20),
})
def nearby(args):
    docs = Disaster.objects(
        geometry__near={
            "type": "Point",
            "coordinates": [args["lon"], args["lat"]],
        },
        geometry__max_distance=args["radius"],
    ).paginate(args["page"], min(args["per_page"], 20))

    return jsonify({
        "meta": {
            "total": docs.total,
            "page": docs.page,
            "per_page": docs.per_page,
        },
        "items": [doc.asdict() for doc in docs.items]
    })


@disaster_api.route("/disasters/verify")
@use_args({
    "lat": Arg(float, required=True),
    "lon": Arg(float, required=True),
    "radius": Arg(float, required=True),
})
def verify(args):
    counter = Disaster.objects(
        geometry__near={
            "$geometry": {
                "type": "Point",
                "coordinates": [args["lon"], args["lat"]],
            },
            "$maxDistance": args["radius"],
        },
        # geometry__max_distance=args["radius"],  # for Mongo < 2.6
    ).count()

    if counter > 0:
        return jsonify({"message": "OK"})
    return jsonify({"message": "Not found"}), 404

@@ -221,16 +221,52 @@
 ort Arg%0A
+from webargs import ValidationError%0A
 from web
@@ -387,24 +387,134 @@
 __name__)%0A%0A%0A
+def radius_gte_zero(val):%0A    if val %3C 0:%0A        raise ValidationError(%22radius must greater than equal 0%22)%0A%0A%0A
 @disaster_ap
@@ -1431,32 +1431,58 @@
 ius%22: Arg(float,
+ validate=radius_gte_zero,
  required=True),
@@ -1620,32 +1620,63 @@
 eometry__near=%7B%0A
+            %22$geometry%22: %7B%0A    
             %22typ
@@ -1680,32 +1680,36 @@
 type%22: %22Point%22,%0A
+    
             %22coo
@@ -1747,32 +1747,36 @@
 lat%22%5D%5D,%0A        
+    
 %7D,%0A        geome
@@ -1770,39 +1770,36 @@
         
-geometry__max_d
+    %22$maxD
 istance
-=
+%22: 
 args%5B%22ra
@@ -1802,24 +1802,35 @@
 %5B%22radius%22%5D,%0A
+        %7D,%0A
     ).pagina
@@ -2235,32 +2235,58 @@
 ius%22: Arg(float,
+ validate=radius_gte_zero,
  required=True),
@@ -2557,76 +2557,8 @@
  %7D,%0A
-        # geometry__max_distance=args%5B%22radius%22%5D,  # for Mongo %3C 2.6%0A
     

fix verify and nearby endpoint