def validate_draft(cls, draftstring, configstring): draft = json.loads(draftstring) config = json.loads(configstring) cls.validate_dict(draft, DraftValidationError, { 'values': list, 'feedback': basestring }) cls.validate_gradeeditor_key(draft, 'commentform') gradeeditor = draft['gradeeditor'] draftval = draft['values'] confval = config['formValues'] for i in xrange(0, len(draftval)): if confval[i][0] == 'check': if not isinstance(draftval[i], bool): errormsg = 'the field labled "' + confval[i][ 3] + '" has to contain a boolean-value' raise ConfigValidationError(errormsg) elif confval[i][0] == 'number': if not isinstance(draftval[i], int): errormsg = 'the field labled "' + confval[i][ 3] + '" has to contain a number 0 or higher' raise ConfigValidationError(errormsg)
def validate_config(cls, configstring): config = cls.decode_configstring(configstring) form = config['formValues'] approvedLimit = config['approvedLimit'] if len(form) == 0: raise ConfigValidationError( 'You have to specify at least one form-field') pointSum = 0 for entry in form: if len(entry) != 4: raise ConfigValidationError( 'You have to specify fieldtype, points, default and label. For text and label fields you may skip these by typing ::' ) if not isinstance(entry[0], basestring): raise ConfigValidationError( 'You have to specify fieldtype as either "number" or "check"' ) if entry[0] != 'number' and entry[0] != 'check' and entry[ 0] != 'text' and entry[0] != 'label': raise ConfigValidationError( 'You have to specify fieldtype as either "number", "text", "label" or "check"' ) if entry[1] == '' and entry[0] != 'text' and entry[0] != 'label': raise ConfigValidationError( 'You have to enter points as a number 0 or higher') #if int(entry[1])<0: # raise ConfigValidationError('You have to enter points as a number 0 or higher') if not isinstance(entry[2], basestring): raise ConfigValidationError( 'You have to enter the field-label as plain text') if entry[2] == '' and entry[0] != 'text' and entry[0] != 'label': raise ConfigValidationError( 'You have to enter a default value') if entry[3] == '': raise ConfigValidationError('You have to enter a field-label') if entry[0] != 'text' and entry[0] != 'label': pointSum += int(entry[1]) if not isinstance(approvedLimit, int): raise ConfigValidationError( 'You have to enter points to pass as a number 0 or higher') #if approvedLimit < 0: # raise ConfigValidationError('You have to enter points to pass as a number 0 or higher') if approvedLimit > pointSum: raise ConfigValidationError( 'Points to pass has to be equal to, or smaller than, the sum of available points' )
def validate_config(cls, configstring): config = cls.decode_configstring(configstring) if config['maxpoints'] < 0: raise ConfigValidationError( 'Maximum points must be a number higher than 0') if config['approvedlimit'] < 0: raise ConfigValidationError( 'Points to pass must be a number higher than 0') if config['approvedlimit'] > config['maxpoints']: raise ConfigValidationError( 'Maximum points must be smaller than points to pass') if len(config['grades']) == 0: raise ConfigValidationError('You have to enter at least one grade') hasZeroGrade = False for grade in config['grades']: if grade[0] == '': raise ConfigValidationError('Grade-name cannot be empty') if grade[1] == '': raise ConfigValidationError('Grade-value cannot be empty') if int(grade[1]) > config['maxpoints']: raise ConfigValidationError( "Grade-value cannot be higher than maxpoints, grade-value = '{}' , maxpoints = '{}' " .format(grade[1], config['maxpoints'])) if grade[1] < 0: raise ConfigValidationError( 'Grade-value cannot be smaller than 0') if int(grade[1]) == 0: hasZeroGrade = True if not hasZeroGrade: raise ConfigValidationError( "You have to enter a grade with pointlimit 0")