<commit_before>"""
Callbacks for Eve event hooks.
"""
import logging
import random
import json
from hashlib import sha1

from flask import abort, g
from bson import ObjectId

from atlas import tasks
from atlas import utilities
from atlas.config import (ATLAS_LOCATION, DEFAULT_CORE, DEFAULT_PROFILE, SERVICE_ACCOUNT_USERNAME)

# Setup a sub-logger. See tasks.py for longer comment.
log = logging.getLogger('atlas.callbacks')


# Callbacks
def pre_post_callback(resource, request):
    """
    :param resource: resource accessed
    :param request: flask.request object
    """
    log.debug('POST | Resource - %s | %s', resource, request)


def pre_post_sites_callback(request):
    """
    :param request: flask.request object
    """
    log.debug('sites | POST | Pre post callback')
    # Check to see if we have a current profile and core.
    core = utilities.get_current_code(name=DEFAULT_CORE, code_type='core')
    log.debug('sites | POST | Pre post callback | core | %s', core)
    profile = utilities.get_current_code(name=DEFAULT_PROFILE, code_type='profile')
    log.debug('sites | POST | Pre post callback | profile | %s', profile)

    if not core and not profile:
        log.error('sites | POST | Pre post callback | No current core or profile')
        abort(409, 'Error: There is no current core or profile.')
    elif not core:
        log.error('sites | POST | Pre post callback | No current core')
        abort(409, 'Error: There is no current core.')
    elif not profile:
        log.error('sites | POST | Pre post callback | No current profile')
        abort(409, 'Error: There is no current profile.')


def pre_delete_code_callback(request, lookup):
    """
    Make sure no sites are using the code.

    :param request: flask.request object
    :param lookup:
    """
    code = utilities.get_single_eve('code', lookup['_id'])
    log.debug('code | Delete | code - %s', code)

    # Check for sites using this piece of code.
    if code['meta']['code_type'] in ['module', 'theme', 'library']:
        code_type = 'package'
    else:
        code_type = code['meta']['code_type']
    log.debug('code | Delete | code - %s | code_type - %s', code['_id'], code_type)
    site_query = 'where={{"code.{0}":"{1}"}}'.format(code_type, code['_id'])
    sites = utilities.get_eve('sites', site_query)<commit_msg>Check if we have something with the same meta.<commit_after>"""
Callbacks for Eve event hooks.
"""
import logging
import random
import json
from hashlib import sha1

from flask import abort, g
from bson import ObjectId

from atlas import tasks
from atlas import utilities
from atlas.config import (ATLAS_LOCATION, DEFAULT_CORE, DEFAULT_PROFILE, SERVICE_ACCOUNT_USERNAME)

# Setup a sub-logger. See tasks.py for longer comment.
log = logging.getLogger('atlas.callbacks')


# Callbacks
def pre_post_callback(resource, request):
    """
    :param resource: resource accessed
    :param request: flask.request object
    """
    log.debug('POST | Resource - %s | %s', resource, request)


def pre_post_sites_callback(request):
    """
    :param request: flask.request object
    """
    log.debug('sites | POST | Pre post callback')
    # Check to see if we have a current profile and core.
    core = utilities.get_current_code(name=DEFAULT_CORE, code_type='core')
    log.debug('sites | POST | Pre post callback | core | %s', core)
    profile = utilities.get_current_code(name=DEFAULT_PROFILE, code_type='profile')
    log.debug('sites | POST | Pre post callback | profile | %s', profile)

    if not core and not profile:
        log.error('sites | POST | Pre post callback | No current core or profile')
        abort(409, 'Error: There is no current core or profile.')
    elif not core:
        log.error('sites | POST | Pre post callback | No current core')
        abort(409, 'Error: There is no current core.')
    elif not profile:
        log.error('sites | POST | Pre post callback | No current profile')
        abort(409, 'Error: There is no current profile.')


def pre_post_code_callback(request):
    """
    :param request: flask.request object
    """
    log.debug('code | POST | Pre post callback | %s', request)
    # Check to see if we have a current profile and core.
    code_query = 'where={{"meta.name":"{0}","meta.version":"{1}","meta.type":"{2}"}}'.format(request['meta']['name'], request['meta']['version'], request['meta']['type'])
    code = utilities.get_eve('code', code_query)

    log.debug('code | POST | Pre post callback | core query result | %s', code)

    if not code['_meta']['total'] == 0:
        log.error('code | POST | Pre post callback | Code exists with same name and version')
        abort(409, 'Error: There is already a code item of that type with that name and version.')


def pre_delete_code_callback(request, lookup):
    """
    Make sure no sites are using the code.

    :param request: flask.request object
    :param lookup:
    """
    code = utilities.get_single_eve('code', lookup['_id'])
    log.debug('code | Delete | code - %s', code)

    # Check for sites using this piece of code.
    if code['meta']['code_type'] in ['module', 'theme', 'library']:
        code_type = 'package'
    else:
        code_type = code['meta']['code_type']
    log.debug('code | Delete | code - %s | code_type - %s', code['_id'], code_type)
    site_query = 'where={{"code.{0}":"{1}"}}'.format(code_type, code['_id'])
    sites = utilities.get_eve('sites', site_query)
{'fraction_non_alphanumeric': 0.12993209763259314, 'fraction_numerical': 0.005689117269223711, 'mean_word_length': 3.5875420875420874, 'pattern_counts': {'":': 5, '<': 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': '7442021', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1609, 'n_words': 542}