<commit_before>from flask import (
    Blueprint,
    redirect,
    request,
    flash,
    url_for,
    render_template)
from flask_login import login_required, current_user
from flask_babel import ngettext as _n
from flask_babel import gettext as _
from sqlalchemy import text

from catwatch.blueprints.admin.models import Dashboard
from catwatch.blueprints.user.decorators import role_required
from catwatch.blueprints.user.models import User
from catwatch.blueprints.issue.models import Issue
from catwatch.blueprints.admin.forms import SearchForm, BulkDeleteForm, \
    UserForm, IssueForm


admin = Blueprint('admin', __name__,
                  template_folder='templates', url_prefix='/admin')


@admin.before_request
@login_required
@role_required('admin')
def before_request():
    """ We are protecting all of our admin endpoints. """
    pass


# Dashboard -------------------------------------------------------------------
@admin.route('')
def dashboard():
    group_and_count_users = Dashboard.group_and_count_users()
    group_and_count_issues = Dashboard.group_and_count_issues()

    return render_template('admin/pages/dashboard.jinja2',
                           group_and_count_users=group_and_count_users,
                           group_and_count_issues=group_and_count_issues)


# Users -----------------------------------------------------------------------
@admin.route('/users', defaults={'page': 1})
@admin.route('/users/page/<int:page>')
def users(page):
    search_form = SearchForm()
    bulk_form = BulkDeleteForm()

    sort_by = User.sort_by(request.args.get('sort', 'name'),
                           request.args.get('direction', 'asc'))
    order_values = '{0} {1}'.format(sort_by[0], sort_by[1])

    paginated_users = User.query \
        .filter(User.search(request.args.get('q', ''),
                            ('email', 'name'))) \
        .order_by(User.role.desc(), text(order_values)) \
        .paginate(page, 20, True)

    return render_template('admin/user/index.jinja2',
                           form=search_form, bulk_form=bulk_form,
                           users=paginated_users)


@admin.route('/users/edit/<int:id>', methods=['GET', 'POST'])
def users_edit(id):
    user = User.query.get(id)
    form = UserForm(obj=user)

    if form.validate_on_submit():
        if User.is_last_admin(user,
                              request.form.get('role', None),
                              request.form.get('active', None)):
            flash(_('You are the last admin, you cannot do that.'),
                  'error')
            return redirect(url_for('admin.users'))<commit_msg>Sort paid members before free members in the user admin<commit_after>from flask import (
    Blueprint,
    redirect,
    request,
    flash,
    url_for,
    render_template)
from flask_login import login_required, current_user
from flask_babel import ngettext as _n
from flask_babel import gettext as _
from sqlalchemy import text

from catwatch.blueprints.admin.models import Dashboard
from catwatch.blueprints.user.decorators import role_required
from catwatch.blueprints.user.models import User
from catwatch.blueprints.issue.models import Issue
from catwatch.blueprints.admin.forms import SearchForm, BulkDeleteForm, \
    UserForm, IssueForm


admin = Blueprint('admin', __name__,
                  template_folder='templates', url_prefix='/admin')


@admin.before_request
@login_required
@role_required('admin')
def before_request():
    """ We are protecting all of our admin endpoints. """
    pass


# Dashboard -------------------------------------------------------------------
@admin.route('')
def dashboard():
    group_and_count_users = Dashboard.group_and_count_users()
    group_and_count_issues = Dashboard.group_and_count_issues()

    return render_template('admin/pages/dashboard.jinja2',
                           group_and_count_users=group_and_count_users,
                           group_and_count_issues=group_and_count_issues)


# Users -----------------------------------------------------------------------
@admin.route('/users', defaults={'page': 1})
@admin.route('/users/page/<int:page>')
def users(page):
    search_form = SearchForm()
    bulk_form = BulkDeleteForm()

    sort_by = User.sort_by(request.args.get('sort', 'name'),
                           request.args.get('direction', 'asc'))
    order_values = '{0} {1}'.format(sort_by[0], sort_by[1])

    paginated_users = User.query \
        .filter(User.search(request.args.get('q', ''),
                            ('email', 'name'))) \
        .order_by(User.role.desc(), User.stripe_customer_id,
                  text(order_values)) \
        .paginate(page, 20, True)

    return render_template('admin/user/index.jinja2',
                           form=search_form, bulk_form=bulk_form,
                           users=paginated_users)


@admin.route('/users/edit/<int:id>', methods=['GET', 'POST'])
def users_edit(id):
    user = User.query.get(id)
    form = UserForm(obj=user)

    if form.validate_on_submit():
        if User.is_last_admin(user,
                              request.form.get('role', None),
                              request.form.get('active', None)):
            flash(_('You are the last admin, you cannot do that.'),
                  'error')
            return redirect(url_for('admin.users'))
{'fraction_non_alphanumeric': 0.16247438047326254, 'fraction_numerical': 0.003353828954723309, 'mean_word_length': 2.3424657534246576, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2995318', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1498, 'n_words': 305}