- def __init__(self, api, person_id_or_email_list = None, extra_fields = [], deleted = False, enabled = None):
- self.api = api
-
- role_max = Roles.role_max
-
- # N.B.: Site IDs returned may be deleted. Persons returned are
- # never deleted, but may not be enabled.
- sql = "SELECT persons.*" \
- ", roles.role_id, roles.name AS role" \
- ", person_site.site_id" \
-
- # N.B.: Joined IDs may be marked as deleted in their primary tables
- join_tables = {
- # extra_field: (extra_table, extra_column, join_using)
- 'address_ids': ('person_address', 'address_id', 'person_id'),
- 'key_ids': ('person_keys', 'key_id', 'person_id'),
- 'slice_ids': ('dslice03_sliceuser', 'slice_id', 'person_id'),
- }
-
- extra_fields = filter(join_tables.has_key, extra_fields)
- extra_tables = ["%s USING (%s)" % \
- (join_tables[field][0], join_tables[field][2]) \
- for field in extra_fields]
- extra_columns = ["%s.%s" % \
- (join_tables[field][0], join_tables[field][1]) \
- for field in extra_fields]
-
- if extra_columns:
- sql += ", " + ", ".join(extra_columns)
-
- sql += " FROM persons" \
- " LEFT JOIN person_roles USING (person_id)" \
- " LEFT JOIN roles USING (role_id)" \
- " LEFT JOIN person_site USING (person_id)"
-
- if extra_tables:
- sql += " LEFT JOIN " + " LEFT JOIN ".join(extra_tables)
-
- # So that people with no roles have empty role_ids and roles values
- sql += " WHERE (role_id IS NULL or role_id <= %(role_max)d)"
-
- if deleted is not None:
- sql += " AND persons.deleted IS %(deleted)s"
-
- if enabled is not None:
- sql += " AND persons.enabled IS %(enabled)s"
-
- if person_id_or_email_list:
- # Separate the list into integers and strings
- person_ids = filter(lambda person_id: isinstance(person_id, (int, long)),
- person_id_or_email_list)
- emails = filter(lambda email: isinstance(email, StringTypes),
- person_id_or_email_list)
- sql += " AND (False"
- if person_ids:
- sql += " OR person_id IN (%s)" % ", ".join(map(str, person_ids))
- if emails:
- # Case insensitive e-mail address comparison
- sql += " OR lower(email) IN (%s)" % ", ".join(api.db.quote(emails)).lower()
- sql += ")"
-
- # The first site_id in the site_ids list is the primary site
- # of the user. See AdmGetPersonSites().
- sql += " ORDER BY person_site.is_primary DESC"
-
- rows = self.api.db.selectall(sql, locals())
- for row in rows:
- if self.has_key(row['person_id']):
- person = self[row['person_id']]
- person.update(row)
+ def __init__(self, api, person_filter = None, columns = None):
+ Table.__init__(self, api, Person, columns)
+ #sql = "SELECT %s FROM view_persons WHERE deleted IS False" % \
+ # ", ".join(self.columns)
+ foreign_fields = {'role_ids': ('role_id', 'person_role'),
+ 'roles': ('name', 'roles'),
+ 'site_ids': ('site_id', 'person_site'),
+ 'key_ids': ('key_id', 'person_key'),
+ 'slice_ids': ('slice_id', 'slice_person')
+ }
+ foreign_keys = {}
+ db_fields = filter(lambda field: field not in foreign_fields.keys(), Person.fields.keys())
+ all_fields = db_fields + [value[0] for value in foreign_fields.values()]
+ fields = []
+ _select = "SELECT "
+ _from = " FROM persons "
+ _join = " LEFT JOIN peer_person USING (person_id) "
+ _where = " WHERE deleted IS False "
+
+ if not columns:
+ # include all columns
+ fields = all_fields
+ tables = [value[1] for value in foreign_fields.values()]
+ tables.sort()
+ for key in foreign_fields.keys():
+ foreign_keys[foreign_fields[key][0]] = key
+ for table in tables:
+ if table in ['roles']:
+ _join += " LEFT JOIN roles USING(role_id) "
+ else:
+ _join += " LEFT JOIN %s USING (person_id) " % (table)
+ else:
+ tables = set()
+ columns = filter(lambda column: column in db_fields+foreign_fields.keys(), columns)
+ columns.sort()
+ for column in columns:
+ if column in foreign_fields.keys():
+ (field, table) = foreign_fields[column]
+ foreign_keys[field] = column
+ fields += [field]
+ tables.add(table)
+ if column in ['roles']:
+ _join += " LEFT JOIN roles USING(role_id) "
+ else:
+ _join += " LEFT JOIN %s USING (person_id)" % \
+ (foreign_fields[column][1])
+
+ else:
+ fields += [column]
+
+ # postgres will return timestamps as datetime objects.
+ # XMLPRC cannot marshal datetime so convert to int
+ timestamps = ['date_created', 'last_updated', 'verification_expires']
+ for field in fields:
+ if field in timestamps:
+ fields[fields.index(field)] = \
+ "CAST(date_part('epoch', %s) AS bigint) AS %s" % (field, field)
+
+ _select += ", ".join(fields)
+ sql = _select + _from + _join + _where
+
+ # deal with filter
+ if person_filter is not None:
+ if isinstance(person_filter, (list, tuple, set)):
+ # Separate the list into integers and strings
+ ints = filter(lambda x: isinstance(x, (int, long)), person_filter)
+ strs = filter(lambda x: isinstance(x, StringTypes), person_filter)
+ person_filter = Filter(Person.fields, {'person_id': ints, 'email': strs})
+ sql += " AND (%s) %s" % person_filter.sql(api, "OR")
+ elif isinstance(person_filter, dict):
+ person_filter = Filter(Person.fields, person_filter)
+ sql += " AND (%s) %s" % person_filter.sql(api, "AND")
+ elif isinstance (person_filter, StringTypes):
+ person_filter = Filter(Person.fields, {'email':[person_filter]})
+ sql += " AND (%s) %s" % person_filter.sql(api, "AND")
+ elif isinstance (person_filter, int):
+ person_filter = Filter(Person.fields, {'person_id':[person_filter]})
+ sql += " AND (%s) %s" % person_filter.sql(api, "AND")
+ else:
+ raise PLCInvalidArgument, "Wrong person filter %r"%person_filter
+
+ # aggregate data
+ all_persons = {}
+ for row in self.api.db.selectall(sql):
+ person_id = row['person_id']
+
+ if all_persons.has_key(person_id):
+ for (key, key_list) in foreign_keys.items():
+ data = row.pop(key)
+ row[key_list] = [data]
+ if data and data not in all_persons[person_id][key_list]:
+ all_persons[person_id][key_list].append(data)