24 |
24 |
import qommon.misc
|
25 |
25 |
from qommon.substitution import Substitutions
|
26 |
26 |
|
|
27 |
from qommon.form import PicklableUpload
|
27 |
28 |
|
28 |
|
def get_dict_with_varnames(fields, data):
|
|
29 |
def get_dict_with_varnames(fields, data, convert_value_to_str=True):
|
29 |
30 |
new_data = {}
|
30 |
31 |
for field in fields:
|
31 |
32 |
if not hasattr(field, str('get_view_value')):
|
32 |
33 |
continue
|
33 |
34 |
if data is not None:
|
34 |
35 |
raw_value = data.get(field.id)
|
35 |
|
if field.convert_value_to_str:
|
|
36 |
if convert_value_to_str and field.convert_value_to_str:
|
36 |
37 |
raw_value = field.convert_value_to_str(raw_value)
|
37 |
38 |
display_value = data.get('%s_display' % field.id)
|
38 |
39 |
else:
|
... | ... | |
62 |
63 |
del d[k]
|
63 |
64 |
|
64 |
65 |
|
|
66 |
class JSONEncoder(json.JSONEncoder):
|
|
67 |
def default(self, obj):
|
|
68 |
if isinstance(obj, time.struct_time):
|
|
69 |
return time.strftime('%Y-%m-%dT%H:%M:%SZ', obj)
|
|
70 |
if isinstance(obj, PicklableUpload):
|
|
71 |
return obj.base_filename
|
|
72 |
# Let the base class default method raise the TypeError
|
|
73 |
return json.JSONEncoder.default(self, obj)
|
|
74 |
|
|
75 |
|
65 |
76 |
class Evolution:
|
66 |
77 |
who = None
|
67 |
78 |
status = None
|
... | ... | |
401 |
412 |
|
402 |
413 |
def export_to_json(self):
|
403 |
414 |
data = {}
|
404 |
|
|
405 |
415 |
data['id'] = '%s/%s' % (self.formdef.url_name, self.id)
|
406 |
416 |
data['display_id'] = self.get_display_id()
|
407 |
|
|
408 |
|
if self.receipt_time is not None:
|
409 |
|
data['receipt_time'] = qommon.strftime.strftime('%Y-%m-%dT%H:%M:%S', self.receipt_time)
|
|
417 |
data['receipt_time'] = self.receipt_time
|
410 |
418 |
|
411 |
419 |
try:
|
412 |
420 |
user = get_publisher().user_class.get(self.user_id)
|
413 |
421 |
except KeyError:
|
414 |
422 |
user = None
|
415 |
|
|
416 |
423 |
# this is custom code so it is possible to mark forms as anonyms, this
|
417 |
424 |
# is done through the VoteAnonymity field, this is very specific but
|
418 |
425 |
# isn't generalised yet into an useful extension mechanism, as it's not
|
... | ... | |
421 |
428 |
if f.key == 'vote-anonymity':
|
422 |
429 |
user = None
|
423 |
430 |
break
|
424 |
|
|
425 |
431 |
if user:
|
426 |
432 |
data['user'] = {'id': user.id, 'name': user.display_name}
|
427 |
433 |
|
... | ... | |
435 |
441 |
|
436 |
442 |
data['fields'] = {}
|
437 |
443 |
for f in self.formdef.fields:
|
438 |
|
if not f.varname:
|
|
444 |
if not f.varname: # exports only named fields
|
439 |
445 |
continue
|
440 |
|
if not self.data.has_key(f.id):
|
441 |
|
continue
|
442 |
|
|
443 |
|
value = self.data[f.id]
|
444 |
|
|
445 |
|
if value is None:
|
446 |
|
value = ''
|
447 |
|
|
448 |
|
if not hasattr(f, str('get_view_value')):
|
449 |
|
continue
|
450 |
|
|
451 |
|
if type(value) is str:
|
452 |
|
value = unicode(value, get_publisher().site_charset)
|
453 |
|
elif type(value) in (unicode, bool, int, type(None), list, tuple, dict):
|
454 |
|
pass
|
|
446 |
if f.store_display_value:
|
|
447 |
data['fields'][f.varname + '_raw'] = self.data.get(f.id)
|
|
448 |
data['fields'][f.varname] = self.data.get('%s_display' % f.id)
|
455 |
449 |
else:
|
456 |
|
value = f.get_view_value(value)
|
457 |
|
data['fields'][f.varname] = value
|
458 |
|
if f.store_display_value and ('%s_display' % f.id) in self.data:
|
459 |
|
data['fields'][f.varname + '_raw'] = data['fields'][f.varname]
|
460 |
|
display_value = self.data['%s_display' % f.id]
|
461 |
|
if display_value is not None:
|
462 |
|
if type(display_value) is str:
|
463 |
|
display_value = unicode(display_value, get_publisher().site_charset)
|
464 |
|
data['fields'][f.varname] = unicode(display_value)
|
|
450 |
data['fields'][f.varname] = self.data.get(f.id)
|
465 |
451 |
|
466 |
452 |
data['workflow'] = {}
|
467 |
453 |
wf_status = self.get_visible_status()
|
... | ... | |
470 |
456 |
if self.workflow_data:
|
471 |
457 |
data['workflow']['data'] = self.workflow_data
|
472 |
458 |
|
473 |
|
return json.dumps(data)
|
|
459 |
return json.dumps(data, cls=JSONEncoder)
|
474 |
460 |
|
475 |
461 |
|
476 |
462 |
# don't pickle _formdef cache
|