-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsignals.py
84 lines (61 loc) · 3.28 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Signals used for indexing
"""
import logging
from django.db import transaction
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from profiles.models import (
Education,
Employment,
Profile,
)
from search.models import PercolateQuery
from search.tasks import (
index_users,
index_percolate_queries,
delete_percolate_query,
)
from roles.models import Role
log = logging.getLogger(__name__)
# all the following signal handlers do basically the same.
# The reason why there is one function per sender is
# because each signal handler needs to be hooked to a single sender
# otherwise it would run for any `post_save`/`post_delete` coming from any model
# pylint: disable=unused-argument
@receiver(post_save, sender=Profile, dispatch_uid="profile_post_save_index")
def handle_update_profile(sender, instance, **kwargs):
"""Update index when Profile model is updated."""
transaction.on_commit(lambda: index_users.delay([instance.user.id], check_if_changed=True))
@receiver(post_save, sender=Education, dispatch_uid="education_post_save_index")
def handle_update_education(sender, instance, **kwargs):
"""Update index when Education model is updated."""
transaction.on_commit(lambda: index_users.delay([instance.profile.user.id], check_if_changed=True))
@receiver(post_save, sender=Employment, dispatch_uid="employment_post_save_index")
def handle_update_employment(sender, instance, **kwargs):
"""Update index when Employment model is updated."""
transaction.on_commit(lambda: index_users.delay([instance.profile.user.id], check_if_changed=True))
@receiver(post_delete, sender=Education, dispatch_uid="education_post_delete_index")
def handle_delete_education(sender, instance, **kwargs):
"""Update index when Education model instance is deleted."""
transaction.on_commit(lambda: index_users.delay([instance.profile.user.id]))
@receiver(post_delete, sender=Employment, dispatch_uid="employment_post_delete_index")
def handle_delete_employment(sender, instance, **kwargs):
"""Update index when Employment model instance is deleted."""
transaction.on_commit(lambda: index_users.delay([instance.profile.user.id]))
@receiver(post_save, sender=PercolateQuery, dispatch_uid="percolate_query_save")
def handle_update_percolate(sender, instance, **kwargs):
"""When a new query is created or a query is updated, update Opensearch too"""
transaction.on_commit(lambda: index_percolate_queries.delay([instance.id]))
@receiver(post_delete, sender=PercolateQuery, dispatch_uid="percolate_query_delete")
def handle_delete_percolate(sender, instance, **kwargs):
"""When a query is deleted, make sure we also delete it on Opensearch"""
transaction.on_commit(lambda: delete_percolate_query.delay(instance.id))
@receiver(post_save, sender=Role, dispatch_uid="role_post_create_index")
def handle_create_role(sender, instance, **kwargs):
"""Update index when Role model instance is created."""
transaction.on_commit(lambda: index_users.delay([instance.user.id]))
@receiver(post_delete, sender=Role, dispatch_uid="role_post_remove_index")
def handle_remove_role(sender, instance, **kwargs):
"""Update index when Role model instance is deleted."""
transaction.on_commit(lambda: index_users.delay([instance.user.id]))