Skip to content

Instantly share code, notes, and snippets.

@yeago
Forked from jpic/wtf_you_want.py
Created September 24, 2011 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeago/1239372 to your computer and use it in GitHub Desktop.
Save yeago/1239372 to your computer and use it in GitHub Desktop.
class BaseNotification(object):
def serialize(self):
# here do what the * you want
def unserialize(self, data):
# here too
def render(self, backend):
# rendering may differ depending on the backend babe
class TextNotification(object):
def __init__(self, text, **format_kwargs):
# accept a lazy_rendering option if you want
self.text = text
self.format_kwargs = format_kwargs
def serialize(self):
# do more if you want
return (self.timestamp, self.text % self.format_kwargs)
def unserialize(self, data):
self.timestamp = data[0]
self.text = data[1]
def render(self, backend):
return self.text
class WikiSyntaxNotification(object):
# what the fuck you want
class TemplateNotification(object):
# what the fuck you want
# example:
def emit_new_follower(user, follower):
Subscription.objects.emit(TextNotification(
text='%(follower)s follows %(user)s',
user=user,
follower=follower
))
subscribers_of=[follower, user],
queue='friends',
)
# or wikisyntax
def emit_new_follower(user, follower):
Subscription.objects.emit(TextNotification(
text='[[%s]] follows [[%s]]' % (follower, user),
**{
user.username: user,
follower.username: follower,
}
))
subscribers_of=[follower, user],
queue='friends',
)
# or with template:
def emit_new_follower(user, follower):
Subscription.objects.emit(TemplateNotification(
type='new_follower',
context={
'user':user,
'follower':follower
},
))
subscribers_of=[follower, user],
queue='friends',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment