Coloring/icon rules: Allow creating a rule for date columns that matches *today*

Fixes #1956006 [[Enhancement] Harmonise Date Conditions](https://bugs.launchpad.net/calibre/+bug/1956006)
This commit is contained in:
Kovid Goyal 2022-01-17 11:58:19 +05:30
parent 2b41671370
commit 3488e9107e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 4 deletions

View File

@ -75,8 +75,9 @@ class ConditionEditor(QWidget): # {{{
),
'datetime' : (
(_('is equal to'), 'eq'),
(_('is less than'), 'lt'),
(_('is greater than'), 'gt'),
(_('is earlier than'), 'lt'),
(_('is later than'), 'gt'),
(_('is today'), 'is today'),
(_('is set'), 'is set'),
(_('is not set'), 'is not set'),
(_('is more days ago than'), 'older count days'),
@ -199,6 +200,8 @@ class ConditionEditor(QWidget): # {{{
@property
def current_val(self):
ans = str(self.value_box.text()).strip()
if not self.value_box.isEnabled():
ans = ''
if self.current_col == 'languages':
rmap = {lower(v):k for k, v in iteritems(lang_map())}
ans = rmap.get(lower(ans), ans)
@ -310,8 +313,7 @@ class ConditionEditor(QWidget): # {{{
tt += '\n' + _('You can match multiple values by separating'
' them with %s')%m['is_multiple']['ui_to_list']
self.value_box.setToolTip(tt)
if action in ('is set', 'is not set', 'is true', 'is false',
'is undefined'):
if action in ('is set', 'is not set', 'is true', 'is false', 'is undefined', 'is today'):
self.value_box.setEnabled(False)
# }}}

View File

@ -6,6 +6,7 @@ import json
import re
from textwrap import dedent
from calibre.utils.date import format_date, now
from polyglot.binary import as_hex_unicode, from_hex_bytes
color_row_key = '*row'
@ -178,6 +179,9 @@ class Rule: # {{{
return ("test(field('%s'), '1', '')"%(col))
if action == 'is not set':
return ("test(field('%s'), '', '1')"%(col))
if action == 'is today':
today = format_date(now(), 'yyyy-MM-dd')
return f"strcmp(format_date(raw_field('{col}'), 'yyyy-MM-dd'), '{today}', '', '1', '')"
lt, eq, gt = {
'eq': ('', '1', ''),
'lt': ('1', '', ''),