Beispiel #1
0
def email_validator(val):
    """Validate email."""
    if val:
        for each_email in val:
            pass
        return val
    raise MissingValueException('email')
Beispiel #2
0
def street_validator(val):
    """Validate street_address."""
    if val:
        for each_street in val:
            pass
        return val
    raise MissingValueException('street address')
Beispiel #3
0
def phone_validator(val):
    """Validate phone number."""
    if val:
        for each_phone in val:
            pass
        return val
    raise MissingValueException('phone')
    def get_dirty_ops(self, with_required=False):
        ''' Returns a dict with the update operations necessary to make the
            changes to this object to the database version.  It is mainly used
            internally for :func:`~mongoalchemy.session.Session.update` but
            may be useful for diagnostic purposes as well.

            :param with_required: Also include any field which is required.  This \
                is useful if the method is being called for the purposes of \
                an upsert where all required fields must always be sent.
        '''
        update_expression = {}
        for name, field in self.get_fields().items():
            if field.db_field == '_id':
                continue
            dirty_ops = field.dirty_ops(self)
            if not dirty_ops and with_required and field.required:
                dirty_ops = field.update_ops(self, force=True)
                if not dirty_ops:
                    raise MissingValueException(name)

            for op, values in dirty_ops.items():
                update_expression.setdefault(op, {})
                for key, value in values.items():
                    update_expression[op][key] = value

        if self.config_extra_fields == 'ignore':
            old_extrakeys = set(self.__extra_fields_orig.keys())
            cur_extrakeys = set(self.__extra_fields.keys())

            new_extrakeys = cur_extrakeys - old_extrakeys
            rem_extrakeys = old_extrakeys - cur_extrakeys
            same_extrakeys = cur_extrakeys & old_extrakeys

            update_expression.setdefault('$unset', {})
            for key in rem_extrakeys:
                update_expression['$unset'][key] = True

            update_expression.setdefault('$set', {})
            for key in new_extrakeys:
                update_expression['$set'][key] = self.__extra_fields[key]

            for key in same_extrakeys:
                if self.__extra_fields[key] != self.__extra_fields_orig[key]:
                    update_expression['$set'][key] = self.__extra_fields[key]
            if not update_expression['$unset']:
                del update_expression['$unset']

        return update_expression
Beispiel #5
0
 def wrap(self):
     ''' Returns a transformation of this document into a form suitable to 
         be saved into a mongo database.  This is done by using the ``wrap()``
         methods of the underlying fields to set values.'''
     res = {}
     for k, v in self.__extra_fields.iteritems():
         res[k] = v
     cls = self.__class__
     for name in self.get_fields():
         field = getattr(cls, name)
         try:
             value = getattr(self, name)
             res[field.db_field] = field.wrap(value)
         except AttributeError, e:
             if field.required:
                 raise MissingValueException(name)
         except FieldNotRetrieved, fne:
             if field.required:
                 raise
Beispiel #6
0
 def wrap(self):
     ''' Returns a transformation of this document into a form suitable to
         be saved into a mongo database.  This is done by using the ``wrap()``
         methods of the underlying fields to set values.'''
     res = {}
     for k, v in self.__extra_fields.items():
         res[k] = v
     cls = self.__class__
     for name in dir(cls):
         field = getattr(cls, name)
         if not isinstance(field, QueryField):
             continue
         try:
             value = getattr(self, name)
             # Ensure we don't insert a bunch of nulls if we have allow_none
             if value is None and field._allow_none:
                 continue
         except AttributeError:
             if field.required:
                 raise MissingValueException(name)
             continue
         res[field.db_field] = field.wrap(value)
     return res
Beispiel #7
0
    def get_dirty_ops(self, with_required=False):
        ''' Returns a dict with the update operations necessary to make the
            changes to this object to the database version.  It is mainly used
            internally for :func:`~mongoalchemy.session.Session.update` but
            may be useful for diagnostic purposes as well.

            :param with_required: Also include any field which is required.  This \
                is useful if the method is being called for the purposes of \
                an upsert where all required fields must always be sent.
        '''
        update_expression = {}
        for name, field in self.get_fields().items():
            dirty_ops = field.dirty_ops(self)
            if not dirty_ops and with_required and field.required:
                dirty_ops = field.update_ops(self)
                if not dirty_ops:
                    raise MissingValueException(name)

            for op, values in dirty_ops.items():
                update_expression.setdefault(op, {})
                for key, value in values.items():
                    update_expression[op][key] = value
        return update_expression