Project

General

Profile

0001-misc-expire-old-drafts-8468.patch

Frédéric Péters, 01 November 2015 09:33 PM

Download (2.65 KB)

View differences:

Subject: [PATCH] misc: expire old drafts (#8468)

 tests/test_formdata.py | 25 +++++++++++++++++++++++++
 wcs/formdef.py         | 17 +++++++++++++++++
 2 files changed, 42 insertions(+)
tests/test_formdata.py
186 186
    pub.cfg['language'] = {'language': 'fr'}
187 187
    assert formdata2.get_substitution_variables()['form_field_date'] == '12/05/2015'
188 188
    pub.cfg['language'] = {'language': 'en'}
189

  
190
def test_clean_drafts(pub):
191
    formdef = FormDef()
192
    formdef.name = 'foo'
193
    formdef.fields = []
194
    formdef.store()
195
    formdef.data_class().wipe()
196

  
197
    d = formdef.data_class()()
198
    d.status = 'draft'
199
    d.receipt_time = time.localtime()
200
    d.store()
201
    d_id1 = d.id
202

  
203
    d = formdef.data_class()()
204
    d.status = 'draft'
205
    d.receipt_time = time.localtime(0) # epoch, 1970-01-01
206
    d.store()
207
    d_id2 = d.id
208

  
209
    assert formdef.data_class().count() == 2
210
    from wcs.formdef import clean_drafts
211
    clean_drafts(pub)
212
    assert formdef.data_class().count() == 1
213
    assert formdef.data_class().select()[0].id == d_id1
wcs/formdef.py
23 23
from qommon.storage import StorableObject
24 24
from quixote import get_request, get_publisher
25 25

  
26
from qommon.cron import CronJob
26 27
from qommon.form import *
27 28
from qommon.misc import simplify, get_as_datetime
28 29
from qommon import get_cfg
29 30
from qommon.substitution import Substitutions
31
from qommon.publisher import get_publisher_class
30 32

  
31 33
from formdata import FormData
32 34
from roles import logged_users_role
......
1087 1089
'''))
1088 1090

  
1089 1091
Substitutions.register('form_name', category=N_('Form'), comment=N_('Form Name'))
1092

  
1093

  
1094
def clean_drafts(publisher):
1095
    import wcs.qommon.storage as st
1096
    removal_date = datetime.date.today() - datetime.timedelta(days=100)
1097
    for formdef in FormDef.select():
1098
        for formdata in formdef.data_class().select(
1099
                [st.Equal('status', 'draft'),
1100
                 st.Less('receipt_time', removal_date.timetuple())]):
1101
            formdata.remove_self()
1102

  
1103
if get_publisher_class():
1104
    # once a month, look for drafts to remove
1105
    get_publisher_class().register_cronjob(CronJob(clean_drafts,
1106
        days=[2], hours=[0], minutes=[0]))
1090
-