Skip to content

Instantly share code, notes, and snippets.

@jpic
Created September 23, 2011 14:29
Show Gist options
  • Save jpic/1237482 to your computer and use it in GitHub Desktop.
Save jpic/1237482 to your computer and use it in GitHub Desktop.
import datetime
class BaseNotification(object):
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
if 'timestamp' in kwargs.keys():
self.sent_at = datetime.datetime.fromtimestamp(kwargs['timestamp'])
def to_dict(self, user):
return {
'timestamp': self.timestamp,
}
def get_display(self, user, backend):
raise NotImplementedError()
class TextNotification(BaseNotification):
def to_dict(self, user):
data = super(TextNotification, self).to_dict(user, sent_at)
data['text'] = self.text % self.format_kwargs
return data
def get_display(self, user, backend):
return self.text
class LazyTextNotification(TextNotification):
def to_dict(self, user):
data = super(TextNotification, self).to_dict(user, sent_at)
data['text'] = self.text
data['format_kwargs'] = self.format_kwargs
return data
def get_display(self, user, backend):
return self.text % self.format_kwargs
class WikiSyntaxNotification(object):
# create ;)
class TemplateNotification(object):
# create ;)
# example:
def emit_new_follower(user, follower):
Subscription.objects.emit(TextNotification(
text='%(follower)s follows %(user)s',
format_kwargs={
'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