def tablename(klass): """ Get the tablename for the given class. If the class has a C{TABLENAME} variable then that will be used - otherwise, it is is inferred from the class name. @param klass: The class to get the tablename for. """ if not hasattr(klass, 'TABLENAME'): inf = Inflector() klass.TABLENAME = inf.tableize(klass.__name__) return klass.TABLENAME
def __init__(self, inst, propname, givenargs): """ Constructor. @param inst: The L{DBObject} instance. @param propname: The property name in the L{DBObject} instance that results in this class being created. @param givenargs: Any arguments given (through the use of a C{dict} in the class variable in L{DBObject} rather than a string to describe the relationship). The given args can include, for all relationships, a C{class_name}. Depending on the relationship, C{association_foreign_key} and C{foreign_key} might also be used. """ self.infl = Inflector() self.inst = inst self.dbconfig = Registry.getConfig() self.args = { 'class_name': propname, 'association_foreign_key': self.infl.foreignKey(self.infl.singularize(propname)), 'foreign_key': self.infl.foreignKey(self.inst.__class__.__name__), 'polymorphic': False } self.args.update(givenargs) otherklassname = self.infl.classify(self.args['class_name']) if not self.args['polymorphic']: self.otherklass = Registry.getClass(otherklassname) self.othername = self.args['association_foreign_key'] self.thisclass = self.inst.__class__ self.thisname = self.args['foreign_key']
class Relationship(object): """ Base class that all specific relationship type classes extend. @see: L{HABTM}, L{HasOne}, L{HasMany}, L{BelongsTo} """ def __init__(self, inst, propname, givenargs): """ Constructor. @param inst: The L{DBObject} instance. @param propname: The property name in the L{DBObject} instance that results in this class being created. @param givenargs: Any arguments given (through the use of a C{dict} in the class variable in L{DBObject} rather than a string to describe the relationship). The given args can include, for all relationships, a C{class_name}. Depending on the relationship, C{association_foreign_key} and C{foreign_key} might also be used. """ self.infl = Inflector() self.inst = inst self.dbconfig = Registry.getConfig() self.args = { 'class_name': propname, 'association_foreign_key': self.infl.foreignKey(self.infl.singularize(propname)), 'foreign_key': self.infl.foreignKey(self.inst.__class__.__name__), 'polymorphic': False } self.args.update(givenargs) otherklassname = self.infl.classify(self.args['class_name']) if not self.args['polymorphic']: self.otherklass = Registry.getClass(otherklassname) self.othername = self.args['association_foreign_key'] self.thisclass = self.inst.__class__ self.thisname = self.args['foreign_key']
def __init__(self): """ Constructor. """ self.infl = Inflector()
class Errors(dict): """ A class to hold errors found during validation of a L{DBObject}. """ def __init__(self): """ Constructor. """ self.infl = Inflector() def add(self, prop, error): """ Add an error to a property. The error message stored for this property will be formed from the humanized name of the property followed by the error message given. For instance, C{errors.add('first_name', 'cannot be empty')} will result in an error message of "First Name cannot be empty" being stored for this property. @param prop: The name of a property to add an error to. @param error: A string error to associate with the given property. """ self[prop] = self.get(prop, []) msg = "%s %s" % (self.infl.humanize(prop), str(error)) if msg not in self[prop]: self[prop].append(msg) def isEmpty(self): """ Returns C{True} if there are any errors associated with any properties, C{False} otherwise. """ for value in self.values(): if len(value) > 0: return False return True def errorsFor(self, prop): """ Get the errors for a specific property. @param prop: The property to fetch errors for. @return: A C{list} of errors for the given property. If there are none, then the returned C{list} will have a length of 0. """ return self.get(prop, []) def __str__(self): """ Return all errors as a single string. """ s = [] for values in self.values(): for value in values: s.append(value) if len(s) == 0: return "No errors." return " ".join(s) def __len__(self): """ Get the sum of all errors for all properties. """ return sum([len(value) for value in self.values()])
class Errors(dict): """ A class to hold errors found during validation of a L{DBObject}. """ def __init__(self): """ Constructor. """ self.infl = Inflector() def add(self, prop, error): """ Add an error to a property. The error message stored for this property will be formed from the humanized name of the property followed by the error message given. For instance, C{errors.add('first_name', 'cannot be empty')} will result in an error message of "First Name cannot be empty" being stored for this property. @param prop: The name of a property to add an error to. @param error: A string error to associate with the given property. """ self[prop] = self.get(prop, []) msg = "%s %s" % (self.infl.humanize(prop), str(error)) if msg not in self[prop]: self[prop].append(msg) def isEmpty(self): """ Returns C{True} if there are any errors associated with any properties, C{False} otherwise. """ for value in self.itervalues(): if len(value) > 0: return False return True def errorsFor(self, prop): """ Get the errors for a specific property. @param prop: The property to fetch errors for. @return: A C{list} of errors for the given property. If there are none, then the returned C{list} will have a length of 0. """ return self.get(prop, []) def __str__(self): """ Return all errors as a single string. """ s = [] for values in self.itervalues(): for value in values: s.append(value) if len(s) == 0: return "No errors." return " ".join(s) def __len__(self): """ Get the sum of all errors for all properties. """ return sum([len(value) for value in self.itervalues()])