Ignore:
Timestamp:
7 Mar 2017, 06:51:42 (8 years ago)
Author:
Henrik Bettermann
Message:

Transform helper functions into class methods to ease customization.

File:
1 edited

Legend:

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

    r14601 r14605  
    3333from waeup.kofa.students.reports.student_statistics import (
    3434    StudentStatisticsReportPDFView)
    35 
    36 
    37 class ISessionResultsPresentation(ILevelReport):
    38 
    39     """ Same interface as for level session results presentation.
    40     """
    41 
    42 def excluded(level_obj):
    43     """Some universities may add further conditions to exclude
    44     students from reports. These conditions can be customized in
    45     this function.
    46     """
    47     return False
    48 
    49 def get_students(faccode, depcode, certcode, session, level=None):
    50     """Get students in a certain department, studying a certain programmen
    51     who registered courses in a certain session at a certain level.
    52 
    53     Returns a list of lists of student data tuples.
    54     """
    55     site = grok.getSite()
    56     cat = queryUtility(ICatalog, name="students_catalog")
    57     if certcode == 'all':
    58         certcode = None
    59     result = cat.searchResults(
    60         depcode = (depcode, depcode), faccode = (faccode, faccode),
    61         certcode = (certcode, certcode)
    62         )
    63     students_utils = getUtility(IStudentsUtils)
    64     table = list()
    65     for i in range(len(students_utils.gpa_boundaries)+1):
    66         # The last list is reserved for students with more than one
    67         # level in the same session.
    68         table.append([])
    69     for stud in result:
    70         if stud.state == GRADUATED:
    71             continue
    72         line = (stud.student_id,
    73                 stud.matric_number,
    74                 stud.display_fullname,
    75                 )
    76         if level != 0:
    77             if not stud['studycourse'].has_key(str(level)):
    78                 continue
    79             level_obj = stud['studycourse'][str(level)]
    80             if level_obj.level_session != session:
    81                 continue
    82             if excluded(level_obj):
    83                 continue
    84         else:
    85             itemcount = 0
    86             for item in stud['studycourse'].values():
    87                 if item.level_session == session:
    88                     level_obj = item
    89                     itemcount += 1
    90             if itemcount == 0:
    91                 # No level registered in this session
    92                 continue
    93             if itemcount > 1:
    94                 # Error: more than one level registered in this session
    95                 table[len(students_utils.gpa_boundaries)].append(line)
    96                 continue
    97         gpaclass = students_utils.getDegreeClassNumber(level_obj)
    98         table[gpaclass].append(line)
    99         for i in range(len(students_utils.gpa_boundaries)+1):
    100             if len(table[i]):
    101                 table[i] = sorted([value for value in table[i]],
    102                     key=lambda value: value[2])
    103     return table
    104 
    10535from reportlab.lib import colors
    10636from reportlab.lib.styles import getSampleStyleSheet
     
    12757    ('ALIGN', (3,1), (-1,-1), 'RIGHT'),
    12858    ]
     59
     60class ISessionResultsPresentation(ILevelReport):
     61    """ Same interface as for level session results presentation.
     62    """
    12963
    13064@implementer(ISessionResultsPresentation)
     
    14175
    14276    note = None
     77
     78    def _excluded(self, level_obj):
     79        """Some universities may add further conditions to exclude
     80        students from reports. These conditions can be customized in
     81        this function.
     82        """
     83        return False
     84
     85    def _get_students(self, faccode, depcode, certcode, session, level=None):
     86        """Get students in a certain department, studying a certain programmen
     87        who registered courses in a certain session at a certain level.
     88
     89        Returns a list of lists of student data tuples.
     90        """
     91        site = grok.getSite()
     92        cat = queryUtility(ICatalog, name="students_catalog")
     93        if certcode == 'all':
     94            certcode = None
     95        result = cat.searchResults(
     96            depcode = (depcode, depcode), faccode = (faccode, faccode),
     97            certcode = (certcode, certcode)
     98            )
     99        students_utils = getUtility(IStudentsUtils)
     100        table = list()
     101        for i in range(len(students_utils.gpa_boundaries)+1):
     102            # The last list is reserved for students with more than one
     103            # level in the same session.
     104            table.append([])
     105        for stud in result:
     106            if stud.state == GRADUATED:
     107                continue
     108            line = (stud.student_id,
     109                    stud.matric_number,
     110                    stud.display_fullname,
     111                    )
     112            if level != 0:
     113                if not stud['studycourse'].has_key(str(level)):
     114                    continue
     115                level_obj = stud['studycourse'][str(level)]
     116                if level_obj.level_session != session:
     117                    continue
     118                if self._excluded(level_obj):
     119                    continue
     120            else:
     121                itemcount = 0
     122                for item in stud['studycourse'].values():
     123                    if item.level_session == session:
     124                        level_obj = item
     125                        itemcount += 1
     126                if itemcount == 0:
     127                    # No level registered in this session
     128                    continue
     129                if itemcount > 1:
     130                    # Error: more than one level registered in this session
     131                    table[len(students_utils.gpa_boundaries)].append(line)
     132                    continue
     133            gpaclass = students_utils.getDegreeClassNumber(level_obj)
     134            table[gpaclass].append(line)
     135            for i in range(len(students_utils.gpa_boundaries)+1):
     136                if len(table[i]):
     137                    table[i] = sorted([value for value in table[i]],
     138                        key=lambda value: value[2])
     139        return table
    143140
    144141    def __init__(self, faccode, depcode, certcode, session, level,
     
    180177        self.creation_dt_string = self.creation_dt.astimezone(
    181178            getUtility(IKofaUtils).tzinfo).strftime("%Y-%m-%d %H:%M:%S %Z")
    182         self.data = get_students(faccode, depcode, certcode, session, level)
     179        self.data = self._get_students(faccode, depcode, certcode, session, level)
    183180
    184181    def create_pdf(self, job_id):
Note: See TracChangeset for help on using the changeset viewer.