82
83
response.setHeader('Content-Type', 'application/vnd.ms-excel')
83
84
response.setHeader('Content-Length', len(data))
85
def listIds(self, header, items, ws, offset, last=False):
86
def listRelationships(self, header, relationships, ws, offset, last=False):
87
items = sorted(relationships.all(), key=lambda i: i.__name__)
88
90
self.write_header(ws, offset + 1, 0, header, merge=1)
89
for n, item in enumerate(sorted(items, key=lambda i: i.__name__)):
91
for n, item in enumerate(items):
90
92
self.write(ws, offset + 2 + n, 0, item.__name__)
91
93
self.write(ws, offset + 2 + n, 1, "")
94
state = relationships.state(item)
95
for x, (date, meaning, code) in enumerate(state):
96
self.write(ws, offset + 2 + n, 2+x*2,
98
self.write(ws, offset + 2 + n, 2+x*2+1, code)
92
99
return 1 + offset + len(items)
94
101
def skipRow(self, ws, offset):
290
303
overall_line_id = 'overall'
292
def print_table(self, table, ws):
293
for x, row in enumerate(table):
294
for y, cell in enumerate(row):
295
self.write(ws, x, y, cell.data, **cell.style)
305
def print_table(self, table, ws, row=0, col=0):
306
for y, cells in enumerate(table):
307
self.print_row(cells, ws, row=(row+y), col=col)
296
308
return len(table)
298
def format_table(self, fields, items, importer=None, major_progress=()):
310
def print_row(self, cells, ws, row=0, col=0):
311
for x, cell in enumerate(cells):
312
self.write(ws, row, col+x, cell.data, **cell.style)
314
def format_table(self, fields, items, importer=None, major_progress=(),
299
316
headers = [Header(header)
300
317
for header, style, getter in fields]
505
523
return self.format_table(fields, items, importer='export_contacts',
506
524
major_progress=(0,2))
526
def format_person_contacts(self, person, major_progress=()):
527
contacts = IContactable(person).contacts
529
for contact in contacts.all():
531
row.append(Text(person.username))
532
target_person = IBasicPerson(contact.__parent__, None)
533
if target_person is None:
534
row.append(Text(contact.__name__))
536
row.append(Text(target_person.username))
538
state = contacts.state(contact)
539
for x, (date, meaning, code) in enumerate(state):
540
row.append(Date(date))
541
row.append(Text(code))
508
545
def format_contact_relationships(self):
509
fields = [('Person ID', Text, attrgetter('person')),
510
('Contact ID', Text, attrgetter('contact')),
511
('Relationship', Text, attrgetter('relationship'))]
514
for person in self.context['persons'].values():
547
persons = self.context['persons']
549
for nperson, person in enumerate(persons.values()):
515
550
person = removeSecurityProxy(person)
516
for contact in IContactable(person).contacts:
518
links = IRelationshipLinks(person)
520
URIPerson, contact, URIContact, URIContactRelationship)
523
target_person = IBasicPerson(contact.__parent__, None)
524
if target_person is None:
525
name = contact.__name__
527
name = target_person.username
528
item = ContactRelationship(person.username,
529
name, link.extra_info.relationship)
532
return self.format_table(fields, items, importer='export_contacts',
533
major_progress=(1,2))
551
person_rows = self.format_person_contacts(
552
person, major_progress=(1, 2, nperson, total))
553
rows.extend(person_rows)
556
headers = [Header('Person ID'), Header('Contact ID'), Header('Relationship')]
557
rows.insert(0, headers)
535
561
def export_contacts(self, wb):
536
562
self.task_progress.force('export_contacts', active=True)
553
579
self.print_table(self.format_resources(), ws)
554
580
self.finish('export_resources')
582
def format_levels(self):
583
fields = [('ID', Text, attrgetter('__name__')),
584
('Title', Text, attrgetter('title'))]
585
levels = ILevelContainer(self.context)
586
items = levels.values()
587
return self.format_table(fields, items, importer='export_levels',
590
def export_levels(self, wb):
591
self.task_progress.force('export_levels', active=True)
592
ws = wb.add_sheet("Grade Levels")
593
self.print_table(self.format_levels(), ws)
594
self.finish('export_levels')
556
596
def format_courses(self):
597
def get_course_level(course):
598
return ', '.join([l.__name__ for l in course.levels])
557
599
fields = [('School Year', Text, lambda c: ISchoolYear(c).__name__),
558
600
('ID', Text, attrgetter('__name__')),
559
601
('Title', Text, attrgetter('title')),
560
602
('Description', Text, attrgetter('description')),
561
603
('Local ID', Text, attrgetter('course_id')),
562
604
('Government ID', Text, attrgetter('government_id')),
563
('Credits', Text, attrgetter('credits'))]
605
('Credits', Text, attrgetter('credits')),
606
('Grade Level ID', Text, get_course_level)]
565
608
school_years = ISchoolYearContainer(self.context).values()
618
660
self.write(ws, row, 6, section.title)
619
661
if section.description:
620
662
self.write(ws, row, 7, section.description)
621
self.write(ws, row, 8, ', '.join(teachers))
622
self.write(ws, row, 9, ', '.join(resources))
663
self.write(ws, row, 8, ', '.join(resources))
624
665
def export_sections(self, wb):
625
666
self.task_progress.force('export_sections', active=True)
626
667
ws = wb.add_sheet("Sections")
627
668
headers = ["School Year", "Courses", "Term", "Section ID",
628
669
"Previous ID", "Next ID", "Title", "Description",
629
"Instructors", "Resources"]
630
671
for index, header in enumerate(headers):
631
672
self.write_header(ws, 0, index, header)
651
692
self.finish('export_sections')
653
def format_student_sections(self, year, student_sections, ws, row):
654
headers = ["School Year", "Term", "Section ID"]
655
for index, header in enumerate(headers):
656
self.write_header(ws, row, index, header)
659
for first, term, section_id, section in sorted(student_sections):
660
self.write(ws, row, 0, year.__name__)
661
self.write(ws, row, 1, term.__name__)
662
self.write(ws, row, 2, section.__name__)
666
def format_students_block(self, students, ws, row):
667
self.write_header(ws, row, 0, "Students")
670
for student in students.split(','):
671
self.write(ws, row, 0, student)
675
def export_section_enrollment(self, wb):
676
self.task_progress.force('export_section_enrollment', active=True)
694
def format_membership_block(self, relationship, headers):
695
items = sorted(relationship.all(),
696
key=lambda item: item.__name__)
701
cells = [Text(item.__name__), Text('')]
702
state = relationship.state(item)
703
for x, (date, meaning, code) in enumerate(state):
704
cells.append(Date(date))
705
cells.append(Text(code))
710
def export_section_enrollment(self, ws, year, term, section, row=0):
711
row += self.print_table([
712
[Header('School Year'), Header('Term'), Header('Section ID')],
713
[Text(year.__name__), Text(term.__name__), Text(section.__name__)],
717
row += self.print_table(
718
self.format_membership_block(section.instructors, [Header('Instructors')]),
721
row += self.print_table(
722
self.format_membership_block(section.members, [Header('Students')]),
727
def export_sections_enrollment(self, wb):
728
self.task_progress.force('export_sections_enrollment', active=True)
677
729
ws = wb.add_sheet("SectionEnrollment")
680
732
years = ISchoolYearContainer(self.context)
681
for ny, year in enumerate(years.values()):
682
sections = year_sections[year] = {}
683
for nt, term in enumerate(year.values()):
684
term_sections = ISectionContainer(term)
685
for ns, section in enumerate(term_sections.values()):
686
if not list(section.courses):
688
student_ids = [s.username for s in section.members]
689
students = ','.join(sorted(student_ids))
690
student_sections = sections.setdefault(students, [])
691
student_sections.append((term.first, term, section.__name__,
693
self.progress('export_section_enrollment', normalized_progress(
696
ns, len(term_sections),
733
total_years = len(years)
734
for ny, year in enumerate(sorted(years.values(), key=lambda year: year.first)):
735
total_terms = len(year)
736
for nt, term in enumerate(sorted(year.values(), key=lambda term: term.first)):
737
sections = ISectionContainer(term)
738
total_sections = len(sections)
739
for ns, section in enumerate(sorted(sections.values(),
740
key=lambda section: section.__name__)):
741
row = self.export_section_enrollment(
742
ws, year, term, section, row=row)
744
'export_sections_enrollment',
700
for ny, (year, sections) in enumerate(sorted(year_sections.items())):
701
for ns, (students, student_sections) in enumerate(sorted(sections.items())):
702
row = self.format_student_sections(year, student_sections, ws,
704
row = self.format_students_block(students, ws, row)
705
self.progress('export_section_enrollment', normalized_progress(
706
ny, len(year_sections),
709
self.finish('export_section_enrollment')
751
self.finish('export_sections_enrollment')
711
753
def format_timetable_sections(self, year, timetable_sections, ws, row):
712
754
headers = ["School Year", "Term", "Section ID"]
794
836
lambda i: ("Description", i.description, None)]
796
838
offset = self.listFields(group, fields, ws, offset)
797
offset = self.listIds("Members", group.members, ws, offset) + 1
798
offset = self.listIds("Leaders", IAsset(group).leaders, ws, offset,
840
offset += self.print_table(
841
self.format_membership_block(group.members, [Header('Members')]),
842
ws, row=offset, col=0)
846
leaders = IAsset(group).leaders
847
offset += self.print_table(
848
self.format_membership_block(leaders, [Header('Leaders')]),
849
ws, row=offset, col=0)
802
853
def export_groups(self, wb):