summaryrefslogtreecommitdiff
path: root/opendc/api/v2/tiles/tileId/endpoint.py
blob: 85cbd9f4a1347469b91565ef4171e8953dbf2f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from opendc.models.tile import Tile
from opendc.util import exceptions
from opendc.util.rest import Response


def GET(request):
    """Get this Tile."""

    # Make sure request parameters are there

    try:
        request.check_required_parameters(path={'tileId': 'int'})

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Tile from the database

    tile = Tile.from_primary_key((request.params_path['tileId'], ))

    # Make sure this Tile exists

    if not tile.exists():
        return Response(404, '{} not found.'.format(tile))

    # Make sure this user is authorized to view this Tile

    if not tile.google_id_has_at_least(request.google_id, 'VIEW'):
        return Response(403, 'Forbidden from retrieving {}.'.format(tile))

    # Return this Tile

    tile.read()

    return Response(200, 'Successfully retrieved {}.'.format(tile), tile.to_JSON())


def DELETE(request):
    """Delete this Tile."""

    # Make sure request parameters are there

    try:
        request.check_required_parameters(path={'tileId': 'int'})

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Tile from the database

    tile = Tile.from_primary_key((request.params_path['tileId'], ))

    # Make sure this Tile exists

    if not tile.exists():
        return Response(404, '{} not found.'.format(tile))

    # Make sure this user is authorized to edit this Tile

    if not tile.google_id_has_at_least(request.google_id, 'EDIT'):
        return Response(403, 'Forbidden from deleting {}.'.format(tile))

    # Delete this Tile

    tile.delete()

    # Return this Tile

    return Response(200, 'Successfully deleted {}.'.format(tile), tile.to_JSON())