Survey mode validation when launching job templates

This commit is contained in:
Matthew Jones 2014-09-09 15:08:49 -04:00
parent 514cd4a1db
commit 75cca21525
2 changed files with 66 additions and 2 deletions

View File

@ -1347,12 +1347,22 @@ class JobTemplateLaunch(GenericAPIView):
data['can_start_without_user_input'] = obj.can_start_without_user_input()
data['passwords_needed_to_start'] = obj.passwords_needed_to_start
data['ask_variables_on_launch'] = obj.ask_variables_on_launch
data['variables_needed_to_start'] = obj.variables_needed_to_start
return Response(data)
def post(self, request, *args, **kwargs):
obj = self.get_object()
if not request.user.can_access(self.model, 'start', obj):
raise PermissionDenied()
if obj.survey_enabled:
if request.DATA == "":
request_data = {}
else:
request_data = request.DATA
validation_errors = obj.survey_variable_validation(request_data)
if validation_errors:
return Response(dict(errors=validation_errors),
status=status.HTTP_400_BAD_REQUEST)
new_job = obj.create_unified_job()
result = new_job.signal_start(**request.DATA)
if not result:
@ -1385,9 +1395,10 @@ class JobTemplateSurveySpec(GenericAPIView):
def post(self, request, *args, **kwargs):
obj = self.get_object()
if not request.user.can_access(self.model, 'change', obj):
if not request.user.can_access(self.model, 'change', obj, request.DATA):
raise PermissionDenied()
print request.DATA
obj.survey_spec = json.dumps(request.DATA)
obj.save()
return Response()
class JobTemplateActivityStreamList(SubListAPIView):

View File

@ -211,6 +211,59 @@ class JobTemplate(UnifiedJobTemplate, JobOptions):
needed.append(pw)
return needed
@property
def variables_needed_to_start(self):
vars = []
if self.survey_enabled:
for survey_element in self.survey_spec:
if survey_element['required']:
vars.append(survey_element['variable'])
return vars
def survey_variable_validation(self, data):
errors = []
if not self.survey_enabled:
return errors
for survey_element in self.survey_spec:
if survey_element['variable'] not in data and \
survey_element['required']:
errors.append("'%s' value missing" % survey_element['variable'])
elif survey_element['type'] in ["textarea", "text"]:
if survey_element['min'] != "" and survey_element['variable'] in data and \
len(data[survey_element['variable']]) < survey_element['min']:
errors.append("'%s' value %s is too small (must be at least %s)" %
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
if survey_element['max'] != "" and survey_element['variable'] in data and \
len(data[survey_element['variable']]) > survey_element['max']:
errors.append("'%s' value %s is too large (must be no more than%s)" %
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
elif survey_element['type'] == 'integer':
if survey_element['min'] != "" and survey_element['variable'] in data and \
data[survey_element['variable']] < survey_element['min']:
errors.append("'%s' value %s is too small (must be at least %s)" %
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
if survey_element['max'] != "" and survey_element['variable'] in data and \
len(data[survey_element['variable']]) > survey_element['max']:
errors.append("'%s' value %s is too large (must be no more than%s)" %
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
elif survey_element['type'] == 'multiselect':
if survey_element['variable'] in data:
if type(data[survey_element['variable']]) != list:
errors.append("'%s' value is expected to be a list" % survey_element['variable'])
else:
for val in data[survey_element['variable']]:
if val not in survey_element['choices']:
errors.append("Value %s for %s expected to be one of %s" % (val, survey_element['variable'],
survey_element['choices']))
elif survey_element['type'] == 'multiplechoice':
if survey_element['variable'] in data:
if data[survey_element['variable']] not in survey_element['choices']:
errors.append("Value %s for %s expected to be one of %s" % (data[survey_element['variable']],
survey_element['variable'],
survey_element['choices']))
return errors
def _can_update(self):
return self.can_start_without_user_input()