@@ -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+
187251def 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