def get_undermines_for_argument_uid(argument_uid, is_supportive=False): """ Returns all uid's of undermines for the argument. :return argument_uid: UID of the argument :return is_supportive: Boolean :return: array with dict() with id (of argument) and text. """ # logger('RelationHelper', 'get_undermines_for_argument_uid', 'main with argument_uid ' + str(self.argument_uid)) if not is_integer(argument_uid): return None if int(argument_uid) < 1: return None db_arguments = get_enabled_arguments_as_query() db_attacked_argument = db_arguments.filter_by(uid=argument_uid).first() if not db_attacked_argument: return [] db_premises = get_enabled_premises_as_query() db_attacked_premises = db_premises \ .filter_by(premisegroup_uid=db_attacked_argument.premisegroup_uid) \ .order_by(Premise.premisegroup_uid.desc()).all() premises_as_statements_uid = set() for premise in db_attacked_premises: premises_as_statements_uid.add(premise.statement_uid) if len(premises_as_statements_uid) == 0: return [] return __get_undermines_for_premises(premises_as_statements_uid, is_supportive)
def user(request): """ View configuration for the public user page. :param request: current request of the server :return: dictionary with title and project name as well as a value, weather the user is logged in """ match_dict = request.matchdict LOG.debug("Return public user page: %s", match_dict) uid = match_dict.get('uid', 0) LOG.debug("User being shown: %s", uid) if not is_integer(uid): raise HTTPNotFound current_user = DBDiscussionSession.query(User).get(uid) if current_user is None: LOG.error("No user found: %s", uid) raise HTTPNotFound() ui_locales = get_language_from_cookie(request) user_dict = get_information_of(current_user, ui_locales) db_user_of_request = DBDiscussionSession.query(User).filter_by(nickname=request.authenticated_userid).first() can_send_notification = False if db_user_of_request: can_send_notification = current_user.uid not in [db_user_of_request.uid, 1] prep_dict = main_dict(request, user_dict['public_nick']) prep_dict.update({ 'user': user_dict, 'can_send_notification': can_send_notification }) return prep_dict
def add_seen_argument(argument_uid, db_user): """ Adds the uid of the argument into the seen_by list as well as all included statements, mapped with the given user uid :param db_user: current user :param argument_uid: uid of the argument :return: undefined """ if not is_integer(argument_uid) or not isinstance( db_user, User) or db_user.nickname == nick_of_anonymous_user: return False LOG.debug("Argument %s, for user %s", argument_uid, db_user.uid) db_argument = DBDiscussionSession.query(Argument).get(argument_uid) __argument_seen_by_user(db_user, argument_uid) db_premises = DBDiscussionSession.query(Premise).filter_by( premisegroup_uid=db_argument.premisegroup_uid).all() for p in db_premises: __statement_seen_by_user(db_user, p.statement_uid) # find the conclusion and mark all arguments on the way while db_argument.conclusion_uid is None: db_argument = DBDiscussionSession.query(Argument).get( db_argument.argument_uid) __argument_seen_by_user(db_user, argument_uid) __statement_seen_by_user(db_user, db_argument.conclusion_uid) transaction.commit() return True
def valid_model(request): uid = request.json_body.get(keyword) db_review = DBDiscussionSession \ .query(model).filter(model.uid == uid, model.is_executed == False).first() if is_integer(uid) else None if db_review: request.validated['db_review'] = db_review return True else: add_error(request, 'Database has no row {} of {}'.format(uid, model)) return False
def valid_model(request): uid = request.json_body.get(keyword) db_something = DBDiscussionSession.query(model).get( uid) if is_integer(uid) and model else None if db_something: request.validated['db_model'] = db_something return True else: add_error(request, 'Database has no row {} of {}'.format(uid, model)) return False
def valid_uid_as_row_in_review_queue(request): uid = request.json_body.get('uid') queue = request.json_body.get('queue', '') model = get_review_model_by_key(queue) db_review = DBDiscussionSession.query(model).get(uid) if is_integer(uid) and model else None if db_review: request.validated['queue'] = queue request.validated['uid'] = uid request.validated['review'] = db_review return True else: add_error(request, 'Invalid id for any review queue found: {}'.format(queue)) return False
def get_forbidden_attacks_based_on_history(history: str) -> List[int]: """ Returns all attacking uids from reaction steps of the history string :param history: String - history of the user :return: list of uids """ forbidden_uids = [] for split in history.split('-'): if 'reaction' in split and len(split.split('/')) > 4: uid = split.split('/')[4] if is_integer(uid): forbidden_uids.append(int(uid)) return forbidden_uids
def get_undercuts_for_argument_uid(argument_uid): """ Calls self.__get_attack_or_support_for_justification_of_argument_uid(key, argument_uid, False) :return argument_uid: UID of the argument :return: array with dict() with id (of argumet) and text. """ # logger('RelationHelper', 'get_undercuts_for_argument_uid', 'main ' + str(self.argument_uid)) if not is_integer(argument_uid): return None if int(argument_uid) < 1: return None return __get_attack_or_support_for_justification_of_argument_uid(argument_uid, False)
def test_is_integer(self): # conditions_response ignore_empty_case_len_zero_true = iv.is_integer(variable='', ignore_empty_case=True) self.assertEqual(ignore_empty_case_len_zero_true, True) long_string_false = iv.is_integer(variable=',' * 1000 + '30' + '?' * 1000, ignore_empty_case=True) self.assertFalse(long_string_false) ignore_empty_case_len_false = iv.is_integer(variable='str', ignore_empty_case=True) self.assertFalse(ignore_empty_case_len_false) not_ignore_empty_case_len_zero_false = iv.is_integer( variable='', ignore_empty_case=False) self.assertFalse(not_ignore_empty_case_len_zero_false) not_ignore_empty_case_len_false = iv.is_integer( variable='str', ignore_empty_case=False) self.assertFalse(not_ignore_empty_case_len_false) ignore_empty_case_int_true = iv.is_integer(variable=123, ignore_empty_case=True) self.assertTrue(ignore_empty_case_int_true) not_ignore_empty_case_int_true = iv.is_integer(variable=1, ignore_empty_case=False) self.assertTrue(not_ignore_empty_case_int_true) input_none_false = iv.is_integer(variable=None, ignore_empty_case=True) self.assertFalse(input_none_false) input_array_false = iv.is_integer(variable=[1, 2, 3, 'str'], ignore_empty_case=True) self.assertFalse(input_array_false)
def add_seen_statement(statement_uid: int, db_user: User): """ Adds the uid of the statement into the seen_by list, mapped with the given user uid :param db_user:current user :param statement_uid: uid of the statement :return: undefined """ if not is_integer(statement_uid) or not isinstance( db_user, User) or db_user.nickname == nick_of_anonymous_user: return False LOG.debug("Statement %s, for user %s", statement_uid, db_user.uid) val = __statement_seen_by_user(db_user, statement_uid) # if val: # transaction.commit() return val
def get_references_for_argument(uid, main_page): """ Returns all references for the premises group of given argument :param uid: uid of the argument :param main_page: current overview page :return: dict """ LOG.debug("%s", uid) if not is_integer(uid): return {}, {} db_arguments = get_enabled_arguments_as_query() db_argument = db_arguments.filter_by(uid=uid).first() if not db_argument: return {}, {} db_premises = get_enabled_premises_as_query() db_premises = db_premises.filter_by( premisegroup_uid=db_argument.premisegroup_uid).all() data = {} text = {} for premise in db_premises: tmp_uid = premise.statement_uid references_array = __get_references_for_statement(tmp_uid, main_page)[tmp_uid] data[premise.statement_uid] = references_array text[premise.statement_uid] = premise.get_text() if db_argument.conclusion_uid is not None: tmp_uid = db_argument.conclusion_uid references_array = __get_references_for_statement(tmp_uid, main_page)[tmp_uid] data[tmp_uid] = references_array db_statement = DBDiscussionSession.query(Statement).get(tmp_uid) text[tmp_uid] = db_statement.get_text() else: d, t = get_references_for_argument(db_argument.argument_uid, main_page) data.update(d) text.update(t) return data, text
def get_references_for_argument(uid, main_page): """ Returns all references for the premises group of given argument :param uid: uid of the argument :param main_page: current main page :return: dict """ logger('ReferenceHelper', str(uid)) if not is_integer(uid): return {}, {} db_arguments = get_enabled_arguments_as_query() db_argument = db_arguments.filter_by(uid=uid).first() if not db_argument: return {}, {} db_premises = get_enabled_premises_as_query() db_premises = db_premises.filter_by( premisegroup_uid=db_argument.premisegroup_uid).all() data = {} text = {} for premise in db_premises: tmp_uid = premise.statement_uid references_array = __get_references_for_statement(tmp_uid, main_page)[tmp_uid] data[premise.statement_uid] = references_array text[premise.statement_uid] = premise.get_text() if db_argument.conclusion_uid is not None: tmp_uid = db_argument.conclusion_uid references_array = __get_references_for_statement(tmp_uid, main_page)[tmp_uid] data[tmp_uid] = references_array text[tmp_uid] = get_text_for_statement_uid(tmp_uid) else: d, t = get_references_for_argument(db_argument.argument_uid, main_page) data.update(d) text.update(t) return data, text
def valid_user_as_author_of_argument(request): """ :param request: :return: """ if valid_user(request): db_user = request.validated['user'] uid = request.json_body.get('uid') db_argument = DBDiscussionSession. \ query(Argument).filter(Argument.uid == uid, Argument.author_uid == db_user.uid).first() if is_integer(uid) else None if db_argument: request.validated['argument'] = db_argument return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, _tn.get(_.userIsNotAuthorOfArgument)) return False
def valid_premisegroup(request): """ Validates the uid of a premisegroup :param request: :return: """ pgroup_uid = request.json_body.get('uid') db_pgroup = None if is_integer(pgroup_uid): db_pgroup = DBDiscussionSession.query(PremiseGroup).get(pgroup_uid) if db_pgroup: request.validated['pgroup'] = db_pgroup return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'PGroup uid is missing', _tn.get(_.internalError)) return False
def add_seen_statement(statement_uid: int, db_user: User): """ Adds the uid of the statement into the seen_by list, mapped with the given user uid :param db_user:current user :param statement_uid: uid of the statement :return: undefined """ if not is_integer(statement_uid) or not isinstance( db_user, User) or db_user.nickname == nick_of_anonymous_user: return False logger( 'VotingHelper', 'statement ' + str(statement_uid) + ', for user ' + str(db_user.uid)) val = __statement_seen_by_user(db_user, statement_uid) if val: transaction.commit() return val
def valid_premisegroup_in_path(request): """ Sets the premisegroup id from the path into the json_body and executes valid_premisegroup :param request: :return: """ pgroup_uid = request.matchdict.get('id', [None])[0] db_pgroup = None if is_integer(pgroup_uid): db_pgroup = DBDiscussionSession.query(PremiseGroup).get(pgroup_uid) if db_pgroup: request.validated['pgroup_uid'] = db_pgroup return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'PGroup uid is missing in path', _tn.get(_.internalError)) return False
def get_supports_for_argument_uid(argument_uid): """ Returns all uid's of supports for the argument. :return argument_uid: UID of the argument :return: array with dict() with id (of argumet) and text """ if not is_integer(argument_uid): return None if int(argument_uid) < 1: return None return_array = [] given_supports = set() db_arguments = get_enabled_arguments_as_query() db_argument = db_arguments.filter_by( uid=argument_uid).join(PremiseGroup).first() if not db_argument: return [] db_arguments_premises = DBDiscussionSession.query(Premise).filter_by( premisegroup_uid=db_argument.premisegroup_uid).all() for arguments_premises in db_arguments_premises: db_arguments = get_enabled_arguments_as_query() db_supports = db_arguments.filter( Argument.conclusion_uid == arguments_premises.statement_uid, Argument.is_supportive == True).join(PremiseGroup).all() if not db_supports: continue for support in db_supports: if support.premisegroup_uid not in given_supports: tmp_dict = dict() tmp_dict['id'] = support.uid tmp_dict['text'] = support.get_premisegroup_text() return_array.append(tmp_dict) given_supports.add(support.premisegroup_uid) return [] if len(return_array) == 0 else return_array
def valid_list_of_premisegroups_in_path(request): """ Fetches the list of premisegroups and checks their validity :param request: :return: """ pgroup_uids = request.matchdict.get('pgroup_ids') if not pgroup_uids or not valid_issue_by_slug(request): _tn = Translator(get_language_from_cookie(request)) add_error(request, 'PGroup uids are missing in path', _tn.get(_.internalError)) return False for pgroup in pgroup_uids: if not is_integer(pgroup): _tn = Translator(get_language_from_cookie(request)) add_error(request, 'One uid of the pgroups in path is malicious', _tn.get(_.internalError)) return False if not check_belonging_of_premisegroups(request.validated['issue'].uid, pgroup_uids): _tn = Translator(get_language_from_cookie(request)) add_error(request, 'One uid of the pgroups does not belong to the issue', _tn.get(_.internalError)) return False for pgroup in pgroup_uids: if not DBDiscussionSession.query(Argument).filter_by( premisegroup_uid=pgroup).first(): _tn = Translator(get_language_from_cookie(request)) add_error(request, 'One uid of the pgroups argument is not supportive', _tn.get(_.internalError)) return False request.validated['pgroup_uids'] = [int(uid) for uid in pgroup_uids] return True
def valid_statement_uid(request) -> bool: """ Validates the uid of a statement :param request: :return: """ statement_uid = request.json_body.get('uid') db_statement = None if is_integer(statement_uid): db_statement: Statement = DBDiscussionSession.query(Statement).get( statement_uid) if db_statement: request.validated['statement'] = db_statement return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Valid statement uid is missing', _tn.get(_.internalError)) return False
def set_seen_statements(uids, path, db_user) -> dict: """ Marks several statements as already seen. :param uids: Uids of statements which should be marked as seen :param path: Current path of the user :param db_user: User :rtype: dict :return: Dictionary with an error field """ # are the statements connected to an argument? if 'justify' in path: url = path[path.index('justify/') + len('justify/'):] additional_argument = int(url[:url.index('/')]) add_seen_argument(additional_argument, db_user) for uid in uids: # we get the premise group id's only if is_integer(uid): add_seen_statement(uid, db_user) return {'status': 'success'}
def get_rebuts_for_argument_uid(argument_uid): """ Returns all uid's of rebuts for the argument. :return argument_uid: UID of the argument :return: array with dict() with id (of argumet) and text. """ if not is_integer(argument_uid): return None if int(argument_uid) < 1: return None # logger('RelationHelper', 'get_rebuts_for_argument_uid', 'main ' + str(self.argument_uid)) db_arguments = get_enabled_arguments_as_query() db_argument = db_arguments.filter_by(uid=int(argument_uid)).first() if not db_argument: return None if db_argument.conclusion_uid is not None: return __get_rebuts_for_arguments_conclusion_uid(db_argument) else: return get_undercuts_for_argument_uid(argument_uid)
def main_user(request): """ View configuration for the public user page. :param request: current request of the server :return: dictionary with title and project name as well as a value, weather the user is logged in """ match_dict = request.matchdict logger('main_user', 'request.matchdict: {}'.format(match_dict)) uid = match_dict.get('uid', 0) logger('main_user', 'uid: {}'.format(uid)) if not is_integer(uid): raise HTTPNotFound current_user = DBDiscussionSession.query(User).get(uid) if current_user is None or current_user.nickname == nick_of_anonymous_user: logger('main_user', 'no user: {}'.format(uid), error=True) raise HTTPNotFound() ui_locales = get_language_from_cookie(request) user_dict = user.get_information_of(current_user, ui_locales) db_user_of_request = DBDiscussionSession.query(User).filter_by( nickname=request.authenticated_userid).first() can_send_notification = False if db_user_of_request: can_send_notification = current_user.uid != db_user_of_request.uid prep_dict = __main_dict(request, user_dict['public_nick']) prep_dict.update({ 'user': user_dict, 'can_send_notification': can_send_notification }) return prep_dict