Ignore:
Timestamp:
8 Feb 2017, 07:38:30 (8 years ago)
Author:
Henrik Bettermann
Message:

Add entry_session and p_session filter to Student Payment Statistics generator.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students/reports
Files:
1 added
2 edited

Legend:

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

    r14438 r14513  
    3737
    3838
    39 def get_student_payment_stats(session, mode, level, breakdown):
     39def get_student_payment_stats(session, mode, level, entry_session,
     40        p_session, breakdown):
    4041    """Get payment amounts of students in a certain session, study mode
    4142    and current level.
     
    8283    cat = queryUtility(ICatalog, name="students_catalog")
    8384    result = cat.searchResults(current_session=(session, session))
    84     table = [[0 for x in xrange(2*len(payment_cats))] for y in xrange(len(codes)+1)]
     85    table = [[0 for x in xrange(2*len(payment_cats))]
     86                for y in xrange(len(codes)+1)]
    8587    mode_groups = getUtility(IKofaUtils).MODE_GROUPS
    8688    for stud in result:
    87         if mode != 'All' and stud.current_mode not in mode_groups[mode]:
     89        if mode != 'All' and stud.current_mode \
     90            and stud.current_mode not in mode_groups[mode]:
    8891            continue
    8992        if level != 0 and stud.current_level != level:
     93            continue
     94        if entry_session != 0 and  stud.entry_session != entry_session:
    9095            continue
    9196        if getattr(stud, breakdown) not in codes:
     
    95100        for key in stud['payments'].keys():
    96101            ticket = stud['payments'][key]
    97             if ticket.p_category not in payment_cats or\
    98                 ticket.p_state != 'paid' or\
    99                 ticket.p_session != session:
     102            if ticket.p_category not in payment_cats:
     103                continue
     104            if ticket.p_state != 'paid':
     105                continue
     106            if p_session != 0 and ticket.p_session != p_session:
    100107                continue
    101108            col = payment_cats.index(ticket.p_category)
     
    181188    session = None
    182189    mode = None
     190    entry_session = None
     191    p_session = None
    183192    pdfcreator = 'landscape'
    184193
    185     def __init__(self, session, mode, level, breakdown, author='System'):
     194    def __init__(self, session, mode, level, entry_session, p_session,
     195                 breakdown, author='System'):
    186196        super(StudentPaymentStatisticsReport, self).__init__(
    187             args=[session, mode, breakdown], kwargs={'author':author})
     197            args=[session, mode, level, entry_session, p_session, breakdown],
     198            kwargs={'author':author})
     199        self.sessioncode = session
     200        self.levelcode = level
     201        self.entrysessioncode = entry_session
     202        self.psessioncode = p_session
    188203        self.studylevelsource = StudyLevelSource().factory
    189204        self.portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
    190205        self.session = academic_sessions_vocab.getTerm(session).title
    191         self.mode = mode
     206        if entry_session != 0:
     207            self.entry_session = academic_sessions_vocab.getTerm(
     208                entry_session).title
     209        else:
     210            self.entry_session = 'All sessions'
     211        if p_session != 0:
     212            self.p_session = academic_sessions_vocab.getTerm(
     213                p_session).title
     214        else:
     215            self.p_session = 'All sessions'
     216        if mode == 'All':
     217            self.mode = 'All study modes'
     218        else:
     219            self.mode = mode
    192220        if level == 0:
    193             self.level = ''
    194         else:
    195             self.level = translate(self.studylevelsource.getTitle(None, int(level)),
     221            self.level = 'All levels'
     222        else:
     223            self.level = translate(
     224                self.studylevelsource.getTitle(None, int(level)),
    196225                'waeup.kofa', target_language=self.portal_language)
    197226        self.breakdown = breakdown
     
    199228        self.creation_dt_string = self.creation_dt.astimezone(
    200229            getUtility(IKofaUtils).tzinfo).strftime("%Y-%m-%d %H:%M:%S %Z")
    201         self.data = get_student_payment_stats(session, mode, level, breakdown)
     230        self.data = get_student_payment_stats(
     231            session, mode, level, entry_session, p_session, breakdown)
    202232
    203233    def create_pdf(self, job_id):
     
    209239                              STYLE["Normal"]),
    210240                    Spacer(1, 12),]
     241        pdf_data += [Paragraph(
     242                    translate(
     243                        'Study Mode: ${a}<br />'
     244                        'Academic Session: ${b}<br />Level: ${c}<br />'
     245                        'Entry Session: ${d}<br />Payment Session: ${e}',
     246                        mapping = {'a':self.mode,
     247                                   'b':self.session,
     248                                   'c':self.level,
     249                                   'd':self.entry_session,
     250                                   'e':self.p_session,
     251                                   }),
     252                    STYLE["Normal"]),
     253                    Spacer(1, 12),]
    211254        pdf_data += [
    212255            Table(table_data, style=TABLE_STYLE, colWidths=col_widths)]
    213         if not self.level:
    214             doc_title = translate(
    215                 _('${a} Student Payments - ${b}',
    216                 mapping = {'a':self.mode, 'b':self.session}))
    217             pdf = creator.create_pdf(
    218                 pdf_data, None, doc_title, self.author, doc_title + '-'
    219                 )
    220         else:
    221             doc_title = translate(
    222                 _('${a} Student Payments - ${b} - ${c}',
    223                 mapping = {'a':self.mode, 'b': self.level, 'c':self.session}))
    224             pdf = creator.create_pdf(
    225                 pdf_data, None, doc_title, self.author, doc_title + '-'
    226                 )
     256        doc_title = translate(_('Student Payment Statistics'))
     257        right_footer = translate(
     258            _('${a} Student Payments - ${b} -',
     259            mapping = {'a':self.mode, 'b':self.session}))
     260        pdf = creator.create_pdf(
     261            pdf_data, None, doc_title, self.author, right_footer
     262            )
    227263        return pdf
    228 
    229264
    230265@implementer(IReportGenerator)
     
    236271    def generate(
    237272        self, site, session=None, mode=None,
    238         level=None, breakdown=None, author=None):
     273        level=None, entry_session=None, p_session=None,
     274        breakdown=None, author=None):
    239275        result = StudentPaymentStatisticsReport(
    240276            session=session, mode=mode, level=level,
     277            entry_session=entry_session, p_session=p_session,
    241278            breakdown=breakdown, author=author)
    242279        return result
     
    261298
    262299    grok.context(StudentPaymentStatisticsReportGenerator)
    263     grok.template('studentstatisticsreportgeneratorpage')
     300    grok.template('studentpaymentstatisticsreportgeneratorpage')
    264301
    265302    label = _('Create student payment statistics report')
    266303
     304    def update(
     305        self, CREATE=None, session=None, mode=None, level=None,
     306        entry_session=None, p_session=None, breakdown=None):
     307        self.parent_url = self.url(self.context.__parent__)
     308        self._set_session_values()
     309        self._set_mode_values()
     310        self._set_level_values()
     311        self._set_breakdown_values()
     312        if CREATE and session:
     313            # create a new report job for students by session and level
     314            container = self.context.__parent__
     315            user_id = self.request.principal.id
     316            kw = dict(
     317                session=int(session),
     318                mode=mode,
     319                level=int(level),
     320                entry_session=int(entry_session),
     321                p_session=int(p_session),
     322                breakdown=breakdown)
     323            self.flash(_('New report is being created in background'))
     324            job_id = container.start_report_job(
     325                self.generator_name, user_id, kw=kw)
     326            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
     327            grok.getSite().logger.info(
     328                '%s - report %s created: %s (session=%s, mode=%s, level=%s, '
     329                'entry_session=%s, p_session=%s, breakdown=%s)' % (
     330                ob_class, job_id, self.context.title,
     331                session, mode, level, entry_session, p_session, breakdown))
     332            self.redirect(self.parent_url)
     333            return
     334        return
     335
     336    def _set_session_values(self):
     337        vocab_terms = academic_sessions_vocab.by_value.values()
     338        self.sessions = [(x.title, x.token) for x in vocab_terms]
     339        self.sessions_plus = [(u'All', 0)] + self.sessions
     340        return
    267341
    268342class StudentPaymentStatisticsReportPDFView(StudentStatisticsReportPDFView):
     
    271345
    272346    def _filename(self):
    273         return 'StudentPaymentStatisticsReport_%s_%s_%s_%s.pdf' % (
    274             self.context.session, self.context.mode, self.context.level,
     347        return 'StudentPaymentStatisticsReport_rno%s_%s.pdf' % (
     348            self.context.__name__,
    275349            self.context.creation_dt_string)
    276 
    277350
    278351class StudentStatsBreadcrumb(Breadcrumb):
  • main/waeup.kofa/trunk/src/waeup/kofa/students/reports/tests/test_student_payment_statistics.py

    r14373 r14513  
    2222        # ensure we fullfill interface contracts
    2323        obj = StudentPaymentStatisticsReport(
    24             2010, 'Undergraduate Full-Time', 0, 'faccode')
     24            2010, 'Undergraduate Full-Time', 0, 0, 0, 'faccode')
    2525        verifyClass(IStudentPaymentStatisticsReport, StudentPaymentStatisticsReport)
    2626        verifyObject(IStudentPaymentStatisticsReport, obj)
     
    3030        # we can get a table with one student
    3131        result1 = get_student_payment_stats(
    32             2010, 'Undergraduate Full-Time', 0, 'faccode')
     32            2010, 'Undergraduate Full-Time', 0, 0, 0, 'faccode')
    3333        result2 = get_student_payment_stats(
    34             2009, 'Undergraduate Full-Time', 0, 'faccode')
     34            2009, 'Undergraduate Full-Time', 0, 0, 0, 'faccode')
    3535        self.assertEqual(
    3636            result1,
     
    5555    def test_create_pdf(self):
    5656        report = StudentPaymentStatisticsReport(
    57             2010, 'Undergraduate Full-Time', 0, 'faccode')
     57            2010, 'Undergraduate Full-Time', 0, 0, 0, 'faccode')
    5858        result = report.create_pdf('JOB_ID')
    5959        self.assertTrue(result.startswith('%PDF-'))
     
    106106                         'application/pdf')
    107107        self.assertTrue(
    108             'filename="StudentPaymentStatisticsReport_2004_2005_All__' in
     108            'filename="StudentPaymentStatisticsReport_rno%s' % job_id in
    109109            self.browser.headers['content-disposition'])
    110110        self.assertEqual(len(self.app['reports'].running_report_jobs), 1)
    111         job_id = self.app['reports'].running_report_jobs[0][0]
    112111        # ... and discarded
    113112        self.browser.open('http://localhost/app/reports')
     
    120119        self.assertTrue(
    121120            'INFO - zope.mgr - students.reports.student_payment_statistics.StudentPaymentStatisticsReportGeneratorPage - '
    122             'report %s created: Student Payment Statistics (session=2004, mode=All, level=0, breakdown=depcode)'
     121            'report %s created: Student Payment Statistics '
     122            '(session=2004, mode=All, level=0, entry_session=0, '
     123            'p_session=0, breakdown=depcode)'
    123124            % job_id in logcontent
    124125            )
    125126        self.assertTrue(
    126127            'INFO - zope.mgr - students.reports.student_payment_statistics.StudentPaymentStatisticsReportPDFView - '
    127             'report %s downloaded: StudentPaymentStatisticsReport_2004_2005_All__'
    128             % job_id in logcontent
     128            'report %s downloaded: StudentPaymentStatisticsReport_rno%s_'
     129            % (job_id, job_id) in logcontent
    129130            )
    130131        self.assertTrue(
Note: See TracChangeset for help on using the changeset viewer.