Projet

Général

Profil

0001-ctl-add-command-to-wipe-formdata-13669.patch

Serghei Mihai (congés, retour 15/05), 26 octobre 2016 17:31

Télécharger (4,75 ko)

Voir les différences:

Subject: [PATCH] ctl: add command to wipe formdata (#13669)

 tests/test_ctl.py    | 49 ++++++++++++++++++++++++++++++++++++++++++
 wcs/ctl/wipe_data.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)
 create mode 100644 wcs/ctl/wipe_data.py
tests/test_ctl.py
1 1
import os
2 2
import pytest
3
import collections
3 4
from email.mime.text import MIMEText
4 5
from email.mime.multipart import MIMEMultipart
5 6

  
......
47 48
                      foobar@localhost
48 49
                      message text follows:''')
49 50
    assert CmdProcessBounce.get_bounce_addrs(msg) == ['foobar@localhost']
51

  
52
def test_wipe_formdata(pub):
53
    from wcs import formdef, fields
54
    from wcs.ctl.wipe_data import CmdWipeData
55

  
56
    form_1 = formdef.FormDef()
57
    form_1.name = 'example'
58
    form_1.fields = [fields.StringField(id='0', label='Your Name'),
59
                      fields.EmailField(id='1', label='Email')]
60
    form_1.store()
61
    form_1.data_class().wipe()
62
    formdata_1 = form_1.data_class()()
63

  
64
    formdata_1.data = {'0': 'John Doe', '1': 'john@example.net'}
65
    formdata_1.store()
66

  
67
    assert form_1.data_class().count() == 1
68

  
69
    form_2 = formdef.FormDef()
70
    form_2.name = 'example2'
71
    form_2.fields = [fields.StringField(id='0', label='First Name'),
72
                    fields.StringField(id='1', label='Last Name')]
73
    form_2.store()
74
    form_2.data_class().wipe()
75
    formdata_2 = form_2.data_class()()
76
    formdata_2.data = {'0': 'John', '1': 'Doe'}
77
    formdata_2.store()
78
    assert form_2.data_class().count() == 1
79

  
80
    wipe_cmd = CmdWipeData()
81
    sub_options_class = collections.namedtuple('Options', ['all'])
82
    sub_options = sub_options_class(False)
83

  
84
    # test with no options
85
    wipe_cmd.wipe(pub, sub_options, [])
86
    assert form_1.data_class().count() == 1
87
    assert form_2.data_class().count() == 1
88

  
89
    # wipe one form formdatas
90
    wipe_cmd.wipe(pub, sub_options, [form_1.url_name])
91
    assert form_1.data_class().count() == 0
92
    assert form_2.data_class().count() == 1
93

  
94
    # wipe all formdatas
95
    sub_options = sub_options_class(True)
96
    wipe_cmd.wipe(pub, sub_options, [])
97
    assert form_1.data_class().count() == 0
98
    assert form_2.data_class().count() == 0
wcs/ctl/wipe_data.py
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2010  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
import os
18
import sys
19

  
20
from qommon.ctl import Command, make_option
21

  
22
class CmdWipeData(Command):
23
    name = 'wipe-data'
24

  
25
    def __init__(self):
26
        Command.__init__(self, [
27
                make_option('--all', metavar='ALL', action='store_false',
28
                            dest='all'),
29
                make_option('--vhost', metavar='VHOST', action='store',
30
                            dest='vhost'),
31
                ])
32

  
33
    def execute(self, base_options, sub_options, args):
34
        if not sub_options.vhost:
35
            print >> sys.stderr, 'you must specify --vhost'
36
            sys.exit(1)
37

  
38
        import publisher
39
        publisher.WcsPublisher.configure(self.config)
40
        pub = publisher.WcsPublisher.create_publisher(
41
                register_cron=False, register_tld_names=False)
42
        pub.app_dir = os.path.join(pub.app_dir, sub_options.vhost)
43
        pub.set_config()
44
        self.wipe(pub, sub_options, args)
45

  
46
    def wipe(self, pub, options, args):
47
        from wcs.formdef import FormDef
48
        app_dir = pub.app_dir
49

  
50
        if options.all:
51
            formdefs = FormDef.select()
52
        elif args:
53
            formdefs = [FormDef.get_by_urlname(arg) for arg in args]
54
        else:
55
            formdefs = []
56

  
57
        for formdef in formdefs:
58
            formdef.data_class().wipe()
59

  
60
CmdWipeData.register()
0
-