<commit_before>import logging
from six.moves import http_client

from flask import url_for, g
from flask.views import MethodView
import marshmallow as ma
from flask_restx import reqparse
from flask_smorest import Blueprint, abort
from marshmallow_sqlalchemy import ModelSchema
from drift.core.extensions.urlregistry import Endpoints
from driftbase.utils import url_player, url_user
from driftbase.models.db import User, CorePlayer, UserIdentity

log = logging.getLogger(__name__)


endpoints = Endpoints()

bp = Blueprint('users', __name__, url_prefix='/users', description='User management')


class PlayerSchema(ModelSchema):
    class Meta:
        model = CorePlayer
        exclude = ('num_logons', )
        strict = True
    player_url = ma.fields.Str(description="Hello")

class UserIdentitySchema(ModelSchema):
    class Meta:
        model = UserIdentity
        strict = True

class UserSchema(ModelSchema):

    class Meta:
        model = User
        strict = True
    user_url = ma.fields.Str(description="Hello")
    client_url = ma.fields.Str()
    user_url = ma.fields.Str()
    players = ma.fields.List(ma.fields.Nested(PlayerSchema))
    identities = ma.fields.List(ma.fields.Nested(UserIdentitySchema))


class UserRequestSchema(ma.Schema):
    class Meta:
        strict = True
        ordered = True


def drift_init_extension(app, api, **kwargs):
    endpoints.init_app(app)
    api.register_blueprint(bp)


<EMAIL>('', endpoint='users')
@bp.route('', endpoint='list')
class UsersListAPI(MethodView):
    @bp.response(UserSchema(many=True))
    def get(self):
        """List Users

        Return users, just the most recent 10 records with no paging or anything I'm afraid.
        """
        ret = []
        users = g.db.query(User).order_by(-User.user_id).limit(10)
        for row in users:
            user = {
                "user_id": row.user_id,
                "user_url": url_for('users.entry', user_id=row.user_id, _external=True)
            }
            ret.append(user)
        return ret


<EMAIL>('/<int:user_id>', endpoint='user')
@bp.route('/<int:user_id>', endpoint='entry')
class UsersAPI(MethodView):
    """

    """
    @bp.response(UserSchema(many=False))
    def get(self, user_id):
        """Single user

        Return a user by ID
        """
        user = g.db.query(User).filter(User.user_id == user_id).first()
        if not user:
            abort(http_client.NOT_FOUND)

        data = user.as_dict()
        data["client_url"] = None
        players = g.db.query(CorePlayer).filter(CorePlayer.user_id == user_id)
        data["players"] = players
        identities = g.db.query(UserIdentity).filter(UserIdentity.user_id == user_id)
        data["identities"] = identities

        return data


@endpoints.register
def endpoint_info(current_user):
    ret = {"users": url_for("users.list", _external=True), }
    ret["my_user"] = None
    if current_user:
        ret["my_user"] = url_for("users.entry", user_id=current_user["user_id"], _external=True)
    return ret
<commit_msg>Disambiguate PlayerSchema<commit_after>import logging
from six.moves import http_client

from flask import url_for, g
from flask.views import MethodView
import marshmallow as ma
from flask_restx import reqparse
from flask_smorest import Blueprint, abort
from marshmallow_sqlalchemy import ModelSchema
from drift.core.extensions.urlregistry import Endpoints
from driftbase.utils import url_player, url_user
from driftbase.models.db import User, CorePlayer, UserIdentity

log = logging.getLogger(__name__)


endpoints = Endpoints()

bp = Blueprint('users', __name__, url_prefix='/users', description='User management')


class UserPlayerSchema(ModelSchema):
    class Meta:
        model = CorePlayer
        exclude = ('num_logons', )
        strict = True
    player_url = ma.fields.Str(description="Hello")

class UserIdentitySchema(ModelSchema):
    class Meta:
        model = UserIdentity
        strict = True

class UserSchema(ModelSchema):

    class Meta:
        model = User
        strict = True
    user_url = ma.fields.Str(description="Hello")
    client_url = ma.fields.Str()
    user_url = ma.fields.Str()
    players = ma.fields.List(ma.fields.Nested(UserPlayerSchema))
    identities = ma.fields.List(ma.fields.Nested(UserIdentitySchema))


class UserRequestSchema(ma.Schema):
    class Meta:
        strict = True
        ordered = True


def drift_init_extension(app, api, **kwargs):
    endpoints.init_app(app)
    api.register_blueprint(bp)


<EMAIL>('', endpoint='users')
@bp.route('', endpoint='list')
class UsersListAPI(MethodView):
    @bp.response(UserSchema(many=True))
    def get(self):
        """List Users

        Return users, just the most recent 10 records with no paging or anything I'm afraid.
        """
        ret = []
        users = g.db.query(User).order_by(-User.user_id).limit(10)
        for row in users:
            user = {
                "user_id": row.user_id,
                "user_url": url_for('users.entry', user_id=row.user_id, _external=True)
            }
            ret.append(user)
        return ret


<EMAIL>('/<int:user_id>', endpoint='user')
@bp.route('/<int:user_id>', endpoint='entry')
class UsersAPI(MethodView):
    """

    """
    @bp.response(UserSchema(many=False))
    def get(self, user_id):
        """Single user

        Return a user by ID
        """
        user = g.db.query(User).filter(User.user_id == user_id).first()
        if not user:
            abort(http_client.NOT_FOUND)

        data = user.as_dict()
        data["client_url"] = None
        players = g.db.query(CorePlayer).filter(CorePlayer.user_id == user_id)
        data["players"] = players
        identities = g.db.query(UserIdentity).filter(UserIdentity.user_id == user_id)
        data["identities"] = identities

        return data


@endpoints.register
def endpoint_info(current_user):
    ret = {"users": url_for("users.list", _external=True), }
    ret["my_user"] = None
    if current_user:
        ret["my_user"] = url_for("users.entry", user_id=current_user["user_id"], _external=True)
    return ret

{'fraction_non_alphanumeric': 0.11935905820797907, 'fraction_numerical': 0.0013080444735120995, 'mean_word_length': 3.121967654986523, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 11, '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': '5683060', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1848, 'n_words': 419}