Skip to content

Commit

Permalink
better sync models with deletetion and root folder changes
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 3, 2024
1 parent 56cbec9 commit 5fdb5b7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
49 changes: 19 additions & 30 deletions addons/box/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from addons.box import settings
from addons.box.serializer import BoxSerializer
from website.util import api_v2_url
from website.settings import DOMAIN

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -99,34 +100,25 @@ def revoke_remote_oauth_access(self, external_account):

@staticmethod
def sync_with_gravyvalet(owner, is_deleted):

resp = requests.get(
f'{settings.GV_DOMAIN}v1/user-references/?filter[user_uri]={owner.uri}'
f'{DOMAIN}v1/user-references/?filter[user_uri]={owner.absolute_url}'
)
settings_obj = None
try:
settings_obj = UserSettings.objects.get(owner=owner)
except UserSettings.DoesNotExist:
if resp.status_code == 404:
# addon not enabled
return None
settings_obj, created = UserSettings.objects.get_or_create(owner=owner)
if resp.status_code == 404 and created:
# addon not enabled
return None

data = resp.json()

# addon disabled on GV, but not here
if settings_obj and resp.status_code == 404 or (is_deleted and data.get('deleted')):
if resp.status_code == 404 or resp.status_code == 410:
settings_obj.delete()
return settings_obj

settings_obj, created = UserSettings.objects.get_or_create(owner=owner)
# addon disabled on GV, but not here

settings_obj.oauth_scopes = data.get('oauth_scopes', settings_obj.oauth_scopes)
settings_obj.is_deleted = data.get('is_deleted', settings_obj.is_deleted)

if 'user_settings' in data:
for key, value in data['user_settings'].items():
setattr(settings_obj, key, value)

settings_obj.save = lambda: NotImplementedError('Can\'t update legacy model') # freeze model

return settings_obj
Expand Down Expand Up @@ -305,27 +297,24 @@ def on_delete(self):
@staticmethod
def sync_with_gravyvalet(owner, is_deleted):
resp = requests.get(
f'{settings.GV_DOMAIN}v1/resource-references/?filter[resource_uri]={owner.uri}'
f'{DOMAIN}v1/resource-references/?filter[resource_uri]={owner.absolute_url}'
)
if resp.status_code == 404:
settings_obj, created = NodeSettings.objects.get_or_create(owner=owner)

if resp.status_code == 404 and created:
# addon not enabled
return None
else:
data = resp.json()

settings_obj, created = NodeSettings.objects.get_or_create(owner=owner)
if resp.status_code == 404 or resp.status_code == 410:
settings_obj.delete()
return settings_obj

if is_deleted or data.get('deleted'):
return None
data = resp.json()

settings_obj.folder_id = data.get('folder_id', settings_obj.folder_id)
settings_obj.folder_name = data.get('folder_name', settings_obj.folder_name)
settings_obj.folder_path = data.get('folder_path', settings_obj.folder_path)
settings_obj.is_deleted = data.get('is_deleted', settings_obj.is_deleted)
configured_storage_addon = data[0]['relationships']['configured_storage_addons']['links']['self']
folder_id = requests.get(configured_storage_addon).json()['attributes']['root_folder']

if 'user_settings' in data:
for key, value in data['user_settings'].items():
setattr(settings_obj, key, value)
settings_obj.set_folder(folder_id)

settings_obj.save = lambda: NotImplemented('Can\'t update legacy model') # freeze model

Expand Down
3 changes: 0 additions & 3 deletions addons/box/settings/defaults.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from website import settings
# OAuth app keys
BOX_KEY = None
BOX_SECRET = None
Expand All @@ -13,5 +12,3 @@

# Max file size permitted by frontend in megabytes
MAX_UPLOAD_SIZE = 250

GV_DOMAIN = settings.DOMAIN # configuratable for local env
8 changes: 0 additions & 8 deletions addons/box/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
patch_client,
mock_responses
)
from waffle.testutils import override_flag
from osf import features

mock_client = MockBox()
pytestmark = pytest.mark.django_db
Expand All @@ -48,9 +46,6 @@ def tearDown(self):
def test_delete_external_account(self):
super(TestAuthViews, self).test_delete_external_account()

def test_delete_external_account_sunset(self):
with override_flag(features.ENABLE_GV, active=True):
self.test_delete_external_account()


class TestConfigViews(BoxAddonTestCase, views_testing.OAuthAddonConfigViewsTestCaseMixin, OsfTestCase):
Expand Down Expand Up @@ -79,9 +74,6 @@ def tearDown(self):
def test_import_auth(self, *args):
super(TestConfigViews, self).test_import_auth()

def test_import_auth_sunset(self):
with override_flag(features.ENABLE_GV, active=True):
self.test_import_auth()

class TestFilebrowserViews(BoxAddonTestCase, OsfTestCase):

Expand Down
6 changes: 3 additions & 3 deletions osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,10 @@ def get_addon(self, name, is_deleted=False):
settings_model = self._settings_model(name)
except LookupError:
return None
if not settings_model and not waffle.flag_is_active(request, features.ENABLE_GV):
return None
if not settings_model:
return settings_model

if waffle.flag_is_active(request, features.ENABLE_GV):
if waffle.flag_is_active(request, features.ENABLE_GV) and hasattr(settings_model, 'sync_with_gravyvalet'):
return settings_model.sync_with_gravyvalet(self, is_deleted)
else:
try:
Expand Down

0 comments on commit 5fdb5b7

Please sign in to comment.