Changeset 3404

Show
Ignore:
Timestamp:
07/29/07 23:18:30 (1 year ago)
Author:
mscott
Message:

Merging #59 to trunk:

Implementation of relabel, and some more thorough documentation
about labels.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Schevo/doc/SchevoPublicApi.txt

    r3254 r3404  
    7070 
    7171 
     72Database labels 
     73--------------- 
     74 
     75By default, a database instance is labeled "Schevo Database":: 
     76 
     77    >>> from schevo.label import label, relabel 
     78    >>> t = DocTest(""" 
     79    ...     class Foo(E.Entity): 
     80    ...         pass 
     81    ...     """) 
     82    >>> print label(t.db) 
     83    Schevo Database 
     84 
     85Override the label by using `relabel`:: 
     86 
     87    >>> relabel(t.db, 'My Database') 
     88    >>> print label(t.db) 
     89    My Database 
     90 
     91 
    7292Extent instances 
    7393================ 
     
    103123 
    104124See the ``tests/test_relax_index.py`` test case for code examples. 
     125 
     126 
     127Extent labels 
     128------------- 
     129 
     130By default, an extent has a singular and plural label based on the 
     131entity class name associated with the extent:: 
     132 
     133    >>> from schevo.label import label, plural 
     134    >>> t = DocTest(""" 
     135    ...     class GreatPerson(E.Entity): 
     136    ... 
     137    ...         name = f.unicode() 
     138    ...     """) 
     139    >>> print label(t.db.GreatPerson) 
     140    Great Person 
     141    >>> print plural(t.db.GreatPerson) 
     142    Great Persons 
     143 
     144To override the singular and/or plural labels of an extent, assign 
     145values to the `_label` and/or `_plural` attributes of the associated 
     146entity class:: 
     147 
     148    >>> from schevo.label import label, plural 
     149    >>> t = DocTest(""" 
     150    ...     class GreatPerson(E.Entity): 
     151    ...       
     152    ...         name = f.unicode() 
     153    ... 
     154    ...         _label = 'Great PERSON' 
     155    ...         _plural = 'Great PEOPLE' 
     156    ...     """) 
     157    >>> print label(t.db.GreatPerson) 
     158    Great PERSON 
     159    >>> print plural(t.db.GreatPerson) 
     160    Great PEOPLE 
    105161 
    106162 
     
    139195 
    140196  # Doctest to go here. 
     197 
     198 
     199Entity labels 
     200------------- 
     201 
     202The `label` of an entity instance is the same as the `unicode` 
     203representation of the entity instance. 
     204 
     205By default, the label is determined by the default key defined in the 
     206entity class:: 
     207 
     208    >>> from schevo.label import label 
     209    >>> t = DocTest(""" 
     210    ...     class State(E.Entity): 
     211    ...      
     212    ...         name = f.unicode() 
     213    ...         abbreviation = f.unicode() 
     214    ...          
     215    ...         _key(name) 
     216    ...         _key(abbreviation) 
     217    ...          
     218    ...         _initial = [ 
     219    ...             ('Missouri', 'MO'), 
     220    ...             ] 
     221    ...     """) 
     222    >>> missouri = t.db.State.findone(name='Missouri') 
     223    >>> print label(missouri) 
     224    Missouri 
     225 
     226If the key has more than one field, the values are separated by two 
     227colons:: 
     228 
     229    >>> t = DocTest(""" 
     230    ...     class City(E.Entity): 
     231    ... 
     232    ...         name = f.unicode() 
     233    ...         state = f.entity('State') 
     234    ...          
     235    ...         _key(name, state) 
     236    ... 
     237    ...         _initial = [ 
     238    ...             ('Monett', ('Missouri',)), 
     239    ...             ] 
     240    ... 
     241    ...     class State(E.Entity): 
     242    ...      
     243    ...         name = f.unicode() 
     244    ...         abbreviation = f.unicode() 
     245    ...          
     246    ...         _key(name) 
     247    ...         _key(abbreviation) 
     248    ...          
     249    ...         _initial = [ 
     250    ...             ('Missouri', 'MO'), 
     251    ...             ] 
     252    ...     """) 
     253    >>> missouri = t.db.State.findone(name='Missouri') 
     254    >>> monett = t.db.City.findone(name='Monett', state=missouri) 
     255    >>> print label(monett) 
     256    Monett :: Missouri 
     257 
     258Override the `__unicode__` method of an entity class to customize the 
     259label:: 
     260 
     261    >>> t = DocTest(""" 
     262    ...     class City(E.Entity): 
     263    ... 
     264    ...         name = f.unicode() 
     265    ...         state = f.entity('State') 
     266    ...          
     267    ...         _key(name, state) 
     268    ... 
     269    ...         def __unicode__(self): 
     270    ...             return u'%s, %s' % (self.name, self.state.abbreviation) 
     271    ... 
     272    ...         _initial = [ 
     273    ...             ('Monett', ('Missouri',)), 
     274    ...             ] 
     275    ... 
     276    ...     class State(E.Entity): 
     277    ...      
     278    ...         name = f.unicode() 
     279    ...         abbreviation = f.unicode() 
     280    ...          
     281    ...         _key(name) 
     282    ...         _key(abbreviation) 
     283    ... 
     284    ...         _initial = [ 
     285    ...             ('Missouri', 'MO'), 
     286    ...             ] 
     287    ...     """) 
     288    >>> missouri = t.db.State.findone(name='Missouri') 
     289    >>> monett = t.db.City.findone(name='Monett', state=missouri) 
     290    >>> print label(monett) 
     291    Monett, MO 
    141292 
    142293 
  • trunk/Schevo/schevo/label.py

    r3306 r3404  
    8282 
    8383 
     84def relabel(obj, new_label): 
     85    """Relabel an object with a new label.""" 
     86    obj._label = new_label 
     87 
     88 
    8489optimize.bind_all(sys.modules[__name__])  # Last line of module. 
    8590 
  • trunk/Schevo/schevo/schema.py

    r3306 r3404  
    2424    'UNASSIGNED', 
    2525    'extentmethod', 
     26    'relabel', 
    2627    'schevo',  # And, indirectly, 'schevo.error'. 
    2728    'with_label', 
     
    4243    UNASSIGNED, 
    4344    ) 
    44 from schevo.label import with_label 
     45from schevo.label import relabel, with_label 
    4546 
    4647import schevo.base 
  • trunk/Schevo/schevo/test/test_label.py

    r3382 r3404  
    130130            tx.from_account = self 
    131131            tx.f.from_account.readonly = True 
    132             tx._label = 'Transfer Funds From %s' % self 
     132            relabel(tx, 'Transfer Funds From %s' % self) 
    133133            return tx 
    134134 
     
    341341        assert label.label(db) == 'Schevo Database' 
    342342        # It can be easily overridden. 
    343         db.label = 'Custom Label' 
     343        label.relabel(db, 'Custom Label') 
    344344        assert label.label(db) == 'Custom Label' 
    345345