<commit_before># -*- coding: utf-8 -*-

"""
    eve_mongoengine.validation
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module implements custom validator based on
    :class:`eve.io.mongo.validation`, which is cerberus-validator extension.

    The purpose of this module is to enable validation for special mongoengine
    fields.

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""

from flask import current_app as app
from mongoengine import ValidationError, FileField

from eve.io.mongo.validation import Validator
from eve_mongoengine._compat import iteritems
from werkzeug.datastructures import FileStorage
from mongoengine.fields import GridFSProxy

import pymongo

class EveMongoengineValidator(Validator):
    """
    Helper validator which adapts mongoengine special-purpose fields
    to cerberus validator API.
    """
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """
        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource and context is None:
            model_cls = app.data.models[self.resource]

            # We must translate any database field names to their corresponding
            # MongoEngine names before attempting to validate them.
            translate = lambda x: model_cls._reverse_db_field_map.get(x, x)
            document = {translate(k): document[k] for k in document}

            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if isinstance(field, FileField) and attr in document:
                    document[attr].stream.seek(0)
            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True

    def _validate_type_dynamic(self, field, value):
        """
        Dummy validation method just to convince cerberus not to validate that
        value.
        """
        pass

    def _validate_type_media(self, field, value):
        """ Enables validation for `media` data type.

        :param field: field name.
        :param value: field value.<commit_msg>add gridfsproxy support for post_internal<commit_after># -*- coding: utf-8 -*-

"""
    eve_mongoengine.validation
    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    This module implements custom validator based on
    :class:`eve.io.mongo.validation`, which is cerberus-validator extension.

    The purpose of this module is to enable validation for special mongoengine
    fields.

    :copyright: (c) 2014 by <NAME>.
    :license: BSD, see LICENSE for more details.
"""

from flask import current_app as app
from mongoengine import ValidationError, FileField

from eve.io.mongo.validation import Validator
from eve_mongoengine._compat import iteritems
from werkzeug.datastructures import FileStorage
from mongoengine.fields import GridFSProxy

import pymongo

class EveMongoengineValidator(Validator):
    """
    Helper validator which adapts mongoengine special-purpose fields
    to cerberus validator API.
    """
    def validate(self, document, schema=None, update=False, context=None):
        """
        Main validation method which simply tries to validate against cerberus
        schema and if it does not fail, repeats the same against mongoengine
        validation machinery.
        """
        # call default eve's validator
        if not Validator.validate(self, document, schema, update, context):
            return False

        # validate using mongoengine field validators
        if self.resource and context is None:
            model_cls = app.data.models[self.resource]

            # We must translate any database field names to their corresponding
            # MongoEngine names before attempting to validate them.
            translate = lambda x: model_cls._reverse_db_field_map.get(x, x)
            document = {translate(k): document[k] for k in document}

            doc = model_cls(**document)
            # rewind all file-like's
            for attr, field in iteritems(model_cls._fields):
                if (isinstance(field, FileField) and
                            attr in document and
                            not isinstance(document[attr], GridFSProxy)):
                    document[attr].stream.seek(0)

            try:
                doc.validate()
            except ValidationError as e:
                for field_name, error in e.errors.items():
                    self._error(field_name, str(e))
                return False

        return True

    def _validate_type_dynamic(self, field, value):
        """
        Dummy validation method just to convince cerberus not to validate that
        value.
        """
        pass

    def _validate_type_media(self, field, value):
        """ Enables validation for `media` data type.

        :param field: field name.
        :param value: field value.
{'fraction_non_alphanumeric': 0.07904035707643667, 'fraction_numerical': 0.0022317277292170355, 'mean_word_length': 2.4081115335868186, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '97275', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1316, 'n_words': 483}