changeset 351:b5dc92631561

Allow to load one project and specific entries from the Backend Add a backend method load_project to allow loading of one project from the database. Also extend load_entries method to load only entries from a specific year or calendar week.
author Björn Ricks <bjoern.ricks@intevation.de>
date Mon, 03 Mar 2014 14:24:44 +0100
parents f581752317fd
children 2e7885dc6669
files getan/backend.py
diffstat 1 files changed, 105 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/getan/backend.py	Mon Mar 03 14:22:30 2014 +0100
+++ b/getan/backend.py	Mon Mar 03 14:24:44 2014 +0100
@@ -54,6 +54,17 @@
     WHERE active
 '''
 
+LOAD_ACTIVE_PROJECT = '''
+SELECT id, key, description, total
+FROM projects LEFT JOIN
+(SELECT
+    project_id,
+    sum(strftime('%s', stop_time) - strftime('%s', start_time)) AS total
+    FROM entries
+    GROUP BY project_id) ON project_id = id
+    WHERE active and key = :project_id
+'''
+
 LOAD_PROJECT_ENTRIES = '''
 SELECT
     id,
@@ -70,6 +81,64 @@
 DESC
 '''
 
+LOAD_PROJECT_ENTRIES_YEAR = '''
+SELECT
+    id,
+    project_id,
+    start_time as "[timestamp]",
+    stop_time  as "[timestamp]",
+    'no description' AS description
+FROM entries
+WHERE
+    project_id = :project_id AND
+    (description IS NULL or length(description) = 0)
+GROUP BY round(julianday(start_time))
+UNION
+SELECT
+    id,
+    project_id,
+    start_time as "[timestamp]",
+    stop_time  as "[timestamp]",
+    description
+FROM entries
+WHERE
+    project_id = :project_id AND
+    (strftime('%Y', start_time) ) = :year AND
+    description IS NOT NULL AND length(description) > 0
+'''
+
+LOAD_PROJECT_ENTRIES_WEEK = '''
+SELECT
+    id,
+    project_id,
+    start_time as "[timestamp]",
+    stop_time  as "[timestamp]",
+    'no description' AS description
+FROM entries
+WHERE
+    project_id = :project_id AND
+    (strftime('%Y', start_time) ) = :year AND
+    (description IS NULL or length(description) = 0)
+    AND (strftime('%W', start_time) = :week
+    OR strftime('%W', stop_time) = :week)
+GROUP BY round(julianday(start_time))
+UNION
+SELECT
+    id,
+    project_id,
+    start_time as "[timestamp]",
+    stop_time  as "[timestamp]",
+    description
+FROM entries
+WHERE
+    project_id = :project_id AND
+    (strftime('%Y', start_time) ) = :year AND
+    description IS NOT NULL AND length(description) > 0
+    AND (strftime('%W', start_time) = :week
+    OR strftime('%W', stop_time) = :week)
+ORDER BY start_time
+'''
+
 INSERT_PROJECT_ENTRY = '''
 INSERT INTO entries (project_id, start_time, stop_time, description)
 VALUES(?,?,?,?)
@@ -154,14 +223,47 @@
         finally:
             close(cur)
 
-    def load_entries(self, project_id):
+    def load_project(self, key):
+        logger.debug("load active projects from database.")
+        cur = None
+        try:
+            cur = self.con.cursor()
+            cur.execute(LOAD_ACTIVE_PROJECT, {"project_id": key})
+            row = cur.fetchone()
+            if not row:
+                raise InvalidProjectKeyError("Project with key %s not "
+                                             "found." % key)
+            # check key
+            if not row[1]:
+                raise InvalidProjectKeyError("Project with id %s needs "
+                                             "a key" % row[0])
+            return Project(self, *row)
+
+        finally:
+            close(cur)
+
+    def load_entries(self, project_id, year=None, week=None):
         """ Loads all entries that belong to a specific project """
         logger.debug("load entries that belong to project %s" % project_id)
         cur = None
+
+        if week and isinstance(week, int):
+            week = "%02d" % (week)
+
         try:
             cur = self.con.cursor()
-            cur.execute(LOAD_PROJECT_ENTRIES,
-                        {"project_id": project_id})
+
+            if year is None and week is None:
+                cur.execute(LOAD_PROJECT_ENTRIES,
+                            {"project_id": project_id})
+            elif week is None:
+                cur.execute(LOAD_PROJECT_ENTRIES_YEAR,
+                            {'project_id': project_id, 'year': str(year)})
+            else:
+                cur.execute(LOAD_PROJECT_ENTRIES_WEEK,
+                            {'project_id': project_id,
+                             'week': week,
+                             'year': str(year)})
 
             entries = []
             while True:
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)