Changes

Jump to: navigation, search

Support/Notifications

3,057 bytes added, 20:40, 6 January 2011
Added an API sketch. Scribble at will.
id 8634
hat_color red
 
= API Sketch =
class ObjectModifiedWatchType(object):
"""A notification which fires on the modification of a specific object"""
code = 'modi' # Key for the event_type column. Should be same size as a 32-bit int (but more memorable) if we store as a binary char(4). Migrations will be easy in case of collisions. All-lowercase codes are reserved for the notification app itself.
@classmethod
def fire(cls, obj):
"""Signal listener which sends any appropriate notifications"""
@classmethod
def create(cls, object):
"""Create (and save, and return) a watch which fires on the modification of the given object."""
...
objectModifiedSignal.register(ObjectCreatedEvent.listener) # modulo spelling
class ContentTypeModifiedWatchType(...)
code = 'modi' # Can share a key with the above since the NULL in the object_id column and the non-NULL in the content_type_id column are sufficient to distinguish them.
class ContentTypeCreatedWatchType(...)
code = 'crea'
class LocaleCreationWatchType(...): # abstract because it doesn't provide a template. Might not really be worth making this abstract. We don't have a lot of locale-having things atm.
"""A notification which fires when an object (like a wiki Document) is created with a given locale.
Works with anything that has a `locale` attr.
"""
code = 'WLCr' # convention: capital letters start a new word
@classmethod
def create(cls, locale, content_type=None):
"""Watch type for the creation of any object of a given locale.
Specify a content_type to limit to a particular type.
"""
watch = Watches.create(content_type=content_type, object_id=None, event_type=cls.code, user=[figure out how to represent this relationship to support anonymous also])
# WatchElements implements a key/value store. We might use 4-char codes as the keys, again because they're more memorable than ints and because they have to be unique only within an event_type. Thus, none are reserved.
WatchElements.create(watch=watch, name='locl', value=crc32(locale))
@classmethod
def fire(cls, obj):
# TODO: Can probably use the ORM for this:
watches = execute("""select distinct user (or whatever) from watches
inner join watch_elements locale on watches.id=lang.watch_id and name='locl' and value={obj.locale}
where content_type IS NULL or content_type={obj.content_type}
AND event_type={cls.code}""")
for w in watches:
cls.notify(obj) # builds and sends one mail, for example
@classmethod
def notify(cls, obj):
# Send a mail or what have you.
raise NotImplementError
def WikiLocaleCreationWatchType(LocaleCreationWatchType):
@classmethod
def notify(cls, obj):
send_mail_in_a_task(some_template.render(obj.this, obj.that, ...))
objectCreatedSignal.register(WikiLocaleCreationWatch.fire)
Confirm
574
edits

Navigation menu