diff --git a/addon_service/management/commands/migrate_authorized_account.py b/addon_service/management/commands/migrate_authorized_account.py index d81cc14a..69a10111 100644 --- a/addon_service/management/commands/migrate_authorized_account.py +++ b/addon_service/management/commands/migrate_authorized_account.py @@ -1,5 +1,7 @@ from urllib.parse import quote_plus +from django.contrib.contenttypes.models import ContentType +from django.core.cache import cache from django.core.management import BaseCommand from django.db import transaction @@ -87,7 +89,16 @@ def fetch_external_accounts(user_id: int, provider: str): def get_node_guid(id_): - return Guid.objects.filter(content_type_id=7, object_id=id_).first()._id + content_type_id = cache.get_or_set( + "node_contenttype_id", + lambda: ContentType.objects.using("osf") + .get(app_label="osf", model="abstractnode") + .id, + timeout=None, + ) + return ( + Guid.objects.filter(content_type_id=content_type_id, object_id=id_).first()._id + ) OSF_BASE = settings.OSF_API_BASE_URL.replace("192.168.168.167", "localhost").replace( @@ -136,15 +147,12 @@ def handle(self, *args, **options): integration_type, service_name, user_settings, - node_settings_class, ) except BaseException as e: print(f"Failed to migrate {service_name} service with error {e}") raise e - def migrate_for_user( - self, integration_type, service_name, user_settings, node_settings_class - ): + def migrate_for_user(self, integration_type, service_name, user_settings): if integration_type == "storage": AuthorizedAccount = AuthorizedStorageAccount ConfiguredAddon = ConfiguredStorageAddon @@ -198,13 +206,15 @@ def migrate_for_user( resource_uri=f"{OSF_BASE}/{get_node_guid(node_settings.owner_id)}" )[0] configured_addon = ConfiguredAddon( - root_folder=get_root_folder_for_provider(node_settings, service_name), int_connected_capabilities=( AddonCapabilities.UPDATE | AddonCapabilities.ACCESS ).value, base_account=account, authorized_resource=resource_reference, ) + root_folder = get_root_folder_for_provider(node_settings, service_name) + if root_folder is not None: + configured_addon.root_folder = root_folder configured_addon.save() def get_credentials(self, external_service, osf_account): diff --git a/addon_service/osf_models/models.py b/addon_service/osf_models/models.py index 0ee5930b..9fa145c7 100644 --- a/addon_service/osf_models/models.py +++ b/addon_service/osf_models/models.py @@ -1,4 +1,6 @@ +from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.fields import ArrayField +from django.core.cache import cache from django.db import models from django.db.models import CharField @@ -75,15 +77,33 @@ class Meta: app_label = "osf" +class AbstractNode(Model): + class Meta: + db_table = "osf_guid" + managed = False + app_label = "osf" + + class OsfUser(Model): @property def guid(self): - return Guid.objects.filter(content_type_id=70, object_id=self.id).first()._id + content_type_id = cache.get_or_set( + "user_contenttype_id", + lambda: ContentType.objects.using("osf") + .get(app_label="osf", model="osfuser") + .id, + timeout=None, + ) + return ( + Guid.objects.filter(content_type_id=content_type_id, object_id=self.id) + .first() + ._id + ) class Meta: db_table = "osf_osfuser" managed = False - app_label = "addons" + app_label = "osf" class UserToExternalAccount(Model): @@ -93,7 +113,7 @@ class UserToExternalAccount(Model): class Meta: db_table = "osf_osfuser_external_accounts" managed = False - app_label = "addons" + app_label = "osf" class BaseOAuthNodeSettings(Model):