Skip to content

Commit

Permalink
update_share from update_search
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Aug 2, 2023
1 parent 8cac5d0 commit cf35cf0
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 55 deletions.
29 changes: 12 additions & 17 deletions api/share/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"""
from django.apps import apps
import random
import requests
from framework.celery_tasks import app as celery_app
from framework.celery_tasks.handlers import enqueue_task
from framework.sentry import log_exception

from website import settings
from celery.exceptions import Retry
from osf.metadata.tools import pls_update_trove_indexcard, pls_delete_trove_indexcard
from osf.metadata.tools import pls_send_trove_indexcard, pls_delete_trove_indexcard

def is_qa_resource(resource):
"""
Expand All @@ -28,36 +28,31 @@ def is_qa_resource(resource):


def update_share(resource):
if _should_delete_indexcard(resource):
resp = pls_delete_trove_indexcard(resource)
else:
resp = pls_update_trove_indexcard(resource)
status_code = resp.status_code
try:
resp.raise_for_status()
except requests.HTTPError:
if status_code >= 500:
async_update_resource_share.delay(resource._id)
else:
log_exception()
if not settings.SHARE_ENABLED:
return
enqueue_task(task__update_share.s(resource._id))


@celery_app.task(bind=True, max_retries=4, acks_late=True)
def async_update_resource_share(self, guid, **kwargs):
def task__update_share(self, guid: str, **kwargs):
"""
This function updates share takes Preprints, Projects and Registrations.
:param self:
:param guid:
:return:
"""
resource = apps.get_model('osf.Guid').load(guid).referent
resp = pls_update_trove_indexcard(resource)
resp = (
pls_delete_trove_indexcard(resource)
if _should_delete_indexcard(resource)
else pls_send_trove_indexcard(resource)
)
try:
resp.raise_for_status()
except Exception as e:
if self.request.retries == self.max_retries:
log_exception()
elif resp.status_code >= 500:
elif resp.status_code >= 500 and settings.USE_CELERY:
try:
self.retry(
exc=e,
Expand Down
5 changes: 5 additions & 0 deletions osf/management/commands/recatalog_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ def add_arguments(self, parser):
action='store_true',
help='recatalog metadata for non-registration projects (and components)',
)
# type_group.add_argument(
# '--files',
# action='store_true',
# help='recatalog metadata for files',
# )

parser.add_argument(
'--start-id',
Expand Down
2 changes: 0 additions & 2 deletions osf/management/commands/reindex_quickfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

PAGE_SIZE = 100
from tqdm import tqdm
from api.share.utils import update_share

def paginated_progressbar(queryset, page_size, function):
paginator = Paginator(queryset, page_size)
Expand All @@ -32,7 +31,6 @@ def reindex_quickfiles():
paginated_progressbar(files_to_reindex, PAGE_SIZE, update_file)

for node in nodes:
update_share(node)
node.update_search()


Expand Down
2 changes: 1 addition & 1 deletion osf/metadata/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def pls_gather_metadata_file(osf_item, format_key, serializer_config=None) -> Se
)


def pls_update_trove_indexcard(osf_item):
def pls_send_trove_indexcard(osf_item):
_iri = osf_iri(osf_item)
if not _iri:
raise ValueError(f'could not get iri for {osf_item}')
Expand Down
2 changes: 2 additions & 0 deletions osf/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField
from osf.utils.fields import NonNaiveDateTimeField
from api.base.utils import waterbutler_api_url_for
from api.share.utils import update_share
from website.files import utils
from website.files.exceptions import VersionNotFoundError
from website.util import api_v2_url, web_url_for, api_url_for
Expand Down Expand Up @@ -443,6 +444,7 @@ def delete(self, user=None, save=True, deleted_on=None):
return self

def update_search(self):
update_share(self)
from website import search
try:
search.search.update_file(self)
Expand Down
7 changes: 2 additions & 5 deletions osf/models/institution.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def banner_path(self):
return '/static/img/institutions/banners/placeholder-banner.png'

def update_search(self):
from website.search.search import update_institution, update_node
from website.search.search import update_institution
from website.search.exceptions import SearchUnavailableError

try:
Expand All @@ -197,10 +197,7 @@ def update_search(self):
logger.exception(e)

for node in self.nodes.filter(is_deleted=False):
try:
update_node(node, async_update=False)
except SearchUnavailableError as e:
logger.exception(e)
node.update_search()

def save(self, *args, **kwargs):
saved_fields = self.get_dirty_fields()
Expand Down
12 changes: 3 additions & 9 deletions osf/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,8 @@ def should_request_identifiers(self):

@classmethod
def bulk_update_search(cls, nodes, index=None):
for _node in nodes:
update_share(_node)
from website import search
try:
serialize = functools.partial(search.search.update_node, index=index, bulk=True, async_update=False)
Expand All @@ -719,8 +721,8 @@ def bulk_update_search(cls, nodes, index=None):
log_exception()

def update_search(self):
update_share(self)
from website import search

try:
search.search.update_node(self, bulk=False, async_update=True)
if self.collection_submissions.exists() and self.is_public:
Expand Down Expand Up @@ -1038,8 +1040,6 @@ def add_tag_log(self, tag, auth):
# Override Taggable
def on_tag_added(self, tag):
self.update_search()
if settings.SHARE_ENABLED:
update_share(self)

def remove_tag(self, tag, auth, save=True):
if not tag:
Expand All @@ -1062,9 +1062,6 @@ def remove_tag(self, tag, auth, save=True):
if save:
self.save()
self.update_search()
if settings.SHARE_ENABLED:
update_share(self)

return True

def remove_tags(self, tags, auth, save=True):
Expand All @@ -1074,9 +1071,6 @@ def remove_tags(self, tags, auth, save=True):
"""
super(AbstractNode, self).remove_tags(tags, auth, save)
self.update_search()
if settings.SHARE_ENABLED:
update_share(self)

return True

def set_visible(self, user, visible, log=True, auth=None, save=False):
Expand Down
4 changes: 4 additions & 0 deletions osf/models/preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
TagNotFoundError
)
from django.contrib.postgres.fields import ArrayField
from api.share.utils import update_share

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -901,6 +902,8 @@ def set_contributor_order(self, contributor_ids):

@classmethod
def bulk_update_search(cls, preprints, index=None):
for _preprint in preprints:
update_share(_preprint)
from website import search
try:
serialize = functools.partial(search.search.update_preprint, index=index, bulk=True, async_update=False)
Expand All @@ -910,6 +913,7 @@ def bulk_update_search(cls, preprints, index=None):
log_exception()

def update_search(self):
update_share(self)
from website import search
try:
search.search.update_preprint(self, bulk=False, async_update=True)
Expand Down
16 changes: 3 additions & 13 deletions osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
)
from dirtyfields import DirtyFieldsMixin

from api.share.utils import update_share
from framework.auth import Auth
from framework.exceptions import PermissionsError
from osf.utils.fields import NonNaiveDateTimeField
Expand Down Expand Up @@ -825,17 +824,9 @@ def withdraw(self):
# Pass auth=None because the registration initiator may not be
# an admin on components (component admins had the opportunity
# to disapprove the retraction by this point)
for node in self.get_descendants_recursive(primary_only=True):
for node in self.node_and_primary_descendants():
node.set_privacy('public', auth=None, log=False)
node.update_search()

# force a save before sending data to share or retraction will not be updated
self.set_privacy('public', auth=None, log=False)
self.update_search()
self.save()

if settings.SHARE_ENABLED:
update_share(self)
AbstractNode.bulk_update_search(self.node_and_primary_descendants())

def copy_registration_responses_into_schema_response(self, draft_registration=None, save=True):
"""Copies registration metadata into schema responses"""
Expand All @@ -862,8 +853,7 @@ def on_schema_response_completed(self):
archive_to_ia(children)

def related_resource_updated(self, log_action=None, api_request=None, **log_params):
if settings.SHARE_ENABLED:
update_share(self)
self.update_search()
if not log_action:
return

Expand Down
2 changes: 2 additions & 0 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
MergeConflictError)
from framework.exceptions import PermissionsError
from framework.sessions.utils import remove_sessions_for_user
from api.share.utils import update_share
from osf.utils.requests import get_current_request
from osf.exceptions import reraise_django_validation_errors, UserStateError
from osf.models.base import BaseModel, GuidMixin, GuidMixinQuerySet
Expand Down Expand Up @@ -1444,6 +1445,7 @@ def is_assumed_ham(self):
return user_has_trusted_email

def update_search(self):
update_share(self)
from website.search.search import update_user
update_user(self)

Expand Down
2 changes: 1 addition & 1 deletion scripts/migration/migrate_share_preprint_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def migrate(dry=True):
logger.info('{}/{} - {}'.format(count, target_count, preprint_id))
try:
if not dry:
on_preprint_updated(preprint_id, update_share=True)
on_preprint_updated(preprint_id)
# Sleep in order to be nice to EZID
time.sleep(1)
except Exception as e:
Expand Down
3 changes: 1 addition & 2 deletions website/archiver/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,5 +313,4 @@ def archive_success(dst_pk, job_pk):
job.save()
dst.sanction.ask(dst.get_active_contributors_recursive(unique_users=True))

if settings.SHARE_ENABLED:
update_share(dst)
update_share(dst)
5 changes: 0 additions & 5 deletions website/preprints/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from framework.celery_tasks import app as celery_app
from framework.postcommit_tasks.handlers import enqueue_postcommit_task, get_task_from_postcommit_queue

from website import settings
from api.share.utils import update_share

logger = logging.getLogger(__name__)

Expand All @@ -27,9 +25,6 @@ def on_preprint_updated(preprint_id, old_subjects=None, saved_fields=None):
if should_update_preprint_identifiers(preprint, old_subjects, saved_fields):
update_or_create_preprint_identifiers(preprint)

if settings.SHARE_ENABLED:
update_share(preprint)


def should_update_preprint_identifiers(preprint, old_subjects, saved_fields):
# Only update identifier metadata iff...
Expand Down

0 comments on commit cf35cf0

Please sign in to comment.