Skip to content

Commit

Permalink
Merge pull request #4040 from mathesar-foundation/add_combo_decorator
Browse files Browse the repository at this point in the history
Add `mathesar_rpc_method` decorator
  • Loading branch information
Anish9901 authored Nov 29, 2024
2 parents 02e3b52 + 822d0bc commit bd1fe16
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 323 deletions.
21 changes: 5 additions & 16 deletions mathesar/rpc/collaborators.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from typing import TypedDict

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

from mathesar.models.base import UserDatabaseRoleMap, Database, ConfiguredRole
from mathesar.models.users import User
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method


class CollaboratorInfo(TypedDict):
Expand Down Expand Up @@ -33,9 +30,7 @@ def from_model(cls, model):
)


@rpc_method(name="collaborators.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="collaborators.list", auth="login")
def list_(*, database_id: int = None, **kwargs) -> list[CollaboratorInfo]:
"""
List information about collaborators. Exposed as `list`.
Expand All @@ -56,9 +51,7 @@ def list_(*, database_id: int = None, **kwargs) -> list[CollaboratorInfo]:
return [CollaboratorInfo.from_model(db_model) for db_model in user_database_role_map_qs]


@rpc_method(name='collaborators.add')
@http_basic_auth_superuser_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="collaborators.add")
def add(
*,
database_id: int,
Expand Down Expand Up @@ -86,9 +79,7 @@ def add(
return CollaboratorInfo.from_model(collaborator)


@rpc_method(name='collaborators.delete')
@http_basic_auth_superuser_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="collaborators.delete")
def delete(*, collaborator_id: int, **kwargs):
"""
Delete a collaborator from a database.
Expand All @@ -100,9 +91,7 @@ def delete(*, collaborator_id: int, **kwargs):
collaborator.delete()


@rpc_method(name='collaborators.set_role')
@http_basic_auth_superuser_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="collaborators.set_role")
def set_role(
*,
collaborator_id: int,
Expand Down
25 changes: 7 additions & 18 deletions mathesar/rpc/columns/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"""
from typing import Literal, Optional, TypedDict

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

from db.columns import (
add_columns_to_table,
Expand All @@ -13,7 +12,7 @@
get_column_info_for_table,
)
from mathesar.rpc.columns.metadata import ColumnMetaDataBlob
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method
from mathesar.rpc.utils import connect
from mathesar.utils.columns import get_columns_meta_data

Expand Down Expand Up @@ -195,9 +194,7 @@ def from_dict(cls, col_info):
)


@rpc_method(name="columns.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.list", auth="login")
def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ColumnInfo]:
"""
List information about columns for a table. Exposed as `list`.
Expand All @@ -215,9 +212,7 @@ def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ColumnInfo]:
return [ColumnInfo.from_dict(col) for col in raw_column_info]


@rpc_method(name="columns.add")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.add", auth="login")
def add(
*,
column_data_list: list[CreatableColumnInfo],
Expand Down Expand Up @@ -245,9 +240,7 @@ def add(
return add_columns_to_table(table_oid, column_data_list, conn)


@rpc_method(name="columns.patch")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.patch", auth="login")
def patch(
*,
column_data_list: list[SettableColumnInfo],
Expand All @@ -273,9 +266,7 @@ def patch(
return alter_columns_in_table(table_oid, column_data_list, conn)


@rpc_method(name="columns.delete")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.delete", auth="login")
def delete(
*, column_attnums: list[int], table_oid: int, database_id: int, **kwargs
) -> int:
Expand All @@ -295,9 +286,7 @@ def delete(
return drop_columns_from_table(table_oid, column_attnums, conn)


@rpc_method(name="columns.list_with_metadata")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.list_with_metadata", auth="login")
def list_with_metadata(*, table_oid: int, database_id: int, **kwargs) -> list:
"""
List information about columns for a table, along with the metadata associated with each column.
Expand Down
13 changes: 3 additions & 10 deletions mathesar/rpc/columns/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"""
from typing import Literal, 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.rpc.decorators import mathesar_rpc_method
from mathesar.utils.columns import get_columns_meta_data, set_columns_meta_data


Expand Down Expand Up @@ -128,9 +125,7 @@ def from_model(cls, model):
)


@rpc_method(name="columns.metadata.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.metadata.list", auth="login")
def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ColumnMetaDataRecord]:
"""
List metadata associated with columns for a table. Exposed as `list`.
Expand All @@ -148,9 +143,7 @@ def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ColumnMetaDataR
]


@rpc_method(name="columns.metadata.set")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="columns.metadata.set", auth="login")
def set_(
*,
column_meta_data_list: list[ColumnMetaDataBlob],
Expand Down
17 changes: 5 additions & 12 deletions mathesar/rpc/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""
from typing import Optional, TypedDict, Union

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

from db.constraints import (
get_constraints_for_table,
create_constraint,
drop_constraint_via_oid,
)
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method
from mathesar.rpc.utils import connect


Expand Down Expand Up @@ -113,9 +112,7 @@ def from_dict(cls, con_info):
)


@rpc_method(name="constraints.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="constraints.list", auth="login")
def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ConstraintInfo]:
"""
List information about constraints in a table. Exposed as `list`.
Expand All @@ -133,9 +130,7 @@ def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ConstraintInfo]
return [ConstraintInfo.from_dict(con) for con in con_info]


@rpc_method(name="constraints.add")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="constraints.add", auth="login")
def add(
*,
table_oid: int,
Expand All @@ -158,9 +153,7 @@ def add(
return create_constraint(table_oid, constraint_def_list, conn)


@rpc_method(name="constraints.delete")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="constraints.delete", auth="login")
def delete(*, table_oid: int, constraint_oid: int, database_id: int, **kwargs) -> str:
"""
Delete a constraint from a table.
Expand Down
25 changes: 7 additions & 18 deletions mathesar/rpc/data_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
"""
from typing import TypedDict

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

from db import links, tables
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method
from mathesar.rpc.utils import connect


@rpc_method(name="data_modeling.add_foreign_key_column")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="data_modeling.add_foreign_key_column", auth="login")
def add_foreign_key_column(
*,
column_name: str,
Expand Down Expand Up @@ -52,9 +49,7 @@ class MappingColumn(TypedDict):
referent_table_oid: int


@rpc_method(name="data_modeling.add_mapping_table")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="data_modeling.add_mapping_table", auth="login")
def add_mapping_table(
*,
table_name: str,
Expand Down Expand Up @@ -82,9 +77,7 @@ def add_mapping_table(
)


@rpc_method(name="data_modeling.suggest_types")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="data_modeling.suggest_types", auth="login")
def suggest_types(*, table_oid: int, database_id: int, **kwargs) -> dict:
"""
Infer the best type for each column in the table.
Expand Down Expand Up @@ -118,9 +111,7 @@ class SplitTableInfo(TypedDict):
new_fkey_attnum: int


@rpc_method(name="data_modeling.split_table")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="data_modeling.split_table", auth="login")
def split_table(
*,
table_oid: int,
Expand Down Expand Up @@ -154,9 +145,7 @@ def split_table(
)


@rpc_method(name="data_modeling.move_columns")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="data_modeling.move_columns", auth="login")
def move_columns(
*,
source_table_oid: int,
Expand Down
22 changes: 6 additions & 16 deletions mathesar/rpc/databases/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from typing import Literal, TypedDict

from modernrpc.core import rpc_method, REQUEST_KEY
from modernrpc.auth.basic import (
http_basic_auth_login_required,
http_basic_auth_superuser_required,
)
from modernrpc.core import REQUEST_KEY

from db.databases import get_database, drop_database
from mathesar.models.base import Database
from mathesar.rpc.utils import connect
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method


class DatabaseInfo(TypedDict):
Expand All @@ -26,7 +22,7 @@ class DatabaseInfo(TypedDict):
oid: int
name: str
owner_oid: int
current_role_priv: list[Literal['CONNECT', 'CREATE', 'TEMPORARY']]
current_role_priv: list[Literal["CONNECT", "CREATE", "TEMPORARY"]]
current_role_owns: bool

@classmethod
Expand All @@ -40,9 +36,7 @@ def from_dict(cls, d):
)


@rpc_method(name="databases.get")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="databases.get", auth="login")
def get(*, database_id: int, **kwargs) -> DatabaseInfo:
"""
Get information about a database.
Expand All @@ -59,9 +53,7 @@ def get(*, database_id: int, **kwargs) -> DatabaseInfo:
return DatabaseInfo.from_dict(db_info)


@rpc_method(name="databases.delete")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="databases.delete", auth="login")
def delete(*, database_oid: int, database_id: int, **kwargs) -> None:
"""
Drop a database from the server.
Expand All @@ -75,9 +67,7 @@ def delete(*, database_oid: int, database_id: int, **kwargs) -> None:
drop_database(database_oid, conn)


@rpc_method(name="databases.upgrade_sql")
@http_basic_auth_superuser_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="databases.upgrade_sql")
def upgrade_sql(
*, database_id: int, username: str = None, password: str = None
) -> None:
Expand Down
13 changes: 4 additions & 9 deletions mathesar/rpc/databases/configured.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from typing import TypedDict

from modernrpc.core import rpc_method, REQUEST_KEY
from modernrpc.auth.basic import http_basic_auth_login_required, http_basic_auth_superuser_required
from modernrpc.core import REQUEST_KEY

from mathesar.models.base import Database
from mathesar.rpc.exceptions.handlers import handle_rpc_exceptions
from mathesar.rpc.decorators import mathesar_rpc_method


class ConfiguredDatabaseInfo(TypedDict):
Expand Down Expand Up @@ -37,9 +36,7 @@ def from_model(cls, model):
)


@rpc_method(name="databases.configured.list")
@http_basic_auth_login_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="databases.configured.list", auth='login')
def list_(*, server_id: int = None, **kwargs) -> list[ConfiguredDatabaseInfo]:
"""
List information about databases for a server. Exposed as `list`.
Expand Down Expand Up @@ -68,9 +65,7 @@ def list_(*, server_id: int = None, **kwargs) -> list[ConfiguredDatabaseInfo]:
return [ConfiguredDatabaseInfo.from_model(db_model) for db_model in database_qs]


@rpc_method(name="databases.configured.disconnect")
@http_basic_auth_superuser_required
@handle_rpc_exceptions
@mathesar_rpc_method(name="databases.configured.disconnect")
def disconnect(*, database_id: int, **kwargs) -> None:
"""
Disconnect a configured database.
Expand Down
Loading

0 comments on commit bd1fe16

Please sign in to comment.