Skip to content

Commit

Permalink
Add support to reverse the search_terms to GetFacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hayes committed Jan 17, 2013
1 parent fa2d44a commit f068407
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/hamster-service
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ class Storage(db.Storage, dbus.service.Object):
return self.remove_fact(fact_id)


@dbus.service.method("org.gnome.Hamster", in_signature='uus', out_signature='a(iiissisasii)')
def GetFacts(self, start_date, end_date, search_terms):
@dbus.service.method("org.gnome.Hamster", in_signature='uusb', out_signature='a(iiissisasii)')
def GetFacts(self, start_date, end_date, search_terms, reverse_search_terms=False):
"""Gets facts between the day of start_date and the day of end_date.
Parameters:
i start_date: Seconds since epoch (timestamp). Use 0 for today
i end_date: Seconds since epoch (timestamp). Use 0 for today
s search_terms: Bleh
b reverse_search_terms: Reverse the behaviour of the search terms (ie.. NOT search_terms)
Returns Array of fact where fact is struct of:
i id
i start_time
Expand All @@ -206,7 +207,7 @@ class Storage(db.Storage, dbus.service.Object):
if end_date:
end = dt.datetime.utcfromtimestamp(end_date).date()

return [to_dbus_fact(fact) for fact in self.get_facts(start, end, search_terms)]
return [to_dbus_fact(fact) for fact in self.get_facts(start, end, search_terms, reverse_search_terms)]


@dbus.service.method("org.gnome.Hamster", out_signature='a(iiissisasii)')
Expand Down
5 changes: 3 additions & 2 deletions src/hamster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def get_todays_facts(self):
"""
return [from_dbus_fact(fact) for fact in self.conn.GetTodaysFacts()]

def get_facts(self, date, end_date = None, search_terms = ""):
def get_facts(self, date, end_date = None, search_terms = "", reverse_search_terms=False):
"""Returns facts for the time span matching the optional filter criteria.
In search terms comma (",") translates to boolean OR and space (" ")
to boolean AND.
Expand All @@ -127,7 +127,8 @@ def get_facts(self, date, end_date = None, search_terms = ""):

return [from_dbus_fact(fact) for fact in self.conn.GetFacts(date,
end_date,
search_terms)]
search_terms,
reverse_search_terms)]

def get_activities(self, search = ""):
"""returns list of activities name matching search criteria.
Expand Down
11 changes: 6 additions & 5 deletions src/hamster/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ def __get_todays_facts(self):
return self.__get_facts(today)


def __get_facts(self, date, end_date = None, search_terms = ""):
def __get_facts(self, date, end_date = None, search_terms = "", reverse_search_terms=False):
try:
from configuration import conf
day_start = conf.get("day_start_minutes")
Expand Down Expand Up @@ -674,14 +674,15 @@ def __get_facts(self, date, end_date = None, search_terms = ""):
self.__check_index(datetime_from, datetime_to)

search_terms = search_terms.replace('\\', '\\\\').replace('%', '\\%').replace('_', '\\_').replace("'", "''")
query += """ AND a.id in (SELECT id
query += """ AND a.id %sIN (SELECT id
FROM fact_index
WHERE fact_index MATCH '%s')""" % search_terms


WHERE fact_index MATCH '%s')""" % (' NOT ' if reverse_search_terms else '', search_terms)

query += " ORDER BY a.start_time, e.name"

f = open('/tmp/hamster.log', 'wa')
f.write(query)

facts = self.fetchall(query, (self._unsorted_localized,
datetime_from,
datetime_to))
Expand Down
4 changes: 2 additions & 2 deletions src/hamster/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def remove_fact(self, fact_id):
self.end_transaction()


def get_facts(self, start_date, end_date, search_terms):
return self.__get_facts(start_date, end_date, search_terms)
def get_facts(self, start_date, end_date, search_terms, reverse_search_terms=False):
return self.__get_facts(start_date, end_date, search_terms, reverse_search_terms)


def get_todays_facts(self):
Expand Down

0 comments on commit f068407

Please sign in to comment.