def valid_new_issue(request): """ Verifies given data for a new issue :param request: :return: """ fn_validator = has_keywords_in_json_path(('title', str), ('info', str), ('long_info', str)) if not fn_validator(request): return False title = escape_if_string(request.validated, 'title') info = escape_if_string(request.validated, 'info') long_info = escape_if_string(request.validated, 'long_info') new_issue = __check_for_empty_fields(title=title, info=info, long_info=long_info, request=request) if new_issue.get('contains_empty_field'): add_error(request, 'There is an empty field', new_issue.get('error')) return False new_issue = __check_for_duplicated_field(title=title, info=info, long_info=long_info, request=request) if new_issue.get('contains_duplicated_field'): add_error(request, 'Issue data is a duplicate', new_issue.get('error')) return False return True
def test_escape_if_string(): test_dict = { 'foo': 'bar', 'js-tag': '<script>', 'int-stays-int': 42, 'list-stays-list': [1, 2, 3] } assert 'bar' == escape_if_string(test_dict, 'foo') assert '<script>' == escape_if_string(test_dict, 'js-tag') assert 42 == escape_if_string(test_dict, 'int-stays-int') assert [1, 2, 3] == escape_if_string(test_dict, 'list-stays-list')
def inner(request): min_length = int(environ.get('MIN_LENGTH_OF_STATEMENT', 10)) text = escape_if_string(request.json_body, keyword) if not text or text and len(text) < min_length: __set_min_length_error(request, min_length) return False else: request.validated[keyword] = text return True
def valid_lang_cookie_fallback(request): """ Get provided language from form, else interpret it from the request. :param request: :return: """ lang = escape_if_string(request.json_body, 'lang') if not lang: lang = get_language_from_cookie(request) request.validated['lang'] = lang
def valid_table_name(request): """ :param request: :return: """ table_name = escape_if_string(request.json_body, 'table') if table_name and table_name.lower() in table_mapper: request.validated['table'] = table_name return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Invalid table name', _tn.get(_.invalidTableName)) return False
def valid_new_issue(request): """ Verifies given data for a new issue :param request: :return: """ fn_validator = has_keywords(('title', str), ('info', str), ('long_info', str)) if not fn_validator(request): return False title = escape_if_string(request.validated, 'title') info = escape_if_string(request.validated, 'info') long_info = escape_if_string(request.validated, 'long_info') db_dup1 = DBDiscussionSession.query(Issue).filter_by(title=title).all() db_dup2 = DBDiscussionSession.query(Issue).filter_by(info=info).all() db_dup3 = DBDiscussionSession.query(Issue).filter_by( long_info=long_info).all() if db_dup1 or db_dup2 or db_dup3: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Issue data is a duplicate', _tn.get(_.duplicate)) return False return True
def valid_text_values(request): min_length = int(environ.get('MIN_LENGTH_OF_STATEMENT', 10)) tvalues = escape_if_string(request.json_body, 'text_values') if not tvalues: __set_min_length_error(request, min_length) return False error = False for text in tvalues: if len(text) < min_length: __set_min_length_error(request, min_length) error = True if not error: request.validated['text_values'] = tvalues return True return False
def __validate_notification_msg(request, key): """ Lookup key in request.json_body and validate it against the necessary length for a message. :param request: :param key: :return: """ notification_text = escape_if_string(request.json_body, key) min_length = int(environ.get('MIN_LENGTH_OF_STATEMENT', 10)) if notification_text and isinstance( notification_text, str) and len(notification_text) >= min_length: request.validated[key] = notification_text return True else: _tn = Translator(get_language_from_cookie(request)) error_msg = '{} ({}: {})'.format(_tn.get(_.empty_notification_input), _tn.get(_.minLength), min_length) add_error(request, 'Notification {} too short or invalid'.format(key), error_msg) return False
def valid_language(request): """ Given a nickname of a user, return the object from the database. :param request: :return: """ lang = escape_if_string(request.json_body, 'lang') _tn = Translator(get_language_from_cookie(request)) if not lang: add_error(request, 'Invalid language', _tn.get(_.checkLanguage)) return False db_lang = DBDiscussionSession.query(Language).filter_by( ui_locales=lang).first() if db_lang: request.validated['lang'] = db_lang return True else: add_error(request, 'Invalid language {}'.format(lang), _tn.get(_.checkLanguage)) return False