-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3641 from mathesar-foundation/column_metadata_rpc
Add `columns.metadata.list` rpc function
- Loading branch information
Showing
12 changed files
with
318 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by Django 4.2.11 on 2024-06-20 08:02 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('mathesar', '0007_users_permissions_remodel'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ColumnMetaData', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('table_oid', models.PositiveIntegerField()), | ||
('attnum', models.PositiveIntegerField()), | ||
('bool_input', models.CharField(blank=True, choices=[('dropdown', 'dropdown'), ('checkbox', 'checkbox')])), | ||
('bool_true', models.CharField(default='True')), | ||
('bool_false', models.CharField(default='False')), | ||
('num_min_frac_digits', models.PositiveIntegerField(blank=True)), | ||
('num_max_frac_digits', models.PositiveIntegerField(blank=True)), | ||
('num_show_as_perc', models.BooleanField(default=False)), | ||
('mon_currency_symbol', models.CharField(default='$')), | ||
('mon_currency_location', models.CharField(choices=[('after-minus', 'after-minus'), ('end-with-space', 'end-with-space')], default='after-minus')), | ||
('time_format', models.CharField(blank=True)), | ||
('date_format', models.CharField(blank=True)), | ||
('duration_min', models.CharField(blank=True, max_length=255)), | ||
('duration_max', models.CharField(blank=True, max_length=255)), | ||
('duration_show_units', models.BooleanField(default=True)), | ||
('database', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mathesar.database')), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name='columnmetadata', | ||
constraint=models.UniqueConstraint(fields=('database', 'table_oid', 'attnum'), name='unique_column_metadata'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='columnmetadata', | ||
constraint=models.CheckConstraint(check=models.Q(('num_max_frac_digits__lte', 20), ('num_min_frac_digits__lte', 20), ('num_min_frac_digits__lte', models.F('num_max_frac_digits'))), name='frac_digits_integrity'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .base import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Classes and functions exposed to the RPC endpoint for managing column metadata. | ||
""" | ||
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.utils.columns import get_columns_meta_data | ||
|
||
|
||
class ColumnMetaData(TypedDict): | ||
""" | ||
Metadata for a column in a table. | ||
Only the `database`, `table_oid`, and `attnum` keys are required. | ||
Attributes: | ||
database_id: The Django id of the database containing the table. | ||
table_oid: The OID of the table containing the column | ||
attnum: The attnum of the column in the table. | ||
bool_input: How the input for a boolean column should be shown. | ||
bool_true: A string to display for `true` values. | ||
bool_false: A string to display for `false` values. | ||
num_min_frac_digits: Minimum digits shown after the decimal point. | ||
num_max_frac_digits: Maximum digits shown after the decimal point. | ||
num_show_as_perc: Whether to show a numeric value as a percentage. | ||
mon_currency_symbol: The currency symbol shown for money value. | ||
mon_currency_location: Where the currency symbol should be shown. | ||
time_format: A string representing the format of time values. | ||
date_format: A string representing the format of date values. | ||
duration_min: The smallest unit for displaying durations. | ||
duration_max: The largest unit for displaying durations. | ||
duration_show_units: Whether to show the units for durations. | ||
""" | ||
database_id: int | ||
table_oid: int | ||
attnum: int | ||
bool_input: Optional[Literal["dropdown", "checkbox"]] | ||
bool_true: Optional[str] | ||
bool_false: Optional[str] | ||
num_min_frac_digits: Optional[int] | ||
num_max_frac_digits: Optional[int] | ||
num_show_as_perc: Optional[bool] | ||
mon_currency_symbol: Optional[str] | ||
mon_currency_location: Optional[Literal["after-minus", "end-with-space"]] | ||
time_format: Optional[str] | ||
date_format: Optional[str] | ||
duration_min: Optional[str] | ||
duration_max: Optional[str] | ||
duration_show_units: Optional[bool] | ||
|
||
@classmethod | ||
def from_model(cls, model): | ||
return cls( | ||
database_id=model.database.id, | ||
table_oid=model.table_oid, | ||
attnum=model.attnum, | ||
bool_input=model.bool_input, | ||
bool_true=model.bool_true, | ||
bool_false=model.bool_false, | ||
num_min_frac_digits=model.num_min_frac_digits, | ||
num_max_frac_digits=model.num_max_frac_digits, | ||
num_show_as_perc=model.num_show_as_perc, | ||
mon_currency_symbol=model.mon_currency_symbol, | ||
mon_currency_location=model.mon_currency_location, | ||
time_format=model.time_format, | ||
date_format=model.date_format, | ||
duration_min=model.duration_min, | ||
duration_max=model.duration_max, | ||
duration_show_units=model.duration_show_units, | ||
) | ||
|
||
|
||
@rpc_method(name="columns.metadata.list") | ||
@http_basic_auth_login_required | ||
@handle_rpc_exceptions | ||
def list_(*, table_oid: int, database_id: int, **kwargs) -> list[ColumnMetaData]: | ||
""" | ||
List metadata associated with columns for a table. Exposed as `list`. | ||
Args: | ||
table_oid: Identity of the table in the user's database. | ||
database_id: The Django id of the database containing the table. | ||
Returns: | ||
A list of column meta data objects. | ||
""" | ||
columns_meta_data = get_columns_meta_data(table_oid, database_id) | ||
return [ | ||
ColumnMetaData.from_model(model) for model in columns_meta_data | ||
] |
Oops, something went wrong.