From 9cd7cbab1cbff177c81bdb1b3d23034edcf0ec9a Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Mon, 24 Oct 2016 16:49:20 +0200 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 diff --git a/tests/test_ctl.py b/tests/test_ctl.py index adf2b4b..301cda5 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -1,5 +1,6 @@ import os import pytest +import collections from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart @@ -47,3 +48,51 @@ def test_get_bounce_addrs(): foobar@localhost message text follows:''') assert CmdProcessBounce.get_bounce_addrs(msg) == ['foobar@localhost'] + +def test_wipe_formdata(pub): + from wcs import formdef, fields + from wcs.ctl.wipe_data import CmdWipeData + + form_1 = formdef.FormDef() + form_1.name = 'example' + form_1.fields = [fields.StringField(id='0', label='Your Name'), + fields.EmailField(id='1', label='Email')] + form_1.store() + form_1.data_class().wipe() + formdata_1 = form_1.data_class()() + + formdata_1.data = {'0': 'John Doe', '1': 'john@example.net'} + formdata_1.store() + + assert form_1.data_class().count() == 1 + + form_2 = formdef.FormDef() + form_2.name = 'example2' + form_2.fields = [fields.StringField(id='0', label='First Name'), + fields.StringField(id='1', label='Last Name')] + form_2.store() + form_2.data_class().wipe() + formdata_2 = form_2.data_class()() + formdata_2.data = {'0': 'John', '1': 'Doe'} + formdata_2.store() + assert form_2.data_class().count() == 1 + + wipe_cmd = CmdWipeData() + sub_options_class = collections.namedtuple('Options', ['all']) + sub_options = sub_options_class(False) + + # test with no options + wipe_cmd.wipe(pub, sub_options, []) + assert form_1.data_class().count() == 1 + assert form_2.data_class().count() == 1 + + # wipe one form formdatas + wipe_cmd.wipe(pub, sub_options, [form_1.url_name]) + assert form_1.data_class().count() == 0 + assert form_2.data_class().count() == 1 + + # wipe all formdatas + sub_options = sub_options_class(True) + wipe_cmd.wipe(pub, sub_options, []) + assert form_1.data_class().count() == 0 + assert form_2.data_class().count() == 0 diff --git a/wcs/ctl/wipe_data.py b/wcs/ctl/wipe_data.py new file mode 100644 index 0000000..3675d55 --- /dev/null +++ b/wcs/ctl/wipe_data.py @@ -0,0 +1,60 @@ +# w.c.s. - web application for online forms +# Copyright (C) 2005-2010 Entr'ouvert +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +import os +import sys + +from qommon.ctl import Command, make_option + +class CmdWipeData(Command): + name = 'wipe-data' + + def __init__(self): + Command.__init__(self, [ + make_option('--all', metavar='ALL', action='store_false', + dest='all'), + make_option('--vhost', metavar='VHOST', action='store', + dest='vhost'), + ]) + + def execute(self, base_options, sub_options, args): + if not sub_options.vhost: + print >> sys.stderr, 'you must specificy --vhost' + sys.exit(1) + + import publisher + publisher.WcsPublisher.configure(self.config) + pub = publisher.WcsPublisher.create_publisher( + register_cron=False, register_tld_names=False) + pub.app_dir = os.path.join(pub.app_dir, sub_options.vhost) + pub.set_config() + self.wipe(pub, sub_options, args) + + def wipe(self, pub, options, args): + from wcs.formdef import FormDef + app_dir = pub.app_dir + + if options.all: + formdefs = FormDef.select() + elif args: + formdefs = [FormDef.get_by_urlname(arg) for arg in args] + else: + formdefs = [] + + for formdef in formdefs: + formdef.data_class().wipe() + +CmdWipeData.register() -- 2.10.1