Page Menu
Home
Search
Configure Global Search
Log In
Files
F13264990
algolia_indexing.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
1 KB
Subscribers
None
algolia_indexing.py
View Options
import
logging
from
algoliasearch.helpers
import
AlgoliaException
import
bson
from
pillar
import
current_app
log
=
logging
.
getLogger
(
__name__
)
def
push_updated_user
(
user_id
:
str
):
"""Push an update to the Algolia index when a user item is updated"""
from
pillar.api.utils.algolia
import
algolia_index_user_save
user_oid
=
bson
.
ObjectId
(
user_id
)
log
.
info
(
'Retrieving user
%s
'
,
user_oid
)
users_coll
=
current_app
.
db
(
'users'
)
user
=
users_coll
.
find_one
({
'_id'
:
user_oid
})
if
user
is
None
:
log
.
warning
(
'Unable to find user
%s
, not updating Algolia.'
,
user_oid
)
return
try
:
algolia_index_user_save
(
user
)
except
AlgoliaException
as
ex
:
log
.
warning
(
'Unable to push user info to Algolia for user "
%s
", id=
%s
;
%s
'
,
# noqa
user
.
get
(
'username'
),
user_id
,
ex
)
def
index_node_save
(
node_id
:
str
):
from
pillar.api.utils.algolia
import
algolia_index_node_save
node_oid
=
bson
.
ObjectId
(
node_id
)
log
.
info
(
'Retrieving node
%s
'
,
node_oid
)
nodes_coll
=
current_app
.
db
(
'nodes'
)
node
=
nodes_coll
.
find_one
({
'_id'
:
node_oid
})
if
node
is
None
:
log
.
warning
(
'Unable to find node
%s
, not updating Algolia.'
,
node_id
)
return
try
:
algolia_index_node_save
(
node
)
except
AlgoliaException
as
ex
:
log
.
warning
(
'Unable to push node info to Algolia for node
%s
;
%s
'
,
node_id
,
ex
)
# noqa
def
index_node_delete
(
node_id
:
str
):
from
pillar.api.utils.algolia
import
algolia_index_node_delete
# Deleting a node takes nothing more than the ID anyway.
# No need to fetch anything from Mongo.
fake_node
=
{
'_id'
:
bson
.
ObjectId
(
node_id
)}
try
:
algolia_index_node_delete
(
fake_node
)
except
AlgoliaException
as
ex
:
log
.
warning
(
'Unable to delete node info to Algolia for node
%s
;
%s
'
,
node_id
,
ex
)
# noqa
File Metadata
Details
Attached
Mime Type
text/x-python
Expires
Sat, Jul 9, 10:38 AM (1 d, 23 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
1a/f9/fcc23dcd7d18e6c0ea2b47b7bd15
Attached To
rPS Pillar
Event Timeline
Log In to Comment