Skip to content

Commit 91145f9

Browse files
committed
feat: fix "All User Stats" download link
This commit brings in a new route, /data/all_user_stats.csv. TODO: Simplify other CSV views. Signed-off-by: Kevin Morris <kevr@0cost.org>
1 parent b0201d4 commit 91145f9

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

home/templates/home/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
Download
6363
</a>
6464
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
65-
<a class="dropdown-item" href="/data/users_agg.csv">All User Stats</a>
65+
<a class="dropdown-item" href="/data/all_user_stats.csv">All User Stats</a>
6666
<a class="dropdown-item" href="/data/users.csv">All User Account Info</a>
6767
<a class="dropdown-item" href="/data/daily_walks.csv">All Walks</a>
6868
<a class="dropdown-item" href="/data/intentional_walks.csv">All Recorded Walks</a>

home/urls.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
views.user_agg_csv_view,
1717
name="users_agg_csv_view",
1818
),
19+
path(
20+
"data/all_user_stats.csv",
21+
views.all_user_stats_csv_view,
22+
name="all_user_stats_csv_view",
23+
),
1924
path("data/users.csv", views.users_csv_view, name="users_csv_view"),
2025
path(
2126
"data/daily_walks.csv",

home/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from .web.home import HomeView
99
from .web.intentionalwalk import IntentionalWalkWebView
1010
from .web.user import UserListView
11+
from .web.data import all_user_stats_csv_view
1112
from .web.data import user_agg_csv_view
1213
from .web.data import users_csv_view
1314
from .web.data import daily_walks_csv_view

home/views/web/data.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,70 @@ def _get_user_daily_step_counts(
184184
return daily_step_counts_by_user
185185

186186

187+
def all_user_stats_csv_view(request) -> HttpResponse:
188+
columns = [
189+
"Participant Name",
190+
"Date Enrolled",
191+
"Email",
192+
"Zip Code",
193+
"Sexual Orientation",
194+
"Sexual Orientation Other",
195+
"Gender Identity",
196+
"Gender Identity Other",
197+
"Race",
198+
"Race Other",
199+
"Is Latino",
200+
"Age",
201+
"Total Daily Walks",
202+
"Total Daily Steps",
203+
"Total Recorded Walks",
204+
"Total Recorded Steps",
205+
]
206+
207+
users = Account.objects.all()
208+
output = []
209+
for user in users:
210+
daily_walks = user.dailywalk_set.values("steps")
211+
cnt_daily = len(daily_walks)
212+
daily_steps = sum([w.get("steps") for w in daily_walks])
213+
214+
rec_walks = user.intentionalwalk_set.values("steps")
215+
cnt_recorded = len(rec_walks)
216+
rec_steps = sum([w.get("steps") for w in rec_walks])
217+
218+
output.append(
219+
{
220+
"Participant Name": user.name,
221+
"Date Enrolled": user.created,
222+
"Email": user.email,
223+
"Zip Code": user.zip,
224+
"Sexual Orientation": user.sexual_orien,
225+
"Sexual Orientation Other": user.sexual_orien_other,
226+
"Gender Identity": user.gender,
227+
"Gender Identity Other": user.gender_other,
228+
"Race": user.race,
229+
"Race Other": user.race_other,
230+
"Is Latino": user.is_latino,
231+
"Age": user.age,
232+
"Total Daily Walks": cnt_daily,
233+
"Total Daily Steps": daily_steps,
234+
"Total Recorded Walks": cnt_recorded,
235+
"Total Recorded Steps": rec_steps,
236+
}
237+
)
238+
239+
response = HttpResponse(content_type="text/csv")
240+
response[
241+
"Content-Disposition"
242+
] = 'attachment; filename="all_users_stats.csv"'
243+
244+
writer = csv.DictWriter(response, fieldnames=columns)
245+
writer.writeheader()
246+
writer.writerows(output)
247+
248+
return response
249+
250+
187251
def user_agg_csv_view(request) -> HttpResponse:
188252
if not request.user.is_authenticated:
189253
return HttpResponse("You are not authorized to view this!")

0 commit comments

Comments
 (0)