<commit_before>import json
from math import floor
from flask import current_app, session, redirect, url_for, render_template, Blueprint, abort
from database.model import Team, Members
from webapp.admin.login import delete_token, set_token, valid_admin
from webapp.forms import AdminLoginForm, ConfirmForm, TeamEditForm

import database as db

bp = Blueprint('admin', __name__)


@bp.route("/login", methods=("GET", "POST"))
def login():
    form = AdminLoginForm()
    if form.validate_on_submit():
        if current_app.config["ADMIN_USER"] == form.login.data and current_app.config[
            "ADMIN_PASSWORD"] == form.password.data:
            set_token()

            return redirect(session.get("next") or url_for(".overview"))

    return render_template("admin/login.html", form=form)


@bp.route("/logout")
@valid_admin
def logout():
    delete_token()
    return redirect(url_for(".login"))


@bp.route("/")
@valid_admin
def overview():
    teams = db.session.query(Team).filter_by(deleted=False).order_by(Team.name)
    return render_template("admin/overview.html", teams=teams)


@bp.route("/map")
@valid_admin
def team_map():
    return render_template("admin/map.html")


_color_map = ["blue", "yellow", "green", "red"]


@bp.route("/map_teams")
@valid_admin
def map_teams():
    teams = db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True).order_by(Team.id).all()
    data = []

    max_working = len(teams) - (len(teams) % 3)
    divider = max_working / 3.0

    def distance_sort(a, b):
        if a.location.center_distance > b.location.center_distance:
            return -1
        if a.location.center_distance < b.location.center_distance:
            return 1
        return 0

    working = teams[:max_working]
    teams = sorted(working, distance_sort) + teams[max_working:]

    for idx, team in enumerate(teams):
        color_idx = 0
        if (divider > 0):
            color_idx = int(floor(idx / divider))
        team_data = {"name": team.name,
                     "confirmed": team.confirmed,
                     "email": team.email,
                     "members": [member.name for member in team.members],
                     "address": team.location.street,
                     "color": _color_map[color_idx]}
        location = {"lat": team.location.lat,
                    "lon": team.location.lon}
        data.append({"location": location,
                     "data": team_data})

    return json.dumps(data)
<commit_msg>Fix broken color choosing for the admin map.<commit_after>import json
from math import floor
from flask import current_app, session, redirect, url_for, render_template, Blueprint, abort
from database.model import Team, Members
from webapp.admin.login import delete_token, set_token, valid_admin
from webapp.forms import AdminLoginForm, ConfirmForm, TeamEditForm

import database as db

bp = Blueprint('admin', __name__)


@bp.route("/login", methods=("GET", "POST"))
def login():
    form = AdminLoginForm()
    if form.validate_on_submit():
        if current_app.config["ADMIN_USER"] == form.login.data and current_app.config[
            "ADMIN_PASSWORD"] == form.password.data:
            set_token()

            return redirect(session.get("next") or url_for(".overview"))

    return render_template("admin/login.html", form=form)


@bp.route("/logout")
@valid_admin
def logout():
    delete_token()
    return redirect(url_for(".login"))


@bp.route("/")
@valid_admin
def overview():
    teams = db.session.query(Team).filter_by(deleted=False).order_by(Team.name)
    return render_template("admin/overview.html", teams=teams)


@bp.route("/map")
@valid_admin
def team_map():
    return render_template("admin/map.html")


_color_map = ["blue", "yellow", "green", "red"]


@bp.route("/map_teams")
@valid_admin
def map_teams():
    teams = db.session.query(Team).filter_by(deleted=False).filter_by(confirmed=True).order_by(Team.id).all()
    data = []

    max_working = len(teams) - (len(teams) % 3)
    divider = max_working / 3.0

    def distance_sort(a, b):
        if a.location.center_distance > b.location.center_distance:
            return -1
        if a.location.center_distance < b.location.center_distance:
            return 1
        return 0

    working = teams[:max_working]
    teams = sorted(working, distance_sort) + teams[max_working:]

    for idx, team in enumerate(teams):
        color_idx = 0
        if (divider > 0):
            color_idx = min(int(floor(idx / divider)), 3)
        team_data = {"name": team.name,
                     "confirmed": team.confirmed,
                     "email": team.email,
                     "members": [member.name for member in team.members],
                     "address": team.location.street,
                     "color": _color_map[color_idx]}
        location = {"lat": team.location.lat,
                    "lon": team.location.lon}
        data.append({"location": location,
                     "data": team_data})

    return json.dumps(data)

{'fraction_non_alphanumeric': 0.13219191718096754, 'fraction_numerical': 0.0033844316145729644, 'mean_word_length': 2.900621118012422, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '639074', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1503, 'n_words': 342}