Skip to content

Commit

Permalink
waffle get_addon code for gravyvalet
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tordoff committed May 11, 2024
1 parent cf4ab0d commit 293e89a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
28 changes: 28 additions & 0 deletions addons/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,31 @@ def format_last_known_metadata(auth, node, file, error_type):
]
return ''.join(parts)
return msg


class GravyValetAddonAppConfig:
class MockNodeSetting:
def __init__(self, resource, auth, legacy_config):
...

class MockUserSetting:
def __init__(self, resource, auth, legacy_config):
...

def __init__(self, gravyvalet_data, resource, auth):
self.gravyvalet_data = gravyvalet_data
self.legacy_config = settings.ADDONS_AVAILABLE_DICT[self.gravyvalet_data['name']]
self.resource = resource
self.auth = auth
self.FOLDER_SELECTED = self.legacy_config.FOLDER_SELECTED
self.NODE_AUTHORIZED = self.legacy_config.NODE_DEAUTHORIZED
self.NODE_DEAUTHORIZED = self.legacy_config.NODE_DEAUTHORIZED
self.actions = self.legacy_config.actions

@property
def node_settings(self):
return self.MockNodeSetting(self.resource, self.auth, self.legacy_config)

@property
def user_settings(self):
return self.MockUserSetting(self.resource, self.auth, self.legacy_config)
17 changes: 16 additions & 1 deletion osf/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import itertools
import logging
import re
import waffle
import requests
from future.moves.urllib.parse import urljoin
import warnings
from rest_framework import status as http_status
Expand Down Expand Up @@ -54,6 +56,7 @@
from .user import OSFUser
from .validators import validate_title, validate_doi
from framework.auth.core import Auth
from osf import features
from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField
from osf.utils.fields import NonNaiveDateTimeField, ensure_str
from osf.utils.requests import get_request_and_user_id, string_type_request_headers
Expand Down Expand Up @@ -83,7 +86,7 @@
from api.caching import settings as cache_settings
from api.caching.utils import storage_usage_cache
from api.share.utils import update_share

from addons.base.utils import GravyValetAddonAppConfig

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -2430,6 +2433,18 @@ def _remove_from_associated_collections(self, auth=None, force=False):
force=True
)

def get_addon(self, name, is_deleted=False, auth=None):
request, user_id = get_request_and_user_id()
if waffle.flag_is_active(request, features.ENABLE_GV):
resp = requests.get(
settings.GV_NODE_ADDON_ENDPOINT.format(account_id=name),
auth=(auth.user.username, auth.user.password)
)
data = resp.json()
return GravyValetAddonAppConfig(data, self, auth)
else:
return super().get_addon(name, is_deleted)


class NodeUserObjectPermission(UserObjectPermissionBase):
"""
Expand Down
17 changes: 17 additions & 0 deletions osf/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,23 @@ def create_unregistered(cls, fullname, email=None):

return user

def get_addon(self, name, is_deleted=False, auth=None):
import waffle
import requests
from osf.utils.requests import get_request_and_user_id
from osf import features
from addons.base.utils import GravyValetAddonAppConfig
request, user_id = get_request_and_user_id()
if waffle.flag_is_active(request, features.ENABLE_GV):
resp = requests.get(
website_settings.GV_USER_ADDON_ENDPOINT.format(account_id=name),
auth=(request.user.username, request.user.password)
)
data = resp.json()['data']
return GravyValetAddonAppConfig(data, self, auth)
else:
return super().get_addon(name, is_deleted)

def update_guessed_names(self):
"""Updates the CSL name fields inferred from the the full name.
"""
Expand Down
8 changes: 8 additions & 0 deletions website/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,3 +2142,11 @@ def from_node_usage(cls, usage_bytes, private_limit=None, public_limit=None):

WAFFLE_VALUES_YAML = 'osf/features.yaml'
DEFAULT_DRAFT_NODE_TITLE = 'Untitled'
GV_RESOURCE_DOMAIN = 'http://192.168.168.167:8004/v1/resource-references/?filter[resource_uri]={owner_uri}'
GV_USER_DOMAIN = 'http://192.168.168.167:8004/v1/user-references/?filter[user_uri]={owner_uri}'
GV_API_ROOT = 'http://192.168.168.167:8004/v1'
GV_RESOURCE_ENDPOINT = GV_API_ROOT + 'resource-references/?filter[resource_uri]={resource_uri}'
GV_USER_ENDPOINT = GV_API_ROOT + 'user-references/?filter[user_uri]={owner_uri}'
# These two are for `get_addon` vs `get_addons`
GV_USER_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/authorized-storage-accounts/{account_id}'
GV_NODE_ADDON_ENDPOINT = 'http://192.168.168.167:8004/v1/configured-storage-addons/{addon_id}'

0 comments on commit 293e89a

Please sign in to comment.