Back to index

plone3  3.1.7
Public Member Functions | Static Public Attributes | Private Attributes | Static Private Attributes
Archetypes.Schema.BasicSchema Class Reference
Inheritance diagram for Archetypes.Schema.BasicSchema:
Inheritance graph
[legend]
Collaboration diagram for Archetypes.Schema.BasicSchema:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __add__
def copy
def edit
def setDefaults
def updateAll
def allow
def validate
def toString
def signature
def changeSchemataForField
def getSchemataNames
def getSchemataFields
def replaceField
def getName
def fields
def editableFields
def viewableFields
def widgets
def filterFields
def __setitem__
def addField
def __delitem__
def __getitem__
def get
def has_key
def keys
def searchable
def hasPrimary

Static Public Attributes

tuple security = ClassSecurityInfo()
 values = fields
 delField = __delitem__
 updateField = addField

Private Attributes

 _props

Static Private Attributes

 __implements__ = ISchema
dictionary _properties = {}

Detailed Description

Manage a list of fields and run methods over them.

Definition at line 398 of file __init__.py.


Constructor & Destructor Documentation

def Archetypes.Schema.BasicSchema.__init__ (   self,
  args,
  kwargs 
)
Initialize a Schema.

The first positional argument may be a sequence of
Fields. (All further positional arguments are ignored.)

Keyword arguments are added to my properties.

Reimplemented from Archetypes.Schema.Schemata.

Reimplemented in Archetypes.Schema.Schema.

Definition at line 408 of file __init__.py.

00408 
00409     def __init__(self, *args, **kwargs):
00410         """
00411         Initialize a Schema.
00412 
00413         The first positional argument may be a sequence of
00414         Fields. (All further positional arguments are ignored.)
00415 
00416         Keyword arguments are added to my properties.
00417         """
00418         Schemata.__init__(self)
00419 
00420         self._props = self._properties.copy()
00421         self._props.update(kwargs)
00422 
00423         if len(args):
00424             if type(args[0]) in [ListType, TupleType]:
00425                 for field in args[0]:
00426                     self.addField(field)
00427             else:
00428                 msg = ('You are passing positional arguments '
00429                        'to the Schema constructor. '
00430                        'Please consult the docstring '
00431                        'for %s.BasicSchema.__init__' %
00432                        (self.__class__.__module__,))
00433                 level = 3
00434                 if self.__class__ is not BasicSchema:
00435                     level = 4
00436                 warn(msg, level=level)
00437                 for field in args:
00438                     self.addField(args[0])

Here is the caller graph for this function:


Member Function Documentation

def Archetypes.Schema.BasicSchema.__add__ (   self,
  other 
)
Returns a new Schemata object that contains all fields and layers
from ``self`` and ``other``.

Reimplemented from Archetypes.Schema.Schemata.

Reimplemented in Archetypes.Schema.Schema.

Definition at line 439 of file __init__.py.

00439 
00440     def __add__(self, other):
00441         c = BasicSchema()
00442         # We can't use update and keep the order so we do it manually
00443         for field in self.fields():
00444             c.addField(field)
00445         for field in other.fields():
00446             c.addField(field)
00447         # Need to be smarter when joining layers
00448         # and internal props
00449         c._props.update(self._props)
00450         return c

Here is the call graph for this function:

def Archetypes.Schema.Schemata.__delitem__ (   self,
  name 
) [inherited]

Definition at line 226 of file __init__.py.

00226 
00227     def __delitem__(self, name):
00228         if not self._fields.has_key(name):
00229             raise KeyError("Schemata has no field '%s'" % name)
00230         del self._fields[name]
00231         self._names.remove(name)

def Archetypes.Schema.Schemata.__getitem__ (   self,
  name 
) [inherited]

Definition at line 232 of file __init__.py.

00232 
00233     def __getitem__(self, name):
00234         return self._fields[name]

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.__setitem__ (   self,
  name,
  field 
) [inherited]

Definition at line 174 of file __init__.py.

00174 
00175     def __setitem__(self, name, field):
00176         assert name == field.getName()
00177         self.addField(field)

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.addField (   self,
  field 
) [inherited]
Adds a given field to my dictionary of fields.

Reimplemented in Archetypes.Schema.MetadataSchema.

Definition at line 180 of file __init__.py.

00180 
00181     def addField(self, field):
00182         """Adds a given field to my dictionary of fields."""
00183         field = aq_base(field)
00184         self._validateOnAdd(field)
00185         name = field.getName()
00186         if name not in self._names:
00187             self._names.append(name)
00188         self._fields[name] = field

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.BasicSchema.allow (   self,
  name 
)

Definition at line 530 of file __init__.py.

00530 
00531     def allow(self, name):
00532         return self.has_key(name)

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.BasicSchema.changeSchemataForField (   self,
  fieldname,
  schemataname 
)
change the schemata for a field 

Definition at line 630 of file __init__.py.

00630 
00631     def changeSchemataForField(self, fieldname, schemataname):
00632         """ change the schemata for a field """
00633         field = self[fieldname]
00634         self.delField(fieldname)
00635         field.schemata = schemataname
00636         self.addField(field)

Here is the call graph for this function:

Returns a deep copy of this Schema.

Reimplemented from Archetypes.Schema.Schemata.

Definition at line 452 of file __init__.py.

00452 
00453     def copy(self):
00454         """Returns a deep copy of this Schema.
00455         """
00456         c = BasicSchema()
00457         for field in self.fields():
00458             c.addField(field.copy())
00459         # Need to be smarter when joining layers
00460         # and internal props
00461         c._props.update(self._props)
00462         return c

Here is the call graph for this function:

def Archetypes.Schema.BasicSchema.edit (   self,
  instance,
  name,
  value 
)

Definition at line 464 of file __init__.py.

00464 
00465     def edit(self, instance, name, value):
00466         if self.allow(name):
00467             instance[name] = value

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.editableFields (   self,
  instance,
  visible_only = False 
) [inherited]
Returns a list of editable fields for the given instance

Definition at line 107 of file __init__.py.

00107 
00108     def editableFields(self, instance, visible_only=False):
00109         """Returns a list of editable fields for the given instance
00110         """
00111         ret = []
00112         for field in self.fields():
00113             if field.writeable(instance, debug=False) and    \
00114                    (not visible_only or
00115                     field.widget.isVisible(instance, 'edit') != 'invisible'):
00116                 ret.append(field)
00117         return ret

Here is the call graph for this function:

def Archetypes.Schema.Schemata.fields (   self) [inherited]
Returns a list of my fields in order of their indices.

Definition at line 98 of file __init__.py.

00098 
00099     def fields(self):
00100         """Returns a list of my fields in order of their indices."""
00101         return [self._fields[name] for name in self._names]
00102 

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.filterFields (   self,
  predicates,
  values 
) [inherited]
Returns a subset of self.fields(), containing only fields that
satisfy the given conditions.

You can either specify predicates or values or both. If you provide
both, all conditions must be satisfied.

For each ``predicate`` (positional argument), ``predicate(field)`` must
return 1 for a Field ``field`` to be returned as part of the result.

Each ``attr=val`` function argument defines an additional predicate:
A field must have the attribute ``attr`` and field.attr must be equal
to value ``val`` for it to be in the returned list.

Definition at line 137 of file __init__.py.

00137 
00138     def filterFields(self, *predicates, **values):
00139         """Returns a subset of self.fields(), containing only fields that
00140         satisfy the given conditions.
00141 
00142         You can either specify predicates or values or both. If you provide
00143         both, all conditions must be satisfied.
00144 
00145         For each ``predicate`` (positional argument), ``predicate(field)`` must
00146         return 1 for a Field ``field`` to be returned as part of the result.
00147 
00148         Each ``attr=val`` function argument defines an additional predicate:
00149         A field must have the attribute ``attr`` and field.attr must be equal
00150         to value ``val`` for it to be in the returned list.
00151         """
00152 
00153         results = []
00154 
00155         for field in self.fields(): # step through each of my fields
00156 
00157             # predicate failed:
00158             failed = [pred for pred in predicates if not pred(field)]
00159             if failed: continue
00160 
00161             # attribute missing:
00162             missing_attrs = [attr for attr in values.keys() \
00163                              if not shasattr(field, attr)]
00164             if missing_attrs: continue
00165 
00166             # attribute value unequal:
00167             diff_values = [attr for attr in values.keys() \
00168                            if getattr(field, attr) != values[attr]]
00169             if diff_values: continue
00170 
00171             results.append(field)
00172 
00173         return results

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.get (   self,
  name,
  default = None 
) [inherited]

Definition at line 236 of file __init__.py.

00236 
00237     def get(self, name, default=None):
00238         return self._fields.get(name, default)

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.getName (   self) [inherited]
Returns the Schemata's name.

Definition at line 68 of file __init__.py.

00068 
00069     def getName(self):
00070         """Returns the Schemata's name."""
00071         return self.__name__
00072 

Here is the caller graph for this function:

Return list of fields belong to schema 'name'
in order of appearing

Definition at line 647 of file __init__.py.

00647 
00648     def getSchemataFields(self, name):
00649         """Return list of fields belong to schema 'name'
00650         in order of appearing
00651         """
00652         return [f for f in self.fields() if f.schemata == name]

Here is the call graph for this function:

Here is the caller graph for this function:

Return list of schemata names in order of appearing

Definition at line 638 of file __init__.py.

00638 
00639     def getSchemataNames(self):
00640         """Return list of schemata names in order of appearing"""
00641         lst = []
00642         for f in self.fields():
00643             if not f.schemata in lst:
00644                 lst.append(f.schemata)
00645         return lst

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.has_key (   self,
  name 
) [inherited]

Definition at line 240 of file __init__.py.

00240 
00241     def has_key(self, name):
00242         return self._fields.has_key(name)

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.hasPrimary (   self) [inherited]
Returns the first primary field or False

Definition at line 263 of file __init__.py.

00263 
00264     def hasPrimary(self):
00265         """Returns the first primary field or False"""
00266         for f in self.fields():
00267             if getattr(f, 'primary', False):
00268                 return f
00269         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.keys (   self) [inherited]

Definition at line 246 of file __init__.py.

00246 
00247     def keys(self):
00248         return self._names

Here is the caller graph for this function:

def Archetypes.Schema.BasicSchema.replaceField (   self,
  name,
  field 
)

Definition at line 655 of file __init__.py.

00655 
00656     def replaceField(self, name, field):
00657         if IField.isImplementedBy(field):
00658             oidx = self._names.index(name)
00659             new_name = field.getName()
00660             self._names[oidx] = new_name
00661             del self._fields[name]
00662             self._fields[new_name] = field
00663         else:
00664             raise ValueError, "Object doesn't implement IField: %r" % field
00665 
00666 InitializeClass(BasicSchema)
00667 

def Archetypes.Schema.Schemata.searchable (   self) [inherited]
Returns a list containing names of all searchable fields.

Definition at line 258 of file __init__.py.

00258 
00259     def searchable(self):
00260         """Returns a list containing names of all searchable fields."""
00261 
00262         return [f.getName() for f in self.fields() if f.searchable]

Here is the call graph for this function:

def Archetypes.Schema.BasicSchema.setDefaults (   self,
  instance 
)
Only call during object initialization. Sets fields to
schema defaults

Definition at line 470 of file __init__.py.

00470 
00471     def setDefaults(self, instance):
00472         """Only call during object initialization. Sets fields to
00473         schema defaults
00474         """
00475         ## TODO think about layout/vs dyn defaults
00476         for field in self.values():
00477             if field.getName().lower() == 'id': continue
00478             if field.type == "reference": continue
00479 
00480             # always set defaults on writable fields
00481             mutator = field.getMutator(instance)
00482             if mutator is None:
00483                 continue
00484             default = field.getDefault(instance)
00485 
00486             args = (default,)
00487             kw = {'field': field.__name__,
00488                   '_initializing_': True}
00489             if shasattr(field, 'default_content_type'):
00490                 # specify a mimetype if the mutator takes a
00491                 # mimetype argument
00492                 # if the schema supplies a default, we honour that, 
00493                 # otherwise we use the site property
00494                 default_content_type = field.default_content_type
00495                 if default_content_type is None:
00496                     default_content_type = getDefaultContentType(instance)
00497                 kw['mimetype'] = default_content_type
00498             mapply(mutator, *args, **kw)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 624 of file __init__.py.

00624 
00625     def signature(self):
00626         from md5 import md5
00627         return md5(self.toString()).digest()

Here is the call graph for this function:

Definition at line 615 of file __init__.py.

00615 
00616     def toString(self):
00617         s = '%s: {' % self.__class__.__name__
00618         for f in self.fields():
00619             s = s + '%s,' % (f.toString())
00620         s = s + '}'
00621         return s

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.BasicSchema.updateAll (   self,
  instance,
  kwargs 
)
This method mutates fields in the given instance.

For each keyword argument k, the key indicates the name of the
field to mutate while the value is used to call the mutator.

E.g. updateAll(instance, id='123', amount=500) will, depending on the
actual mutators set, result in two calls: ``instance.setId('123')`` and
``instance.setAmount(500)``.

Definition at line 501 of file __init__.py.

00501 
00502     def updateAll(self, instance, **kwargs):
00503         """This method mutates fields in the given instance.
00504 
00505         For each keyword argument k, the key indicates the name of the
00506         field to mutate while the value is used to call the mutator.
00507 
00508         E.g. updateAll(instance, id='123', amount=500) will, depending on the
00509         actual mutators set, result in two calls: ``instance.setId('123')`` and
00510         ``instance.setAmount(500)``.
00511         """
00512 
00513         keys = kwargs.keys()
00514 
00515         for name in keys:
00516 
00517             field = self.get(name, None)
00518 
00519             if field is None:
00520                 continue
00521 
00522             if not field.writeable(instance):
00523                 continue
00524 
00525             # If passed the test above, mutator is guaranteed to
00526             # exist.
00527             method = field.getMutator(instance)
00528             method(kwargs[name])

Here is the call graph for this function:

def Archetypes.Schema.BasicSchema.validate (   self,
  instance = None,
  REQUEST = None,
  errors = None,
  data = None,
  metadata = None 
)
Validate the state of the entire object.

The passed dictionary ``errors`` will be filled with human readable
error messages as values and the corresponding fields' names as
keys.

If a REQUEST object is present, validate the field values in the
REQUEST.  Otherwise, validate the values currently in the object.

Definition at line 535 of file __init__.py.

00535 
00536                  errors=None, data=None, metadata=None):
00537         """Validate the state of the entire object.
00538 
00539         The passed dictionary ``errors`` will be filled with human readable
00540         error messages as values and the corresponding fields' names as
00541         keys.
00542 
00543         If a REQUEST object is present, validate the field values in the
00544         REQUEST.  Otherwise, validate the values currently in the object.
00545         """
00546         if REQUEST:
00547             fieldset = REQUEST.form.get('fieldset', None)
00548             fieldsets = REQUEST.form.get('fieldsets', None)
00549         else:
00550             fieldset = fieldsets = None
00551         fields = []
00552 
00553         if fieldsets is not None:
00554             schemata = instance.Schemata()
00555             for fieldset in fieldsets:
00556                 fields += [(field.getName(), field)
00557                            for field in schemata[fieldset].fields()]            
00558         elif fieldset is not None:
00559             schemata = instance.Schemata()
00560             fields = [(field.getName(), field)
00561                       for field in schemata[fieldset].fields()]            
00562         else:
00563             if data:
00564                 fields.extend([(field.getName(), field)
00565                                for field in self.filterFields(isMetadata=0)])
00566             if metadata:
00567                 fields.extend([(field.getName(), field)
00568                                for field in self.filterFields(isMetadata=1)])
00569 
00570         if REQUEST:
00571             form = REQUEST.form
00572         else:
00573             form = None
00574             
00575         for name, field in fields:
00576             
00577             # Should not validate something we can't write to anyway
00578             if not field.writeable(instance):
00579                 continue
00580             
00581             error = 0
00582             value = None
00583             widget = field.widget
00584             
00585             if form:
00586                 result = widget.process_form(instance, field, form,
00587                                              empty_marker=_marker)
00588             else:
00589                 result = None
00590             if result is None or result is _marker:
00591                 accessor = field.getEditAccessor(instance) or field.getAccessor(instance)
00592                 if accessor is not None:
00593                     value = accessor()
00594                 else:
00595                     # can't get value to validate -- bail
00596                     continue
00597             else:
00598                 value = result[0]
00599 
00600             res = field.validate(instance=instance,
00601                                  value=value,
00602                                  errors=errors,
00603                                  REQUEST=REQUEST)
00604             if res:
00605                 errors[field.getName()] = res
00606         return errors
00607 

Here is the call graph for this function:

Here is the caller graph for this function:

def Archetypes.Schema.Schemata.viewableFields (   self,
  instance 
) [inherited]
Returns a list of viewable fields for the given instance

Definition at line 119 of file __init__.py.

00119 
00120     def viewableFields(self, instance):
00121         """Returns a list of viewable fields for the given instance
00122         """
00123         return [field for field in self.fields()
00124                 if field.checkPermission('view', instance)]

Here is the call graph for this function:

def Archetypes.Schema.Schemata.widgets (   self) [inherited]
Returns a dictionary that contains a widget for
each field, using the field name as key.

Definition at line 126 of file __init__.py.

00126 
00127     def widgets(self):
00128         """Returns a dictionary that contains a widget for
00129         each field, using the field name as key."""
00130 
00131         widgets = {}
00132         for f in self.fields():
00133             widgets[f.getName()] = f.widget
00134         return widgets

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Archetypes.Schema.BasicSchema.__implements__ = ISchema [static, private]

Reimplemented from Archetypes.Schema.Schemata.

Reimplemented in Archetypes.Schema.ManagedSchema, and Archetypes.Schema.Schema.

Definition at line 401 of file __init__.py.

dictionary Archetypes.Schema.BasicSchema._properties = {} [static, private]

Definition at line 406 of file __init__.py.

Definition at line 419 of file __init__.py.

Definition at line 251 of file __init__.py.

tuple Archetypes.Schema.BasicSchema.security = ClassSecurityInfo() [static]

Definition at line 255 of file __init__.py.

Definition at line 104 of file __init__.py.


The documentation for this class was generated from the following file: