""" Provide django-style hooks for model events. """ from peewee import Model as _Model class Signal(object): def __init__(self): self._flush() def connect(self, receiver, name=None, sender=None): name = name or receiver.__name__ if name not in self._receivers: self._receivers[name] = (receiver, sender) self._receiver_list.append(name) else: raise ValueError('receiver named %s already connected' % name) def disconnect(self, receiver=None, name=None): if receiver: name = receiver.__name__ if name: del self._receivers[name] self._receiver_list.remove(name) else: raise ValueError('a receiver or a name must be provided') def __call__(self, name=None, sender=None): def decorator(fn): self.connect(fn, name, sender) return fn return decorator def send(self, instance, *args, **kwargs): sender = type(instance) responses = [] for name in self._receiver_list: r, s = self._receivers[name] if s is None or isinstance(instance, s): responses.append((r, r(sender, instance, *args, **kwargs))) return responses def _flush(self): self._receivers = {} self._receiver_list = [] pre_save = Signal() post_save = Signal() pre_delete = Signal() post_delete = Signal() pre_init = Signal() post_init = Signal() class Model(_Model): def __init__(self, *args, **kwargs): super(Model, self).__init__(*args, **kwargs) pre_init.send(self) def prepared(self): super(Model, self).prepared() post_init.send(self) def save(self, *args, **kwargs): pk_value = self._get_pk_value() created = kwargs.get('force_insert', False) or not bool(pk_value) pre_save.send(self, created=created) ret = super(Model, self).save(*args, **kwargs) post_save.send(self, created=created) return ret def delete_instance(self, *args, **kwargs): pre_delete.send(self) ret = super(Model, self).delete_instance(*args, **kwargs) post_delete.send(self) return ret
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
__init__.py | File | 0 B | 0644 |
|
_speedups.cpython-38-x86_64-linux-gnu.so | File | 239.53 KB | 0755 |
|
_sqlite_ext.cpython-38-x86_64-linux-gnu.so | File | 72.59 KB | 0755 |
|
_sqlite_udf.cpython-38-x86_64-linux-gnu.so | File | 88.42 KB | 0755 |
|
apsw_ext.py | File | 4.94 KB | 0644 |
|
berkeleydb.py | File | 4.04 KB | 0644 |
|
csv_loader.py | File | 75 B | 0644 |
|
csv_utils.py | File | 11.34 KB | 0644 |
|
dataset.py | File | 10.74 KB | 0644 |
|
db_url.py | File | 3.8 KB | 0644 |
|
djpeewee.py | File | 7.72 KB | 0644 |
|
fields.py | File | 11.53 KB | 0644 |
|
flask_utils.py | File | 5.53 KB | 0644 |
|
gfk.py | File | 5.89 KB | 0644 |
|
hybrid.py | File | 1.44 KB | 0644 |
|
kv.py | File | 4.69 KB | 0644 |
|
migrate.py | File | 23.49 KB | 0644 |
|
pool.py | File | 8.01 KB | 0644 |
|
postgres_ext.py | File | 13.13 KB | 0644 |
|
read_slave.py | File | 1.42 KB | 0644 |
|
reflection.py | File | 21.33 KB | 0644 |
|
shortcuts.py | File | 7.03 KB | 0644 |
|
signals.py | File | 2.2 KB | 0644 |
|
sqlcipher_ext.py | File | 4.12 KB | 0644 |
|
sqlite_ext.py | File | 34.94 KB | 0644 |
|
sqlite_udf.py | File | 13.26 KB | 0644 |
|
sqliteq.py | File | 8.34 KB | 0644 |
|
test_utils.py | File | 2.73 KB | 0644 |
|