[hamster-applet] get_activity_by name now pulls also category to avoid mismatch between user input and database resul



commit c8d8f6569c83a04c2e5a21f5a532d348120b737b
Author: Toms Bauģis <toms baugis gmail com>
Date:   Tue Jan 19 13:51:03 2010 +0000

    get_activity_by name now pulls also category to avoid mismatch between user input and database results

 hamster/applet.py      |   12 ++++++------
 hamster/db.py          |   39 ++++++++++++++++++++++-----------------
 hamster/hamsterdbus.py |    8 ++++----
 3 files changed, 32 insertions(+), 27 deletions(-)
---
diff --git a/hamster/applet.py b/hamster/applet.py
index 2caebba..8184e53 100755
--- a/hamster/applet.py
+++ b/hamster/applet.py
@@ -618,13 +618,13 @@ class HamsterApplet(object):
                 if parsed_activity.category_name:
                     category_id = runtime.storage.get_category_by_name(parsed_activity.category_name)
 
-                activity_id = runtime.storage.get_activity_by_name(parsed_activity.activity_name,
-                                                                   category_id,
-                                                                   ressurect = False)
-                if activity_id:
+                activity = runtime.storage.get_activity_by_name(parsed_activity.activity_name,
+                                                                category_id,
+                                                                ressurect = False)
+                if activity:
                     # we need dict below
-                    activity = dict(name = parsed_activity.activity_name,
-                                    category = parsed_activity.category_name,
+                    activity = dict(name = activity['name'],
+                                    category = activity['category'],
                                     description = parsed_activity.description,
                                     tags = parsed_activity.tags)
 
diff --git a/hamster/db.py b/hamster/db.py
index 09c0000..b856985 100644
--- a/hamster/db.py
+++ b/hamster/db.py
@@ -157,12 +157,12 @@ class Storage(storage.Storage):
     def __change_category(self, id, category_id):
         # first check if we don't have an activity with same name before us
         activity = self.fetchone("select name from activities where id = ?", (id, ))
-        existing_id = self.__get_activity_by_name(activity['name'], category_id)
+        existing_activity = self.__get_activity_by_name(activity['name'], category_id)
 
-        if id == existing_id: # we are already there, go home
+        if id == existing_activity['id']: # we are already there, go home
             return False
 
-        if existing_id: #ooh, we have something here!
+        if existing_activity: #ooh, we have something here!
             # first move all facts that belong to movable activity to the new one
             update = """
                        UPDATE facts
@@ -170,7 +170,7 @@ class Storage(storage.Storage):
                         WHERE activity_id = ?
             """
 
-            self.execute(update, (existing_id, id))
+            self.execute(update, (existing_activity['id'], id))
 
             # and now get rid of our friend
             self.__remove_activity(id)
@@ -234,23 +234,26 @@ class Storage(storage.Storage):
 
         if category_id:
             query = """
-                       SELECT id, deleted from activities
-                        WHERE lower(name) = lower(?)
+                       SELECT a.id, a.name, a.deleted, coalesce(b.name, ?) as category
+                         FROM activities a
+                    LEFT JOIN categories b ON category_id = b.id
+                        WHERE lower(a.name) = lower(?)
                           AND category_id = ?
-                     ORDER BY deleted, id desc
+                     ORDER BY a.deleted, a.id desc
                         LIMIT 1
             """
 
-            res = self.fetchone(query, (name, category_id))
+            res = self.fetchone(query, (_("Unsorted"), name, category_id))
         else:
             query = """
-                       SELECT id, deleted from activities
-                        WHERE lower(name) = lower(?)
-                     ORDER BY deleted, id desc
+                       SELECT a.id, a.name, a.deleted, coalesce(b.name, ?) as category
+                         FROM activities a
+                    LEFT JOIN categories b ON category_id = b.id
+                        WHERE lower(a.name) = lower(?)
+                     ORDER BY a.deleted, a.id desc
                         LIMIT 1
             """
-
-            res = self.fetchone(query, (name, ))
+            res = self.fetchone(query, (_("Unsorted"), name, ))
 
         if res:
             # if the activity was marked as deleted, ressurect on first call
@@ -265,7 +268,7 @@ class Storage(storage.Storage):
                         """
                 self.execute(update, (res['id'], ))
 
-            return res['id']
+            return res
 
         return None
 
@@ -505,6 +508,8 @@ class Storage(storage.Storage):
         if not activity_id:
             activity_id = self.__add_activity(activity.activity_name,
                                               category_id)
+        else:
+            activity_id = activity_id['id']
 
 
         # if we are working on +/- current day - check the last_activity
@@ -771,9 +776,9 @@ class Storage(storage.Storage):
 
     def __add_activity(self, name, category_id = None):
         # first check that we don't have anything like that yet
-        activity_id = self.__get_activity_by_name(name, category_id)
-        if activity_id:
-            return activity_id
+        activity = self.__get_activity_by_name(name, category_id)
+        if activity:
+            return activity['id']
 
         #now do the create bit
         category_id = category_id or -1
diff --git a/hamster/hamsterdbus.py b/hamster/hamsterdbus.py
index 26c8f29..a8544bb 100644
--- a/hamster/hamsterdbus.py
+++ b/hamster/hamsterdbus.py
@@ -227,17 +227,17 @@ class HamsterDbusController(dbus.service.Object):
         runtime.storage.remove_fact(fact_id)
 
     @dbus.service.method(HAMSTER_URI, in_signature='ss')
-    def RemoveActivity(self, activity, category):
+    def RemoveActivity(self, activity_name, category):
         """Removes an activity
         Parameters:
         s activity: Activity name
         s category: Category name. Use '' for Unsorted activity
         """
         category_id = runtime.storage.get_category_by_name(category)
-        activity_id = runtime.storage.get_activity_by_name(activity, category_id)
+        activity = runtime.storage.get_activity_by_name(activity_name, category_id)
 
-        if activity_id:
-            runtime.storage.remove_activity(activity_id)
+        if activity:
+            runtime.storage.remove_activity(activity['id'])
 
     @dbus.service.method(HAMSTER_URI, in_signature='s')
     def RemoveCategory(self, category):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]