Exemple #1
0
 def validate(self, value):
     if len(value) != SHA1Field.hash_length:
         raise ShieldException('SHA1 value is wrong length',
                               self.field_name, value)
     try:
         int(value, 16)
     except:
         raise ShieldException('SHA1 value is not hex', self.field_name,
                               value)
Exemple #2
0
    def validate(self, value):
        if not URLField.URL_REGEX.match(value):
            raise ShieldException('Invalid URL', self.field_name, value)

        if self.verify_exists:
            import urllib2
            try:
                request = urllib2.Request(value)
                urllib2.urlopen(request)
            except Exception:
                message = 'URL does not exist'
                raise ShieldException(message, self.field_name, value)
Exemple #3
0
    def validate(self, value):
        """Make sure that a list of valid fields is being used.
        """
        if not isinstance(value, (dict, MultiValueDict)):
            raise ShieldException(
                'Only dictionaries or MultiValueDict may be '
                'used in a DictField', self.field_name, value)

        if any(('.' in k or '$' in k) for k in value):
            raise ShieldException(
                'Invalid dictionary key name - keys may not '
                'contain "." or "$" characters', self.field_name, value)
Exemple #4
0
    def validate(self, value):
        assert isinstance(value, (str, unicode))

        if self.max_length is not None and len(value) > self.max_length:
            raise ShieldException('String value is too long', self.field_name,
                                  value)

        if self.min_length is not None and len(value) < self.min_length:
            raise ShieldException('String value is too short', self.uniq_field,
                                  value)

        if self.regex is not None and self.regex.match(value) is None:
            message = 'String value did not match validation regex',
            raise ShieldException(message, self.uniq_field, value)
Exemple #5
0
 def validate(self, value):
     if not isinstance(value, bson.objectid.ObjectId):
         try:
             value = bson.objectid.ObjectId(unicode(value))
         except Exception, e:
             raise ShieldException('Invalid ObjectId', self.field_name,
                                   value)
Exemple #6
0
 def validate(self, value):
     if not isinstance(value, (basestring, str)):
         try:
             value = self.encoder.encode_hex(value)
         except Exception, e:
             raise ShieldException('Invalid IntField', self.field_name,
                                   value)
Exemple #7
0
    def validate(self, validate_all=False):
        """
        Taken from dictshield
        Ensure that all fields' values are valid and that
        required fields are present.
        Throws a ShieldDocException if Document is invalid
        """
        # Get a list of tuples of field names and their current values
        fields = [(field, getattr(self, name))
                  for name, field in self._fields.items()]

        # Ensure that each field is matched to a valid value
        errs = []
        for field, value in fields:
            err = None
            # treat empty strings as nonexistent
            if value is not None and value != '':
                try:
                    field._validate(value)
                except ShieldException, e:
                    err = e
                except (ValueError, AttributeError, AssertionError):
                    err = ShieldException('Invalid value',
                                          field.field_name,
                                          value)
Exemple #8
0
    def validate(self, value):
        try:
            value = self.number_class(value)
        except:
            raise ShieldException('Not %s' % self.number_type, self.field_name,
                                  value)

        if self.min_value is not None and value < self.min_value:
            raise ShieldException(
                '%s value below min_value: %s' %
                (self.number_type, self.min_value), self.field_name, value)

        if self.max_value is not None and value > self.max_value:
            raise ShieldException(
                '%s value above max_value: %s' %
                (self.number_type, self.max_value), self.field_name, value)
Exemple #9
0
    def _validate_helper(cls,
                         field_inspector,
                         values,
                         validate_all=False,
                         delete_rogues=True):
        """This is a convenience function that loops over the given values
        and attempts to validate them against the class definition. It only
        validates the data in values and does not guarantee a complete document
        is present.

        'not present' is defined as not having a value OR having '' (or u'')
        as a value.
        """
        if not hasattr(cls, '_fields'):
            raise ValueError('cls is not a Document instance')

        internal_fields = cls._get_internal_fields()

        # Create function for handling exceptions
        exceptions = list()
        handle_exception = cls._gen_handle_exception(validate_all, exceptions)

        # Create function for handling a flock of frakkin palins (rogue fields)
        data_fields = set(values.keys())
        class_fields = list()
        handle_class_field = cls._gen_handle_class_field(
            delete_rogues, class_fields)

        # Loop across fields present in model
        for k, v in cls._fields.items():

            # handle common id name
            if k is 'id':
                k = '_id'

            handle_class_field(k)

            # we don't accept internal fields from users
            if k in internal_fields and k in values:
                value_is_default = (values[k] is v.default)
                if not value_is_default:
                    error_msg = 'Overwrite of internal fields attempted'
                    e = ShieldException(error_msg, k, v)
                    handle_exception(e)
                    continue

            if field_inspector(k, v):
                datum = values[k]
                # if datum is None, skip
                if datum is None:
                    continue
                # treat empty strings as empty values and skip
                if isinstance(datum, (str, unicode)) and \
                       len(datum.strip()) == 0:
                    continue
                try:
                    v.validate(datum)
                except ShieldException, e:
                    handle_exception(e)
Exemple #10
0
    def validate(self, value):
        """Make sure that a list of valid fields is being used.
        """
        if not isinstance(value, (list, tuple)):
            error_msg = 'Only lists and tuples may be used in a list field'
            raise ShieldException(error_msg, self.field_name, value)

        if not self.fields:  # if we want everything to validate
            return

        for item in value:
            try:
                for field in self.fields:
                    field.validate(item)
            except Exception, e:
                raise ShieldException('Invalid ListField item',
                                      self.field_name, str(item))
Exemple #11
0
    def _validate(self, value):
        # check choices
        if self.choices is not None:
            if value not in self.choices:
                raise ShieldException("Value must be one of %s." %
                                      unicode(self.choices))

        # check validation argument
        if self.validation is not None:
            if callable(self.validation):
                if not self.validation(value):
                    raise ShieldException('Value does not match custom' \
                                          'validation method.')
            else:
                raise ValueError('validation argument must be a callable.')

        self.validate(value)
Exemple #12
0
    def validate(self, value):
        if not isinstance(value, decimal.Decimal):
            if not isinstance(value, basestring):
                value = str(value)
            try:
                value = decimal.Decimal(value)
            except Exception:
                raise ShieldException('Could not convert to decimal',
                                      self.field_name, value)

        if self.min_value is not None and value < self.min_value:
            raise ShieldException(
                'Decimal value below min_value: %s' % self.min_value,
                self.field_name, value)

        if self.max_value is not None and value > self.max_value:
            raise ShieldException(
                'Decimal value above max_value: %s' % self.max_value,
                self.field_name, value)
Exemple #13
0
 def validate(self, value):
     """Make sure that the document instance is an instance of the
     EmbeddedDocument subclass provided when the document was defined.
     """
     # Using isinstance also works for subclasses of self.document
     if not isinstance(value, self.document_type):
         raise ShieldException(
             'Invalid embedded document instance '
             'provided to an EmbeddedDocumentField', self.field_name, value)
     self.document_type.validate(value)
Exemple #14
0
 def __init__(self, document_type, **kwargs):
     is_embeddable = lambda dt: issubclass(dt, EmbeddedDocument)
     if not isinstance(document_type, basestring):
         if not document_type or not is_embeddable(document_type):
             raise ShieldException(
                 'Invalid embedded document class '
                 'provided to an EmbeddedDocumentField', self.field_name,
                 document_type)
     self.document_type_obj = document_type
     super(EmbeddedDocumentField, self).__init__(**kwargs)
Exemple #15
0
 def validate(self, value):
     """Make sure the value is a valid uuid representation.  See
     http://docs.python.org/library/uuid.html for accepted formats.
     """
     if not isinstance(value, (uuid.UUID, )):
         try:
             uuid.UUID(value)
         except ValueError:
             raise ShieldException('Not a valid UUID value',
                                   self.field_name, value)
Exemple #16
0
    def validate(self):
        """Ensure that all fields' values are valid and that required fields
        are present.
        """
        # Get a list of tuples of field names and their current values
        fields = [(field, getattr(self, name))
                  for name, field in self._fields.items()]

        # Ensure that each field is matched to a valid value
        for field, value in fields:
            # treat empty strings is nonexistent
            if value is not None and value != '':
                try:
                    field._validate(value)
                except (ValueError, AttributeError, AssertionError):
                    raise ShieldException('Invalid value', field.field_name,
                                          value)
            elif field.required:
                raise ShieldException('Required field missing',
                                      field.field_name, value)
Exemple #17
0
 def validate(self, value):
     """Make sure that a geo-value is of type (x, y)
     """
     if not len(value) == 2:
         raise ShieldException('Value must be a two-dimensional point',
                               self.field_name, value)
     if isinstance(value, dict):
         for v in value.values():
             if not isinstance(v, (float, int)):
                 error_msg = 'Both values in point must be float or int'
                 raise ShieldException(error_msg, self.field_name, value)
     elif isinstance(value, (list, tuple)):
         if (not isinstance(value[0], (float, int))
                 and not isinstance(value[1], (float, int))):
             error_msg = 'Both values in point must be float or int'
             raise ShieldException(error_msg, self.field_name, value)
     else:
         raise ShieldException(
             'GeoPointField can only accept tuples, '
             'lists of (x, y), or dicts of {k1: v1, '
             'k2: v2}', self.field_name, value)
Exemple #18
0
class BaseDocument(object):
    def __init__(self, **values):
        self._data = {}
        minimized_field_map = {}

        # Assign default values to instance
        for attr_name, attr_value in self._fields.items():
            # Use default value if present
            value = getattr(self, attr_name, None)
            setattr(self, attr_name, value)
            if attr_value.minimized_field_name:
                field_name = attr_value.minimized_field_name
                minimized_field_map[field_name] = attr_value.uniq_field

        # Assign initial values to instance
        for attr_name, attr_value in values.items():
            try:
                if attr_name == '_id':
                    attr_name = 'id'
                setattr(self, attr_name, attr_value)
                if attr_name in minimized_field_map:
                    setattr(self, minimized_field_map[attr_name], attr_value)
            # Put a diaper on the keys that don't belong and send 'em home
            except AttributeError:
                pass

    def validate(self, validate_all=False):
        """Ensure that all fields' values are valid and that required fields
        are present.

        Throws a ShieldDocException if Document is invalid
        """
        # Get a list of tuples of field names and their current values
        fields = [(field, getattr(self, name))
                  for name, field in self._fields.items()]

        # Ensure that each field is matched to a valid value
        errs = []
        for field, value in fields:
            err = None
            # treat empty strings as nonexistent
            if value is not None and value != '':
                try:
                    field._validate(value)
                except ShieldException, e:
                    err = e
                except (ValueError, AttributeError, AssertionError):
                    err = ShieldException('Invalid value', field.field_name,
                                          value)
            elif field.required:
                err = ShieldException('Required field missing',
                                      field.field_name, value)
Exemple #19
0
 def date_to_iso8601(cls, dt, format):
     """Classmethod that goes the opposite direction of iso8601_to_date.
        Defaults to using isoformat(), but can use the optional format
        argument either as a strftime format string or as a custom
        date formatting function or lambda.
     """
     if isinstance(format, str):
         iso_dt = dt.strftime(format)
     elif hasattr(format, '__call__'):
         iso_dt = format(dt)
     else:
         raise ShieldException(
             'DateTimeField format must be a string or callable')
     return iso_dt
Exemple #20
0
class Model(six.with_metaclass(ModelMetaClass)):

    #__metaclass__ = ModelMetaClass

    def __init__(self):
        self._data = {}

    def to_python(self):
        """
        Returns a Python dictionary representing the Document's
        meta-structure and values.
        """
        fun = lambda f, v: f.for_python(v)
        data = self._to_fields(fun)
        return data

    def _to_fields(self, field_converter):
        """
        Returns a Python dictionary representing the Document's
        meta-structure and values.
        """
        data = {}

        # First map the subclasses of BaseField
        for field_name, field in self._fields.items():
            value = getattr(self, field_name, None)
            if value is not None:
                if field.id_field:
                    data[field.field_name] = field_converter(field, value)
                else:
                    data[field.uniq_field] = field_converter(field, value)

        return data

    def set_dict(self, dict_obj):
        self.dict_obj = dict_obj
        self.set_attr(dict_obj)

    def set_attr(self, dict_obj):
        for key, value in dict_obj.items():
            if isinstance(value, dict):
                self.set_attr(value)

            if hasattr(self, key):
                setattr(self, key, value)

    def save_wide(self, key, ttl=None):
        """
        Insert/updates a column into dynamic column family
        """
        if not self.column_key:
            raise ValueError("Wide table requires a column key")

        try:
            if isinstance(self.column_key, basestring):
                self.column_key = uuid.UUID(self.column_key)
        except ValueError:
            print '-'

        self.objects.insert(str(key), {
            self.column_key: str(self.to_python())
        }, ttl=ttl)

    def save(self, cols=None, ttl=None):
        """
        Insert/updates a row
        """
        try:
            key = getattr(self, self._key_field)
        except AttributeError:
            raise AttributeError("Key not set")


        if hasattr(self._meta, 'family_type') and \
                        self._meta.family_type == ColumnFamilyTypes.WIDE:
            return self.save_wide(key, ttl=ttl)

        row = self.to_python()

        if self._key_field in row:
            del row[self._key_field]
        self.objects.insert(key, row, ttl=ttl)

    def dict(self):
        """
        Use this to serialise the object back into a dict object
        """
        data = {}

        if hasattr(self, 'key'):
            data['key'] = str(self.key)

        if hasattr(self, 'column_key'):
            data['column_key'] = str(self.column_key)

        for key, value in self._data.items():
            if isinstance(value, uuid.UUID):
                value = str(value)
            elif isinstance(value, datetime.datetime):
                value = value.strftime('%m/%d/%Y')

            if value is not None:
                data[key] = value
        return data

    # @property
    # def column_key(self):
    #     return self._column_key
    #
    # @column_key.setter
    # def column_key(self, column_name):
    #     self._column_key = column_name

    def validate(self, validate_all=False):
        """
        Taken from dictshield
        Ensure that all fields' values are valid and that
        required fields are present.
        Throws a ShieldDocException if Document is invalid
        """
        # Get a list of tuples of field names and their current values
        fields = [(field, getattr(self, name))
                  for name, field in self._fields.items()]

        # Ensure that each field is matched to a valid value
        errs = []
        for field, value in fields:
            err = None
            # treat empty strings as nonexistent
            if value is not None and value != '':
                try:
                    field._validate(value)
                except ShieldException, e:
                    err = e
                except (ValueError, AttributeError, AssertionError):
                    err = ShieldException('Invalid value',
                                          field.field_name,
                                          value)
            elif field.required:
                err = ShieldException('Required field missing',
                                      field.field_name,
                                      value)
Exemple #21
0
 def validate(self, value):
     if len(value) != BCryptField.hash_length:
         raise ShieldException('BCrypt value is wrong length',
                               self.field_name, value)
     return value
Exemple #22
0
 def validate(self, value):
     if not isinstance(value, bool):
         raise ShieldException('Not a boolean', self.field_name, value)
Exemple #23
0
 def validate(self, value):
     try:
         bson.objectid.ObjectId(unicode(value))
     except Exception, e:
         raise ShieldException('Invalid ObjectId', self.field_name, value)
Exemple #24
0
 def for_python(self, value):
     try:
         return bson.objectid.ObjectId(unicode(value))
     except Exception, e:
         raise ShieldException('Invalid ObjectId', self.field_name, value)
Exemple #25
0
 def validate(self, value):
     if not EmailField.EMAIL_REGEX.match(value):
         raise ShieldException('Invalid email address', self.field_name,
                               value)
Exemple #26
0
 def validate(self, value):
     if not isinstance(value, datetime.datetime):
         raise ShieldException('Not a datetime', self.field_name, value)