""" Provide django-style hooks for model events. """ from peewee import Model as _Model class Signal(object): def __init__(self): self._flush() def _flush(self): self._receivers = set() self._receiver_list = [] def connect(self, receiver, name=None, sender=None): name = name or receiver.__name__ key = (name, sender) if key not in self._receivers: self._receivers.add(key) self._receiver_list.append((name, receiver, sender)) else: raise ValueError('receiver named %s (for sender=%s) already ' 'connected' % (name, sender or 'any')) def disconnect(self, receiver=None, name=None, sender=None): if receiver: name = name or receiver.__name__ if not name: raise ValueError('a receiver or a name must be provided') key = (name, sender) if key not in self._receivers: raise ValueError('receiver named %s for sender=%s not found.' % (name, sender or 'any')) self._receivers.remove(key) self._receiver_list = [(n, r, s) for n, r, s in self._receiver_list if n != name and s != sender] 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 n, r, s in self._receiver_list: if s is None or isinstance(instance, s): responses.append((r, r(sender, instance, *args, **kwargs))) return responses pre_save = Signal() post_save = Signal() pre_delete = Signal() post_delete = Signal() pre_init = Signal() class Model(_Model): def __init__(self, *args, **kwargs): super(Model, self).__init__(*args, **kwargs) pre_init.send(self) def save(self, *args, **kwargs): pk_value = self._pk if self._meta.primary_key else True 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 |
|
apsw_ext.py | File | 4.82 KB | 0644 |
|
cockroachdb.py | File | 9 KB | 0644 |
|
dataset.py | File | 14.09 KB | 0644 |
|
db_url.py | File | 4.15 KB | 0644 |
|
fields.py | File | 1.66 KB | 0644 |
|
flask_utils.py | File | 8 KB | 0644 |
|
hybrid.py | File | 1.49 KB | 0644 |
|
kv.py | File | 5.48 KB | 0644 |
|
migrate.py | File | 30.11 KB | 0644 |
|
mysql_ext.py | File | 3.17 KB | 0644 |
|
pool.py | File | 11.21 KB | 0644 |
|
postgres_ext.py | File | 14.41 KB | 0644 |
|
psycopg3_ext.py | File | 1.15 KB | 0644 |
|
reflection.py | File | 30.2 KB | 0644 |
|
shortcuts.py | File | 11.25 KB | 0644 |
|
signals.py | File | 2.46 KB | 0644 |
|
sqlcipher_ext.py | File | 3.55 KB | 0644 |
|
sqlite_changelog.py | File | 4.68 KB | 0644 |
|
sqlite_ext.py | File | 45.65 KB | 0644 |
|
sqlite_udf.py | File | 13.34 KB | 0644 |
|
sqliteq.py | File | 9.75 KB | 0644 |
|
test_utils.py | File | 1.81 KB | 0644 |
|