Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interval field #573

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Interval field #573

wants to merge 12 commits into from

Conversation

regisin
Copy link

@regisin regisin commented Aug 28, 2024

Handles #572

starlette_admin/fields.py Outdated Show resolved Hide resolved
starlette_admin/helpers.py Outdated Show resolved Hide resolved
Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi thank you for the PR, l left some few comments.

Also can we add a converter for Interval in sqlalchemy and timedelta for Odmantic

@@ -0,0 +1,33 @@
<div class="{% if error %}{{ field.error_class }}{% endif %}">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I'm not sure what to do with these converters..what do they do? I just mimic'd the "Time" one:

    @converts("Interval")
    def conv_interval(self, *args: Any, **kwargs: Any) -> BaseField:
        return IntervalField(
            **self._field_common(*args, **kwargs),
        )

Is that it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's it. The converter's role is to automatically find the appropriate field for each attribute. For SQLAlchemy, it uses the attribute type. Think of it as a map between the attribute type and the corresponding field.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the pr.

Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks good just some minor comments

@@ -18,10 +18,16 @@
Union,
)

from babel.dates import format_timedelta
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to make this optional and require it only when using the Interval field. here is an example:

if not arrow: # pragma: no cover

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example is not clear to me. Is the arrow package only imported when using the ArrowField? Looks more like the user cannot use the arrow field if the import fails.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the user is not able to use it if the import fails, but they are required to install it before using the field. The idea is to make the arrow package optional, so users who don't want to use ArrowField are not forced to install it.

),
granularity="second",
threshold=1,
locale="en",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function returns the current user locale

Suggested change
locale="en",
locale=get_locale(),

def conv_bson_datetime(self, *args: Any, **kwargs: Any) -> BaseField:
return IntervalField(**self._standard_type_common(**kwargs))

@converts(odmantic.bson)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@converts(odmantic.bson)
@converts(odmantic.bson._datetime)

Comment on lines +113 to +116
@converts("timedelta")
def conv_bson_timedelta(self, *args: Any, **kwargs: Any) -> BaseField:
return IntervalField(**self._standard_type_common(**kwargs))

Copy link
Owner

@jowilf jowilf Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems odmantic does not support timedelta art049/odmantic#120, we can remove this and just keep for sqlalchemy

@regisin
Copy link
Author

regisin commented Sep 12, 2024

@jowilf looks like the version of mkdocstring needs attention. I did some digging and found this mkdocstrings/autorefs#57

Either adding this to the mkdoc.yaml:

  - autorefs:
      resolve_closest: false`

or bumping the mkdocstring version: "mkdocstrings[python] >=0.26.1, <0.27.0",

Lets build the docs but gives a bunch of warnings.

Copy link
Owner

@jowilf jowilf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR, @regisin. I'll look into the mkdocs issue later, as it’s not related to the changes you've made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants