346 |
346 |
347 if name not in schema: |
347 if name not in schema: |
348 if 'NEW_' + name in POST: |
348 if 'NEW_' + name in POST: |
349 # new Choice question, set generic type and get its index |
349 # new Choice question, set generic type and get its index |
350 schema[name] = {'type': 'choice'} |
350 schema[name] = {'type': 'choice'} |
351 schema[name]['index'] = int(POST['index_for_' + name]) |
|
352 |
351 |
353 if name in schema and schema[name]['type'] in CHOICE_TYPES: |
352 if name in schema and schema[name]['type'] in CHOICE_TYPES: |
354 # build an index:content dictionary |
353 # build an index:content dictionary |
355 if name in survey_fields: |
354 if name in survey_fields: |
356 if value not in survey_fields[name]: |
355 if value not in survey_fields[name]: |
357 survey_fields[name][int(number)] = value |
356 survey_fields[name][int(number)] = value |
358 else: |
357 else: |
359 survey_fields[name] = {int(number): value} |
358 survey_fields[name] = {int(number): value} |
360 |
359 |
361 elif key.startswith('survey__'): # new Text question |
360 elif key.startswith('survey__'): # Text question |
362 # this is super ugly but unless data is serialized the regex is needed |
361 # this is super ugly but unless data is serialized the regex is needed |
363 prefix = re.compile('survey__([0-9]{1,3})__') |
362 prefix = re.compile('survey__([0-9]{1,3})__') |
364 prefix_match = re.match(prefix, key) |
363 prefix_match = re.match(prefix, key) |
365 |
364 |
366 index = prefix_match.group(0).replace('survey', '').replace('__','') |
365 index = prefix_match.group(0).replace('survey', '').replace('__','') |
371 |
370 |
372 for ptype in PROPERTY_TYPES: |
371 for ptype in PROPERTY_TYPES: |
373 # should only match one |
372 # should only match one |
374 if ptype + "__" in field_name: |
373 if ptype + "__" in field_name: |
375 field_name = field_name.replace(ptype + "__", "") |
374 field_name = field_name.replace(ptype + "__", "") |
376 schema[field_name] = {} |
375 if field_name not in schema: |
|
376 schema[field_name]= {} |
377 schema[field_name]["index"] = index |
377 schema[field_name]["index"] = index |
378 schema[field_name]["type"] = ptype |
378 schema[field_name]["type"] = ptype |
379 |
379 |
|
380 # store text question tooltip from the input/textarea value |
|
381 schema[field_name]["tip"] = value |
|
382 |
|
383 # add the question as a dynamic property to survey_content |
380 survey_fields[field_name] = value |
384 survey_fields[field_name] = value |
381 |
385 |
382 def getSchemaOptions(self, schema, survey_fields, POST): |
386 def getSchemaOptions(self, schema, survey_fields, POST): |
383 """Get question, type, rendering and option order for choice questions. |
387 """Get question, type, rendering and option order for choice questions. |
384 """ |
388 """ |
395 render_for = 'render_for_' + key |
399 render_for = 'render_for_' + key |
396 if render_for in POST: |
400 if render_for in POST: |
397 schema[key]['render'] = RENDER[POST[render_for]] |
401 schema[key]['render'] = RENDER[POST[render_for]] |
398 schema[key]['type'] = RENDER_TYPES[POST[render_for]] |
402 schema[key]['type'] = RENDER_TYPES[POST[render_for]] |
399 |
403 |
|
404 # set the choice question's tooltip |
|
405 tip_for = 'tip_for_' + key |
|
406 schema[key]['tip'] = POST.get(tip_for) |
|
407 |
400 # handle reordering fields |
408 # handle reordering fields |
401 ordered = False |
409 ordered = False |
402 order = 'order_for_' + key |
410 order = 'order_for_' + key |
403 if order in POST and isinstance(survey_fields[key], dict): |
411 if order in POST and isinstance(survey_fields[key], dict): |
404 order = POST[order] |
412 order = POST[order] |
420 ordered = sorted(survey_fields[key].items()) |
428 ordered = sorted(survey_fields[key].items()) |
421 survey_fields[key] = [value for index, value in ordered] |
429 survey_fields[key] = [value for index, value in ordered] |
422 |
430 |
423 # set 'question' entry (free text label for question) in schema |
431 # set 'question' entry (free text label for question) in schema |
424 question_for = 'NEW_' + key |
432 question_for = 'NEW_' + key |
425 if question_for in POST: |
433 if question_for in POST and POST[question_for]: |
426 schema[key]["question"] = POST[question_for] |
434 schema[key]["question"] = POST[question_for] |
427 |
435 |
428 # set wheter the question is required |
436 # set wheter the question is required |
429 required_for = 'required_for_' + key |
437 required_for = 'required_for_' + key |
430 schema[key]['required'] = BOOL[POST[required_for]] |
438 schema[key]['required'] = BOOL[POST[required_for]] |
431 |
439 |
432 # set wheter the question allows comments |
440 # set wheter the question allows comments |
433 comment_for = 'comment_for_' + key |
441 comment_for = 'comment_for_' + key |
434 schema[key]['has_comment'] = BOOL[POST[comment_for]] |
442 schema[key]['has_comment'] = BOOL[POST[comment_for]] |
|
443 |
|
444 # set the question index from JS-calculated value |
|
445 index_for = 'index_for_' + key |
|
446 if index_for in POST: |
|
447 schema[key]['index'] = int(POST[index_for].replace('__', '')) |
435 |
448 |
436 def createGet(self, request, context, params, seed): |
449 def createGet(self, request, context, params, seed): |
437 """Pass the question types for the survey creation template. |
450 """Pass the question types for the survey creation template. |
438 """ |
451 """ |
439 |
452 |