<commit_before># -*- coding: utf-8 -*-
# AUTHOR: RaNaN, vuolter

import datetime
import logging  # test
import operator
import os
import sys
import time
from urllib.parse import unquote

import flask

from pyload import PKGDIR
from pyload.core.utils import formatSize

from ..filters import unquotepath
from ..helpers import (
    static_file_url,
    clear_session,
    get_permission,
    login_required,
    permlist,
    render_base,
    render_template,
    set_permission,
    set_session,
    is_authenticated,
    get_redirect_url
)


bp = flask.Blueprint("app", __name__)


@bp.route("/favicon.ico", endpoint="favicon")
def favicon():
    location = static_file_url('img/favicon.ico')
    return flask.redirect(location)


@bp.route("/robots.txt", endpoint="robots")
def robots():
    return "User-agent: *\nDisallow: /"


# TODO: Rewrite login route using flask-login
@bp.route("/login", methods=["GET", "POST"], endpoint="login")
def login():
    api = flask.current_app.config["PYLOAD_API"]
    
    next = get_redirect_url(fallback=flask.url_for("app.dashboard"))
    
    if flask.request.method == "POST":
        user = flask.request.form["username"]
        password = flask.request.form["password"]
        user_info = api.checkAuth(user, password)
        
        if not user_info:
            return render_template("login.html", next=next, errors=True)

        set_session(user_info)
        flask.flash("Logged in successfully")
    
    if is_authenticated():
        return flask.redirect(next)
        
    if api.getConfigValue("webui", "autologin"):
        allusers = api.getAllUserData()
        if len(allusers) == 1: # TODO: check if localhost
            user_info = next(allusers.values())
            set_session(user_info)
            # NOTE: Double-check autentication here because if session[name] is empty, 
            #       next login_required redirects here again and all loop out.
            if is_authenticated():
                return flask.redirect(next)
        
    return render_template("login.html", next=next)
            

@bp.route("/logout", endpoint="logout")
def logout():
    # logout_user()
    clear_session()
    return render_template("logout.html")


@bp.route("/", endpoint="index")
@bp.route("/dashboard", endpoint="dashboard")
@login_required("ALL")
def dashboard():
    api = flask.current_app.config["PYLOAD_API"]
    links = api.statusDownloads()

    for link in links:
        if link["status"] == 12:
            current_size = link["size"] - link["bleft"]
            speed = link["speed"]
            link["information"] = f"{current_size} KiB @ {speed} KiB/s"<commit_msg>Fix iter-i-typo in webui :p<commit_after># -*- coding: utf-8 -*-
# AUTHOR: RaNaN, vuolter

import datetime
import logging  # test
import operator
import os
import sys
import time
from urllib.parse import unquote

import flask

from pyload import PKGDIR
from pyload.core.utils import formatSize

from ..filters import unquotepath
from ..helpers import (
    static_file_url,
    clear_session,
    get_permission,
    login_required,
    permlist,
    render_base,
    render_template,
    set_permission,
    set_session,
    is_authenticated,
    get_redirect_url
)


bp = flask.Blueprint("app", __name__)


@bp.route("/favicon.ico", endpoint="favicon")
def favicon():
    location = static_file_url('img/favicon.ico')
    return flask.redirect(location)


@bp.route("/robots.txt", endpoint="robots")
def robots():
    return "User-agent: *\nDisallow: /"


# TODO: Rewrite login route using flask-login
@bp.route("/login", methods=["GET", "POST"], endpoint="login")
def login():
    api = flask.current_app.config["PYLOAD_API"]
    
    next = get_redirect_url(fallback=flask.url_for("app.dashboard"))
    
    if flask.request.method == "POST":
        user = flask.request.form["username"]
        password = flask.request.form["password"]
        user_info = api.checkAuth(user, password)
        
        if not user_info:
            return render_template("login.html", next=next, errors=True)

        set_session(user_info)
        flask.flash("Logged in successfully")
    
    if is_authenticated():
        return flask.redirect(next)
        
    if api.getConfigValue("webui", "autologin"):
        allusers = api.getAllUserData()
        if len(allusers) == 1: # TODO: check if localhost
            user_info = next(iter(allusers.values()))
            # user_info = allusers[next(allusers)]            
            set_session(user_info)
            # NOTE: Double-check autentication here because if session[name] is empty, 
            #       next login_required redirects here again and all loop out.
            if is_authenticated():
                return flask.redirect(next)
        
    return render_template("login.html", next=next)
            

@bp.route("/logout", endpoint="logout")
def logout():
    # logout_user()
    clear_session()
    return render_template("logout.html")


@bp.route("/", endpoint="index")
@bp.route("/dashboard", endpoint="dashboard")
@login_required("ALL")
def dashboard():
    api = flask.current_app.config["PYLOAD_API"]
    links = api.statusDownloads()

    for link in links:
        if link["status"] == 12:
            current_size = link["size"] - link["bleft"]
            speed = link["speed"]
            link["information"] = f"{current_size} KiB @ {speed} KiB/s"
{'fraction_non_alphanumeric': 0.12534818941504178, 'fraction_numerical': 0.0014856081708449396, 'mean_word_length': 2.945787545787546, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2760413', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1623, 'n_words': 377}