From c18dc5243544ba3e93afb6c63b288fc3050af4b0 Mon Sep 17 00:00:00 2001
From: "Jonathan S. Katz" <jonathan.katz@excoventures.com>
Date: Mon, 6 May 2013 14:09:58 -0400
Subject: [PATCH] Added the ability to list PUGs and its appropraite page

PUGs can now be dynamically listed using the admin utility of Django.
The page where they are listed is in the community directory of the
site and is located at /community/user-groups/

The infrastructure is in place to allow core.Organisation managers to
manage their own PUGs, and also allow for the submission of PUGs via
our web tools.  Other add-ons can be added in the future, such as blog
and news aggregators.
---
 pgweb/pugs/admin.py       |   11 +++++++++++
 pgweb/pugs/models.py      |   17 +++++++++++++++++
 pgweb/pugs/views.py       |   23 +++++++++++++++++++++++
 pgweb/settings.py         |    1 +
 pgweb/urls.py             |    1 +
 pgweb/util/contexts.py    |    1 +
 templates/pugs/index.html |   27 +++++++++++++++++++++++++++
 7 files changed, 81 insertions(+)
 create mode 100644 pgweb/pugs/__init__.py
 create mode 100644 pgweb/pugs/admin.py
 create mode 100644 pgweb/pugs/models.py
 create mode 100644 pgweb/pugs/views.py
 create mode 100644 templates/pugs/index.html

diff --git a/pgweb/pugs/__init__.py b/pgweb/pugs/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/pgweb/pugs/admin.py b/pgweb/pugs/admin.py
new file mode 100644
index 0000000..a436eed
--- /dev/null
+++ b/pgweb/pugs/admin.py
@@ -0,0 +1,11 @@
+from django.contrib import admin
+
+from util.admin import PgwebAdmin
+from models import *
+
+class PUGAdmin(PgwebAdmin):
+	list_display = ('title', 'approved', )
+	list_filter = ('approved', )
+	search_fields = ('title', )
+
+admin.site.register(PUG, PUGAdmin)
diff --git a/pgweb/pugs/models.py b/pgweb/pugs/models.py
new file mode 100644
index 0000000..0f7aaea
--- /dev/null
+++ b/pgweb/pugs/models.py
@@ -0,0 +1,17 @@
+from django.db import models
+from pgweb.util.bases import PgModel
+
+class PUG(PgModel, models.Model):
+    """
+    contains information about a local PostgreSQL user group
+    """
+    country = models.ForeignKey('core.Country')
+    org = models.ForeignKey('core.Organisation', null=True, blank=True, help_text='Organization that manages the PUG and its contents')
+    approved = models.BooleanField(null=False, blank=False, default=False)
+    locale = models.CharField(max_length=255, help_text="Locale where the PUG meets, e.g. 'New York City'")
+    title = models.CharField(max_length=255, help_text="Title/Name of the PUG, e.g. 'NYC PostgreSQL User Group'")
+    website_url = models.TextField(null=True, blank=True)
+    mailing_list_url = models.TextField(null=True, blank=True)
+
+    def __unicode__(self):
+        return self.title
diff --git a/pgweb/pugs/views.py b/pgweb/pugs/views.py
new file mode 100644
index 0000000..9d7f8ac
--- /dev/null
+++ b/pgweb/pugs/views.py
@@ -0,0 +1,23 @@
+from django.shortcuts import render_to_response
+
+from pgweb.util.decorators import cache
+from pgweb.util.contexts import NavContext
+
+from models import PUG
+
+def index(request):
+    """
+    contains list of PUGs, in country/locale alphabetical order
+    """
+    pug_list = []
+    for pug in PUG.objects.filter(approved=True).order_by('country__name', 'title').all():
+        if pug_list and pug_list[-1].get('country') == pug.country.name:
+            pug_list['pugs'].append(pug)
+        else:
+            pug_list.append({
+                'country': pug.country.name,
+                'pugs': [pug]
+            })
+    return render_to_response('pugs/index.html', {
+        'pug_list': pug_list,
+    }, NavContext(request, 'community'))
diff --git a/pgweb/settings.py b/pgweb/settings.py
index c4e6958..8bf9fde 100644
--- a/pgweb/settings.py
+++ b/pgweb/settings.py
@@ -110,6 +110,7 @@ INSTALLED_APPS = [
     'pgweb.featurematrix',
 	'pgweb.pwn',
 	'pgweb.search',
+    'pgweb.pugs',
 ]
 
 
diff --git a/pgweb/urls.py b/pgweb/urls.py
index 2bdd008..41658ce 100644
--- a/pgweb/urls.py
+++ b/pgweb/urls.py
@@ -54,6 +54,7 @@ urlpatterns = patterns('',
     (r'^community/lists/listinfo/$', 'lists.views.listinfo'),
     (r'^community/survey/vote/(\d+)/$', 'survey.views.vote'),
     (r'^community/survey[/\.](\d+)(-.*)?/$', 'survey.views.results'),
+    (r'^community/user-groups/$', 'pugs.views.index'),
 	(r'^community/weeklynews/$', 'pwn.views.index'),
 	(r'^community/weeklynews/pwn(\d{4})(\d{2})(\d{2})/$', 'pwn.views.post'),
 
diff --git a/pgweb/util/contexts.py b/pgweb/util/contexts.py
index e83ddac..6b5035c 100644
--- a/pgweb/util/contexts.py
+++ b/pgweb/util/contexts.py
@@ -51,6 +51,7 @@ sitenav = {
 			{'title': 'Archives',   'link':'http://archives.postgresql.org/'},
 		]},
 		{'title': 'IRC',                'link':'/community/irc/'},
+		{'title': 'Local User Groups',  'link':'/community/user-groups/'},
 		{'title': 'Featured Users',     'link':'/about/users/'},
 		{'title': 'International Sites','link':'/community/international/'},
 		{'title': 'Propaganda',         'link':'/community/propaganda/'},
diff --git a/templates/pugs/index.html b/templates/pugs/index.html
new file mode 100644
index 0000000..0e5c61d
--- /dev/null
+++ b/templates/pugs/index.html
@@ -0,0 +1,27 @@
+{%extends "base/page.html"%}
+{%block title%}Local PostgreSQL User Groups (PUGS){%endblock%}
+{%block contents%}
+
+<h1>Local User Groups</h1>
+<p>The PostgreSQL community is proud to have many local chapters that advocate and educate users about PostgreSQL.  Below is a list of PostgreSQL User Groups (PUGs) sorted by country and local area.  If you would like to start a PostgreSQL User Group, please join the <a href="{% url lists.views.subscribe %}">pgsql-advocacy mailing list</a> and describe the PUG that you want to create.</p>
+<p>If a PUG already exists in your area, follow the URLs below to find out how to attend and participate.</p>
+
+
+{% for pug_group in pug_list %}
+  <h2>{{ pug_group.country }}</h2>
+  <ul>
+  {% for pug in pug_group.pugs %}
+    <li>
+      <strong>{{ pug.locale }}</strong>:
+      {{ pug.title }}
+      {% if pug.website_url %}
+        (<a href="{{ pug.website_url }}">website</a>)
+      {% endif %}
+      {% if pug.mailing_list_url %}
+        (<a href="{{ pug.mailing_list_url }}">mailing list</a>)
+      {% endif %}
+    </li>
+  {% endfor %}
+  </ul>
+{% endfor %}
+{%endblock%}
-- 
1.7.9.6 (Apple Git-31.1)

