Hi, im using tutor and i want to export data to generate reports, like generated certificates, users, etc…
i have already created this script:
from lms.djangoapps.certificates.models import GeneratedCertificate
from apice_registration.models import ExtendedProfile
import csv
from openedx.core.djangoapps.content.course_overviews.models import CourseOverview
report_dir = "/openedx/data/logs/"
header = ['name', 'country', 'state', 'city', 'birthyear', 'created_at',
'role', 'role_other', 'educational_stage', 'institution',
'apice_1_cert', 'cert_date_1', 'apice_2_cert', 'cert_date_2', 'apice_3_cert', 'cert_date_3']
data = []
ex_profile = ExtendedProfile.objects.filter(user__is_staff=False)
cert_qs = GeneratedCertificate.objects.filter(status='downloadable', user__is_staff=False)
co = CourseOverview.objects.all()
course_cert = {
'apice1': '',
'cert_date_1': '',
'apice2': '',
'cert_date_2': '',
'apice3': '',
'cert_date_3': '',
}
with open(report_dir + "stats.csv", 'w', encoding='UTF8') as f:
writer = csv.writer(f)
writer.writerow(header)
for person in ex_profile:
counter = 0
for c in co:
counter += 1
person_cert = cert_qs.filter(user=person.user, course_id=c.id)
if person_cert:
course_cert['apice{}'.format(counter)] = 1
course_cert['cert_date_{}'.format(counter)] = person_cert[0].created_date
data = ([person.user.profile.name, person.current_country, person.current_state, person.current_city,
person.birthyear, person.user.date_joined, person.role, person.role_other,
person.educational_stage, person.institution, course_cert['apice1'], course_cert['cert_date_1'],
course_cert['apice2'], course_cert['cert_date_2'], course_cert['apice3'],
course_cert['cert_date_3']])
course_cert = course_cert.fromkeys(course_cert, '')
writer.writerow(data)
so i run it in tutor shell and it generate a csv with my data.
the problem is that sometimes it generates a csv with broken data, i think thats because some timeout issues with aws machine, cpu, or RAM. I dont know…
in django docs, it mentions StreamingHttpResponse. Its the way to generate large csv files, but i cant figure it out how to use in tutor shell.
is there any way to do this? how can i improve my script?
thanks!