def clean(self, val, doc=None): try: if isinstance(val, list): if len(val) == 2: return val else: raise FieldException("to many values: %s" % val) else: raise FieldException("must be type array") except: raise FieldException("%s must be array" % val)
def clean(self, val, doc=None): if not self._database: raise FieldException("database is required") if isinstance(val, ObjectId): return val try: f = GridFS(self._database, collection=self._collection) self._args["filename"] = self._args.get("filename", self._name) return f.put(val, **self._args) except Exception as e: raise FieldException(e.message)
def clean(self, val, doc=None): if isinstance(val, Document): if val._id is not None: cls = "%s.%s" % (val.__module__, val.__class__.__name__) return {"cls":cls, "id":val._id} else: raise FieldException("Document does not have an id. Be sure to save first.") elif isinstance(val, dict): return val else: raise FieldException("%s is not a valid document type" % val.__class__.__name__)
def clean(self, val, doc=None): v = val._id if hasattr(val, '_id') else val if hasattr(val, '_id') and not val._id: raise FieldException("Object has not been saved yet") if v: try: v = ObjectId(v) except Exception as e: raise FieldException("Invalid ObjectId") return v
def clean(self, val, doc=None): try: if isinstance(val, bool): return val v = bool(val) return bool(val) except: raise FieldException("%s invalid boolean" % val)
def clean(self, val, doc=None): val = super(Regex, self).clean(val, doc) if not self._reg.search(val): raise FieldException( "%s: pattern not found" % val if not self._disp_error else self._disp_error) return val
def get_choices(self, render=None): if render: cur = self._type.find(self._query, fields=self._fields) cur = cur.sort(self._sort) if self._sort else cur return [render(i) for i in cur] else: raise FieldException("no render method available")
def clean(self, val, doc=None): val = super(Choice, self).clean(val, doc=doc) vals = [ opt['value'] if isinstance(opt, dict) else opt for opt in self._choices ] if not val in vals: raise FieldException("%s is not a valid option") return val
def clean(self, val, doc=None): try: val = self._type(val) if self._max is not None and len(val) > self._max: raise MaxException("must be less than %s" % self._max) if self._min is not None and len(val) < self._min: raise MinException("must be greater than %s" % self._min) return val except FieldException as e: raise e except: raise FieldException("%s is not a valid %s" % (val, self._exception_display))
def get_choices(self, render=None): if render: print self._db print self._collection cur = self._conn[self._db][self._collection].find( self._query, fields=self._fields) cur = cur.sort(self._sort) if self._sort else cur return [render(i) for i in cur] else: raise FieldException("no render method available")
def clean(self, val, doc=None): try: if val not in [None, False]: val = self._type(val) if self._max != None and val > self._max: raise MaxException("must be less than %s" % self._max) if self._min != None and val < self._min: raise MinException("must be greater than %s" % self._min) return val except FieldException as e: raise e except: raise FieldException("%s is not a valid %s" % (val, self._exception_display))
def clean(self, val, doc=None): try: if isinstance(val, datetime.datetime): return val return datetime.datetime(val) except: raise FieldException("%s: invalid datetime" % val)
def __call__(self): if isinstance(self._value, ObjectId): f = GridFS(self._database, collection=self._collection) return f.get(self._value) else: raise FieldException("No file associated")
def clean(self, val, doc=None): val = super(Phone, self).clean(val, doc) try: return parse_phone(val) except: raise FieldException("%s is not a valid format" % val)
def __call__(self): if not self._value is None and self._type: return self._type(id=self._value) else: raise FieldException("Cannot instantiate %s with id %s" % (self._type, self._value))