t.d.DBObject(Validator) : class documentation

Part of twistar.dbobject View In Hierarchy

Known subclasses: twistar.tests.utils.Avatar, twistar.tests.utils.Blogpost, twistar.tests.utils.Boy, twistar.tests.utils.Category, twistar.tests.utils.Coltest, twistar.tests.utils.FakeObject, twistar.tests.utils.FavoriteColor, twistar.tests.utils.Girl, twistar.tests.utils.Nickname, twistar.tests.utils.Picture, twistar.tests.utils.User (hide last 8 again) ... and 8 more

A base class for representing objects stored in a RDBMS.
See AlsoRelationship, HasMany, HasOne, HABTM, BelongsTo

Split Table into Classes Show Methods in One Table

Line # Kind Name Docs
0 Class Variable HASMANY A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of, for instance 'users'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasMany for more information.
0 Class Variable HASONE A list made up of some number of strings and dicts. If an element is a string, it represents what the class has one of, for instance 'location'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasOne for more information.
0 Class Variable HABTM A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of (and which in turn has many of this current object type), for instance a teacher has and belongs to many students. Both the Student and Teacher classes should have a class variable that is HABTM = ['teachers'] and HABTM = ['students'], respectively. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HABTM for more information.
0 Class Variable BELONGSTO A list made up of some number of strings and dicts. If an element is a string, it represents what the class belongs to, for instance 'user'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and BelongsTo for more information.
0 Class Variable TABLENAME If specified, use the given tablename as the one for this object. Otherwise, use the lowercase, plural version of this class's name. See the DBObject.tablename method.
61 Method __init__ Constructor. DO NOT OVERWRITE. Use the DBObject.afterInit method.
80 Method updateAttrs Set the attributes of this object based on the given dict.
91 Method save Save this object to the database. Validation is performed first; if the validation fails (that is, if obj.errors.isEmpty() is False) then the object will not be saved. To test for errors, use obj.errors.isEmpty().
114 Method validate No summary
124 Method isValid This method first calls validate and then returns a deferred that returns True if there were no errors and False otherwise.
134 Method beforeCreate Method called before a new object is created. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
142 Method beforeUpdate Method called before an existing object is updated. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
150 Method beforeSave Method called before an object is saved. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
161 Method afterInit Method called when a new DBObject is instantiated. Classes can overwrite this method. This method may return a Deferred.
168 Method beforeDelete Method called before a DBObject is deleted. Classes can overwrite this method. If False is returned, then the DBObject is not deleted from database. This method may return a Deferred.
222 Method refresh Update the properties for this object from the database.
231 Method toHash Convert this object to a dictionary.
257 Method delete Delete this instance from the database. Calls beforeDelete before deleting from the database.
284 Method loadRelations No summary
313 Class Method addRelation Add a relationship to the given Class.
342 Class Method initRelationshipCache Initialize the cache of relationship objects for this class.
353 Class Method tablename Get the tablename for the given class. If the class has a TABLENAME variable then that will be used - otherwise, it is is inferred from the class name.
368 Class Method findOrCreate Find all instances of a given class based on the attributes given (just like findBy).
382 Class Method findBy Find all instances of the given class based on an exact match of attributes.
396 Class Method find Find instances of a given class.
428 Class Method count Count instances of a given class.
444 Class Method all Get all instances of the given class in the database. Note that this is the equivalent of calling find with no arguments.
456 Class Method deleteAll Delete all instances of klass in the database without instantiating the records first or invoking callbacks (beforeDelete is not called). This will run a single SQL DELETE statement in the database.
473 Class Method exists Find whether or not at least one instance of the given klass exists, optionally with specific conditions specified in where.
489 Method __str__ Get the string version of this object.
501 Method __getattribute__ Get the given attribute.
520 Method __eq__ Determine if this object is the same as another (only taking the type of the other class and it's id into account).
534 Method __neq__ Determine if this object is not the same as another (only taking the type of the other class and it's id into account).
176 Method _create Method to actually create an object in the DB. Handles calling this class's beforeCreate followed by it's beforeSave method.
199 Method _update Method to actually save an existing object in the DB. Handles calling this class's beforeUpdate and beforeSave methods.

Inherited from Validator:

Line # Kind Name Docs
0 Class Variable VALIDATIONS A list of functions to call when testing whether or not a particular instance is valid.
95 Class Method clearValidations Clear the given class's validations.
103 Class Method addValidator Add a function to the given classes validation list.
121 Class Method validatesPresenceOf A validator to test whether or not some named properties are set. For those named properties that are not set, an error will be recorded in obj.errors.
137 Class Method validatesUniquenessOf A validator to test whether or not some named properties are unique. For those named properties that are not unique, an error will be recorded in obj.errors.
153 Class Method validatesLengthOf No summary
175 Class Method _validate Validate a given object using all of the set validators for the objects class. If errors are found, they will be recorded in the objects errors property.
Line # Kind Name Docs
520 Method __eq__ Determine if this object is the same as another (only taking the type of the other class and it's id into account).
501 Method __getattribute__ Get the given attribute.
61 Method __init__ Constructor. DO NOT OVERWRITE. Use the DBObject.afterInit method.
534 Method __neq__ Determine if this object is not the same as another (only taking the type of the other class and it's id into account).
489 Method __str__ Get the string version of this object.
313 Class Method addRelation Add a relationship to the given Class.
103 Class Method addValidator Add a function to the given classes validation list.
161 Method afterInit Method called when a new DBObject is instantiated. Classes can overwrite this method. This method may return a Deferred.
444 Class Method all Get all instances of the given class in the database. Note that this is the equivalent of calling find with no arguments.
134 Method beforeCreate Method called before a new object is created. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
168 Method beforeDelete Method called before a DBObject is deleted. Classes can overwrite this method. If False is returned, then the DBObject is not deleted from database. This method may return a Deferred.
150 Method beforeSave Method called before an object is saved. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
142 Method beforeUpdate Method called before an existing object is updated. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
0 Class Variable BELONGSTO A list made up of some number of strings and dicts. If an element is a string, it represents what the class belongs to, for instance 'user'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and BelongsTo for more information.
95 Class Method clearValidations Clear the given class's validations.
428 Class Method count Count instances of a given class.
257 Method delete Delete this instance from the database. Calls beforeDelete before deleting from the database.
456 Class Method deleteAll Delete all instances of klass in the database without instantiating the records first or invoking callbacks (beforeDelete is not called). This will run a single SQL DELETE statement in the database.
473 Class Method exists Find whether or not at least one instance of the given klass exists, optionally with specific conditions specified in where.
396 Class Method find Find instances of a given class.
382 Class Method findBy Find all instances of the given class based on an exact match of attributes.
368 Class Method findOrCreate Find all instances of a given class based on the attributes given (just like findBy).
0 Class Variable HABTM A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of (and which in turn has many of this current object type), for instance a teacher has and belongs to many students. Both the Student and Teacher classes should have a class variable that is HABTM = ['teachers'] and HABTM = ['students'], respectively. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HABTM for more information.
0 Class Variable HASMANY A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of, for instance 'users'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasMany for more information.
0 Class Variable HASONE A list made up of some number of strings and dicts. If an element is a string, it represents what the class has one of, for instance 'location'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasOne for more information.
342 Class Method initRelationshipCache Initialize the cache of relationship objects for this class.
124 Method isValid This method first calls validate and then returns a deferred that returns True if there were no errors and False otherwise.
284 Method loadRelations No summary
222 Method refresh Update the properties for this object from the database.
91 Method save Save this object to the database. Validation is performed first; if the validation fails (that is, if obj.errors.isEmpty() is False) then the object will not be saved. To test for errors, use obj.errors.isEmpty().
0 Class Variable TABLENAME If specified, use the given tablename as the one for this object. Otherwise, use the lowercase, plural version of this class's name. See the DBObject.tablename method.
353 Class Method tablename Get the tablename for the given class. If the class has a TABLENAME variable then that will be used - otherwise, it is is inferred from the class name.
231 Method toHash Convert this object to a dictionary.
80 Method updateAttrs Set the attributes of this object based on the given dict.
114 Method validate No summary
153 Class Method validatesLengthOf No summary
121 Class Method validatesPresenceOf A validator to test whether or not some named properties are set. For those named properties that are not set, an error will be recorded in obj.errors.
137 Class Method validatesUniquenessOf A validator to test whether or not some named properties are unique. For those named properties that are not unique, an error will be recorded in obj.errors.
0 Class Variable VALIDATIONS A list of functions to call when testing whether or not a particular instance is valid.
176 Method _create Method to actually create an object in the DB. Handles calling this class's beforeCreate followed by it's beforeSave method.
199 Method _update Method to actually save an existing object in the DB. Handles calling this class's beforeUpdate and beforeSave methods.
175 Class Method _validate Validate a given object using all of the set validators for the objects class. If errors are found, they will be recorded in the objects errors property.
HASMANY =
A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of, for instance 'users'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasMany for more information.
HASONE =
A list made up of some number of strings and dicts. If an element is a string, it represents what the class has one of, for instance 'location'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HasOne for more information.
HABTM =
A list made up of some number of strings and dicts. If an element is a string, it represents what the class has many of (and which in turn has many of this current object type), for instance a teacher has and belongs to many students. Both the Student and Teacher classes should have a class variable that is HABTM = ['teachers'] and HABTM = ['students'], respectively. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and HABTM for more information.
BELONGSTO =
A list made up of some number of strings and dicts. If an element is a string, it represents what the class belongs to, for instance 'user'. If an element is a dict, then it should minimally have a name attribute (with a value the same as if the element were a string) and then any additional options. See Relationship and BelongsTo for more information.
TABLENAME =
If specified, use the given tablename as the one for this object. Otherwise, use the lowercase, plural version of this class's name. See the DBObject.tablename method.
def __init__(self, **kwargs):
Constructor. DO NOT OVERWRITE. Use the DBObject.afterInit method.
ParameterskwargsAn optional dictionary containing the properties that should be initially set for this object.
See AlsoDBObject.afterInit
def updateAttrs(self, kwargs):
Set the attributes of this object based on the given dict.
ParameterskwargsA dict whose keys will be turned into properties and whose values will then be assigned to those properties.
def save(self):
Save this object to the database. Validation is performed first; if the validation fails (that is, if obj.errors.isEmpty() is False) then the object will not be saved. To test for errors, use obj.errors.isEmpty().
ReturnsA Deferred object. If a callback is added to that deferred the value of the saved (or unsaved if there are errors) object will be returned.
See AlsoValidator, Errors
def validate(self):
Run all validations associated with this object's class. This will return a deferred (actually a DeferredList). When this deferred is finished, this object's errors dictionary property will either be empty or will contain the errors from this object (keys are property names, values are the error messages describing the error).
def isValid(self):
This method first calls validate and then returns a deferred that returns True if there were no errors and False otherwise.
def beforeCreate(self):
Method called before a new object is created. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
def beforeUpdate(self):
Method called before an existing object is updated. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.
def beforeSave(self):
Method called before an object is saved. Classes can overwrite this method. If False is returned, then the object is not saved in the database. This method may return a Deferred.

This method is called after beforeCreate when an object is being created, and after beforeUpdate when an existing object (whose id is not None) is being saved.

def afterInit(self):
Method called when a new DBObject is instantiated. Classes can overwrite this method. This method may return a Deferred.
def beforeDelete(self):
Method called before a DBObject is deleted. Classes can overwrite this method. If False is returned, then the DBObject is not deleted from database. This method may return a Deferred.
def _create(self):
Method to actually create an object in the DB. Handles calling this class's beforeCreate followed by it's beforeSave method.
ReturnsA Deferred object. If a callback is added to that deferred the value of the saved object will be returned (unless the beforeCreate or beforeSave methods returns False, in which case the unsaved object will be returned).
def _update(self):
Method to actually save an existing object in the DB. Handles calling this class's beforeUpdate and beforeSave methods.
ReturnsA Deferred object. If a callback is added to that deferred the value of the saved object will be returned (unless the beforeUpdate or beforeSave methods returns False, in which case the unsaved object will be returned).
def refresh(self):
Update the properties for this object from the database.
ReturnsA Deferred object.
def toHash(self, cols, includeBlank=False, exclude=None, base=None):
Convert this object to a dictionary.
ParametersincludeBlankBoolean representing whether or not properties that have not been set should be included (the initial property list is retrieved from the schema of the database for the given class's schema).
exclueA list of properties to ignore when creating the dict to return.
baseAn initial base dict to add this objects properties to.
ReturnsA dict formed from the properties and values of this object.
def delete(self):
Delete this instance from the database. Calls beforeDelete before deleting from the database.
ReturnsA Deferred.
def loadRelations(self, *relations):
Preload a a list of relationships. For instance, if you have an instance of an object User (named user) that has many Addresses and has one Avatar, you could call user.loadRelations('addresses', 'avatar').addCallback('handleUser') instead of having to call user.addresses.get() and user.avatar.get() and assign callbacks to the results of those calls. In the first case, the function handleUser would accept one argument, which will be a dictionary whose keys are the property names and whose values are the results of the get() calls. This just makes it easier to load multiple properties at once, without having to create a long list of callbacks.

If the method is called without any arguments, then all relations will loaded.

ReturnsA Deferred.
@classmethod
def addRelation(klass, relation, rtype):
Add a relationship to the given Class.
ParametersklassThe class extending this one.
relationEither a string with the name of property to create for this class or a dictionary decribing the relationship. For instance, if a User HasMany Pictures then the relation could either by 'pictures' or a dictionary with at least one "name" key, as in {'name': 'pictures', ...} along with other options.
rtypeThe relationship type. It should be a key value from the TYPES class variable in the class Relationship.
@classmethod
def initRelationshipCache(klass):
Initialize the cache of relationship objects for this class.
@classmethod
def tablename(klass):
Get the tablename for the given class. If the class has a TABLENAME variable then that will be used - otherwise, it is is inferred from the class name.
ParametersklassThe class to get the tablename for.
@classmethod
def findOrCreate(klass, **attrs):
Find all instances of a given class based on the attributes given (just like findBy).

If a match isn't found, create a new instance and return that.

@classmethod
def findBy(klass, **attrs):
Find all instances of the given class based on an exact match of attributes.

For instance: User.find(first_name='Bob', last_name='Smith')

Will return all matches.

@classmethod
def find(klass, id=None, where=None, group=None, limit=None, orderby=None):
Find instances of a given class.
ParametersidThe integer of the klass to find. For instance, Klass.find(1) will return an instance of Klass from the row with an id of 1 (unless it isn't found, in which case None is returned).
whereA list whose first element is the string version of the condition with question marks in place of any parameters. Further elements of the list should be the values of any parameters specified. For instance, ['first_name = ? AND age > ?', 'Bob', 21].
groupA str describing the grouping, like group='first_name'.
limitAn int specifying the limit of the results. If this is 1, then the return value will be either an instance of klass or None.
orderbyA str describing the ordering, like orderby='first_name DESC'.
ReturnsA Deferred which returns the following to a callback: If id is specified (or limit is 1) then a single instance of klass will be returned if one is found that fits the criteria, None otherwise. If id is not specified and limit is not 1, then a list will be returned with all matching results.
@classmethod
def count(klass, where=None):
Count instances of a given class.
ParameterswhereAn optional list whose first element is the string version of the condition with question marks in place of any parameters. Further elements of the list should be the values of any parameters specified. For instance, ['first_name = ? AND age > ?', 'Bob', 21].
ReturnsA Deferred which returns the total number of db records to a callback.
@classmethod
def all(klass):
Get all instances of the given class in the database. Note that this is the equivalent of calling find with no arguments.
ReturnsA Deferred which returns the following to a callback: A list containing all of the instances in the database.
@classmethod
def deleteAll(klass, where=None):
Delete all instances of klass in the database without instantiating the records first or invoking callbacks (beforeDelete is not called). This will run a single SQL DELETE statement in the database.
ParameterswhereConditionally delete instances. This parameter is of the same form found in find.
ReturnsA Deferred.
@classmethod
def exists(klass, where=None):
Find whether or not at least one instance of the given klass exists, optionally with specific conditions specified in where.
ParameterswhereConditionally find instances. This parameter is of the same form found in find.
ReturnsA Deferred which returns the following to a callback: A boolean as to whether or not at least one object was found.
def __str__(self):
Get the string version of this object.
def __getattribute__(self, name):
Get the given attribute.
ParametersnameThe name of the property to get.
ReturnsIf the name is a relationship based property, then a Relationship instance will be returned. Otherwise the set property of the class will be returned.
def __eq__(self, other):
Determine if this object is the same as another (only taking the type of the other class and it's id into account).
ParametersotherThe other object to compare this one to.
ReturnsA boolean.
def __neq__(self, other):
Determine if this object is not the same as another (only taking the type of the other class and it's id into account).
ParametersotherThe other object to compare this one to.
ReturnsA boolean.
API Documentation for twistar, generated by pydoctor at 2012-06-12 10:00:49.