<commit_before>""" API relating to configuration """
from flask_restful import fields, marshal_with, Resource
from flask_security import login_required

from .base import BaseDetailResource, BaseRequestParser
from .fields import NonBlankInput
from .util import new_edit_parsers
from dockci.models.auth import AuthenticatedRegistry
from dockci.server import API, DB


BASIC_FIELDS = {
    'display_name': fields.String(),
    'base_name': fields.String(),
    'username': fields.String(),
    'email': fields.String(),
    'detail': fields.Url('registry_detail'),
}

SHARED_PARSER_ARGS = {
    'display_name': dict(
        help="Registry display name",
        required=None, type=NonBlankInput(),
    ),
    'username': dict(
        help="Username for logging into the registry",
        required=None, type=NonBlankInput(),
    ),
    'password': dict(help="<PASSWORD> logging into the registry"),
    'email': dict(help="Email for logging into the registry"),
}

REGISTRY_NEW_PARSER = BaseRequestParser()
REGISTRY_EDIT_PARSER = BaseRequestParser()
new_edit_parsers(REGISTRY_NEW_PARSER, REGISTRY_EDIT_PARSER, SHARED_PARSER_ARGS)


# pylint:disable=no-self-use


class RegistryList(Resource):
    """ API resource that handles listing registries """
    @marshal_with(BASIC_FIELDS)
    def get(self):
        """ List of all projects """
        return AuthenticatedRegistry.query.all()


class RegistryDetail(BaseDetailResource):
    """
    API resource to handle getting registry details, creating new registries,
    updating existing registries, and deleting registries
    """
    @login_required
    @marshal_with(BASIC_FIELDS)
    def get(self, base_name):
        """ Get registry details """
        return AuthenticatedRegistry.query.filter_by(
            base_name=base_name
        ).first_or_404()

    @login_required
    @marshal_with(BASIC_FIELDS)
    def put(self, base_name):
        """ Create a new registry """
        registry = AuthenticatedRegistry(base_name=base_name)
        return self.handle_write(registry, REGISTRY_NEW_PARSER)

    @login_required
    @marshal_with(BASIC_FIELDS)
    def post(self, base_name):
        """ Update an existing registry """
        registry = AuthenticatedRegistry.query.filter_by(
            base_name=base_name,
        ).first_or_404()
        return self.handle_write(registry, REGISTRY_EDIT_PARSER)

    @login_required
    def delete(self, base_name):
        """ Delete a registry """
        registry = AuthenticatedRegistry.query.filter_by(
            base_name=base_name,
        ).first_or_404()
        display_name = registry.display_name
        DB.session.delete(registry)
        DB.session.commit()
        return {'message': '%s deleted' % display_name}


API.add_resource(RegistryList,
                 '/registries',
                 endpoint='registry_list')
API.add_resource(RegistryDetail,
                 '/registries/<string:base_name>',
                 endpoint='registry_detail')
<commit_msg>Username isn't a required field<commit_after>""" API relating to configuration """
from flask_restful import fields, inputs, marshal_with, Resource
from flask_security import login_required

from .base import BaseDetailResource, BaseRequestParser
from .fields import NonBlankInput
from .util import new_edit_parsers
from dockci.models.auth import AuthenticatedRegistry
from dockci.server import API, DB


BASIC_FIELDS = {
    'display_name': fields.String(),
    'base_name': fields.String(),
    'username': fields.String(),
    'email': fields.String(),
    'detail': fields.Url('registry_detail'),
}

SHARED_PARSER_ARGS = {
    'display_name': dict(
        help="Registry display name",
        required=None, type=NonBlankInput(),
    ),
    'username': dict(help="Username for logging into the registry"),
    'password': dict(help="<PASSWORD>"),
    'email': dict(help="Email for logging into the registry"),
}

REGISTRY_NEW_PARSER = BaseRequestParser()
REGISTRY_EDIT_PARSER = BaseRequestParser()
new_edit_parsers(REGISTRY_NEW_PARSER, REGISTRY_EDIT_PARSER, SHARED_PARSER_ARGS)


# pylint:disable=no-self-use


class RegistryList(Resource):
    """ API resource that handles listing registries """
    @marshal_with(BASIC_FIELDS)
    def get(self):
        """ List of all projects """
        return AuthenticatedRegistry.query.all()


class RegistryDetail(BaseDetailResource):
    """
    API resource to handle getting registry details, creating new registries,
    updating existing registries, and deleting registries
    """
    @login_required
    @marshal_with(BASIC_FIELDS)
    def get(self, base_name):
        """ Get registry details """
        return AuthenticatedRegistry.query.filter_by(
            base_name=base_name
        ).first_or_404()

    @login_required
    @marshal_with(BASIC_FIELDS)
    def put(self, base_name):
        """ Create a new registry """
        registry = AuthenticatedRegistry(base_name=base_name)
        return self.handle_write(registry, REGISTRY_NEW_PARSER)

    @login_required
    @marshal_with(BASIC_FIELDS)
    def post(self, base_name):
        """ Update an existing registry """
        registry = AuthenticatedRegistry.query.filter_by(
            base_name=base_name,
        ).first_or_404()
        return self.handle_write(registry, REGISTRY_EDIT_PARSER)

    @login_required
    def delete(self, base_name):
        """ Delete a registry """
        registry = AuthenticatedRegistry.query.filter_by(
            base_name=base_name,
        ).first_or_404()
        display_name = registry.display_name
        DB.session.delete(registry)
        DB.session.commit()
        return {'message': '%s deleted' % display_name}


API.add_resource(RegistryList,
                 '/registries',
                 endpoint='registry_list')
API.add_resource(RegistryDetail,
                 '/registries/<string:base_name>',
                 endpoint='registry_detail')

{'fraction_non_alphanumeric': 0.101892531260561, 'fraction_numerical': 0.003041568097330179, 'mean_word_length': 3.4436936936936937, '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': '6396508', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1717, 'n_words': 378}