Skip to content

Instantly share code, notes, and snippets.

@jpic
Created September 24, 2011 14:25
Show Gist options
  • Select an option

  • Save jpic/1239376 to your computer and use it in GitHub Desktop.

Select an option

Save jpic/1239376 to your computer and use it in GitHub Desktop.
import datetime
class BaseNotification(object):
def to_dict(self, user, sent_at):
return {
'timestamp': datetime.time.mktime(sent_at.timetuple()),
}
def from_dict(self, data):
self.timestamp = data['timestamp']
self.sent_at = datetime.datetime.fromtimestamp(data['timestamp'])
def get_display(self, user, backend):
raise NotImplementedError()
class TextNotification(BaseNotification):
def __init__(self, kwargs):
self.text = kwargs.pop('text')
self.kwargs = kwargs
def to_dict(self, user, sent_at):
data = super(TextNotification, self).to_dict(user, sent_at)
data['text'] = self.text % self.kwargs
return data
def from_dict(self, data):
super(TextNotification, self).from_dict(data)
self.text = data['text']
def get_display(self, user, backend):
return self.text
class LazyTextNotification(TextNotification):
def to_dict(self, user, sent_at):
data = super(TextNotification, self).to_dict(user, sent_at)
data['text'] = self.text
data['kwargs'] = self.kwargs
return data
def from_dict(self, data):
super(LazyTextNotification, self).from_dict(data)
self.kwargs = data['kwargs']
def get_display(self, user, backend):
return self.text % self.kwargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment