| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6675f23 commit 133dbb6
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,8 +30,16 @@ Minor features | |||
| 30 | 30 | ||
| 31 | 31 | * Use reversed() in manager/command.py instead of .__reversed__(). | |
| 32 | 32 | ||
| 33 | - * DecimalStringCol - similar to DecimalCol but stores data as strings to | ||
| 34 | - work around problems in some drivers and type affinity problem in SQLite. | ||
| 33 | + * Added DecimalStringCol - similar to DecimalCol but stores data as strings | ||
| 34 | + to work around problems in some drivers and type affinity problem in | ||
| 35 | + SQLite. | ||
| 36 | + | ||
| 37 | + * Added sqlobject.include.hascol.HashCol - a column type that automatically | ||
| 38 | + hashes anything going into it, and returns out an object that hashes | ||
| 39 | + anything being compared to itself. Basically, it's good for really simple | ||
| 40 | + one-way password fields, and it even supports the assignment of None to | ||
| 41 | + indicate no password set. By default, it uses the md5 library for | ||
| 42 | + hashing, but this can be changed in a HashCol definition. | ||
| 35 | 43 | ||
| 36 | 44 | SQLObject 0.10.3 | |
| 37 | 45 | ================ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,62 @@ | |||
| 1 | + __all__ = ['HashCol'] | ||
| 2 | + | ||
| 3 | + import sqlobject.col | ||
| 4 | + | ||
| 5 | + class DbHash: | ||
| 6 | + """ Presents a comparison object for hashes, allowing plain text to be | ||
| 7 | + automagically compared with the base content. """ | ||
| 8 | + | ||
| 9 | + def __init__( self, hash, hashMethod ): | ||
| 10 | + self.hash = hash | ||
| 11 | + self.hashMethod = hashMethod | ||
| 12 | + | ||
| 13 | + def __cmp__( self, other ): | ||
| 14 | + if other is None: | ||
| 15 | + if self.hash is None: | ||
| 16 | + return 0 | ||
| 17 | + return True | ||
| 18 | + if not isinstance( other, basestring ): | ||
| 19 | + raise TypeError( "A hash may only be compared with a string, or None." ) | ||
| 20 | + return cmp( self.hashMethod( other ), self.hash ) | ||
| 21 | + | ||
| 22 | + def __repr__( self ): | ||
| 23 | + return "<DbHash>" | ||
| 24 | + | ||
| 25 | + class HashValidator( sqlobject.col.StringValidator ): | ||
| 26 | + """ Provides formal SQLObject validation services for the HashCol. """ | ||
| 27 | + | ||
| 28 | + def to_python( self, value, state ): | ||
| 29 | + """ Passes out a hash object. """ | ||
| 30 | + if value is None: | ||
| 31 | + return None | ||
| 32 | + return DbHash( hash = value, hashMethod = self.hashMethod ) | ||
| 33 | + | ||
| 34 | + def from_python( self, value, state ): | ||
| 35 | + """ Store the given value as a MD5 hash, or None if specified. """ | ||
| 36 | + if value is None: | ||
| 37 | + return None | ||
| 38 | + return self.hashMethod( value ) | ||
| 39 | + | ||
| 40 | + class SOHashCol( sqlobject.col.SOStringCol ): | ||
| 41 | + """ The internal HashCol definition. By default, enforces a md5 digest. """ | ||
| 42 | + | ||
| 43 | + def __init__( self, **kw ): | ||
| 44 | + if 'hashMethod' not in kw: | ||
| 45 | + from md5 import md5 | ||
| 46 | + self.hashMethod = lambda v: md5( v ).hexdigest() | ||
| 47 | + if 'length' not in kw: | ||
| 48 | + kw['length'] = 32 | ||
| 49 | + else: | ||
| 50 | + self.hashMethod = kw['hashMethod'] | ||
| 51 | + del kw['hashMethod'] | ||
| 52 | + super( sqlobject.col.SOStringCol, self ).__init__( **kw ) | ||
| 53 | + | ||
| 54 | + def createValidators( self ): | ||
| 55 | + return [HashValidator( name=self.name, hashMethod=self.hashMethod )] + \ | ||
| 56 | + super( SOHashCol, self ).createValidators() | ||
| 57 | + | ||
| 58 | + class HashCol( sqlobject.col.StringCol ): | ||
| 59 | + """ End-user HashCol class. May be instantiated with 'hashMethod', a function | ||
| 60 | + which returns the string hash of any other string (i.e. basestring). """ | ||
| 61 | + | ||
| 62 | + baseClass = SOHashCol | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments