<commit_before>from collections import defaultdict
from flask import abort, render_template, redirect, request, url_for
import string

from wuvt import app
from wuvt import db
from wuvt.admin import bp
from wuvt.auth import check_access
from wuvt.trackman.models import DJ, Track, TrackLog
from wuvt.trackman.tasks import deduplicate_track_by_id


@bp.route('/library')
@check_access('library')
def library_index():
    letters = list(string.digits + string.ascii_uppercase)
    return render_template('admin/library_index.html',
                           letters=letters + ['all'])


@bp.route('/library/<string:letter>')
@bp.route('/library/<string:letter>/<int:page>')
@check_access('library')
def library_letter(letter, page=1):
    artists_query = Track.query.with_entities(Track.artist)

    if len(letter) == 1:
        artists_query = artists_query.filter(
            Track.artist.ilike('{}%'.format(letter)))
    elif letter != 'all':
        abort(400)

    artists = artists_query.group_by(Track.artist).order_by(Track.artist).\
        paginate(page, app.config['ARTISTS_PER_PAGE'])
    artists.items = [x[0] for x in artists.items]
    return render_template('admin/library_letter.html', letter=letter,
                           artists=artists)


@bp.route('/library/djs')
@bp.route('/library/djs/<int:page>')
@check_access('library')
def library_djs(page=1):
    djs = DJ.query.order_by(DJ.airname).paginate(
        page, app.config['ARTISTS_PER_PAGE'])
    return render_template('admin/library_djs.html', djs=djs)


@bp.route('/library/dj/<int:id>')
@bp.route('/library/dj/<int:id>/<int:page>')
@check_access('library')
def library_dj(id, page=1):
    dj = DJ.query.get_or_404(id)
    subquery = TrackLog.query.\
        with_entities(TrackLog.track_id).\
        filter(TrackLog.dj_id == id).\
        group_by(TrackLog.track_id).subquery()
    tracks = Track.query.join(subquery).order_by(Track.artist, Track.title).\
        paginate(page, app.config['ARTISTS_PER_PAGE'])

    return render_template('admin/library_dj.html', dj=dj, tracks=tracks)


@bp.route('/library/artist')
@check_access('library')
def library_artist():
    artist = request.args['artist']
    tracks = Track.query.filter(Track.artist == artist).\
        order_by(Track.album, Track.title).all()

    return render_template('admin/library_artist.html', artist=artist,
                           tracks=tracks)


@bp.route('/library/fixup')
@check_access('library')
def library_fixup():
    return render_template('admin/library_fixup.html')


@bp.route('/library/fixup/<string:key>')
@bp.route('/library/fixup/<string:key>/<int:page>')
@check_access('library')
def library_fixup_tracks(key, page=1):
    if key == 'bad_album':
        title = "Invalid Album"
        query = Track.query.filter(db.or_(
            Track.album == "?",
            Track.album == "7\"",
            Track.album == "Not Available",
            Track.album == "s/t"
        ))
    elif key == 'bad_label':
        title = "Invalid Label"<commit_msg>library: include "Self-Titled" in invalid albums<commit_after>from collections import defaultdict
from flask import abort, render_template, redirect, request, url_for
import string

from wuvt import app
from wuvt import db
from wuvt.admin import bp
from wuvt.auth import check_access
from wuvt.trackman.models import DJ, Track, TrackLog
from wuvt.trackman.tasks import deduplicate_track_by_id


@bp.route('/library')
@check_access('library')
def library_index():
    letters = list(string.digits + string.ascii_uppercase)
    return render_template('admin/library_index.html',
                           letters=letters + ['all'])


@bp.route('/library/<string:letter>')
@bp.route('/library/<string:letter>/<int:page>')
@check_access('library')
def library_letter(letter, page=1):
    artists_query = Track.query.with_entities(Track.artist)

    if len(letter) == 1:
        artists_query = artists_query.filter(
            Track.artist.ilike('{}%'.format(letter)))
    elif letter != 'all':
        abort(400)

    artists = artists_query.group_by(Track.artist).order_by(Track.artist).\
        paginate(page, app.config['ARTISTS_PER_PAGE'])
    artists.items = [x[0] for x in artists.items]
    return render_template('admin/library_letter.html', letter=letter,
                           artists=artists)


@bp.route('/library/djs')
@bp.route('/library/djs/<int:page>')
@check_access('library')
def library_djs(page=1):
    djs = DJ.query.order_by(DJ.airname).paginate(
        page, app.config['ARTISTS_PER_PAGE'])
    return render_template('admin/library_djs.html', djs=djs)


@bp.route('/library/dj/<int:id>')
@bp.route('/library/dj/<int:id>/<int:page>')
@check_access('library')
def library_dj(id, page=1):
    dj = DJ.query.get_or_404(id)
    subquery = TrackLog.query.\
        with_entities(TrackLog.track_id).\
        filter(TrackLog.dj_id == id).\
        group_by(TrackLog.track_id).subquery()
    tracks = Track.query.join(subquery).order_by(Track.artist, Track.title).\
        paginate(page, app.config['ARTISTS_PER_PAGE'])

    return render_template('admin/library_dj.html', dj=dj, tracks=tracks)


@bp.route('/library/artist')
@check_access('library')
def library_artist():
    artist = request.args['artist']
    tracks = Track.query.filter(Track.artist == artist).\
        order_by(Track.album, Track.title).all()

    return render_template('admin/library_artist.html', artist=artist,
                           tracks=tracks)


@bp.route('/library/fixup')
@check_access('library')
def library_fixup():
    return render_template('admin/library_fixup.html')


@bp.route('/library/fixup/<string:key>')
@bp.route('/library/fixup/<string:key>/<int:page>')
@check_access('library')
def library_fixup_tracks(key, page=1):
    if key == 'bad_album':
        title = "Invalid Album"
        query = Track.query.filter(db.or_(
            Track.album == "?",
            Track.album == "7\"",
            Track.album == "Not Available",
            Track.album == "s/t",
            Track.album == "Self-Titled"
        ))
    elif key == 'bad_label':
        title = "Invalid Label"
{'fraction_non_alphanumeric': 0.1533061224489796, 'fraction_numerical': 0.0042448979591836735, 'mean_word_length': 3.571641791044776, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '1179725', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2005, 'n_words': 340}