def valid_conclusion(request): """ Given a conclusion id, query the object from the database and return it in the request. :param request: :return: """ conclusion_id = request.json_body.get('conclusion_id') issue = request.validated.get('issue') _tn = Translator(get_language_from_cookie(request)) if not issue: find_issue_in_request = issue_handler.get_issue_id(request) if find_issue_in_request: issue = DBDiscussionSession.query(Issue).get( issue_handler.get_issue_id(request)) else: add_error(request, 'Issue is missing', _tn.get(_.issueNotFound)) return False if conclusion_id and isinstance(conclusion_id, int): db_conclusion = DBDiscussionSession.query(Statement).filter_by( uid=conclusion_id, issue_uid=issue.uid, is_disabled=False).first() if db_conclusion: request.validated['conclusion'] = db_conclusion return True else: add_error(request, 'Conclusion is missing', _tn.get(_.conclusionIsMissing)) return False else: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Conclusion id is missing', _tn.get(_.conclusionIsMissing)) return False
def main_review(request): """ View configuration for the review index. :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 """ logger('main_review', 'def {}'.format(request.matchdict)) nickname = request.authenticated_userid _tn = Translator(get_language_from_cookie(request)) review_dict = review_queue_helper.get_review_queues_as_lists( request.application_url, _tn, nickname) count, all_rights = review_reputation_helper.get_reputation_of(nickname) prep_dict = __main_dict(request, _tn.get(_.review)) prep_dict.update({ 'review': review_dict, 'privilege_list': review_reputation_helper.get_privilege_list(_tn), 'reputation_list': review_reputation_helper.get_reputation_list(_tn), 'reputation': { 'count': count, 'has_all_rights': all_rights } }) return prep_dict
def queue_details(request): """ View configuration for the review content. :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 """ LOG.debug("Queue Details %s / %s", request.matchdict, request.params) ui_locales = get_language_from_cookie(request) _tn = Translator(ui_locales) queue_name = request.validated['queue'] db_user = request.validated['user'] application_url = request.application_url queue = subclass_by_name(queue_name) adapter = QueueAdapter(queue=queue(), db_user=db_user, application_url=application_url, translator=_tn) subpage_dict = adapter.get_subpage_of_queue(request.session, queue_name) request.session.update(subpage_dict['session']) prep_dict = main_dict(request, _tn.get(get_title_by_key(queue_name))) prep_dict.update({ 'extras': request.decorated['extras'], 'subpage': subpage_dict, 'lock_time': dbas.review.queue.max_lock_time_in_sec }) return prep_dict
def index(request): """ View configuration for the review index. :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 """ LOG.debug("Review Index: %s / %s", request.matchdict, request.params) db_user = request.validated['user'] _tn = Translator(get_language_from_cookie(request)) adapter = QueueAdapter(db_user=db_user, main_page=request.application_url, translator=_tn) review_dict = adapter.get_review_queues_as_lists() count, all_rights = get_reputation_of(db_user) prep_dict = main_dict(request, _tn.get(_.review)) prep_dict.update({ 'review': review_dict, 'privilege_list': get_privilege_list(_tn), 'reputation_list': get_reputation_reasons_list(_tn), 'reputation': { 'count': count, 'has_all_rights': all_rights } }) return prep_dict
def review_content(request): """ View configuration for the review content. :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 """ logger('review_content', 'def {}'.format(request.matchdict)) ui_locales = get_language_from_cookie(request) _tn = Translator(ui_locales) subpage_name = request.matchdict['queue'] nickname = request.authenticated_userid session = request.session application_url = request.application_url subpage_dict = review_page_helper.get_subpage_elements_for( nickname, session, application_url, subpage_name, _tn) request.session.update(subpage_dict['session']) if not subpage_dict['elements'] and not subpage_dict[ 'has_access'] and not subpage_dict['no_arguments_to_review']: logger('review_content', 'subpage error', error=True) raise HTTPNotFound() title = _tn.get(_.review) if subpage_name in review_queue_helper.title_mapping: title = _tn.get(review_queue_helper.title_mapping[subpage_name]) prep_dict = __main_dict(request, title) prep_dict.update({ 'extras': request.decorated['extras'], 'subpage': subpage_dict, 'lock_time': review_queue_helper.max_lock_time_in_sec }) return prep_dict
def __append_extras_dict_without_flag(request: Request, db_user: User, db_issue: Issue, db_statement: Statement, pdict: dict, attitude: Attitudes): """ :param request: :param db_user: :param db_issue: :param db_statement: :param pdict: :param attitude: :return: """ item_len = len(pdict['items']['elements']) _dh = DictionaryHelper(get_language_from_cookie(request), db_issue.lang) supportive = attitude in [Attitudes.AGREE, Attitudes.DONT_KNOW] logged_in = db_user is not None and db_user.nickname != nick_of_anonymous_user extras_dict = _dh.prepare_extras_dict(db_issue.slug, False, True, True, request.registry, request.application_url, request.path, db_user) if item_len == 0 or item_len == 1 and logged_in: # is the discussion at the end? _dh.add_discussion_end_text(pdict['discussion'], extras_dict, db_user.nickname, at_justify=True, current_premise=db_statement.get_text(), supportive=supportive) pdict['extras'] = extras_dict
def get_d3_partial_dump(request): LOG.debug("Create partial d3 dump. %s", request.json_body) path = request.validated['path'] uid = request.validated['uid'] is_argument = request.validated['is_argument'] db_issue = request.validated['issue'] return_dict = { 'type': 'partial' } if is_argument: graph, error = get_partial_graph_for_argument(uid, db_issue) else: graph, error = get_partial_graph_for_statement(uid, db_issue, path) if not error: return_dict.update(graph) return_dict.update({'node_opinion_factors': get_opinion_data(db_issue)}) return_dict.update({'path': get_path_of_user(request.application_url, path, db_issue)}) return_dict.update({'error': ''}) else: # gets called if the data itself is malicious ui_locales = get_language_from_cookie(request) _t = Translator(ui_locales) error = _t.get(_.internalKeyError) return_dict = { 'error': error } return return_dict
def main_table(request): """ View configuration for the content view. Only logged in user can reach this page. :param request: current webservers request :return: dictionary with title and project name as well as a value, weather the user is logged in """ LOG.debug("Entering the main_table method from the admin interface.") ui_locales = get_language_from_cookie(request) extras_dict = DictionaryHelper( ui_locales).prepare_extras_dict_for_normal_page( request.registry, request.application_url, request.path, request.validated['user']) table_name = request.matchdict['table'] if not table_name.lower() in lib.table_mapper: return exception_response(400) table_dict = lib.get_table_dict(table_name, request.application_url) return { 'language': str(ui_locales), 'title': 'Admin - ' + table_name, 'project': project_name, 'extras': extras_dict, 'table': table_dict, 'discussion': { 'broke_limit': False } }
def valid_premisegroups(request): """ Validates the correct build of premisegroups :param request: :return: """ premisegroups = request.json_body.get('premisegroups') if not premisegroups \ or not isinstance(premisegroups, list) \ or not all([isinstance(l, list) for l in premisegroups]): _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Invalid conclusion id', _tn.get(_.requestFailed)) return False min_length = int(environ.get('MIN_LENGTH_OF_STATEMENT', 10)) for premisegroup in premisegroups: for premise in premisegroup: if isinstance(premise, str): if len(premise) < min_length: __set_min_length_error(request, min_length) return False else: add_error(request, 'At least one premise isn\'t a string!') return False request.validated['premisegroups'] = premisegroups return True
def prepare_request_dict(request: Request): """ :param request: :return: """ LOG.debug("Preparing request dict for renderer") db_user = request.validated['user'] nickname = db_user.nickname if db_user.nickname != nick_of_anonymous_user else None db_last_topic = history_handler.get_last_issue_of(db_user) slug = __get_slug(request.matchdict) db_issue = __get_issue(request, slug, db_last_topic) issue_handler.save_issue_id_in_session(db_issue.uid, request) history = history_handler.save_and_set_cookie(request, db_user, db_issue) set_language_for_visit(request) return { 'nickname': nickname, 'user': db_user, 'path': request.path, 'app_url': request.application_url, 'matchdict': request.matchdict, 'params': request.params, 'session': request.session, 'registry': request.registry, 'issue': db_issue, 'history': history, 'ui_locales': get_language_from_cookie(request) }
def __append_extras_dict_with_flag(request: Request, db_user: User, db_issue: Issue, db_statement: Statement, pdict: dict): """ :param request: :param db_user: :param db_issue: :param db_statement: :param pdict: :return: """ item_len = len(pdict['items']['elements']) _dh = DictionaryHelper(get_language_from_cookie(request), db_issue.lang) extras_dict = _dh.prepare_extras_dict(db_issue.slug, True, True, True, request.registry, request.application_url, request.path, db_user=db_user) if item_len == 0: # is the discussion at the end? _dh.add_discussion_end_text(pdict['discussion'], extras_dict, db_user.nickname, at_dont_know=True, current_premise=db_statement.get_text()) pdict['extras'] = extras_dict
def __check_for_empty_fields(title: str, info: str, long_info: str, request: dict) -> dict: """ This method checks if there is a empty field in the data of the new issue. It also creates a error-message with the empty fields. :param title: The title of the new issue :param info: The info of the new issue :param long_info: The long info of the new issue :param request: The request with the data of the new issue :return: a dict with a boolean which tells if there is any empty field and a equivalent error-message. """ _tn = Translator(get_language_from_cookie(request)) error = _tn.get(_.newIssueErrorMsg) + ': ' title_is_empty = title.strip() == '' info_is_empty = info.strip() == '' long_info_is_emtpy = long_info.strip() == '' if title_is_empty: error = error + _tn.get(_.newIssueTitle) + ', ' if info_is_empty: error = error + _tn.get(_.newIssueInfo) + ', ' if long_info_is_emtpy: error = error + _tn.get(_.newIssueLongInfo) + ', ' return { "contains_empty_field": title_is_empty or info_is_empty or long_info_is_emtpy, "error": error[:-2] }
def debug_function(request): """ Minimal debug interface for the websocket :param request: current webservers reqquest :return: dict() """ LOG.debug("Preparing debug information for websocket") ui_locales = get_language_from_cookie(request) extras_dict = DictionaryHelper( ui_locales).prepare_extras_dict_for_normal_page( request.registry, request.application_url, request.path, request.validated['user']) return { 'language': str(ui_locales), 'title': 'Debug Socket.IO Connection', 'project': project_name, 'extras': extras_dict, 'is_admin': request.validated['user'].is_admin(), 'discussion': { 'broke_limit': False } }
def append_extras_dict_during_justification_argument(request: Request, db_user: User, db_issue: Issue, pdict: dict): """ :param request: :param db_user: :param db_issue: :param pdict: :return: """ system_lang = get_language_from_cookie(request) item_len = len(pdict['items']['elements']) _dh = DictionaryHelper(system_lang, db_issue.lang) logged_in = (db_user and db_user.nickname != nick_of_anonymous_user) is not None extras_dict = _dh.prepare_extras_dict(db_issue.slug, False, True, True, request.registry, request.application_url, request.path, db_user=db_user) # is the discussion at the end? if item_len == 0 or item_len == 1 and logged_in or 'login' in pdict[ 'items']['elements'][0].get('id'): _dh.add_discussion_end_text(pdict['discussion'], extras_dict, request.authenticated_userid, at_justify_argumentation=True) pdict['extras'] = extras_dict
def settings(request): """ View configuration for the personal settings view. Only logged in user can reach this 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 """ LOG.debug("Show settings %s", request.params) ui_locales = get_language_from_cookie(request) old_pw, new_pw, confirm_pw, message = '', '', '', '' success, error = False, False db_user: User = request.validated['user'] if 'form.passwordchange.submitted' in request.params: old_pw = escape_string(request.params['passwordold']) new_pw = escape_string(request.params['password']) confirm_pw = escape_string(request.params['passwordconfirm']) message, success = change_password(db_user, old_pw, new_pw, confirm_pw, ui_locales) error = not success settings_dict = DictionaryHelper(ui_locales).prepare_settings_dict(success, old_pw, new_pw, confirm_pw, error, message, db_user, request.application_url, request.decorated['extras']['use_with_ldap']) prep_dict = main_dict(request, Translator(ui_locales).get(_.settings)) prep_dict.update({ 'settings': settings_dict }) return prep_dict
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 main_admin(request): """ View configuration for the content view. Only logged in user can reach this page. :param request: current webservers request :return: dictionary with title and project name as well as a value, weather the user is logged in """ LOG.debug("def") db_user = request.validated['user'] ui_locales = get_language_from_cookie(request) extras_dict = DictionaryHelper( ui_locales).prepare_extras_dict_for_normal_page( request.registry, request.application_url, request.path, db_user) dashboard_elements = { 'entities': lib.get_overview(request.path), 'api_tokens': lib.get_application_tokens() } return { 'language': str(ui_locales), 'title': 'Admin' if db_user.is_admin() else '(B)admin', 'project': project_name, 'extras': extras_dict, 'dashboard': dashboard_elements, 'discussion': { 'broke_limit': False } }
def __check_for_duplicated_field(title: str, info: str, long_info: str, request: dict) -> dict: """ This method checks if there is a duplication in any field of the new issue. It also creates a error-message with the fields which are containing the duplication. :param title: The title of the new issue :param info: The info of the new issue :param long_info: The long info of the new issue :param request: The request with the data of the new issue :return: a dict with a boolean which tells if there is any duplicated field and a equivalent error-message. """ _tn = Translator(get_language_from_cookie(request)) error = _tn.get(_.duplicate) + ': ' title_is_duplicate = DBDiscussionSession.query(Issue).filter_by( title=title).all() info_is_duplicate = DBDiscussionSession.query(Issue).filter_by( info=info).all() long_info_is_duplicate = DBDiscussionSession.query(Issue).filter_by( long_info=long_info).all() if title_is_duplicate: error = error + _tn.get(_.newIssueTitle) + ', ' if info_is_duplicate: error = error + _tn.get(_.newIssueInfo) + ', ' if long_info_is_duplicate: error = error + _tn.get(_.newIssueLongInfo) + ', ' return { "contains_duplicated_field": title_is_duplicate or info_is_duplicate or long_info_is_duplicate, "error": error[:-2] }
def valid_notification_recipient(request): """ Recipients must exist, author and recipient must be different users. :param request: :return: """ _tn = Translator(get_language_from_cookie(request)) if not valid_user(request): add_error(request, 'Not logged in', _tn.get(_.notLoggedIn)) return False db_author = request.validated["user"] recipient_nickname = str(request.json_body.get('recipient')).replace( '%20', ' ') db_recipient = get_user_by_private_or_public_nickname(recipient_nickname) if not db_recipient or recipient_nickname == 'admin' or recipient_nickname == nick_of_anonymous_user: add_error(request, 'Recipient not found', _tn.get(_.recipientNotFound)) return False elif db_author and db_author.uid == db_recipient.uid: add_error(request, 'Author and Recipient are the same user', _tn.get(_.senderReceiverSame)) return False else: request.validated["recipient"] = db_recipient return True
def get_news(request): """ ajax interface for getting news :param request: current request of the server :return: json-set with all news """ LOG.debug("Return News from AJAX") return news_handler.get_news(get_language_from_cookie(request))
def get_news(request): """ ajax interface for getting news :param request: current request of the server :return: json-set with all news """ logger('views', 'main') return news_handler.get_news(get_language_from_cookie(request))
def notifications(request): """ View configuration for the notification view. Only logged in user can reach this 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 """ LOG.debug("Show Notifications") _tn = Translator(get_language_from_cookie(request)) return main_dict(request, _tn.get(_.message))
def get_public_user_data(request): """ Returns dictionary with public user data :param request: request of the web server :return: """ LOG.debug("Return public user data. %s", request.json_body) return user.get_public_data(request.validated['user_id'], get_language_from_cookie(request))
def experiment(request): """ View configuration for fieldtest. :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 """ LOG.debug("Return experiment Information view.") ui_locales = get_language_from_cookie(request) return main_dict(request, Translator(ui_locales).get(_.fieldtest))
def docs(request): """ View configuration for the documentation. :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 """ LOG.debug("Return docs view.") return main_dict(request, Translator(get_language_from_cookie(request)).get(_.docs))
def get_public_user_data(request): """ Returns dictionary with public user data :param request: request of the web server :return: """ logger('views', 'main: {}'.format(request.json_body)) return user.get_public_data(request.validated['nickname'], get_language_from_cookie(request))
def get_user_history(request): """ Request the complete user track. :param request: current request of the server :return: json-dict() """ ui_locales = get_language_from_cookie(request) db_user = request.validated['user'] return history_handler.get_from_database(db_user, ui_locales)
def get_all_edits_of_user(request): """ Request for all edits of the user :param request: current request of the server :return: json-dict() """ ui_locales = get_language_from_cookie(request) db_user = request.validated['user'] return user.get_textversions(db_user, ui_locales).get('edits', [])
def get_all_argument_clicks(request): """ Request for all clicked arguments of the user :param request: current request of the server :return: json-dict() """ ui_locales = get_language_from_cookie(request) db_user = request.validated['user'] return user.get_clicked_elements_of(db_user, True, ui_locales)
def get_all_marked_statements(request): """ Request for all marked statements of the user :param request: current request of the server :return: json-dict() """ ui_locales = get_language_from_cookie(request) db_user = request.validated['user'] return user.get_marked_elements_of(db_user, False, ui_locales)