Ignore:
Timestamp:
20 Sep 2016, 08:33:44 (8 years ago)
Author:
Henrik Bettermann
Message:

Add report purge button.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
4 edited

Legend:

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

    r13958 r14169  
    1919"""
    2020import grok
     21from datetime import datetime, timedelta
    2122from zope.i18n import translate
    2223from zope.component import getUtility, queryUtility
     
    2526from waeup.kofa.interfaces import IJobManager, IKofaUtils
    2627from waeup.kofa.interfaces import MessageFactory as _
    27 from waeup.kofa.browser.layout import KofaPage
     28from waeup.kofa.browser.layout import KofaPage, jsaction
    2829from waeup.kofa.utils.helpers import get_current_principal
    2930from waeup.kofa.reports import (
     
    4748        return self.url(self.context, '%s/pdf' % job_id)
    4849
    49     def update(self, job_id=None, DISCARD=None, DOWNLOAD=None):
     50    def update(self, job_id=None, DISCARD=None, DOWNLOAD=None, PURGE=None):
    5051        self.entries = []
     52        ob_class = self.__implemented__.__name__.replace('waeup.kofa.', '')
    5153        if job_id and DISCARD:
    5254            entry = self.context.report_entry_from_job_id(job_id)
    5355            self.context.delete_report_entry(entry)
    5456            self.flash('Report discarded: %s' % job_id)
    55             ob_class = self.__implemented__.__name__.replace(
    56                 'waeup.kofa.', '')
    5757            grok.getSite().logger.info(
    5858                '%s - report %s discarded' % (ob_class, job_id))
    5959        if not checkPermission('waeup.manageReports', self.context):
    60             user = get_current_principal()
    61             self.entries = self._generate_entries(user_id=user.id)
     60            user_id = get_current_principal().id
    6261        else:
    63             self.entries = self._generate_entries(user_id=None)
     62            user_id = None
     63        self.entries = self._generate_entries(user_id=user_id)
    6464        if job_id and DOWNLOAD:
    6565            self.redirect(self._report_url(job_id))
    6666            return
     67        if PURGE:
     68            counter = 0
     69            for entry in self.context.get_running_report_jobs(user_id=user_id):
     70                job_id, gen_name, user = entry
     71                job = getUtility(IJobManager).get(job_id)
     72                starttime = getattr(job, 'begin_after', None)
     73                delta = timedelta(weeks=4)
     74                tz = getUtility(IKofaUtils).tzinfo
     75                if datetime.now(tz) - delta > starttime:
     76                    self.context.delete_report_entry(entry)
     77                    counter += 1
     78                    grok.getSite().logger.info(
     79                        '%s - report %s purged' % (ob_class, job_id))
     80            self.flash('%s report(s) purged' % counter)
     81            self.redirect(self.url(self.context))
    6782        return
    6883
     
    87102            entries.append(new_entry)
    88103        return entries
    89 
    90104
    91105class ReportsContainerTraverser(grok.Traverser):
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/templates/reportscontainerpage.pt

    r11254 r14169  
    4747  </tbody>
    4848</table>
     49
    4950<form method="POST" i18n:domain="waeup.kofa">
    50   <a tal:attributes="href python:view.url(context, 'create')"
    51      class="btn btn-primary" i18n:translate="">
    52     Create new report
    53   </a>
     51  <div class="form-group">
     52    <a tal:attributes="href python:view.url(context, 'create')"
     53       class="btn btn-primary" i18n:translate="">
     54      Create new report
     55    </a>
     56    <input type="submit" class="btn btn-default"
     57      name="PURGE" value="Purge outdated reports"
     58      data-toggle="tooltip" title="All reports older than 4 weeks will be discarded!"
     59      onclick="return window.confirm('Are you really sure?')"/>
     60  </div>
    5461</form>
     62
     63
     64
  • main/waeup.kofa/trunk/src/waeup/kofa/students/reports/tests/test_student_statistics.py

    r12897 r14169  
    11# Tests for student related reports
    22import os
     3from datetime import datetime, timedelta
    34from zc.async.testing import wait_for_result
    45from zope.interface.verify import verifyClass, verifyObject
    56from zope.component import getUtility
    6 from waeup.kofa.interfaces import IJobManager
     7from waeup.kofa.interfaces import IJobManager, IKofaUtils
    78from waeup.kofa.students.reports.student_statistics import (
    89    get_student_stats, StudentStatisticsReport, IStudentStatisticsReport)
     
    207208            )
    208209        return
     210
     211    def test_report_purge(self):
     212        self.trigger_report_creation()
     213        job_id = self.wait_for_report_job_completed()
     214        self.browser.open('http://localhost/app/reports')
     215        self.browser.getControl("Purge").click()
     216        self.assertTrue('0 report(s) purged' in self.browser.contents)
     217        job_id, gen_name, user = self.app['reports'].get_running_report_jobs(user_id=None)[0]
     218        job = getUtility(IJobManager).get(job_id)
     219        tz = getUtility(IKofaUtils).tzinfo
     220        delta = timedelta(weeks=5)
     221        setattr(job, '_begin_after', datetime.now(tz) - delta)
     222        self.browser.getControl("Purge").click()
     223        self.assertTrue('1 report(s) purged' in self.browser.contents)
     224        logfile = os.path.join(
     225            self.app['datacenter'].storage, 'logs', 'main.log')
     226        logcontent = open(logfile).read()
     227        self.assertTrue(
     228            'INFO - zope.mgr - browser.reports.ReportsContainerPage - '
     229            'report %s purged' % job_id in logcontent
     230            )
     231        return
  • main/waeup.kofa/trunk/src/waeup/kofa/students/student.py

    r13224 r14169  
    3737    IObjectHistory, IUserAccount, IFileStoreNameChooser, IFileStoreHandler,
    3838    IKofaUtils, registration_states_vocab, IExtFileStore,
    39     CREATED, ADMITTED, CLEARANCE, PAID, REGISTERED, VALIDATED, RETURNING)
     39    CREATED, ADMITTED, CLEARANCE, PAID, REGISTERED, VALIDATED, RETURNING,
     40    GRADUATED)
    4041from waeup.kofa.students.accommodation import StudentAccommodation
    4142from waeup.kofa.students.interfaces import (
     
    218219    @property
    219220    def before_payment(self):
    220         non_fresh_states = (PAID, REGISTERED, VALIDATED, RETURNING, )
     221        non_fresh_states = (PAID, REGISTERED, VALIDATED, RETURNING, GRADUATED)
    221222        if self.is_fresh and self.state not in non_fresh_states:
    222223            return True
Note: See TracChangeset for help on using the changeset viewer.