<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import datetime

import flask
from flask import json
import sqlalchemy.sql

from dci.server.api.v1 import api
from dci.server.api.v1 import utils as v1_utils
from dci.server.common import exceptions as dci_exc
from dci.server.common import schemas
from dci.server.common import utils
from dci.server.db import models_core as models

# associate column names with the corresponding SA Column object
_C_COLUMNS = v1_utils.get_columns_name_with_objects(models.COMPONENTS)
_VALID_EMBED = {'componenttype': models.COMPONENTYPES}


def _verify_existence_and_get_c(c_id):
    return v1_utils.verify_existence_and_get(
        models.COMPONENTS, c_id,
        sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                           models.COMPONENTS.c.name == c_id))


@api.route('/components', methods=['POST'])
def create_components():
    data_json = flask.request.json
    # verif post
    etag = utils.gen_etag()
    values = {'id': utils.gen_uuid(),
              'created_at': datetime.datetime.utcnow().isoformat(),
              'updated_at': datetime.datetime.utcnow().isoformat(),
              'etag': etag,
              'name': data_json['name'],
              'canonical_project_name': data_json.get(
                  'canonical_project_name', None),
              'data': data_json.get('data', None),
              'sha': data_json.get('sha', None),
              'title': data_json.get('title', None),
              'message': data_json.get('message', None),
              'url': data_json.get('url', None),
              'git': data_json.get('git', None),
              'ref': data_json.get('ref', None),
              'componenttype_id': data_json.get('componenttype_id', None)}

    query = models.COMPONENTS.insert().values(**values)

    flask.g.db_conn.execute(query)

    # verif dump
    result = {'component': values}
    result = json.dumps(result)
    return flask.Response(result, 201, headers={'ETag': etag},
                          content_type='application/json')


@api.route('/components', methods=['GET'])
def get_all_components(ct_id=None):
    """Get all components.

    If ct_id is not None, then return all the components with a type
    pointed by ct_id.
    """
    # get the diverse parameters
    args = schemas.args(flask.request.args.to_dict())

    v1_utils.verify_embed_list(args['embed'], _VALID_EMBED.keys())

    # the default query with no parameters
    query = sqlalchemy.sql.select([models.COMPONENTS])

    # if embed then construct the query with a join
    if args['embed']:
        resources_to_embed = (_VALID_EMBED[elem] for elem in args['embed'])
        query = v1_utils.get_query_with_join(models.COMPONENTS,
                                             *resources_to_embed)

    query = v1_utils.sort_query(query, args['sort'], _C_COLUMNS)
    query = v1_utils.where_query(query, args['where'], models.COMPONENTS,
                                 _C_COLUMNS)

    # used for counting the number of rows when ct_id is not None
    where_ct_cond = None
    if ct_id is not None:
        where_ct_cond = models.COMPONENTS.c.componenttype_id == ct_id
        query = query.where(where_ct_cond)

    # adds the limit/offset parameters
    query = query.limit(args['limit']).offset(args['offset'])

    # get the number of rows for the '_meta' section
    nb_cts = utils.get_number_of_rows(models.COMPONENTS, where_ct_cond)

    rows = flask.g.db_conn.execute(query).fetchall()
    result = [v1_utils.group_embedded_resources(args['embed'], row)
              for row in rows]

    # verif dump
    result = {'components': result, '_meta': {'count': nb_cts}}
    result = json.dumps(result, default=utils.json_encoder)
    return flask.Response(result, 200, content_type='application/json')


@api.route('/components/<c_id>', methods=['GET'])
def get_component_by_id_or_name(c_id):
    # get the diverse parameters
    embed = schemas.args(flask.request.args.to_dict())['embed']
    v1_utils.verify_embed_list(embed, _VALID_EMBED.keys())

    # the default query with no parameters
    query = sqlalchemy.sql.select([models.COMPONENTS])

    # if embed then construct the query with a join
    if embed:
        resources_to_embed = (_VALID_EMBED[elem] for elem in embed)
        query = v1_utils.get_query_with_join(models.COMPONENTS,
                                             *resources_to_embed)

    query = query.where(sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                                           models.COMPONENTS.c.name == c_id))

    row = flask.g.db_conn.execute(query).fetchone()
    component = v1_utils.group_embedded_resources(embed, row)

    if row is None:
        raise dci_exc.DCIException("component '%s' not found." % c_id,
                                   status_code=404)

    etag = component['etag']
    # verif dump
    component = {'component': component}
    component = json.dumps(component, default=utils.json_encoder)
    return flask.Response(component, 200, headers={'ETag': etag},
                          content_type='application/json')


@api.route('/components/<c_id>', methods=['DELETE'])
def delete_component_by_id_or_name(c_id):
    # get If-Match header
    if_match_etag = utils.check_and_get_etag(flask.request.headers)

    _verify_existence_and_get_c(c_id)

    query = models.COMPONENTS.delete().where(
        sqlalchemy.sql.and_(
            sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                               models.COMPONENTS.c.name == c_id),
            models.COMPONENTS.c.etag == if_match_etag))

    result = flask.g.db_conn.execute(query)

    if result.rowcount == 0:
        raise dci_exc.DCIException("Component '%s' already deleted or "
                                   "etag not matched." % c_id,
                                   status_code=409)

    return flask.Response(None, 204, content_type='application/json')
<commit_msg>Integration of voluptuous validation on component<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Red Hat, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import datetime

import flask
from flask import json
import sqlalchemy.sql

from dci.server.api.v1 import api
from dci.server.api.v1 import utils as v1_utils
from dci.server.common import exceptions as dci_exc
from dci.server.common import schemas
from dci.server.common import utils
from dci.server.db import models_core as models

# associate column names with the corresponding SA Column object
_C_COLUMNS = v1_utils.get_columns_name_with_objects(models.COMPONENTS)
_VALID_EMBED = {'componenttype': models.COMPONENTYPES}


def _verify_existence_and_get_c(c_id):
    return v1_utils.verify_existence_and_get(
        models.COMPONENTS, c_id,
        sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                           models.COMPONENTS.c.name == c_id))


@api.route('/components', methods=['POST'])
def create_components():
    etag = utils.gen_etag()
    values = schemas.component.post(flask.request.json)
    values.update({'id': utils.gen_uuid(),
                   'created_at': datetime.datetime.utcnow().isoformat(),
                   'updated_at': datetime.datetime.utcnow().isoformat(),
                   'etag': etag})

    query = models.COMPONENTS.insert().values(**values)

    flask.g.db_conn.execute(query)

    result = json.dumps({'component': values})
    return flask.Response(result, 201, headers={'ETag': etag},
                          content_type='application/json')


@api.route('/components', methods=['GET'])
def get_all_components(ct_id=None):
    """Get all components.

    If ct_id is not None, then return all the components with a type
    pointed by ct_id.
    """
    # get the diverse parameters
    args = schemas.args(flask.request.args.to_dict())

    v1_utils.verify_embed_list(args['embed'], _VALID_EMBED.keys())

    # the default query with no parameters
    query = sqlalchemy.sql.select([models.COMPONENTS])

    # if embed then construct the query with a join
    if args['embed']:
        resources_to_embed = (_VALID_EMBED[elem] for elem in args['embed'])
        query = v1_utils.get_query_with_join(models.COMPONENTS,
                                             *resources_to_embed)

    query = v1_utils.sort_query(query, args['sort'], _C_COLUMNS)
    query = v1_utils.where_query(query, args['where'], models.COMPONENTS,
                                 _C_COLUMNS)

    # used for counting the number of rows when ct_id is not None
    where_ct_cond = None
    if ct_id is not None:
        where_ct_cond = models.COMPONENTS.c.componenttype_id == ct_id
        query = query.where(where_ct_cond)

    # adds the limit/offset parameters
    query = query.limit(args['limit']).offset(args['offset'])

    # get the number of rows for the '_meta' section
    nb_cts = utils.get_number_of_rows(models.COMPONENTS, where_ct_cond)

    rows = flask.g.db_conn.execute(query).fetchall()
    result = [v1_utils.group_embedded_resources(args['embed'], row)
              for row in rows]

    result = {'components': result, '_meta': {'count': nb_cts}}
    result = json.dumps(result, default=utils.json_encoder)
    return flask.Response(result, 200, content_type='application/json')


@api.route('/components/<c_id>', methods=['GET'])
def get_component_by_id_or_name(c_id):
    # get the diverse parameters
    embed = schemas.args(flask.request.args.to_dict())['embed']
    v1_utils.verify_embed_list(embed, _VALID_EMBED.keys())

    # the default query with no parameters
    query = sqlalchemy.sql.select([models.COMPONENTS])

    # if embed then construct the query with a join
    if embed:
        resources_to_embed = (_VALID_EMBED[elem] for elem in embed)
        query = v1_utils.get_query_with_join(models.COMPONENTS,
                                             *resources_to_embed)

    query = query.where(sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                                           models.COMPONENTS.c.name == c_id))

    row = flask.g.db_conn.execute(query).fetchone()
    component = v1_utils.group_embedded_resources(embed, row)

    if row is None:
        raise dci_exc.DCIException("component '%s' not found." % c_id,
                                   status_code=404)

    etag = component['etag']
    component = {'component': component}
    component = json.dumps(component, default=utils.json_encoder)
    return flask.Response(component, 200, headers={'ETag': etag},
                          content_type='application/json')


@api.route('/components/<c_id>', methods=['DELETE'])
def delete_component_by_id_or_name(c_id):
    # get If-Match header
    if_match_etag = utils.check_and_get_etag(flask.request.headers)

    _verify_existence_and_get_c(c_id)

    query = models.COMPONENTS.delete().where(
        sqlalchemy.sql.and_(
            sqlalchemy.sql.or_(models.COMPONENTS.c.id == c_id,
                               models.COMPONENTS.c.name == c_id),
            models.COMPONENTS.c.etag == if_match_etag))

    result = flask.g.db_conn.execute(query)

    if result.rowcount == 0:
        raise dci_exc.DCIException("Component '%s' already deleted or "
                                   "etag not matched." % c_id,
                                   status_code=409)

    return flask.Response(None, 204, content_type='application/json')

{'fraction_non_alphanumeric': 0.11032429387623723, 'fraction_numerical': 0.006598535447010541, 'mean_word_length': 2.8789013732833957, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3604641', 'n_tokens_mistral': 3936, 'n_tokens_neox': 3750, 'n_words': 896}