Repository: gratipay/gratipay.com

Issue:
Count of community members is always zero on user profiles
Broke it in #2682.

![gittip-2692](https://cloud.githubusercontent.com/assets/1581590/4059864/0803a83a-2dea-11e4-86f2-5f76d55b0d36.png)



Discussion:

Resolving patch:
diff --git a/gratipay/models/community.py b/gratipay/models/community.py
--- a/gratipay/models/community.py
+++ b/gratipay/models/community.py
@@ -18,53 +18,47 @@ def slugize(slug):
     return slug
 
 
-def get_list_for(db, username):
+def get_list_for(db, participant_id):
     """Return a listing of communities.
 
     :database: One SELECT, multiple rows
 
     """
-    if username is None:
-        member_test = ''
-        sort_order = 'DESC'
-        params = ()
+    if participant_id is None:
+        return db.all("""
+            SELECT c.*
+              FROM communities c
+          ORDER BY nmembers DESC, slug
+        """)
     else:
-        member_test = 'AND participant = %s'
-        sort_order = 'ASC'
-        params = (username,)
-
-    return db.all("""
-
-        SELECT max(name) AS name
-             , slug
-             , count(*) AS nmembers
-          FROM current_communities
-         WHERE is_member {0}
-      GROUP BY slug
-      ORDER BY nmembers {1}, slug
-
-    """.format(member_test, sort_order), params)
+        return db.all("""
+            SELECT c.*
+              FROM current_community_members ccm
+              JOIN communities c ON c.slug = ccm.slug
+             WHERE ccm.is_member AND ccm.participant = %s
+          ORDER BY c.nmembers ASC, c.slug
+        """, (participant_id,))
 
 class Community(Model):
     """Model a community on Gratipay.
     """
 
-    typname = "community_summary"
+    typname = "communities"
 
     @classmethod
     def from_slug(cls, slug):
         return cls.db.one("""
-            SELECT community_summary.*::community_summary
-            FROM community_summary WHERE slug=%s;
+            SELECT c.*::communities FROM communities c WHERE slug=%s;
         """, (slug,))
 
     def get_members(self, limit=None, offset=None):
         return self.db.all("""
             SELECT p.*::participants
-              FROM current_communities c
-              JOIN participants p ON p.username = c.participant
+              FROM current_community_members c
+              JOIN participants p ON p.id = c.participant
              WHERE c.slug = %s
-               AND is_member
+               AND c.is_member
+               AND p.is_suspicious IS NOT true
           ORDER BY c.ctime
              LIMIT %s
             OFFSET %s;
@@ -72,5 +66,7 @@ def get_members(self, limit=None, offset=None):
 
     def check_membership(self, participant):
         return self.db.one("""
-            SELECT * FROM current_communities WHERE slug=%s AND participant=%s
-        """, (self.slug, participant.username))
+            SELECT is_member
+              FROM current_community_members
+             WHERE slug=%s AND participant=%s
+        """, (self.slug, participant.id))
diff --git a/gratipay/models/participant.py b/gratipay/models/participant.py
--- a/gratipay/models/participant.py
+++ b/gratipay/models/participant.py
@@ -404,10 +404,10 @@ def clear_personal_information(self, cursor):
             self.remove_all_members(cursor)
         r = cursor.one("""
 
-            INSERT INTO communities (ctime, name, slug, participant, is_member) (
-                SELECT ctime, name, slug, %(username)s, false
-                  FROM current_communities
-                 WHERE participant=%(username)s
+            INSERT INTO community_members (slug, participant, ctime, name, is_member) (
+                SELECT slug, participant, ctime, name, false
+                  FROM community_members
+                 WHERE participant=%(participant_id)s
                    AND is_member IS true
             );
 
@@ -436,7 +436,7 @@ def clear_personal_information(self, cursor):
              WHERE username=%(username)s
          RETURNING *;
 
-        """, dict(username=self.username))
+        """, dict(username=self.username, participant_id=self.id))
         self.set_attributes(**r._asdict())
 
 
@@ -464,25 +464,26 @@ def accepts_tips(self):
 
 
     def insert_into_communities(self, is_member, name, slug):
-        username = self.username
+        participant_id = self.id
         self.db.run("""
 
-            INSERT INTO communities
+            INSERT INTO community_members
                         (ctime, name, slug, participant, is_member)
                  VALUES ( COALESCE (( SELECT ctime
-                                        FROM communities
-                                       WHERE (participant=%s AND slug=%s)
+                                        FROM community_members
+                                       WHERE participant=%(participant_id)s
+                                         AND slug=%(slug)s
                                        LIMIT 1
                                       ), CURRENT_TIMESTAMP)
-                        , %s, %s, %s, %s
+                        , %(name)s, %(slug)s, %(participant_id)s, %(is_member)s
                          )
               RETURNING ( SELECT count(*) = 0
-                            FROM communities
-                           WHERE participant=%s
+                            FROM community_members
+                           WHERE participant=%(participant_id)s
                          )
                      AS first_time_community
 
-        """, (username, slug, name, slug, username, is_member, username))
+        """, locals())
 
 
     def change_username(self, suggested):


Test patch: