Skip to content

Commit

Permalink
fix: first attempt at resource API
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Sep 28, 2024
1 parent f2bd121 commit 0e0fdad
Show file tree
Hide file tree
Showing 41 changed files with 10,858 additions and 387 deletions.
27 changes: 15 additions & 12 deletions arches_orm/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
from functools import partial, wraps
from contextlib import contextmanager
from contextvars import ContextVar

from abc import ABC, abstractmethod

from .view_models._base import ResourceInstanceViewModel
from .view_models.concepts import ConceptValueViewModel

_ADMINISTRATION_MODE: bool = False

logger = logging.getLogger(__name__)

class Adapter(ABC):
config: dict[str, Any]
_context: ContextVar[dict[str, Any] | None]
_singleton: Adapter | None = None

def __init__(self, key):
self.config = {}
Expand All @@ -29,6 +27,17 @@ def __init__(self, key):
def __init_subclass__(cls):
ADAPTER_MANAGER.register_adapter(cls)

def __str__(self):
return self.key

def __repr__(self):
return f"<AORA:{self.key}>"

@property
@abstractmethod
def key(self):
...

def set_context_free(self):
self._context.set(None)

Expand All @@ -53,12 +62,6 @@ def load_from_id(self, resource_id: str, from_prefetch: Callable[[str], Any] | N
...

def get_context(self):
if _ADMINISTRATION_MODE:
try:
self._context.get()
except LookupError:
self._context.set(None)

return self._context

@contextmanager
Expand Down Expand Up @@ -119,6 +122,7 @@ def register_adapter(self, adapter_cls, key=None):
"Must set a default adapter, if registering multiple in one process"
)
adapter = adapter_cls(key=key)
adapter_cls._singleton = adapter
self.adapters[key] = adapter

def set_default_adapter(self, default_adapter):
Expand Down Expand Up @@ -170,9 +174,8 @@ def admin(adapter_key: str | None=None):
with get_adapter(adapter_key).context(None, _override=True) as cvar:
yield cvar

def admin_everywhere():
global _ADMINISTRATION_MODE
_ADMINISTRATION_MODE = True
def admin_everywhere(key=None):
get_adapter(key=key).set_context_free()
logger.warning(
"ARCHES ORM ADMINISTRATION MODE ON: use for debugging only, "
"otherwise use the `context_free` or `context` decorator/with statement to "
Expand Down
30 changes: 10 additions & 20 deletions arches_orm/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import enum
from . import utils

class DataTypeNames(enum.Enum):
SEMANTIC = "semantic"
STRING = "string"
CONCEPT = "concept"
CONCEPT_LIST = "concept-list"
RESOURCE_INSTANCE = "resource-instance"
RESOURCE_INSTANCE_LIST = "resource-instance-list"
GEOJSON_FEATURE_COLLECTION = "geojson-feature-collection"
EDTF = "edtf"
FILE_LIST = "file-list"
USER = "user"
BOOLEAN = "boolean"
NUMBER = "number"
DATE = "date"
URL = "url"
DOMAIN_VALUE = "domain-value"
NODE_VALUE = "node-value"
BNGCENTREPOINT = "bngcentrepoint"
DOMAIN_VALUE_LIST = "domain-value-list"
DJANGO_GROUP = "django-group"
DataTypeNames = enum.Enum(
"DataTypeNames",
[(es.name, es.value) for es in utils.StandardDataTypeNames]
+ [(key, value) for key, (value, _) in utils._CUSTOM_DATATYPES.items()]
)

COLLECTS_MULTIPLE_VALUES = {
DataTypeNames.CONCEPT_LIST,
DataTypeNames.FILE_LIST,
DataTypeNames.DOMAIN_VALUE_LIST,
DataTypeNames.RESOURCE_INSTANCE_LIST,
}
COLLECTS_MULTIPLE_VALUES |= {DataTypeNames(key) for key, (_, c) in utils._CUSTOM_DATATYPES.items() if c}

# Prevent further custom datatypes being added.
utils._CUSTOM_DATATYPES = None
18 changes: 9 additions & 9 deletions arches_orm/graphql/_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@
from starlette_context import context as starlette_context
from arches_orm.adapter import get_adapter

from .auth import BasicAuthBackend
#from .django_auth import BasicAuthBackend
from .resources import ResourceQuery, FullResourceMutation
from .concepts import ConceptQuery, FullConceptMutation
from .resource_models import ResourceModelQuery
# RMV from .concepts import ConceptQuery, FullConceptMutation
# RMV from .resource_models import ResourceModelQuery

class UnauthorizedException(Exception):
...

DEBUG = os.getenv("DEBUG", "False") == "True"

resources_schema = graphene.Schema(query=ResourceQuery, mutation=FullResourceMutation)
concept_schema = graphene.Schema(query=ConceptQuery, mutation=FullConceptMutation)
resource_model_schema = graphene.Schema(query=ResourceModelQuery)
# RMV concept_schema = graphene.Schema(query=ConceptQuery, mutation=FullConceptMutation)
# RMV resource_model_schema = graphene.Schema(query=ResourceModelQuery)

class App(HTTPEndpoint):
async def get(self, request):
return PlainTextResponse("OK")

class ORMContextMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
with get_adapter().context(user=starlette_context.data["user"]) as _:
with get_adapter().context(user=starlette_context.data.get("user")) as _:
return await call_next(request)

# TODO: More elegant solution, using the schema.
Expand All @@ -57,7 +57,7 @@ def admin_middleware(next_mw, root, info, **args):
plugins.CorrelationIdPlugin()
)
),
Middleware(AuthenticationMiddleware, backend=BasicAuthBackend()),
#Middleware(AuthenticationMiddleware, backend=BasicAuthBackend()),
Middleware(ORMContextMiddleware),
]

Expand All @@ -67,5 +67,5 @@ def admin_middleware(next_mw, root, info, **args):

app = Starlette(debug=DEBUG, routes=[Route("/", App)], middleware=middleware)
app.mount("/resources/", GraphQLApp(resources_schema, on_get=make_graphiql_handler()))
app.mount("/concepts/", GraphQLApp(concept_schema, on_get=make_graphiql_handler(), middleware=graphql_admin_middleware))
app.mount("/resource-models/", GraphQLApp(resource_model_schema, on_get=make_graphiql_handler(), middleware=graphql_admin_middleware))
# RMV app.mount("/concepts/", GraphQLApp(concept_schema, on_get=make_graphiql_handler(), middleware=graphql_admin_middleware))
# RMV app.mount("/resource-models/", GraphQLApp(resource_model_schema, on_get=make_graphiql_handler(), middleware=graphql_admin_middleware))
Loading

0 comments on commit 0e0fdad

Please sign in to comment.