Created
January 18, 2011 08:53
-
-
Save imankulov/784153 to your computer and use it in GitHub Desktop.
Status object which can be used as boolean or string (useful to allow / reasonable forbid an action)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example | |
Create objects: | |
>>> status = Status(False, u'You are not allowed to do this') | |
>>> status = Status(True) | |
Use objects: | |
>>> if status: | |
... do_smth() | |
... else: | |
... raise AccessForbiddenException(str(status)) | |
""" | |
from django.utils.encoding import smart_unicode, smart_str | |
class Status(object): | |
def __init__(self, status, comment=None): | |
self.status = status | |
self.comment = comment | |
def __nonzero__(self): | |
return bool(self.status) | |
def __str__(self): | |
return smart_str(self.comment) | |
def __unicode__(self): | |
return smart_unicode(self.comment) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment