From d1b80c75f4ae39d3296d734b679633c9e8020034 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 delete formdata (#13669) --- tests/test_formdata.py | 26 +++++++++++++++++++++++++ wcs/ctl/delete_formdata.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 wcs/ctl/delete_formdata.py diff --git a/tests/test_formdata.py b/tests/test_formdata.py index fcbdfd2..60072c4 100644 --- a/tests/test_formdata.py +++ b/tests/test_formdata.py @@ -3,6 +3,7 @@ import pytest import sys import shutil import time +import collections from quixote import cleanup from quixote.http_request import Upload @@ -549,3 +550,28 @@ def test_backoffice_field_varname(pub): formdata.data = {'bo1': 'test'} substvars = formdata.get_substitution_variables() assert substvars.get('form_var_backoffice_blah') == 'test' + + +def test_delete_formdata(pub): + from wcs.ctl.delete_formdata import DeleteFormdata + formdef = FormDef() + formdef.name = 'example' + formdef.fields = [fields.StringField(id='0', label='Your Name'), + fields.EmailField(id='1', label='Email')] + formdef.store() + formdef.data_class().wipe() + formdata = formdef.data_class()() + formdata.data = {'0': 'John Doe', '1': 'john@example.net'} + formdata.store() + + formdata2 = formdef.data_class()() + formdata2.data = {'0': 'Foo Bar', '1': 'foot@bar.net'} + formdata2.store() + assert formdef.data_class().count() == 2 + + delete_cmd = DeleteFormdata() + base_options = {} + sub_options_class = collections.namedtuple('Options', ['hostname']) + sub_options = sub_options_class(False) + delete_cmd.delete_formdata(pub, 'example.net') + assert formdef.data_class().count() == 0 diff --git a/wcs/ctl/delete_formdata.py b/wcs/ctl/delete_formdata.py new file mode 100644 index 0000000..255df27 --- /dev/null +++ b/wcs/ctl/delete_formdata.py @@ -0,0 +1,48 @@ +# 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 + +from qommon.ctl import Command, make_option + + +class DeleteFormdata(Command): + name = 'delete_formdata' + + def __init__(self): + Command.__init__(self, [ + make_option('--hostname', metavar='HOSTNAME', action='store', + dest='hostname'), + ]) + + def execute(self, base_options, sub_options, args): + import publisher + + publisher.WcsPublisher.configure(self.config) + publisher = publisher.WcsPublisher.create_publisher( + register_cron=False, register_tld_names=False) + publisher.app_dir = os.path.join(publisher.app_dir, ) + publisher.set_config() + self.delete_formdata(publisher, sub_options.hostname) + + def delete_formdata(self, pub, hostname): + from wcs.formdef import FormDef + app_dir = pub.app_dir + for formdef in FormDef.select(): + formdata = formdef.data_class() + formdata.wipe() + +DeleteFormdata.register() -- 2.10.1