Skip to content

Commit

Permalink
views: only expose total stats to admins
Browse files Browse the repository at this point in the history
As this view is accessible to anyone with a login, filter access to the
stats.
  • Loading branch information
bors-ltd committed Sep 6, 2021
1 parent 59b3683 commit 790b121
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
10 changes: 2 additions & 8 deletions APITaxi2/tests/test_zupc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def test_ok(self, operateur, moteur, QueriesTracker):
'type': 'city',
'name': 'Paris',
'insee': '75056',
'stats': {
'total': 0
},
'stats': {},
}]}

zupc = ZUPCFactory()
Expand All @@ -45,9 +43,7 @@ def test_ok(self, operateur, moteur, QueriesTracker):
'type': 'ZUPC',
'name': zupc.nom,
'zupc_id': zupc.zupc_id,
'stats': {
'total': 0,
},
'stats': {},
}]

resp = operateur.client.get('zupc?lon=2.35&lat=48.86')
Expand All @@ -57,7 +53,6 @@ def test_ok(self, operateur, moteur, QueriesTracker):
'name': zupc.nom,
'zupc_id': zupc.zupc_id,
'stats': {
'total': 0,
'operators': {
operateur.user.email: 0,
}
Expand All @@ -84,7 +79,6 @@ def test_ok(self, operateur, QueriesTracker):
assert resp.json['data'][0]['id'] in (zupc.zupc_id, zupc2.zupc_id)
assert resp.json['data'][0]['nom'] in (zupc.nom, zupc2.nom)
assert resp.json['data'][0]['stats'] == {
'total': 0,
'operators': {
operateur.user.email: 0
}
Expand Down
21 changes: 12 additions & 9 deletions APITaxi2/views/zupc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@
blueprint = Blueprint('zupc', __name__)


def _get_zupc_stats(filter_name, filter_value, include_operators):
def _get_zupc_stats(filter_name, filter_value, include_total, include_operators):
"""`filter_name` is either "insee_code" or "zupc_id", which are parameters
expected by influx_backend.get_nb_active_taxis.
This function returns the total number of taxis within the INSEE code or
ZUPC. If include_operators is True, it also returns the numer of taxis of
If include_admin is True, this function returns the total number of taxis
within the INSEE code or ZUPC.
If include_operators is True, it also returns the number of taxis of
the current user.
"""
stats = {
'total': influx_backend.get_nb_active_taxis(**{filter_name: filter_value})
}
stats = {}
if include_total:
stats['total'] = influx_backend.get_nb_active_taxis(**{filter_name: filter_value})
if include_operators:
stats['operators'] = {
current_user.email: influx_backend.get_nb_active_taxis(
Expand Down Expand Up @@ -81,20 +82,21 @@ def zupc_list():
ZUPC.id
).all()

is_admin = current_user.has_role('admin')
is_operator = current_user.has_role('operateur')

if not zupcs:
ret = schema.dump({
'data': [
(town, _get_zupc_stats('insee_code', town.insee, is_operator))
(town, _get_zupc_stats('insee_code', town.insee, is_admin, is_operator))
for town in towns
]
})
return ret

ret = schema.dump({
'data': [
(zupc, _get_zupc_stats('zupc_id', zupc.zupc_id, is_operator))
(zupc, _get_zupc_stats('zupc_id', zupc.zupc_id, is_admin, is_operator))
for zupc in zupcs
]
})
Expand Down Expand Up @@ -124,6 +126,7 @@ def zupc_live():
)

zupcs = query.all()
is_admin = current_user.has_role('admin')
is_operator = current_user.has_role('operateur')

schema = schemas.DataZUPCGeomSchema()
Expand All @@ -132,6 +135,6 @@ def zupc_live():
'id': zupc.zupc_id,
'nom': zupc.nom,
'geojson': json.loads(zupc.geojson),
'stats': _get_zupc_stats('zupc_id', zupc.zupc_id, is_operator),
'stats': _get_zupc_stats('zupc_id', zupc.zupc_id, is_admin, is_operator),
} for zupc in zupcs]
})

0 comments on commit 790b121

Please sign in to comment.