<commit_before>""" API relating to Project model objects """
import re

import flask_restful
import sqlalchemy

from flask import request
from flask_restful import (fields,
                           inputs,
                           marshal,
                           marshal_with,
                           reqparse,
                           Resource,
                           )
from flask_security import current_user, login_required

from .base import BaseDetailResource, BaseRequestParser
from .exceptions import NoModelError, WrappedValueError
from .fields import NonBlankInput, RegexField, RegexInput, RewriteUrl
from .util import (clean_attrs,
                   DT_FORMATTER,
                   new_edit_parsers,
                   )
from dockci.models.auth import AuthenticatedRegistry
from dockci.models.job import Job
from dockci.models.project import Project
from dockci.server import API


DOCKER_REPO_RE = re.compile(r'^[a-z0-9]+(?:[._-][a-z0-9]+)*$')


def docker_repo_field(value, name):
    """ User input validation that a value is a valid Docker image name """
    if not DOCKER_REPO_RE.match(value):
        raise ValueError(("Invalid %s. Must start with a lower case, "
                          "alphanumeric character, and contain only the "
                          "additional characters '-', '_' and '.'") % name)
    return value


BASIC_FIELDS = {
    'name': fields.String(),
    'slug': fields.String(),
    'utility': fields.Boolean(),
    'status': fields.String(),
    'display_repo': fields.String(),
}


LIST_FIELDS = {
    'detail': RewriteUrl('project_detail', rewrites=dict(project_slug='slug')),
}
LIST_FIELDS.update(BASIC_FIELDS)

LATEST_JOB_FIELDS = {
    'detail': RewriteUrl('job_detail', rewrites=dict(
        project_slug='project.slug',
        job_slug='slug',
    )),
    'state': fields.String(),
    'create_ts': DT_FORMATTER,
}
LIST_FIELDS_LATEST_JOB = {
    'latest_job': fields.Nested(
        LATEST_JOB_FIELDS,
        attribute=lambda project: project.latest_job(),
        allow_null=True,
    ),
}
LIST_FIELDS_LATEST_JOB.update(LIST_FIELDS)
<commit_msg>Add git author gravatar to project latest job<commit_after>""" API relating to Project model objects """
import re

import flask_restful
import sqlalchemy

from flask import request
from flask_restful import (fields,
                           inputs,
                           marshal,
                           marshal_with,
                           reqparse,
                           Resource,
                           )
from flask_security import current_user, login_required

from .base import BaseDetailResource, BaseRequestParser
from .exceptions import NoModelError, WrappedValueError
from .fields import (GravatarUrl,
                     NonBlankInput,
                     RegexField,
                     RegexInput,
                     RewriteUrl,
                     )
from .util import (clean_attrs,
                   DT_FORMATTER,
                   new_edit_parsers,
                   )
from dockci.models.auth import AuthenticatedRegistry
from dockci.models.job import Job
from dockci.models.project import Project
from dockci.server import API


DOCKER_REPO_RE = re.compile(r'^[a-z0-9]+(?:[._-][a-z0-9]+)*$')


def docker_repo_field(value, name):
    """ User input validation that a value is a valid Docker image name """
    if not DOCKER_REPO_RE.match(value):
        raise ValueError(("Invalid %s. Must start with a lower case, "
                          "alphanumeric character, and contain only the "
                          "additional characters '-', '_' and '.'") % name)
    return value


BASIC_FIELDS = {
    'name': fields.String(),
    'slug': fields.String(),
    'utility': fields.Boolean(),
    'status': fields.String(),
    'display_repo': fields.String(),
}


LIST_FIELDS = {
    'detail': RewriteUrl('project_detail', rewrites=dict(project_slug='slug')),
}
LIST_FIELDS.update(BASIC_FIELDS)

LATEST_JOB_FIELDS = {
    'detail': RewriteUrl('job_detail', rewrites=dict(
        project_slug='project.slug',
        job_slug='slug',
    )),
    'state': fields.String(),
    'create_ts': DT_FORMATTER,
    'git_author_avatar': GravatarUrl(attr_name='git_author_email')
}
LIST_FIELDS_LATEST_JOB = {
    'latest_job': fields.Nested(
        LATEST_JOB_FIELDS,
        attribute=lambda project: project.latest_job(),
        allow_null=True,
    ),
}
LIST_FIELDS_LATEST_JOB.update(LIST_FIELDS)

{'fraction_non_alphanumeric': 0.10127716782433341, 'fraction_numerical': 0.0017925162446784674, 'mean_word_length': 2.3998476770754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4187585', 'n_tokens_mistral': 1361, 'n_tokens_neox': 1332, 'n_words': 302}