From 1fbe7ea049c44ded2df2337269841a6a756ff0bb Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Mon, 3 Feb 2014 22:15:30 +0000 Subject: [PATCH] Add HasDayOfWeek rule --- gramps/gen/filters/rules/event/__init__.py | 4 +- .../gen/filters/rules/event/_hasdayofweek.py | 54 +++++++++++++++++++ gramps/gen/lib/date.py | 10 ++++ gramps/gui/editors/filtereditor.py | 4 ++ po/POTFILES.in | 1 + 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 gramps/gen/filters/rules/event/_hasdayofweek.py diff --git a/gramps/gen/filters/rules/event/__init__.py b/gramps/gen/filters/rules/event/__init__.py index ea379e162..5238e8aeb 100644 --- a/gramps/gen/filters/rules/event/__init__.py +++ b/gramps/gen/filters/rules/event/__init__.py @@ -48,6 +48,7 @@ from ._hasattribute import HasAttribute from ._hasdata import HasData from ._changedsince import ChangedSince from ._matchesplacefilter import MatchesPlaceFilter +from ._hasdayofweek import HasDayOfWeek editor_rule_list = [ AllEvents, @@ -68,5 +69,6 @@ editor_rule_list = [ HasAttribute, HasData, ChangedSince, - MatchesPlaceFilter + MatchesPlaceFilter, + HasDayOfWeek ] diff --git a/gramps/gen/filters/rules/event/_hasdayofweek.py b/gramps/gen/filters/rules/event/_hasdayofweek.py new file mode 100644 index 000000000..b12035af1 --- /dev/null +++ b/gramps/gen/filters/rules/event/_hasdayofweek.py @@ -0,0 +1,54 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2013 Nick Hall +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +#------------------------------------------------------------------------- +# +# Standard Python modules +# +#------------------------------------------------------------------------- +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.sgettext + +#------------------------------------------------------------------------- +# +# Gramps modules +# +#------------------------------------------------------------------------- +from .. import Rule + +#------------------------------------------------------------------------- +# +# HasDayOfWeek +# +#------------------------------------------------------------------------- +class HasDayOfWeek(Rule): + """Rule that matches an event occurring on a particular day of the week.""" + + labels = [ _('Day of Week:') ] + name = _('Events occurring on a particular day of the week') + description = _('Matches events occurring on a particular day of the week') + category = _('General filters') + + def apply(self, db, event): + if not self.list[0]: + return False + else: + dow = event.get_date_object().get_dow() + return dow == int(self.list[0]) diff --git a/gramps/gen/lib/date.py b/gramps/gen/lib/date.py index af7cea24a..cbf2810c5 100644 --- a/gramps/gen/lib/date.py +++ b/gramps/gen/lib/date.py @@ -1488,6 +1488,16 @@ class Date(object): """ return self.text + def get_dow(self): + """ + Return an integer representing the day of the week associated with the + date (Monday=0). + + If the day is not defined, a None is returned. If the date is a + compound date, the lower date day is returned. + """ + return self.sortval % 7 if self.is_regular() else None + def _zero_adjust_ymd(self, y, m, d): year = y if y != 0 else 1 month = max(m, 1) diff --git a/gramps/gui/editors/filtereditor.py b/gramps/gui/editors/filtereditor.py index c5c6c11a2..f6d083375 100644 --- a/gramps/gui/editors/filtereditor.py +++ b/gramps/gui/editors/filtereditor.py @@ -74,6 +74,7 @@ from gramps.gen.utils.db import family_name from gramps.gen.utils.string import conf_strings from gramps.gen.constfunc import cuni from ..widgets import DateEntry +from gramps.gen.datehandler import long_days #------------------------------------------------------------------------- # @@ -583,6 +584,9 @@ class EditRule(ManagedWindow): [_(conf_strings[i]) for i in range(5)]) elif v == _('Date:'): t = DateEntry(self.uistate, self.track) + elif v == _('Day of Week:'): + days_of_week = long_days[2:] + long_days[1:2] + t = MyList(map(str, range(7)), days_of_week) else: t = MyEntry() tlist.append(t) diff --git a/po/POTFILES.in b/po/POTFILES.in index d07ab236f..295984d64 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -66,6 +66,7 @@ gramps/gen/filters/rules/event/_eventprivate.py gramps/gen/filters/rules/event/_hasattribute.py gramps/gen/filters/rules/event/_hascitation.py gramps/gen/filters/rules/event/_hasdata.py +gramps/gen/filters/rules/event/_hasdayofweek.py gramps/gen/filters/rules/event/_hasgallery.py gramps/gen/filters/rules/event/_hasidof.py gramps/gen/filters/rules/event/_hasnote.py