diff getan/backend.py @ 550:28b1c18c900f

Specify project keys in getan-report with SQL patterns * In getan-report project keys can now be specified with SQL patterns. This allows projects and their sub-projects to be better selected for output. % - zero, one, or multiple characters _ - single character
author Magnus Schieder <mschieder@intevation.de>
date Tue, 11 Feb 2020 15:46:37 +0100
parents fea767901dbc
children fbe26b0e59ed
line wrap: on
line diff
--- a/getan/backend.py	Mon Feb 03 15:51:33 2020 +0100
+++ b/getan/backend.py	Tue Feb 11 15:46:37 2020 +0100
@@ -80,6 +80,17 @@
     WHERE active
 '''
 
+LOAD_ACTIVE_PROJECTS_LIKE = '''
+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 LIKE :project_id
+'''
+
 LOAD_ACTIVE_PROJECT = '''
 SELECT id, key, description, total
 FROM projects LEFT JOIN
@@ -272,6 +283,33 @@
         finally:
             close(cur)
 
+    def load_projects_like(self, key):
+        """ Loads active projects matching the SQL LIKE pattern from the
+        database and returns them as array. """
+        cur = None
+        try:
+            cur = self.con.cursor()
+            cur.execute(LOAD_ACTIVE_PROJECTS_LIKE, {"project_id": key})
+
+            projects = []
+            while True:
+                row = cur.fetchone()
+
+                if not row:
+                    break
+                # check key
+                if not row[1]:
+                    raise InvalidProjectKeyError("Project with id %s needs "
+                                                 "a key" % row[0])
+                proj = Project(self, *row)
+                projects.append(proj)
+
+            logger.info("found %i active projects." % len(projects))
+            return projects
+
+        finally:
+            close(cur)
+
     def load_recover(self):
         """If there is an entry in the recovery table, the entry is moved to
         its project."""
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)