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

Fix date issues when user inputs: date out of bound or date_to earlier than date_from #33

Merged
merged 15 commits into from
Feb 9, 2024
4 changes: 2 additions & 2 deletions src/senaite/databox/browser/templates/databox_controls.pt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<input type="date"
class="form-control"
tal:define="date_from here/date_from"
tal:attributes="value python:date_from and date_from.strftime('%Y-%m-%d')"
tal:attributes="value python:view.dtime.date_to_string(date_from) if date_from else ''"
name="senaite.databox.date_from">
</div>
</div>
Expand All @@ -161,7 +161,7 @@
<input type="date"
class="form-control"
tal:define="date_to here/date_to"
tal:attributes="value python:date_to and date_to.strftime('%Y-%m-%d')"
tal:attributes="value python:view.dtime.date_to_string(date_to) if date_to else ''"
name="senaite.databox.date_to">
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/senaite/databox/browser/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
class DataBoxView(ListingView):
"""The default DataBox view
"""
from senaite.core.api import dtime
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it have a reason why you added the import at class level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I recall the reason was that dtime was unreachable from a template expression statement, if import statement declared for module level..

Would you think its better to return import statement to the module level and provide sort of wrapper from DataBoxView class to that template expression? Or even somehow import dtime right in template (is that possible?)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, that's strange... Maybe this is because of restricted Python, but not sure.
Anyhow, I think that accessing the module directly is bad style and should be avoided.
Better create two more property methods in the class, that return the right value for you, e.g.:

@property
def date_from(self):
    if not self.context.date_from:
        return ""
    return dtime.date_to_string(self.context.date_from)

And use then this in your template:

<input type="date"
       class="form-control"
       tal:attributes="value python:view.date_from"
       name="senaite.databox.date_from"/>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I've added properties.

Also, I've added date_to > date_from comparison, which should make report behaviour more intuitive. In case user puts date_to earlier than date_from - we use [date_from, date_from] range for date query.

template = ViewPageTemplateFile("templates/databox_view.pt")

def __init__(self, context, request):
Expand Down