Skip to content

Commit

Permalink
Merge pull request #205 from opaduchak/fix/account_migrations
Browse files Browse the repository at this point in the history
[ENG-6932] Fix account migrations
  • Loading branch information
adlius authored Jan 15, 2025
2 parents db51d92 + 7cd2ccf commit 7e1d267
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
22 changes: 16 additions & 6 deletions addon_service/management/commands/migrate_authorized_account.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
26 changes: 23 additions & 3 deletions addon_service/osf_models/models.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 7e1d267

Please sign in to comment.