add cronjob

This commit is contained in:
Frederik Jaeckel 2024-02-27 22:40:59 +01:00
parent 132fcbb0d5
commit 9f3e33d225
11 changed files with 339 additions and 168 deletions

View file

@ -7,11 +7,15 @@ from trytond.pool import Pool
from .ir import Rule from .ir import Rule
from .planner import ScheduledBooking from .planner import ScheduledBooking
from .cashbook import Cashbook from .cashbook import Cashbook
from .cron import Cron
from .nextrun import NextRun
def register(): def register():
Pool.register( Pool.register(
Rule, Rule,
ScheduledBooking, ScheduledBooking,
NextRun,
Cashbook, Cashbook,
Cron,
module='cashbook_planner', type_='model') module='cashbook_planner', type_='model')

18
cron.py Normal file
View file

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# This file is part of the cashbook-planner from m-ds for Tryton.
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.pool import PoolMeta
class Cron(metaclass=PoolMeta):
__name__ = 'ir.cron'
@classmethod
def __setup__(cls):
super(Cron, cls).__setup__()
cls.method.selection.append(
('cashbook.planner|cronjob', "Execute scheduled bookings"))
# end Cron

15
cron.xml Normal file
View file

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!-- This file is part of the cashbook-planner from m-ds for Tryton.
The COPYRIGHT file at the top level of this repository contains the
full copyright notices and license terms. -->
<tryton>
<data>
<record model="ir.cron" id="planner_cron">
<field name="method">cashbook.planner|cronjob</field>
<field name="interval_number" eval="1"/>
<field name="interval_type">days</field>
</record>
</data>
</tryton>

View file

@ -3,6 +3,14 @@ msgid ""
msgstr "Content-Type: text/plain; charset=utf-8\n" msgstr "Content-Type: text/plain; charset=utf-8\n"
###########
# ir.cron #
###########
msgctxt "selection:ir.cron,method:"
msgid "Execute scheduled bookings"
msgstr "geplante Buchungen ausführen"
############# #############
# res.group # # res.group #
############# #############
@ -182,6 +190,26 @@ msgctxt "help:cashbook.planner,setpos:"
msgid "For example, if you want to run the rule on the second Wednesday of the month, enter 2 here." msgid "For example, if you want to run the rule on the second Wednesday of the month, enter 2 here."
msgstr "Wenn Sie die Regel z.B. am zweiten Mittwoch im Monat ausführen möchten, tragen Sie hier 2 ein." msgstr "Wenn Sie die Regel z.B. am zweiten Mittwoch im Monat ausführen möchten, tragen Sie hier 2 ein."
msgctxt "field:cashbook.planner,nextrun:"
msgid "Next Execution Date"
msgstr "Nächster Ausführungstermin"
############################
# cashbook.planner.nextrun #
############################
msgctxt "model:cashbook.planner.nextrun,name:"
msgid "Next Execution Date"
msgstr "Nächster Ausführungstermin"
msgctxt "field:cashbook.planner.nextrun,planner:"
msgid "Planner"
msgstr "geplante Buchung"
msgctxt "field:cashbook.planner.nextrun,date:"
msgid "Date"
msgstr "Datum"
################# #################
# cashbook.book # # cashbook.book #

View file

@ -2,6 +2,10 @@
msgid "" msgid ""
msgstr "Content-Type: text/plain; charset=utf-8\n" msgstr "Content-Type: text/plain; charset=utf-8\n"
msgctxt "selection:ir.cron,method:"
msgid "Execute scheduled bookings"
msgstr "Execute scheduled bookings"
msgctxt "model:res.group,name:group_planner" msgctxt "model:res.group,name:group_planner"
msgid "Cashbook - Scheduled Bookings" msgid "Cashbook - Scheduled Bookings"
msgstr "Cashbook - Scheduled Bookings" msgstr "Cashbook - Scheduled Bookings"

30
nextrun.py Normal file
View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# This file is part of the cashbook-planner from m-ds for Tryton.
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from trytond.model import ModelSQL, ModelView, fields
from trytond.report import Report
class NextRun(ModelSQL, ModelView):
'Next Execution Date'
__name__ = 'cashbook.planner.nextrun'
planner = fields.Many2One(
string='Planner', required=True, ondelete='CASCADE',
model_name='cashbook.planner')
date = fields.Date(string='Date', required=True)
def get_rec_name(self, name):
""" get date for record name
Args:
name (string): name of field
Returns:
string: formatted date
"""
return Report.format_date(self.date) if self.date is not None else '-'
# end NextRun

View file

@ -84,6 +84,9 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
string='Next Dates', readonly=True, string='Next Dates', readonly=True,
help='the next 5 appointments based on the configured rule'), help='the next 5 appointments based on the configured rule'),
'on_change_with_nextdates') 'on_change_with_nextdates')
nextrun = fields.One2Many(
string='Next Execution Date', size=1, field='planner',
model_name='cashbook.planner.nextrun')
@classmethod @classmethod
def __setup__(cls): def __setup__(cls):
@ -277,4 +280,52 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
IrDate = Pool().get('ir.date') IrDate = Pool().get('ir.date')
return IrDate.today() return IrDate.today()
@classmethod
def update_next_occurence(cls, records):
""" compute date of next execution, create/update nextrun-record,
delete nextrun-record if scheduled booking is disabled
Args:
records (list): scheduled-booking records
"""
pool = Pool()
IrDate = pool.get('ir.date')
NextRun = pool.get('cashbook.planner.nextrun')
to_create = []
to_write = []
to_delete = []
for record in records:
if not record.active:
# delete nextrun-record if disabled
if record.nextrun:
to_delete.extend(record.nextrun)
elif record.active:
# get next-run date
next_date = record._compute_dates_by_rrule(
start_date=IrDate.today(), count=1)
if next_date:
next_date = next_date[0]
else:
continue
if not record.nextrun:
# add record if not exist
to_create.append({'planner': record.id, 'date': next_date})
else:
# update existing records
for nxrun in record.nextrun:
if nxrun.date != next_date:
to_write.extend([[nxrun], {'date': next_date}])
if to_create:
NextRun.create(to_create)
if to_delete:
NextRun.delete(to_delete)
if to_write:
NextRun.write(*to_write)
@classmethod
def cronjob(cls):
pass
# ens ScheduledBooking # ens ScheduledBooking

View file

@ -207,4 +207,19 @@ class PlannerTestCase(object):
'setpos': 5, 'monthday': None, 'weekday': '2', 'setpos': 5, 'monthday': None, 'weekday': '2',
'end_date': None}]) 'end_date': None}])
@with_transaction()
def test_planner_create_update_nextrun(self):
""" create job, check nextrun-record
"""
Planner = Pool().get('cashbook.planner')
job = self.prep_create_job()
self.assertEqual(
job._compute_dates_by_rrule(
count=1, start_date=date(2022, 5, 1)), [
date(2022, 5, 1)])
job.update_next_occurence([job])
self.assertEqual(len(job.nextrun), 1)
# end PlannerTestCase # end PlannerTestCase

View file

@ -8,4 +8,5 @@ xml:
group.xml group.xml
planner.xml planner.xml
cashbook.xml cashbook.xml
cron.xml
menu.xml menu.xml

View file

@ -35,6 +35,10 @@ full copyright notices and license terms. -->
<separator id="sep2" colspan="6" string="Result of the recurrence rule"/> <separator id="sep2" colspan="6" string="Result of the recurrence rule"/>
<field name="nextdates" colspan="6"/> <field name="nextdates" colspan="6"/>
<label name="nextrun"/>
<field name="nextrun"/>
<newline/>
<label name="description"/> <label name="description"/>
<newline/> <newline/>
<field name="description" colspan="6"/> <field name="description" colspan="6"/>

View file

@ -6,4 +6,5 @@ full copyright notices and license terms. -->
<field name="active"/> <field name="active"/>
<field name="name"/> <field name="name"/>
<field name="cashbook"/> <field name="cashbook"/>
<field name="nextrun"/>
</tree> </tree>