Skip to content

Commit d064d92

Browse files
authored
Split map SVG into separate route (#538)
1 parent 5ac2e4f commit d064d92

File tree

5 files changed

+56
-63
lines changed

5 files changed

+56
-63
lines changed

OpenOversight/app/main/views.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ def index():
160160
state_count=state_count,
161161
department_count=department_count,
162162
departments_by_state=departments_by_state,
163-
map_paths=current_app.config["MAP_DATA"],
164163
)
165164

166165

@@ -179,10 +178,19 @@ def set_session_timezone():
179178
@main.route("/browse", methods=[HTTPMethod.GET])
180179
def browse():
181180
departments_by_state = Department.by_state()
182-
return render_template(
183-
"browse.html",
184-
departments_by_state=departments_by_state,
185-
map_paths=current_app.config["MAP_DATA"],
181+
return render_template("browse.html", departments_by_state=departments_by_state)
182+
183+
184+
@main.route("/map.svg", methods=[HTTPMethod.GET])
185+
def render_map():
186+
departments_by_state = Department.by_state()
187+
return Response(
188+
render_template(
189+
"map.svg",
190+
departments_by_state=departments_by_state,
191+
map_paths=current_app.config["MAP_DATA"],
192+
),
193+
mimetype="image/svg+xml",
186194
)
187195

188196

OpenOversight/app/templates/browse.html

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,7 @@ <h1 class="hero">Browse Officers by Department</h1>
1313
<div class="text-center">
1414
<div class="col-lg-6 text-start mx-auto py-5">
1515
<p class="mb-5 vertical-padding">Use the map or list below to browse officers by department.</p>
16-
<svg xmlns="http://www.w3.org/2000/svg"
17-
version="1.1"
18-
viewBox="0 0 1000 618">
19-
<defs>
20-
<style>
21-
.cls-1 {
22-
fill: #0099c9;
23-
}
24-
25-
.cls-1,
26-
.cls-2 {
27-
stroke: #fff;
28-
stroke-width: .8px;
29-
}
30-
31-
.cls-2 {
32-
fill: #d3d3d3;
33-
}
34-
</style>
35-
</defs>
36-
{% for state, path in map_paths.items() %}
37-
{% if state in departments_by_state.keys() %}
38-
<a href="#state-{{ state }}">
39-
<path class="cls-1" d="{{ path }}" />
40-
</a>
41-
{% else %}
42-
<path class="cls-2" d="{{ path }}" />
43-
{% endif %}
44-
{% endfor %}
45-
</svg>
16+
<object type="image/svg+xml" data="{{ url_for('main.render_map') }}"></object>
4617
{% for state, departments in departments_by_state.items() %}
4718
<div id="state-{{ state }}" class="state-row mt-5">
4819
<h3>{{ state | get_state_full_name }}</h3>

OpenOversight/app/templates/index.html

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,8 @@ <h1>OpenOversight</h1>
2323
</div>
2424
<div class="row display-flex horizontal-padding text-start">
2525
<div class="col-lg-6">
26-
<svg xmlns="http://www.w3.org/2000/svg"
27-
version="1.1"
28-
viewBox="0 0 1000 618">
29-
<defs>
30-
<style>
31-
.cls-1 {
32-
fill: #0099c9;
33-
}
34-
35-
.cls-1,
36-
.cls-2 {
37-
stroke: #fff;
38-
stroke-width: .8px;
39-
}
40-
41-
.cls-2 {
42-
fill: #d3d3d3;
43-
}
44-
</style>
45-
</defs>
46-
{% for state, path in map_paths.items() %}
47-
{% if state in departments_by_state.keys() %}
48-
<path class="cls-1" d="{{ path }}" />
49-
{% else %}
50-
<path class="cls-2" d="{{ path }}" />
51-
{% endif %}
52-
{% endfor %}
53-
</svg>
26+
<img alt="Map of states with departments in OpenOversight"
27+
src="{{ url_for('main.render_map') }}" />
5428
</div>
5529
<div class="col-lg-6">
5630
<a href="{{ url_for('main.get_officer') }}"
Lines changed: 28 additions & 0 deletions
Loading

OpenOversight/tests/routes/test_other.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from flask import current_app, url_for
66

7+
from OpenOversight.app.models.database import Department
78
from OpenOversight.app.utils.constants import ENCODING_UTF_8, KEY_TIMEZONE
89
from OpenOversight.tests.constants import GENERAL_USER_USERNAME
910
from OpenOversight.tests.routes.route_helpers import login_user
@@ -83,3 +84,14 @@ def test_timezone_setting_empty_string(client):
8384
assert rv.status_code == HTTPStatus.OK
8485
with client.session_transaction() as session:
8586
assert session[KEY_TIMEZONE] == current_app.config.get(KEY_TIMEZONE)
87+
88+
89+
def test_map_rendering(client, mockdata):
90+
with current_app.test_request_context():
91+
departments_by_state = Department.by_state()
92+
rv = client.get(url_for("main.render_map"))
93+
assert rv.status_code == HTTPStatus.OK
94+
assert rv.mimetype == "image/svg+xml"
95+
96+
for state in departments_by_state.keys():
97+
assert f"#state-{state}" in rv.data.decode(ENCODING_UTF_8)

0 commit comments

Comments
 (0)