Changeset e4392cd in tailbone


Ignore:
Timestamp:
11/19/2022 05:44:09 PM (3 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
d4801f5
Parents:
163c6560
Message:

Allow disabling, or per-day scheduling, of problem reports

Location:
tailbone
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • tailbone/templates/reports/problems/view.mako

    r163c6560 re4392cd  
    6767  <script type="text/javascript">
    6868
     69    % if weekdays_data is not Undefined:
     70        ${form.component_studly}Data.weekdaysData = ${json.dumps(weekdays_data)|n}
     71    % endif
     72
    6973    ThisPageData.runReportShowDialog = false
    7074    ThisPageData.runReportSubmitting = false
  • tailbone/views/reports.py

    r163c6560 re4392cd  
    2727from __future__ import unicode_literals, absolute_import
    2828
     29import calendar
     30import json
    2931import re
    3032import datetime
     
    564566
    565567    creatable = False
    566     editable = False
    567568    deletable = False
    568569    filterable = False
     
    572573    labels = {
    573574        'system_key': "System",
     575        'days': "Schedule",
    574576    }
    575577
     
    581583    ]
    582584
    583     form_fields = [
    584         'system_key',
    585         'problem_title',
    586         'email_key',
    587         'email_recipients',
    588     ]
    589 
    590585    def __init__(self, request):
    591586        super(ProblemReportView, self).__init__(request)
    592587
    593588        app = self.get_rattail_app()
    594         self.handler = app.get_problem_report_handler()
     589        self.problem_handler = app.get_problem_report_handler()
     590        # TODO: deprecate / remove this
     591        self.handler = self.problem_handler
    595592
    596593    def normalize(self, report, keep_report=True):
    597         data = {
    598             'system_key': report.system_key,
    599             'problem_key': report.problem_key,
    600             'problem_title': report.problem_title,
    601             'email_key': self.handler.get_email_key(report),
    602         }
    603 
    604         app = self.get_rattail_app()
    605         handler = app.get_email_handler()
    606         email = handler.get_email(data['email_key'])
    607         data['email_recipients'] = email.get_recips('all')
    608 
     594        data = self.problem_handler.normalize_problem_report(
     595            report, include_schedule=True, include_recipients=True)
    609596        if keep_report:
    610597            data['_report'] = report
     
    654641        super(ProblemReportView, self).configure_form(f)
    655642
    656         f.set_renderer('email_key', self.render_email_key)
    657         f.set_renderer('email_recipients', self.render_email_recipients)
     643        # email_*
     644        if self.editing:
     645            f.remove('email_key',
     646                     'email_recipients')
     647        else:
     648            f.set_renderer('email_key', self.render_email_key)
     649            f.set_renderer('email_recipients', self.render_email_recipients)
     650
     651        # enabled
     652        f.set_type('enabled', 'boolean')
     653
     654        # days
     655        f.set_renderer('days', self.render_days)
     656        f.set_widget('days', DaysWidget())
     657        f.set_vuejs_field_converter('days', self.convert_vuejs_days)
     658        f.set_helptext('days', "NB. enabling a given day means you want the "
     659                       "report to be available that morning (assuming that "
     660                       "reports run overnight)")
     661
     662        # only allow edit of certain fields
     663        if self.editing:
     664            editable = ('enabled', 'days')
     665            for field in f:
     666                if field not in editable:
     667                    f.set_readonly(field)
     668
     669    def convert_vuejs_days(self, days):
     670        days = dict(days)
     671        for key in days:
     672            if days[key] is colander.null:
     673                days[key] = 'null'
     674        return days
    658675
    659676    def render_email_recipients(self, report_info, field):
     
    661678        return ', '.join(recips)
    662679
     680    def render_days(self, report_info, field):
     681        g = self.get_grid_factory()('days', [],
     682                                    columns=['weekday_name', 'enabled'],
     683                                    labels={'weekday_name': "Weekday"})
     684        return HTML.literal(g.render_buefy_table_element(data_prop='weekdaysData'))
     685
     686    def template_kwargs_view(self, **kwargs):
     687        kwargs = super(ProblemReportView, self).template_kwargs_view(**kwargs)
     688        report_info = kwargs['instance']
     689
     690        data = []
     691        for i in range(7):
     692            data.append({
     693                'weekday': i,
     694                'weekday_name': calendar.day_name[i],
     695                'enabled': "Yes" if report_info['day{}'.format(i)] else "No",
     696            })
     697        kwargs['weekdays_data'] = data
     698
     699        return kwargs
     700
     701    def save_edit_form(self, form):
     702        app = self.get_rattail_app()
     703        session = self.Session()
     704        data = form.validated
     705        report = self.get_instance()
     706        key = '{}.{}'.format(report['system_key'],
     707                             report['problem_key'])
     708
     709        app.save_setting(session, 'rattail.problems.{}.enabled'.format(key),
     710                         six.text_type(data['enabled']).lower())
     711
     712        for i in range(7):
     713            daykey = 'day{}'.format(i)
     714            app.save_setting(session, 'rattail.problems.{}.{}'.format(key, daykey),
     715                             six.text_type(data['days'][daykey]).lower())
     716
    663717    def execute_instance(self, report_info, user, progress=None, **kwargs):
    664718        report = report_info['_report']
    665         problems = self.handler.run_problem_report(report, progress=progress)
     719        problems = self.handler.run_problem_report(report, progress=progress,
     720                                                   force=True)
    666721        return "Report found {} problems".format(len(problems))
    667722
    668723
     724class ProblemReportDays(colander.MappingSchema):
     725
     726    day0 = colander.SchemaNode(colander.Boolean(),
     727                               title=calendar.day_abbr[0])
     728    day1 = colander.SchemaNode(colander.Boolean(),
     729                               title=calendar.day_abbr[1])
     730    day2 = colander.SchemaNode(colander.Boolean(),
     731                               title=calendar.day_abbr[2])
     732    day3 = colander.SchemaNode(colander.Boolean(),
     733                               title=calendar.day_abbr[3])
     734    day4 = colander.SchemaNode(colander.Boolean(),
     735                               title=calendar.day_abbr[4])
     736    day5 = colander.SchemaNode(colander.Boolean(),
     737                               title=calendar.day_abbr[5])
     738    day6 = colander.SchemaNode(colander.Boolean(),
     739                               title=calendar.day_abbr[6])
     740
     741
    669742class ProblemReportSchema(colander.MappingSchema):
    670743
    671     system_key = colander.SchemaNode(colander.String())
    672 
    673     problem_key = colander.SchemaNode(colander.String())
    674 
    675     problem_title = colander.SchemaNode(colander.String())
    676 
    677     email_key = colander.SchemaNode(colander.String())
     744    system_key = colander.SchemaNode(colander.String(),
     745                                     missing=colander.null)
     746
     747    problem_key = colander.SchemaNode(colander.String(),
     748                                     missing=colander.null)
     749
     750    problem_title = colander.SchemaNode(colander.String(),
     751                                     missing=colander.null)
     752
     753    description = colander.SchemaNode(colander.String(),
     754                                     missing=colander.null)
     755
     756    email_key = colander.SchemaNode(colander.String(),
     757                                     missing=colander.null)
     758
     759    email_recipients = colander.SchemaNode(colander.String(),
     760                                           missing=colander.null)
     761
     762    enabled = colander.SchemaNode(colander.Boolean())
     763
     764    days = ProblemReportDays()
     765
     766
     767class DaysWidget(dfwidget.Widget):
     768    template = 'problem_report_days'
     769
     770    def serialize(self, field, cstruct, **kw):
     771        if cstruct in (colander.null, None):
     772            cstruct = ""
     773        readonly = kw.get("readonly", self.readonly)
     774        template = self.template
     775        values = dict(kw)
     776        if 'day_labels' not in values:
     777            values['day_labels'] = self.get_day_labels()
     778        values = self.get_template_values(field, cstruct, values)
     779        return field.renderer(template, **values)
     780
     781    def get_day_labels(self):
     782        labels = {}
     783        for i in range(7):
     784            labels[i] = {'name': calendar.day_name[i],
     785                         'abbr': calendar.day_abbr[i]}
     786        return labels
     787
     788    def deserialize(self, field, pstruct):
     789        from deform.compat import string_types
     790        if pstruct is colander.null:
     791            return colander.null
     792        elif not isinstance(pstruct, string_types):
     793            raise colander.Invalid(field.schema, "Pstruct is not a string")
     794        pstruct = json.loads(pstruct)
     795        return pstruct
    678796
    679797
Note: See TracChangeset for help on using the changeset viewer.