SciPost Code Repository
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
SciPost
Manage
Activity
Members
Labels
Plan
Issues
118
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SciPost
SciPost
Commits
6f6b77db
Commit
6f6b77db
authored
6 years ago
by
Jean-Sébastien Caux
Browse files
Options
Downloads
Patches
Plain Diff
Fix through beloved ContentType (don't pass objects to celery tasks!)
parent
a02b3933
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
SciPost_v1/signalprocessors.py
+30
-26
30 additions, 26 deletions
SciPost_v1/signalprocessors.py
with
30 additions
and
26 deletions
SciPost_v1/signalprocessors.py
+
30
−
26
View file @
6f6b77db
...
...
@@ -2,6 +2,8 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__
=
"
AGPL v3
"
from
django.contrib.contenttypes.models
import
ContentType
from
haystack
import
signals
from
haystack.exceptions
import
NotHandled
...
...
@@ -11,13 +13,27 @@ from submissions.models import Submission
class
AutoSearchIndexingProcessor
(
signals
.
RealtimeSignalProcessor
):
@app.task
(
bind
=
True
,
name
=
'
signalprocessors.remove_object_indexes
'
,
serializer
=
'
yaml
'
)
def
remove_objects_indexes
(
self
,
sender
,
objects
):
@app.task
(
bind
=
True
,
name
=
'
signalprocessors.remove_object_indexes
'
)
def
remove_objects_indexes
(
self
,
sender_type_id
,
object_type_id
,
object_id
):
"""
Given a set of `objects` model instances, remove them from the index as preparation
for the new index.
"""
sender
=
ContentType
.
get_for_id
(
sender_type_id
)
object_type
=
ContentType
.
get_for_id
(
object_type_id
)
instance
=
object_type
.
get_object_for_this_type
(
pk
=
object_id
)
if
isinstance
(
instance
,
Submission
):
# Submission have complex status handling, so a status change should lead to
# more drastic reindexing.
ids_list
=
[
k
[
'
id
'
]
for
k
in
list
(
instance
.
thread
.
public
().
values
(
'
id
'
))]
objects
=
Submission
.
objects
.
filter
(
pk__in
=
ids_list
)
else
:
# Objects such as Reports, Comments, Commentaries, etc. may get rejected. This
# does not remove them from the index. Therefore, do a complete rebuild_index
# action on that specific instance every time the index signal is triggered.
objects
=
[
instance
]
try
:
using_backends
=
self
.
connection_router
.
for_write
(
instance
=
objects
[
0
])
except
IndexError
:
...
...
@@ -33,12 +49,15 @@ class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor):
# TODO: Maybe log it or let the exception bubble?
pass
@app.task
(
bind
=
True
,
name
=
'
signalprocessors.update_instance_indexes
'
,
serializer
=
'
yaml
'
)
def
update_instance_indexes
(
self
,
sender
,
instance
):
@app.task
(
bind
=
True
,
name
=
'
signalprocessors.update_instance_indexes
'
)
def
update_instance_indexes
(
self
,
sender_type_id
,
object_type_id
,
object_id
):
"""
Given an individual model instance, update its entire indexes.
"""
sender
=
ContentType
.
get_for_id
(
sender_type_id
)
object_type
=
ContentType
.
get_for_id
(
object_type_id
)
instance
=
object_type
.
get_object_for_this_type
(
pk
=
object_id
)
try
:
using_backends
=
self
.
connection_router
.
for_write
(
instance
=
instance
)
except
IndexError
:
...
...
@@ -54,23 +73,8 @@ class AutoSearchIndexingProcessor(signals.RealtimeSignalProcessor):
pass
def
handle_save
(
self
,
sender
,
instance
,
**
kwargs
):
if
isinstance
(
instance
,
Submission
):
# Submission have complex status handling, so a status change should lead to
# more drastic reindexing.
chain
=
(
self
.
remove_objects_indexes
.
s
(
sender
,
instance
.
thread
.
public
())
|
self
.
update_instance_indexes
.
s
(
sender
,
instance
)
)
chain
()
else
:
# Objects such as Reports, Comments, Commentaries, etc. may get rejected. This
# does not remove them from the index. Therefore, do a complete rebuild_index
# action on that specific instance every time the index signal is triggered.
chain
=
(
self
.
remove_objects_indexes
.
s
(
sender
,
[
instance
])
|
self
.
update_instance_indexes
.
s
(
sender
,
instance
)
)
chain
()
sender_type_id
=
ContentType
.
objects
.
get_for_model
(
sender
).
id
instance_type_id
=
ContentType
.
objects
.
get_for_model
(
instance
).
id
chain
=
(
self
.
remove_objects_indexes
.
s
(
sender_type_id
,
instance_type_id
,
instance
.
id
)
|
self
.
update_instance_indexes
.
s
(
sender_type_id
,
instance_type_id
,
instance
.
id
))
chain
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment