def export_detailed_csv(request, slug): """Display detailed CSV for certain users.""" project = get_object_or_404(Project, slug=slug) # Preprocessing tracker_models.update_metrics_cache(project) organizers = project.organizers(include_deleted=True).order_by('user__username') organizer_profiles = (organizer.user for organizer in organizers) organizer_ids = organizers.values('user_id') participants = project.non_organizer_participants(include_deleted=True).order_by( 'user__username') participant_profiles = (participant.user for participant in participants) participant_ids = participants.values('user_id') followers = project.non_participant_followers(include_deleted=True).order_by( 'source__username') follower_profiles = (follower.source for follower in followers) follower_ids = followers.values('source_id') previous_followers = project.previous_followers(include_deleted=True).order_by( 'source__username') previous_follower_profiles = (previous.source for previous in previous_followers) previous_follower_ids = project.previous_followers().values('source_id') headers = ["Time on Pages", "Non-zero Length Page Views", "Zero-length Page Views", "Comments", "Page Edits"] # Create csv response response = http.HttpResponse(mimetype='text/csv') response['Content-Disposition'] = 'attachment; ' response['Content-Disposition'] += 'filename=detailed_report.csv' writer = unicodecsv.writer(response) writer.writerow(["Course: " + project.name]) writer.writerow(["Data generated: " + datetime.datetime.now().strftime( "%b %d, %Y")]) writer.writerow([]) writer.writerow([]) # Write Total Metrics writer.writerow(["TOTALS"]) writer.writerow(["Organizers"] + headers) metrics = tracker_models.user_total_metrics(project, organizer_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants"] + headers) metrics = tracker_models.user_total_metrics(project, participant_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers"] + headers) for row in tracker_models.user_total_metrics(project, follower_profiles): writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers"] + headers) metrics = tracker_models.user_total_metrics(project, previous_follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors"] + headers) for row in tracker_models.unauth_total_metrics(project): writer.writerow(row + ["0"] * 2) writer.writerow([]) writer.writerow([]) # Restoring profile iterators organizer_profiles = (organizer.user for organizer in organizers) participant_profiles = (participant.user for participant in participants) follower_profiles = (follower.source for follower in followers) previous_follower_profiles = (previous.source for previous in previous_followers) # Write Per Page Total Metrics writer.writerow(["PER PAGE TOTALS"]) writer.writerow(["Organizers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, organizer_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, participant_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, previous_follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Page Paths"] + headers[:-2]) for row in tracker_models.unauth_total_per_page_metrics(project): writer.writerow(row) writer.writerow([]) writer.writerow([]) # Write Chronological Metrics writer.writerow(["CHRONOLOGICAL"]) writer.writerow(["Organizers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, organizer_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, participant_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, previous_follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Dates"] + headers) for row in tracker_models.chronological_unauth_metrics(project): writer.writerow(row + ["0"] * 2) writer.writerow([]) writer.writerow([]) # Write Chronological Per Page Metrics writer.writerow(["CHRONOLOGICAL PER PAGE"]) writer.writerow(["Organizers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, organizer_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, participant_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics(project, previous_follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Dates", "Page Paths"] + headers[:-2]) for row in tracker_models.chronological_unauth_per_page_metrics(project): writer.writerow(row) writer.writerow([]) writer.writerow([]) return response
def export_detailed_csv(request, slug): """Display detailed CSV for certain users.""" project = get_object_or_404(Project, slug=slug) # Preprocessing organizers = project.organizers(include_deleted=True).order_by('user__username') organizer_profiles = (organizer.user for organizer in organizers) organizer_ids = organizers.values('user_id') participants = project.non_organizer_participants(include_deleted=True).order_by( 'user__username') participant_profiles = (participant.user for participant in participants) participant_ids = participants.values('user_id') followers = project.non_participant_followers(include_deleted=True).order_by( 'source__username') follower_profiles = (follower.source for follower in followers) follower_ids = followers.values('source_id') previous_followers = project.previous_followers(include_deleted=True).order_by( 'source__username') previous_follower_profiles = (previous.source for previous in previous_followers) previous_follower_ids = project.previous_followers().values('source_id') headers = ["Time on Pages", "Non-zero Length Page Views", "Zero-length Page Views", "Comments", "Page Edits"] # Create csv response response = http.HttpResponse(mimetype='text/csv') response['Content-Disposition'] = 'attachment; ' response['Content-Disposition'] += 'filename=detailed_report.csv' writer = unicodecsv.writer(response) writer.writerow(["Course: " + project.name]) writer.writerow(["Data generated: " + datetime.datetime.now().strftime( "%b %d, %Y")]) writer.writerow([]) writer.writerow([]) # Write Total Metrics writer.writerow(["TOTALS"]) writer.writerow(["Organizers"] + headers) metrics = tracker_models.user_total_metrics(project, organizer_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants"] + headers) metrics = tracker_models.user_total_metrics(project, participant_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers"] + headers) for row in tracker_models.user_total_metrics(project, follower_profiles): writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers"] + headers) metrics = tracker_models.user_total_metrics(project, previous_follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors"] + headers) for row in tracker_models.unauth_total_metrics(project): writer.writerow(row + ["0"] * 2) writer.writerow([]) writer.writerow([]) # Restoring profile iterators organizer_profiles = (organizer.user for organizer in organizers) participant_profiles = (participant.user for participant in participants) follower_profiles = (follower.source for follower in followers) previous_follower_profiles = (previous.source for previous in previous_followers) # Write Per Page Total Metrics writer.writerow(["PER PAGE TOTALS"]) writer.writerow(["Organizers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, organizer_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, participant_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Page Paths"] + headers[:-2]) metrics = tracker_models.user_total_per_page_metrics(project, previous_follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Page Paths"] + headers[:-2]) for row in tracker_models.unauth_total_per_page_metrics(project): writer.writerow(row) writer.writerow([]) writer.writerow([]) # Write Chronological Metrics writer.writerow(["CHRONOLOGICAL"]) writer.writerow(["Organizers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, organizer_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, participant_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Dates"] + headers) metrics = tracker_models.chronological_user_metrics(project, previous_follower_profiles) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Dates"] + headers) for row in tracker_models.chronological_unauth_metrics(project): writer.writerow(row + ["0"] * 2) writer.writerow([]) writer.writerow([]) # Write Chronological Per Page Metrics writer.writerow(["CHRONOLOGICAL PER PAGE"]) writer.writerow(["Organizers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, organizer_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Participants", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, participant_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Followers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics( project, follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Previous Followers", "Dates", "Page Paths"] + headers[:-2]) metrics = tracker_models.chronological_user_per_page_metrics(project, previous_follower_ids) for row in metrics: writer.writerow(row) writer.writerow([]) writer.writerow(["Unauthenticated Visitors", "Dates", "Page Paths"] + headers[:-2]) for row in tracker_models.chronological_unauth_per_page_metrics(project): writer.writerow(row) writer.writerow([]) writer.writerow([]) return response