<commit_before>#    This file is part of rhizi, a collaborative knowledge graph editor.
#    Copyright (C) 2014-2015  Rhizi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""
Rhizi web API

@deprecated: destined to split into rz_api_rest & rz_api_websocket
"""
from flask import current_app
from flask import escape
from flask import render_template
from flask import request
from flask import session
import logging

from .rz_api_common import sanitize_input__rzdoc_name
from .rz_req_handling import common_resp_handle__client_error


log = logging.getLogger('rhizi')

def index(rzdoc_name=None):
    return rz_mainpage(rzdoc_name)

def rz_mainpage(rzdoc_name=None):
    """
    Main application load function.

    [!] Do not use as a flask endpoint as multiple functions redirect here
    """

    # fetch rz_username for welcome message
    email_address = session.get('username')
    rz_username = "Anonymous Stranger"
    role_set = []
    if None != email_address:  # session cookie passed & contains uid (email_address)
        try:
            _uid, u_account = current_app.user_db.lookup_user__by_email_address(email_address)
        except Exception as e:
            # may occur on user_db reset or malicious cookie != stale cookie,
            # for which the user would at least be known to the user_db
            # FIXME: remove the cookie
            pass
        else:
            role_set = u_account.role_set
            rz_username = escape(u_account.rz_username)

    rz_config = current_app.rz_config
    if None == rzdoc_name:
        s_rzdoc_name = rz_config.rzdoc__mainpage_name
    else:
        try:
            s_rzdoc_name = sanitize_input__rzdoc_name(rzdoc_name)
        except Exception as e:
            log.exception(e)
            return common_resp_handle__client_error(status=404)

    # establish rz_config template values
    host_addr, host_port = request.host_sock_addr[0], request.host_sock_addr[1]
    rz_config = {'rz_config__rzdoc_cur__name': s_rzdoc_name,
                 'rz_config__rzdoc__mainpage_name': rz_config.rzdoc__mainpage_name,
                 'rz_config__hostname': host_addr,
                 'rz_config__port': host_port,
                 'rz_config__optimized_main': 'true' if rz_config.optimized_main else 'false',
                 'rz_config__role_set': role_set,
                 'fragment_d_path': rz_config.fragment_d_path,
                 }<commit_msg>server: fix client breakage on document names with quotes in name<commit_after>#    This file is part of rhizi, a collaborative knowledge graph editor.
#    Copyright (C) 2014-2015  Rhizi
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""
Rhizi web API

@deprecated: destined to split into rz_api_rest & rz_api_websocket
"""
from flask import current_app
from flask import escape
from flask import render_template
from flask import request
from flask import session
import logging

from .rz_api_common import sanitize_input__rzdoc_name
from .rz_req_handling import common_resp_handle__client_error


log = logging.getLogger('rhizi')

def index(rzdoc_name=None):
    return rz_mainpage(rzdoc_name)

def rz_mainpage(rzdoc_name=None):
    """
    Main application load function.

    [!] Do not use as a flask endpoint as multiple functions redirect here
    """

    # fetch rz_username for welcome message
    email_address = session.get('username')
    rz_username = "Anonymous Stranger"
    role_set = []
    if None != email_address:  # session cookie passed & contains uid (email_address)
        try:
            _uid, u_account = current_app.user_db.lookup_user__by_email_address(email_address)
        except Exception as e:
            # may occur on user_db reset or malicious cookie != stale cookie,
            # for which the user would at least be known to the user_db
            # FIXME: remove the cookie
            pass
        else:
            role_set = u_account.role_set
            rz_username = escape(u_account.rz_username)

    rz_config = current_app.rz_config
    if None == rzdoc_name:
        s_rzdoc_name = rz_config.rzdoc__mainpage_name
    else:
        try:
            s_rzdoc_name = sanitize_input__rzdoc_name(rzdoc_name)
        except Exception as e:
            log.exception(e)
            return common_resp_handle__client_error(status=404)

    # establish rz_config template values
    host_addr, host_port = request.host_sock_addr[0], request.host_sock_addr[1]
    rz_config = {'rz_config__rzdoc_cur__name': s_rzdoc_name.replace('"', r'\"'),
                 'rz_config__rzdoc__mainpage_name': rz_config.rzdoc__mainpage_name,
                 'rz_config__hostname': host_addr,
                 'rz_config__port': host_port,
                 'rz_config__optimized_main': 'true' if rz_config.optimized_main else 'false',
                 'rz_config__role_set': role_set,
                 'fragment_d_path': rz_config.fragment_d_path,
                 }
{'fraction_non_alphanumeric': 0.06387096774193549, 'fraction_numerical': 0.004516129032258065, 'mean_word_length': 3.0214007782101167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7412446', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1763, 'n_words': 585}