-
-
Save yeago/1239372 to your computer and use it in GitHub Desktop.
This file contains 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
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