Skip to content

Commit

Permalink
sort out urls and syncing data
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 1, 2024
1 parent 39823f1 commit 56cbec9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
59 changes: 46 additions & 13 deletions addons/box/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,35 @@ def revoke_remote_oauth_access(self, external_account):

@staticmethod
def sync_with_gravyvalet(owner, is_deleted):

resp = requests.get(
f'{settings.DOMAIN}v1/resource-references/{owner.uri}/authorized_storage_accounts/'
f'{settings.GV_DOMAIN}v1/user-references/?filter[user_uri]={owner.uri}'
)
if resp.status_code == 404:
# addon not enabled
return None
else:
data = resp.json()
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 = UserSettings.objects.get_or_create(owner=owner)
data = resp.json()

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

settings_obj, created = UserSettings.objects.get_or_create(owner=owner)

settings_obj.oauth_scopes = data.get('oauth_scopes', settings_obj.oauth_scopes)
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)

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
settings_obj.save = lambda: NotImplementedError('Can\'t update legacy model') # freeze model

return settings_obj

Expand Down Expand Up @@ -297,3 +301,32 @@ def after_delete(self, user=None):
def on_delete(self):
self.deauthorize(add_log=False)
self.save()

@staticmethod
def sync_with_gravyvalet(owner, is_deleted):
resp = requests.get(
f'{settings.GV_DOMAIN}v1/resource-references/?filter[resource_uri]={owner.uri}'
)
if resp.status_code == 404:
# addon not enabled
return None
else:
data = resp.json()

settings_obj, created = NodeSettings.objects.get_or_create(owner=owner)

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

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)

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

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

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

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

GV_DOMAIN = settings.DOMAIN # configuratable for local env
1 change: 0 additions & 1 deletion osf/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import markupsafe
import logging
import waffle
import requests

from django.apps import apps
from django.contrib.auth.models import Group, AnonymousUser
Expand Down

0 comments on commit 56cbec9

Please sign in to comment.