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
1 change: 1 addition & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.5.0 (unreleased)
------------------

- #33 Fix date issues when user inputs: date out of bound or date_to earlier than date_from
- #35 Allow to query Auto Import Log type


Expand Down
6 changes: 3 additions & 3 deletions src/senaite/databox/behaviors/databox.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ def query(self):
query["sort_order"] = self.sort_order

if self.date_index:
date_from = self.date_from or DateTime("2000-01-01")
date_to = self.date_to or DateTime()
date_from = DateTime(self.date_from or "2000-01-01")
date_to = DateTime(self.date_to) if self.date_to else DateTime()
# always make the to_date inclusive
query[self.date_index] = {
"query": (DateTime(date_from), DateTime(date_to) + 1),
"query": (date_from, (date_to if date_from <= date_to else date_from) + 1),
"range": "minmax"
}

Expand Down
5 changes: 2 additions & 3 deletions src/senaite/databox/browser/templates/databox_controls.pt
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@
</div>
<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.date_from"
name="senaite.databox.date_from">
</div>
</div>
Expand All @@ -161,7 +160,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.date_to"
name="senaite.databox.date_to">
</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions src/senaite/databox/browser/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from senaite.app.listing.view import ListingView
from senaite.app.supermodel.model import SuperModel
from senaite.core.api import dtime
from senaite.databox import logger
from senaite.databox.behaviors.databox import IDataBoxBehavior
from senaite.databox.interfaces import IFieldConverter
Expand Down Expand Up @@ -198,6 +199,20 @@ def databox(self):
def catalog(self):
return self.databox.get_query_catalog()

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

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

@view.memoize
def get_query_types(self):
"""Returns the `query_types` list of the context as JSON
Expand Down
Loading