Changeset 8414 for main


Ignore:
Timestamp:
10 May 2012, 22:06:17 (13 years ago)
Author:
uli
Message:

Reduce redundance a bit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/export.py

    r8411 r8414  
    3333                  'coursetickets', 'studentpayments')
    3434
    35 class StudentsExporter(grok.GlobalUtility, ExporterBase):
    36     """Exporter for Students.
    37     """
     35def get_students(site):
     36    """Get all students registered in catalog in `site`.
     37    """
     38    catalog = queryUtility(
     39        ICatalog, context=site, name='students_catalog', default=None)
     40    if catalog is None:
     41        return []
     42    students = catalog.searchResults(student_id=(None, None))
     43    return students
     44
     45def get_studycourses(students):
     46    """Get studycourses of `students`.
     47    """
     48    return [x.get('studycourse', None) for x in students
     49            if x is not None]
     50
     51def get_levels(students):
     52    """Get all studylevels of `students`.
     53    """
     54    levels = []
     55    for course in get_studycourses(students):
     56        for level in course.values():
     57            levels.append(level)
     58    return levels
     59
     60def get_tickets(students):
     61    """Get all course tickets of `students`.
     62    """
     63    tickets = []
     64    for level in get_levels(students):
     65        for ticket in level.values():
     66            tickets.append(ticket)
     67    return tickets
     68
     69def get_payments(students):
     70    """Get all payments of `students`.
     71    """
     72    payments = []
     73    for student in students:
     74        for payment in student.get('payments', {}).values():
     75            payments.append(payment)
     76    return payments
     77
     78
     79class StudentExporterBase(ExporterBase):
     80    """Exporter for students or related objects.
     81
     82    This is a baseclass.
     83    """
     84    grok.baseclass()
    3885    grok.implements(ICSVStudentExporter)
    3986    grok.provides(ICSVStudentExporter)
     87
     88    def export(self, values, filepath=None):
     89        """Export `values`, an iterable, as CSV file.
     90
     91        If `filepath` is ``None``, a raw string with CSV data is returned.
     92        """
     93        writer, outfile = self.get_csv_writer(filepath)
     94        for value in values:
     95            self.write_item(value, writer)
     96        return self.close_outfile(filepath, outfile)
     97
     98
     99class StudentsExporter(grok.GlobalUtility, StudentExporterBase):
     100    """Exporter for Students.
     101    """
    40102    grok.name('students')
    41103
     
    46108    title = _(u'Students')
    47109
    48     def export(self, students, filepath=None):
    49         """Export `students`, an iterable, as CSV file.
    50 
    51         If `filepath` is ``None``, a raw string with CSV data is returned.
    52         """
    53         writer, outfile = self.get_csv_writer(filepath)
    54         for student in students:
    55             self.write_item(student, writer)
    56         return self.close_outfile(filepath, outfile)
    57 
    58110    def export_all(self, site, filepath=None):
    59111        """Export students into filepath as CSV data.
     
    61113        If `filepath` is ``None``, a raw string with CSV data is returned.
    62114        """
    63         catalog = queryUtility(
    64             ICatalog, context=site, name='students_catalog', default=None)
    65         if catalog is None:
    66             return self.export([], filepath)
    67         students = catalog.searchResults(
    68             student_id=(None, None))
    69         return self.export(students, filepath)
     115        return self.export(get_students(site), filepath)
    70116
    71117    def export_student(self, student, filepath=None):
    72118        return self.export([student], filepath=filepath)
    73119
    74 class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase):
     120
     121class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
    75122    """Exporter for StudentStudyCourses.
    76123    """
    77     grok.implements(ICSVStudentExporter)
    78     grok.provides(ICSVStudentExporter)
    79124    grok.name('studentstudycourses')
    80125
     
    98143            value, name, context=context)
    99144
    100 
    101     def export(self, study_courses, filepath=None):
    102         """Export `study_courses`, an iterable, as CSV file.
    103 
    104         If `filepath` is ``None``, a raw string with CSV data is returned.
    105         """
    106         writer, outfile = self.get_csv_writer(filepath)
    107         for study_course in study_courses:
    108             self.write_item(study_course, writer)
    109         return self.close_outfile(filepath, outfile)
    110 
    111145    def export_all(self, site, filepath=None):
    112146        """Export study courses into filepath as CSV data.
     
    114148        If `filepath` is ``None``, a raw string with CSV data is returned.
    115149        """
    116         catalog = queryUtility(
    117             ICatalog, context=site, name='students_catalog', default=None)
    118         if catalog is None:
    119             return self.export([], filepath)
    120         students = catalog.searchResults(
    121             student_id=(None, None))
    122         study_courses = [x.get('studycourse', None) for x in students
    123                          if x is not None]
    124         return self.export(study_courses, filepath)
    125 
    126     def export_student(self, student, filepath=None):
    127         if 'studycourse' in student.keys():
    128             return self.export([student['studycourse']], filepath=filepath)
    129         return self.export([], filepath=filepath)
    130 
    131 
    132 class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase):
     150        return self.export(get_studycourses(get_students(site)), filepath)
     151
     152    def export_student(self, student, filepath=None):
     153        """Export studycourse of a single student object.
     154        """
     155        return self.export(get_studycourses([student]), filepath)
     156
     157
     158class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
    133159    """Exporter for StudentStudyLevels.
    134160    """
    135     grok.implements(ICSVStudentExporter)
    136     grok.provides(ICSVStudentExporter)
    137161    grok.name('studentstudylevels')
    138162
     
    163187            value, name, context=context)
    164188
    165     def export(self, study_levels, filepath=None):
    166         """Export `study_levels`, an iterable, as CSV file.
    167 
    168         If `filepath` is ``None``, a raw string with CSV data is returned.
    169         """
    170         writer, outfile = self.get_csv_writer(filepath)
    171         for study_level in study_levels:
    172             self.write_item(study_level, writer)
    173         return self.close_outfile(filepath, outfile)
    174 
    175189    def export_all(self, site, filepath=None):
    176190        """Export study levels into filepath as CSV data.
     
    178192        If `filepath` is ``None``, a raw string with CSV data is returned.
    179193        """
    180         catalog = queryUtility(
    181             ICatalog, context=site, name='students_catalog', default=None)
    182         if catalog is None:
    183             return self.export([], filepath)
    184         students = catalog.searchResults(
    185             student_id=(None, None))
    186         levels = []
    187         study_courses = [x.get('studycourse', None) for x in students
    188                          if x is not None]
    189         for course in study_courses:
    190             for level in course.values():
    191                 levels.append(level)
    192         return self.export(levels, filepath)
    193 
    194     def export_student(self, student, filepath=None):
    195         if 'studycourse' in student.keys():
    196             return self.export(student['studycourse'].values(),
    197                                filepath=filepath)
    198         return self.export([], filepath=filepath)
    199 
    200 class CourseTicketExporter(grok.GlobalUtility, ExporterBase):
     194        return self.export(get_levels(get_students(site)), filepath)
     195
     196    def export_student(self, student, filepath=None):
     197        return self.export(get_levels([student]), filepath)
     198
     199class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
    201200    """Exporter for CourseTickets.
    202201    """
    203     grok.implements(ICSVStudentExporter)
    204     grok.provides(ICSVStudentExporter)
    205202    grok.name('coursetickets')
    206203
     
    226223            value, name, context=context)
    227224
    228     def export(self, course_tickets, filepath=None):
    229         """Export `course_tickets`, an iterable, as CSV file.
    230 
    231         If `filepath` is ``None``, a raw string with CSV data is returned.
    232         """
    233         writer, outfile = self.get_csv_writer(filepath)
    234         for ticket in course_tickets:
    235             self.write_item(ticket, writer)
    236         return self.close_outfile(filepath, outfile)
    237 
    238225    def export_all(self, site, filepath=None):
    239226        """Export course tickets into filepath as CSV data.
     
    241228        If `filepath` is ``None``, a raw string with CSV data is returned.
    242229        """
    243         catalog = queryUtility(
    244             ICatalog, context=site, name='students_catalog', default=None)
    245         if catalog is None:
    246             return self.export([], filepath)
    247         students = catalog.searchResults(
    248             student_id=(None, None))
    249         levels = []
    250         study_courses = [x.get('studycourse', None) for x in students
    251                          if x is not None]
    252         for course in study_courses:
    253             for level in course.values():
    254                 levels.append(level)
    255 
    256         tickets = []
    257         for level in levels:
    258             for ticket in level.values():
    259                 tickets.append(ticket)
    260         return self.export(tickets, filepath)
    261 
    262     def export_student(self, student, filepath=None):
    263         if 'studycourse' not in student.keys():
    264             return self.export([], filepath=filepath)
    265         levels = student['studycourse'].values()
    266         tickets = []
    267         for level in levels:
    268             for ticket in level.values():
    269                 tickets.append(ticket)
    270         return self.export(tickets, filepath=filepath)
    271 
    272 class PaymentsExporter(grok.GlobalUtility, ExporterBase):
     230        return self.export(get_tickets(get_students(site)), filepath)
     231
     232    def export_student(self, student, filepath=None):
     233        return self.export(get_tickets([student]), filepath)
     234
     235
     236class PaymentsExporter(grok.GlobalUtility, StudentExporterBase):
    273237    """Exporter for OnlinePayment instances.
    274238    """
    275     grok.implements(ICSVStudentExporter)
    276     grok.provides(ICSVStudentExporter)
    277239    grok.name('studentpayments')
    278240
     
    296258            value, name, context=context)
    297259
    298     def export(self, payments, filepath=None):
    299         """Export `payments`, an iterable, as CSV file.
    300 
    301         If `filepath` is ``None``, a raw string with CSV data is returned.
    302         """
    303         writer, outfile = self.get_csv_writer(filepath)
    304         for payment in payments:
    305             self.write_item(payment, writer)
    306         return self.close_outfile(filepath, outfile)
    307 
    308260    def export_all(self, site, filepath=None):
    309261        """Export payments into filepath as CSV data.
     
    311263        If `filepath` is ``None``, a raw string with CSV data is returned.
    312264        """
    313         catalog = queryUtility(
    314             ICatalog, context=site, name='students_catalog', default=None)
    315         if catalog is None:
    316             return self.export([], filepath)
    317         students = catalog.searchResults(
    318             student_id=(None, None))
    319         payments = []
    320         for student in students:
    321             if not 'payments' in student.keys():
    322                 continue
    323             for payment in student['payments'].values():
    324                 payments.append(payment)
    325         return self.export(payments, filepath)
    326 
    327     def export_student(self, student, filepath=None):
    328         if not 'payments' in student.keys():
    329             return self.export([], filepath=filepath)
    330         return self.export(student['payments'].values(), filepath=filepath)
     265        return self.export(get_payments(get_students(site)), filepath)
     266
     267    def export_student(self, student, filepath=None):
     268        return self.export(get_payments([student]), filepath)
Note: See TracChangeset for help on using the changeset viewer.