get_regions()   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 41
rs 8.5806
cc 4
1
"""
2
Endpoints:
3
----------
4
5
- **/api/1.0/regions/** - **GET** List all regions
6
- **/api/1.0/regions/<id>** - **GET** Detailed information about region *id*
7
"""
8
9
from flask import jsonify, request, url_for, current_app
10
11
from ..models import Region
12
from .blueprint import api
13
14
15
@api.route('/regions/')
16
def get_regions():
17
    """
18
    List all regions
19
20
    Example response: ::
21
22
        { "count": 5,
23
          "next": "http://riverflo.ws/api/1.0/regions/?page=2",
24
          "prev": null,
25
          "regions": [
26
            { "html": "http://riverflo.ws/region/mahoosucs/",
27
              "id": 1,
28
              "name": "Mahoosuc Mountains",
29
              "url": "http://riverflo.ws/api/1.0/regions/1"
30
            },
31
            { "html": "http://riverflo.ws/region/whites/",
32
              "id": 2,
33
              "name": "White Mountains",
34
              "url": "http://riverflo.ws/api/1.0/regions/2"
35
            }
36
          ]
37
        }
38
39
    """
40
    page = request.args.get('page', 1, type=int)
41
    pagination = Region.query.paginate(page,
42
                                       per_page=current_app.config['API_GAGES_PER_PAGE'],  # noqa
43
                                       error_out=False)
44
    regions = pagination.items
45
    prev = None
46
    if pagination.has_prev:
47
        prev = url_for('.get_regions', page=page-1, _external=True)
48
    next_p = None
49
    if pagination.has_next:
50
        next_p = url_for('.get_regions', page=page+1, _external=True)
51
    return jsonify({
52
        'regions': [region.to_json() for region in regions],
53
        'prev': prev,
54
        'next': next_p,
55
        'count': pagination.total
56
    })
57
58
59
@api.route('/regions/<int:rid>')
60
def get_region(rid):
61
    """
62
    Detailed information about region *id*
63
64
    Parameters:
65
        id (int): Primary id key of region
66
67
    Example response: ::
68
69
        { "description": "The Mahoosuc Mountains are in Maine.",
70
          "gages": [
71
            { "id": 3,
72
              "location": "Bear River near RT 2 in Newry Maine",
73
              "name": "Bear River at Newry",
74
              "url": "http://riverflo.ws/api/1.0/gages/3"
75
            },
76
            { "id": 1,
77
              "location": "Bull Branch at Twin Bridges in Ketchum Maine",
78
              "name": "Bull Branch",
79
              "url": "http://riverflo.ws/api/1.0/gages/1"
80
            }
81
          ],
82
          "html": "http://riverflo.ws/region/mahoosucs/",
83
          "id": 1,
84
          "name": "Mahoosuc Mountains",
85
          "sections": [
86
            { "id": 2,
87
              "name": "Bear River",
88
              "url": "http://riverflo.ws/api/1.0/sections/2"
89
            },
90
            { "id": 1,
91
              "name": "Bull Branch",
92
              "url": "http://riverflo.ws/api/1.0/sections/1"
93
            }
94
          ],
95
          "url": "http://riverflo.ws/api/1.0/regions/1"
96
        }
97
    """
98
    region = Region.query.get_or_404(rid)
99
    return jsonify(region.to_long_json())
100