def validateForCodes(value): """Ensure there are at least 1 and at most 3 codes and values add to 100. """ if len(value) == 0: raise Invalid(_(u"You must have at least 1 FoR code defined.")) elif len(value) > 3: raise Invalid(_(u"You have too many FoR codes defined. \ Please specify a maximum of 3 FoR codes.")) validateTotalOfCodes(value) validateAllCodes(value, anzsrc_codes.FOR_CODES)
def validateAllCodes(value, filename): #Check that all codes are valid by looking up a QuerySource all_codes = anzsrc_codes.listAnzsrcCodesFromFile(filename) invalid_codes = [item['code'] for item in value if item['code'] not in all_codes] if invalid_codes: output_codes = ', '.join(invalid_codes) raise Invalid(_(u"The following codes are note valid: %s. \ Please check your input or else use the drop-down \ menus to select codes." % output_codes))
def validateTotalOfCodes(value): if sum(item['value'] for item in value) != 100: raise Invalid(_(u"Code percentage values must be integers and \ add up to 100%.")) if any(item['value'] < 0 for item in value): raise Invalid(_(u"All code percentage values must be positive integers."))
def validateResearchThemes(value): """Ensure there are at least 1 theme or 'not aligned' selected in checkbox """ if len(value) == 0: raise Invalid(_(u"You must select one or more themes or \ select 'not aligned'"))
def valid_date_range(data): start = data.temporal_coverage_start end = data.temporal_coverage_end if (start is not None and end is not None) and start > end: raise validation.StartBeforeEnd(\ _(u"The start date must be before the end date."))