changeset 206:31b28f369fd3

more flexible getMemberList()
author casties
date Thu, 27 Jun 2013 17:46:28 +0200
parents 2b27332c9545
children 938add25f81b
files MPIWGStaff.py
diffstat 1 files changed, 24 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/MPIWGStaff.py	Thu Jun 27 16:14:19 2013 +0200
+++ b/MPIWGStaff.py	Thu Jun 27 17:46:28 2013 +0200
@@ -99,37 +99,48 @@
         return len(res) > 0
 
 
-    def getMemberList(self, department=None, sortBy='last_name', onlyCurrent=False, arrivedWithin=None, limit=0,withEmail=True):
+    def getMemberList(self, department=None, sortBy='last_name', onlyCurrent=False, arrivedWithin=None, 
+                      onlyPublished=True, onlyWithEmail=True, onlyScholar=True, limit=0):
         """Return the list of members.
         
         Returns a list of MPIWGStaffMember objects.
         """
         members = []
+        args = []
+        wheres = []
         
-        if withEmail:
-            query = "select * from personal_www_list where publish_the_data = 'yes' and is_scholar='yes' and e_mail <> '' "
-        else:
-            query = "select * from personal_www_list where publish_the_data = 'yes' and is_scholar='yes'"
-        args = []
-        
+        if onlyPublished:
+            wheres.append("publish_the_data = 'yes'")
+
+        if onlyWithEmail:
+            wheres.append("e_mail <> ''")
+
+        if onlyScholar:
+            wheres.append("is_scholar = 'yes'")
+
         if department is not None:
-            query += " and department ilike %s"
+            wheres.append("department ilike %s")
             args.append('%%%s%%'%department)
         
         if onlyCurrent:
-            query += " and date_from < CURRENT_DATE"
+            wheres.append("date_from < CURRENT_DATE")
             
         if arrivedWithin is not None:
-            query += " and date_from > CURRENT_DATE - interval %s"
+            wheres.append("date_from > CURRENT_DATE - interval %s")
             args.append(arrivedWithin)
+        
+        # assemble query
+        query = "SELECT * FROM personal_www_list"
+        if len(wheres) > 0:
+            query += " WHERE " + " AND ".join(wheres)
             
         if sortBy == 'last_name':
-            query += " order by lower(last_name)"
+            query += " ORDER BY lower(last_name)"
         elif sortBy == 'date_from':
-            query += " order by date_from DESC"
+            query += " ORDER BY date_from DESC"
             
         if limit > 0:
-            query += " limit %s"%int(limit)
+            query += " LIMIT %s"%int(limit)
             
         result = self.executeZSQL(query, args)
         for res in result: