- 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)
+
+ view = "view_persons"
+ for tagname in self.tag_columns:
+ view= "%s left join %s using (%s)"%(view,Person.tagvalue_view_name(tagname),
+ Person.primary_key)
+
+ sql = "SELECT %s FROM %s WHERE deleted IS False" % \
+ (", ".join(self.columns.keys()+self.tag_columns.keys()),view)
+
+ 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")