From c9709c899c5ddb19dde61009a532c2a8004d26f9 Mon Sep 17 00:00:00 2001
From: Ralph Bean <rbean@redhat.com>
Date: Mar 23 2016 20:32:44 +0000
Subject: Populate both groups and users from FAS.


---

diff --git a/hubs/defaults.py b/hubs/defaults.py
index c37b6cb..0a9e4f1 100755
--- a/hubs/defaults.py
+++ b/hubs/defaults.py
@@ -80,3 +80,50 @@ def add_user_widgets(session, hub, username, fullname):
         }))
     hub.widgets.append(widget)
     return hub
+
+
+def add_group_widgets(session, hub, name, summary,
+                      # These are all things that come from FAS
+                      apply_rules=None,
+                      irc_channel=None,
+                      irc_network=None,
+                      join_message=None,
+                      mailing_list=None,
+                      mailing_list_url=None):
+    """ Some defaults for an automatically created group hub. """
+
+    if apply_rules:
+        widget = hubs.models.Widget(
+            plugin='sticky', index=0, left=True,
+            _config=json.dumps({
+                "text": apply_rules,
+            }))
+        hub.widgets.append(widget)
+
+    if join_message:
+        widget = hubs.models.Widget(
+            plugin='about', index=0,
+            _config=json.dumps({
+                "text": join_message,
+            }))
+        hub.widgets.append(widget)
+
+    widget = hubs.models.Widget(
+        plugin='rules', index=1,
+        _config=json.dumps({
+            # TODO -- can we guess their urls?
+            'link': None,
+            'schedule_text': None,
+            'schedule_link': None,
+            'minutes_link': None,
+        })
+    )
+    hub.widgets.append(widget)
+
+    # TODO - set up irc widget with the right irc channel
+    # TODO - set up mailing list with the right mailing list.
+
+    # TODO - set up feed with aggregate of users stuff unless we have some
+    # other preset.
+
+    return hub
diff --git a/hubs/models.py b/hubs/models.py
index 9bbbe80..abd92f4 100755
--- a/hubs/models.py
+++ b/hubs/models.py
@@ -160,6 +160,15 @@ class Hub(BASE):
             if idle > limit:
                 return name
 
+    @classmethod
+    def get_or_create(cls, session, name, summary, **extra):
+        self = cls.by_name(session, name)
+        if not self:
+            self = cls.create_group_hub(session, name, summary, **extra)
+            session.add(self)
+            session.commit()
+        return self
+
     @property
     def owners(self):
         return [assoc.user for assoc in self.associations
@@ -227,6 +236,18 @@ class Hub(BASE):
 
         user = User.by_username(session, username)
         hub.subscribe(session, user, role='owner')
+        return hub
+
+    @classmethod
+    def create_group_hub(cls, session, name, summary, **extra):
+        hub = cls(name=name, summary=summary,
+                  # TODO -- do something else, smarter for group avatars
+                  avatar=username2avatar(name),
+                  user_hub=False)
+        session.add(hub)
+
+        hubs.defaults.add_group_widgets(session, hub, name, summary, **extra)
+        return hub
 
     @property
     def right_width(self):
diff --git a/hubs/templates/groups.html b/hubs/templates/groups.html
index e3368b0..a26fd17 100644
--- a/hubs/templates/groups.html
+++ b/hubs/templates/groups.html
@@ -82,7 +82,8 @@
   <div class="row">
     <div class="col-md-12">
       {% for group in promoted %}
-      {{ group.name }} is a promoted group, and it has {{ group.subscribers | length }} subscribers and {{ group.members | length }} members, and {{group.owners | length }} owners
+        <a href="{{ url_for('hub', name=group.name)}}">{{group.name}}</a>
+        is a promoted group, and it has {{ group.subscribers | length }} subscribers and {{ group.members | length }} members, and {{group.owners | length }} owners
       {% endfor %}
     </div>
   </div>
@@ -108,7 +109,8 @@
   <div class="row">
     <div class="col-md-12">
       {% for group in secondary %}
-        {{ group.name }} is not a promoted group
+        <a href="{{ url_for('hub', name=group.name)}}">{{group.name}}</a>
+        is not a promoted group
         but it has {{ group.subscribers | length }} subscribers and {{ group.members | length }} members, and {{group.owners | length }} owners
         <br/>
       {% endfor %}
diff --git a/populate-all-users.py b/populate-all-users.py
deleted file mode 100755
index 401a20e..0000000
--- a/populate-all-users.py
+++ /dev/null
@@ -1,38 +0,0 @@
-#!/usr/bin/env python
-""" Populate the hubs db with lots of data from FAS. """
-
-import getpass
-import socket
-import string
-
-import hubs.models
-
-import fedora.client.fas2
-import fedmsg.config
-fedmsg_config = fedmsg.config.load_config()
-
-fasclient = fedora.client.fas2.AccountSystem(
-    username=raw_input('Your FAS username: '),
-    password=getpass.getpass(),
-)
-timeout = socket.getdefaulttimeout()
-socket.setdefaulttimeout(None)
-
-for letter in reversed(sorted(list(set(string.letters.lower())))):
-    session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri'], True, True)
-    print "Querying FAS for the %r users.. hang on." % letter
-    request = fasclient.send_request('/user/list',
-                                    req_params={'search': '%s*' % letter},
-                                    auth=True,
-                                    timeout=500)
-    users = request['people']
-
-    for user in users:
-        username = user['username']
-        fullname = user['human_name']
-        openid = '%s.id.fedoraproject.org' % username
-        print "Creating account for %r" % openid
-        hubs.models.User.get_or_create(
-            session, openid=openid, fullname=fullname)
-
-    session.commit()
diff --git a/populate-from-fas.py b/populate-from-fas.py
new file mode 100755
index 0000000..754de7f
--- /dev/null
+++ b/populate-from-fas.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+""" Populate the hubs db with lots of data from FAS. """
+
+import getpass
+import socket
+import string
+
+import hubs.models
+
+import fedora.client.fas2
+import fedmsg.config
+fedmsg_config = fedmsg.config.load_config()
+
+fasclient = fedora.client.fas2.AccountSystem(
+    username=raw_input('Your FAS username: '),
+    password=getpass.getpass(),
+)
+timeout = socket.getdefaulttimeout()
+socket.setdefaulttimeout(None)
+
+
+prefix_blacklist = [
+    u'git',
+    u'svn',
+    u'bzr',
+    u'cvs',
+    u'hg',
+    u'cla_',
+    u'sysadmin-',
+]
+suffix_blacklist = [
+    u'commits',
+    u'git',
+]
+
+
+for letter in reversed(sorted(list(set(string.letters.lower())))):
+    session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri'], True, True)
+    print "Querying FAS for the %r users.. hang on." % letter
+    request = fasclient.send_request('/user/list',
+                                    req_params={'search': '%s*' % letter},
+                                    auth=True,
+                                    timeout=500)
+    users = request['people']
+
+    for user in users:
+        username = user['username']
+        fullname = user['human_name']
+        openid = '%s.id.fedoraproject.org' % username
+        print "Creating account for %r" % openid
+        hubs_user = hubs.models.User.get_or_create(
+            session, openid=openid, fullname=fullname)
+
+    session.commit()
+
+    # Go back now and set up the groups...
+    session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri'], True, True)
+    for user in users:
+        hubs_user = hubs.models.User.by_username(session, user['username'])
+
+        # Build a lookup by group id
+        roles = dict([(r.group_id, r) for r in user['roles']])
+
+        for membership in user['memberships']:
+            role = roles[membership['id']]
+            if role['role_status'] != u'approved':
+                continue
+
+            name = membership['name']
+            summary = membership['display_name']
+
+            if any([name.startswith(prefix) for prefix in prefix_blacklist]):
+                continue
+            if any([name.endswith(suffix) for suffix in suffix_blacklist]):
+                continue
+
+            hub = hubs.models.Hub.get_or_create(
+                session,
+                name=name,
+                summary=summary,
+                apply_rules=membership['apply_rules'],
+                irc_channel=membership['irc_channel'],
+                irc_network=membership['irc_network'],
+                join_message=membership['joinmsg'],
+                mailing_list=membership['mailing_list'],
+                mailing_list_url=membership['mailing_list_url'],
+            )
+
+
+            if not hubs_user in hub.subscribers:
+                hub.subscribe(session, hubs_user, role='subscriber')
+            if not hubs_user in hub.members:
+                hub.subscribe(session, hubs_user, role='member')
+            if not hubs_user in hub.owners:
+                if role['role_type'] in [u'administrator', u'sponsor']:
+                    hub.subscribe(session, hubs_user, role='owner')
+
+    session.commit()

