Skip to content

Instantly share code, notes, and snippets.

@imankulov
Created January 18, 2011 08:53
Show Gist options
  • Save imankulov/784153 to your computer and use it in GitHub Desktop.
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)
"""
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