<commit_before>"""HUB API"""

from flask import jsonify, request
from flask.views import MethodView
from flask_oauthlib.utils import create_response, extract_params
from flask_peewee.rest import Authentication, RestAPI, RestResource
from flask_peewee.utils import slugify
from oauthlib.common import add_params_to_uri, to_unicode, urlencode
from werkzeug.exceptions import NotFound

import models
from models import (EMAIL_REGEX, ORCID_ID_REGEX, User, UserOrg)
from application import api, app, oauth


class AppRestResource(RestResource):
    def get_api_name(self):
        return slugify(self.model.__name__ + 's')

    def response_forbidden(self):
        return jsonify({"error": 'Forbidden'}), 403

    def response_bad_method(self):
        return jsonify({"error": f'Unsupported method "{request.method}"'}), 405

    def response_bad_request(self):
        return jsonify({"error": 'Bad request'}), 400

    def api_detail(self, pk, method=None):
        try:
            return super().api_detail(pk, method)
        except NotFound:
            return jsonify({"error": 'Not found'}), 404


class UserResource(AppRestResource):
    exclude = (
        "password",
        "email",
    )


api.register(models.Organisation, AppRestResource)
api.register(models.Task, AppRestResource)
# api.register(models.User, UserResource)
api.setup()


@app.route('/api/me')
@app.route("/api/v0.1/me")
@oauth.require_oauth()
def me():
    user = request.oauth.user
    return jsonify(email=user.email, name=user.name)


class UserAPI(MethodView):
    decorators = [oauth.require_oauth(), ]

    def get(self, identifier=None):
        if identifier is None:
            return jsonify({"error": "Need at least one parameter: email or ORCID ID."}), 400
        try:
            if EMAIL_REGEX.match(identifier):
                user = User.get(email=identifier)
            elif ORCID_ID_REGEX.match(identifier):
                try:
                    models.validate_orcid_id(identifier)
                except Exception as ex:
                    return jsonify({"error": f"Incorrect identifier value '{identifier}': {ex}"}), 400
                user = User.get(orcid=identifier)
            else:
                return jsonify({"error": f"Incorrect identifier value: {identifier}."}), 400
        except User.DoesNotExist:
            return jsonify({"error": f"User with specified identifier '{identifier}' not found."}), 404
        if not UserOrg.select().where(UserOrg.org == request.oauth.client.org, UserOrg.user == user).exists():
            return jsonify({"error": "Access Denied"}), 403
        return jsonify({
            "found": True,
            "resut": {
                "orcid": user.orcid,
                "email": user.email,
                "eppn": user.eppn
        }}), 200

app.add_url_rule("/api/v0.1/users/<identifier>",
        view_func=UserAPI.as_view("users"),
        methods=["GET", ])
<commit_msg>formatting<commit_after>"""HUB API"""

from flask import jsonify, request
from flask.views import MethodView
from flask_oauthlib.utils import create_response, extract_params
from flask_peewee.rest import Authentication, RestAPI, RestResource
from flask_peewee.utils import slugify
from oauthlib.common import add_params_to_uri, to_unicode, urlencode
from werkzeug.exceptions import NotFound

import models
from application import api, app, oauth
from models import EMAIL_REGEX, ORCID_ID_REGEX, User, UserOrg


class AppRestResource(RestResource):
    def get_api_name(self):
        return slugify(self.model.__name__ + 's')

    def response_forbidden(self):
        return jsonify({"error": 'Forbidden'}), 403

    def response_bad_method(self):
        return jsonify({"error": f'Unsupported method "{request.method}"'}), 405

    def response_bad_request(self):
        return jsonify({"error": 'Bad request'}), 400

    def api_detail(self, pk, method=None):
        try:
            return super().api_detail(pk, method)
        except NotFound:
            return jsonify({"error": 'Not found'}), 404


class UserResource(AppRestResource):
    exclude = (
        "password",
        "email",
    )


api.register(models.Organisation, AppRestResource)
api.register(models.Task, AppRestResource)
# api.register(models.User, UserResource)
api.setup()


@app.route('/api/me')
@app.route("/api/v0.1/me")
@oauth.require_oauth()
def me():
    user = request.oauth.user
    return jsonify(email=user.email, name=user.name)


class UserAPI(MethodView):
    decorators = [
        oauth.require_oauth(),
    ]

    def get(self, identifier=None):
        if identifier is None:
            return jsonify({"error": "Need at least one parameter: email or ORCID ID."}), 400
        try:
            if EMAIL_REGEX.match(identifier):
                user = User.get(email=identifier)
            elif ORCID_ID_REGEX.match(identifier):
                try:
                    models.validate_orcid_id(identifier)
                except Exception as ex:
                    return jsonify({
                        "error": f"Incorrect identifier value '{identifier}': {ex}"
                    }), 400
                user = User.get(orcid=identifier)
            else:
                return jsonify({"error": f"Incorrect identifier value: {identifier}."}), 400
        except User.DoesNotExist:
            return jsonify({
                "error": f"User with specified identifier '{identifier}' not found."
            }), 404
        if not UserOrg.select().where(UserOrg.org == request.oauth.client.org,
                                      UserOrg.user == user).exists():
            return jsonify({"error": "Access Denied"}), 403
        return jsonify({
            "found": True,
            "resut": {
                "orcid": user.orcid,
                "email": user.email,
                "eppn": user.eppn
            }
        }), 200


app.add_url_rule(
    "/api/v0.1/users/<identifier>", view_func=UserAPI.as_view("users"), methods=[
        "GET",
    ])
{'fraction_non_alphanumeric': 0.11956702747710242, 'fraction_numerical': 0.011323896752706078, 'mean_word_length': 2.615894039735099, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6446633', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1730, 'n_words': 420}