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

Format of Time with SQLAlchemy (and daterangepicker) #1210

Closed
nypgand1 opened this issue Feb 19, 2016 · 1 comment
Closed

Format of Time with SQLAlchemy (and daterangepicker) #1210

nypgand1 opened this issue Feb 19, 2016 · 1 comment

Comments

@nypgand1
Copy link

Briefly speaking, I 'm try to use the '%M:%S' format of Time like 23:30.

What I have done:

  1. Set the form_args and form_widget_args variable of ModelView like example here https://github.com/flask-admin/flask-admin/blob/master/flask_admin/model/base.py#L572
  2. Make a new AdminModelConverter which override the method convert_time to return TimeField object with formats=('%H:%M:%S', '%M:%S') and default_formatt='%M:%S'.

What I got and need help with:

  1. A edit page with '%M:%S' format, but a list page with '%H:%M:%S' format.
  2. A pop up daterangepicker with hour selector & minute selector instead of minute selector & second selector in the edit page which I want.

If TimeField's formats=(''%M:%S') only, it says 'Invalid time format' all the time when I press save in edit page.

And I found that the widget_format parameter in constructor of TimeField may not be used anymore.
https://github.com/flask-admin/flask-admin/blob/master/flask_admin/form/fields.py#L48

@nypgand1
Copy link
Author

Alright, I figure it out.
Leave my comment here for others who needs it.

I got my CustomModelView like the following:

class CustomModelView(ModelView):
    # changes how the input is parsed by strptime (12 hour time)
    form_args = dict(
        clock_start=dict(default_format='%M:%S', formats=('%H:%M:%S', '%M:%S')),
        clock_end=dict(default_format='%M:%S', formats=('%H:%M:%S', '%M:%S'))
    )
    # changes how the TimeField displays the time
    form_widget_args = dict(
        clock_start={'data-role': u'timepickersecs', 'data-date-format': u'mm:ss'},
        clock_end={'data-role': u'timepickersecs', 'data-date-format': u'mm:ss'}
    )
    column_formatters = dict(
        clock_start=lambda v, c, m, p:m.clock_start.strftime('%M:%S'),
        clock_end=lambda v, c, m, p:m.clock_end.strftime('%M:%S')
    )

And it shows right in both list and edit page.
(The 'data-role' part will be explained next)

I found a timePickerSeconds option here:
https://github.com/flask-admin/flask-admin/blob/master/flask_admin/static/vendor/bootstrap-daterangepicker/daterangepicker.js#L1281
So I add a new data-role 'timepickersecs' copy from here:
https://github.com/flask-admin/flask-admin/blob/master/flask_admin/static/admin/js/form.js#L398
with timePickerSeconds: true, and it's all good

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

No branches or pull requests

1 participant