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

Out of range date in strftime() causes report failure #32

Open
toropok opened this issue Dec 8, 2023 · 8 comments
Open

Out of range date in strftime() causes report failure #32

toropok opened this issue Dec 8, 2023 · 8 comments

Comments

@toropok
Copy link
Contributor

toropok commented Dec 8, 2023

If user puts date with year value earlier than 1900 year in date_from and date_to field:

Screenshot 2023-12-08 at 17 21 27

that causes exception in databox_controls.pt:

...
Module Products.PageTemplates.engine, line 378, in __call__
  Module z3c.pt.pagetemplate, line 176, in render
  Module chameleon.zpt.template, line 302, in render
  Module chameleon.template, line 215, in render
  Module chameleon.template, line 192, in render
  Module 2cfdf7d550b9a6fb1f96f131bf2f6928, line 993, in render
  Module zope.tales.pythonexpr, line 73, in __call__
   - __traceback_info__: (date_to and date_to.strftime('%Y-%m-%d'))
  Module <string>, line 1, in <module>
ValueError: year=23 is before 1900; the datetime strftime() methods require year >= 1900

 - Expression: "python:date_to and date_to.strftime('%Y-%m-%d')"
 - Filename:   ... rc/senaite/databox/browser/templates/databox_controls.pt
 - Location:   (line 164: col 43)
 - Source:     ... es="value python:date_to and date_to.strftime('%Y-%m-%d')"
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 - Expression: "provider:plone.abovecontentbody"
 - Filename:   ... te/core/browser/main_template/templates/main_template.pt
 - Location:   (line 132: col 84)
 - Source:     ... 
...

would you mind if we add year check in the setter method, like this:

...
from senaite.core.api.dtime import to_DT
...
    # DATE TO

    def _set_date_to(self, value):
        self.context.date_to = to_DT(value) if to_DT(value).year() >= 1900 else None

Or there any better options?

@toropok
Copy link
Contributor Author

toropok commented Dec 8, 2023

If there any way to prevent user from the wrong input - thats great

@ramonski
Copy link
Contributor

ramonski commented Dec 8, 2023

Hi @toropok,
we handle this error already here:
https://github.com/senaite/senaite.core/blob/2.x/src/senaite/core/api/dtime.py#L376
However, I don't know how the catalog search will be affected by dates before 1900

@toropok
Copy link
Contributor Author

toropok commented Dec 8, 2023

Hi @toropok, we handle this error already here: https://github.com/senaite/senaite.core/blob/2.x/src/senaite/core/api/dtime.py#L376 However, I don't know how the catalog search will be affected by dates before 1900

@ramonski aha, cool. Does it make sense to remove strftime conversion from the 149 and 164 lines at databox_controls.pt ????

@ramonski
Copy link
Contributor

ramonski commented Dec 8, 2023

The value is needed for this input field to work properly.
But you could try to do the conversion in the view and use the dtime.date_to_string function there or directly in the template, e.g. something like this might work:

<input type="date"
       class="form-control"
       tal:define="date_from here/date_from"
       tal:attributes="value python:view.dtime.date_to_string(date_from) if date_from else ''"
       name="senaite.databox.date_from">

@toropok
Copy link
Contributor Author

toropok commented Dec 11, 2023

date_to_string(

yeah, that works (if import dtime directly in DataBoxView class, otherwise it is invisible in databox_controls.pt):

class DataBoxView(ListingView):
    """The default DataBox view
    """
    from senaite.core.api import dtime
    template = ViewPageTemplateFile("templates/databox_view.pt")

should we add it as PR?

@ramonski
Copy link
Contributor

should we add it as PR?

Yes, that would be great! Then it will get part of the upcoming 2.5 release.
Thanks!

@toropok
Copy link
Contributor Author

toropok commented Jan 4, 2024

should we add it as PR?

Yes, that would be great! Then it will get part of the upcoming 2.5 release. Thanks!

im very sorry I missed somehow your message :( my bad

created that micro PR: #33

@ramonski
Copy link
Contributor

ramonski commented Jan 4, 2024

im very sorry I missed somehow your message :( my bad

No worries, we can make another dot release if more comes

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

No branches or pull requests

2 participants