Projet

Général

Profil

Development #13805 » generate_formdata.py

Serghei Mihai, 31 octobre 2016 15:56

 
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2016  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 time
18
import random
19
from wcs.formdef import FormDef
20

    
21
import sys
22

    
23
def get_form_fields(formdef):
24
    data = {}
25
    for field in formdef.fields:
26
        if hasattr(field, 'items'):
27
            # pick a random value from choices
28
            data[field.id] = random.choice(field.items)
29
        else:
30
            value = field.varname or field.label
31
            data[field.id] = '%s %s' % (value, random.randint(1, 10))
32
    return data
33

    
34
def generate_formdata(form_slug, number=10):
35
    try:
36
        formdef = FormDef.get_by_urlname(form_slug)
37
    except KeyError:
38
        print >> sys.stderr, 'formdef does not exist'
39
        sys.exit(1)
40

    
41
    for i in xrange(int(number)):
42
        formdata = formdef.data_class()()
43
        formdata.data = get_form_fields(formdef)
44
        formdata.status = 'wf-new'
45
        formdata.receipt_time = time.localtime(time.time()-random.randint(0, 3600)*i)
46
        formdata.store()
47

    
48

    
49
if len(sys.argv) < 2:
50
    print >> sys.stderr, 'form slug must be provided in arguments'
51
    sys.exit(1)
52

    
53
generate_formdata(*sys.argv[1:])