forked from 20/gitannex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignals.py
50 lines (40 loc) · 1.69 KB
/
signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import logging
import django.dispatch
"""
Arquivo de definicao dos sinais.
Os sinais sao usados para interligar diferentes *apps* do Django.
"""
def get_subclasses(classes, level=0):
"""
Procura as subclasses da uma classe dada, retorna a lista de subclasses.
Return the list of all subclasses given class (or list of classes) has.
Inspired by this question:
http://stackoverflow.com/questions/3862310/how-can-i-find-all-subclasses-of-a-given-class-in-python
"""
# for convenience, only one class can can be accepted as argument
# converting to list if this is the case
if not isinstance(classes, list):
classes = [classes]
if level < len(classes):
classes += classes[level].__subclasses__()
return get_subclasses(classes, level+1)
else:
return classes
def receiver_subclasses(signal, sender, dispatch_uid_prefix, **kwargs):
"""
Decorador para conectar todos os sinais do *receiver* e das subclasses do *receiver*.
A decorator for connecting receivers and all receiver's subclasses to signals. Used by passing in the
signal and keyword arguments to connect::
@receiver_subclasses(post_save, sender=MyModel)
def signal_receiver(sender, **kwargs):
...
"""
def _decorator(func):
all_senders = get_subclasses(sender)
logging.info(all_senders)
for snd in all_senders:
signal.connect(func, sender=snd, dispatch_uid=dispatch_uid_prefix+'_'+snd.__name__, **kwargs)
return func
return _decorator
## Novo sinal para alertar que os repositorios sao sincronizados
filesync_done = django.dispatch.Signal(providing_args=["repositoryName", "repositoryDir"])