Skip to content

Commit

Permalink
Merge pull request #3676 from mathesar-foundation/types_rpc
Browse files Browse the repository at this point in the history
RPC implementation for `types.list`
  • Loading branch information
mathemancer authored Jul 12, 2024
2 parents 024c1b5 + fb5b487 commit fee3a80
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/settings/common_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def pipe_delim(pipe_string):
'mathesar.rpc.roles',
'mathesar.rpc.schemas',
'mathesar.rpc.tables',
'mathesar.rpc.tables.metadata'
'mathesar.rpc.tables.metadata',
'mathesar.rpc.types'
]

TEMPLATES = [
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/api/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ To use an RPC function:
- ColumnMetaData
- SettableColumnMetaData

## Types

::: types
options:
members:
- list_
- TypeInfo

## Constraints

::: constraints
Expand Down
47 changes: 47 additions & 0 deletions mathesar/rpc/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Classes and functions exposed to the RPC endpoint for listing types in a database.
"""

from typing import Optional, TypedDict

from modernrpc.core import rpc_method
from modernrpc.auth.basic import http_basic_auth_login_required

from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.database.types import UIType
from mathesar.api.display_options import DISPLAY_OPTIONS_BY_UI_TYPE


class TypeInfo(TypedDict):
"""
Information about a type.
Attributes:
identifier: Specifies the type class that db_type(s) belongs to.
name: Specifies the UI name for a type class.
db_types: Specifies the name(s) of types present on the database.
display_options: Specifies metadata related to a type class.
"""
identifier: str
name: str
db_types: list
display_options: Optional[dict]

@classmethod
def from_dict(cls, type):
return cls(
identifier=type.id,
name=type.display_name,
db_types=[db_type.id for db_type in type.db_types],
display_options=DISPLAY_OPTIONS_BY_UI_TYPE.get(type, None)
)


@rpc_method(name="types.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
def list_() -> list[TypeInfo]:
"""
List information about types available on the database. Exposed as `list`.
"""
return [TypeInfo.from_dict(type) for type in UIType]
6 changes: 6 additions & 0 deletions mathesar/tests/rpc/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from mathesar.rpc import roles
from mathesar.rpc import schemas
from mathesar.rpc import tables
from mathesar.rpc import types

METHODS = [
(
Expand Down Expand Up @@ -101,6 +102,11 @@
"schemas.patch",
[user_is_authenticated]
),
(
types.list_,
"types.list",
[user_is_authenticated]
),
(
tables.list_,
"tables.list",
Expand Down

0 comments on commit fee3a80

Please sign in to comment.