get_sections()   B
last analyzed

Complexity

Conditions 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 38
rs 8.5806
cc 4
1
"""
2
Endpoints:
3
----------
4
5
- **/api/1.0/sections/** - **GET** List all sections
6
- **/api/1.0/sections/<id>** - **GET** Detailed information about section *id*
7
"""
8
from flask import jsonify, request, url_for, current_app
9
10
from ..models import Section
11
from .blueprint import api
12
13
14
@api.route('/sections/')
15
def get_sections():
16
    """
17
    List all sections
18
19
    Example response: ::
20
21
        { "count": 2,
22
          "next": null,
23
          "prev": null,
24
          "sections": [
25
            { "id": 2,
26
              "name": "Bear River",
27
              "url": "http://riverflo.ws/api/1.0/sections/2"
28
            },
29
            { "id": 1,
30
              "name": "Bull Branch",
31
              "url": "http://riverflo.ws/api/1.0/sections/1"
32
            }
33
          ]
34
        }
35
    """
36
    page = request.args.get('page', 1, type=int)
37
    pagination = Section.query.paginate(page,
38
                                        per_page=current_app.config['API_GAGES_PER_PAGE'],  # noqa
39
                                        error_out=False)
40
    sections = pagination.items
41
    prev = None
42
    if pagination.has_prev:
43
        prev = url_for('.get_sections', page=page-1)
44
    next_p = None
45
    if pagination.has_next:
46
        next_p = url_for('.get_sections', page=page+1)
47
    return jsonify({
48
        'sections': [section.to_json() for section in sections],
49
        'prev': prev,
50
        'next': next_p,
51
        'count': pagination.total
52
    })
53
54
55
@api.route('/sections/map/')
56
def get_section_geojson():
57
    """
58
    Returns a GeoJSON FeatureCollection for the sections
59
    """
60
    sections = Section.query.all()
61
    geojson = {
62
        'type': 'FeatureCollection',
63
        'features': [section.geojson() for section in sections]
64
    }
65
    return jsonify(geojson)
66
67
68
@api.route('/sections/<int:sid>')
69
def get_section(sid):
70
    """
71
    Detailed information about section *id*
72
73
    Parameters:
74
        id (int): Primary key for section
75
76
    Example response: ::
77
78
        { "access": null,
79
          "description": "Most commonly run from the where Goose Eye Brook",
80
          "id": 1,
81
          "latitude": 44.517188630525425,
82
          "location": "Ketchum, ME",
83
          "longitude": -70.93158602714539,
84
          "name": "Bull Branch",
85
          "regions": [
86
            { "html": "http://riverflo.ws/region/mahoosucs/",
87
              "id": 1,
88
              "name": "Mahoosuc Mountains",
89
              "url": "http://riverflo.ws/api/1.0/regions/1"
90
            },
91
            { "html": "http://127.0.0.1:5000/region/whites/",
92
              "id": 2,
93
              "name": "White Mountains",
94
              "url": "http://riverflo.ws/api/1.0/regions/2"
95
            },
96
            { "html": "http://riverflo.ws/region/maine/",
97
              "id": 4,
98
              "name": "Maine",
99
              "url": "http://riverflo.ws/api/1.0/regions/4"
100
            }
101
          ],
102
          "url": "http://riverflo.ws/api/1.0/sections/1"
103
        }
104
    """
105
    section = Section.query.get_or_404(sid)
106
    return jsonify(section.to_long_json())
107