[snowy] Adding new user account moderation for tomboy online



commit cb9de3eba835bc0a9e3e1c6add16bff9b912d924
Author: Jeff Schroeder <jeffschroeder computer org>
Date:   Sat Aug 21 18:14:59 2010 -0700

    Adding new user account moderation for tomboy online
    
        - Adds a new optional setting which defaults to off: MODERATE_NEW_USERS
        - Adds an ExtendedUserAdmin for dealing with users  faster in the admin
    
    Fixes bug#627394

 accounts/admin.py                              |   53 ++++++++++++++++++++++++
 accounts/templates/registration/moderated.html |   22 ++++++++++
 accounts/views.py                              |    8 +++-
 local_settings.py.in                           |    3 +
 notes/templates/notes/note_index.html          |    9 ++++
 notes/views.py                                 |    4 +-
 site_media/img/face-sad.png                    |  Bin 0 -> 20363 bytes
 7 files changed, 97 insertions(+), 2 deletions(-)
---
diff --git a/accounts/admin.py b/accounts/admin.py
new file mode 100644
index 0000000..ab0ee42
--- /dev/null
+++ b/accounts/admin.py
@@ -0,0 +1,53 @@
+#
+# Copyright (c) 2010 Jeff Schroeder <jeffschroeder computer org>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+from django.contrib.auth.models import User
+from django.contrib.auth.admin import UserAdmin
+from django.utils.translation import ugettext_lazy as _
+from django.contrib import admin
+
+class ExtendedUserAdmin(UserAdmin):
+    """Simplify mass user moderation"""
+
+    list_display = ('username', 'email', 'is_active', 'is_staff')
+    list_editable = ('is_active',)
+    actions = ('batch_user_enable', 'batch_user_disable',)
+
+    def _batch_user_modify(self, request, queryset, enable):
+        rows_updated = queryset.update(is_active=enable)
+        if rows_updated == 1:
+            msg = "1 user was"
+        else:
+            msg = "%s users were" % rows_updated
+        if enable:
+            action = "enabled"
+        else:
+            action = "disabled"
+        # Django's built-in messaging requires basestring-like objects in 1.1
+        self.message_user(request, unicode(_("%s successfully %s" % (msg, action))))
+
+    def batch_user_enable(self, request, queryset):
+        self._batch_user_modify(request, queryset, enable=True)
+
+    def batch_user_disable(self, request, queryset):
+        self._batch_user_modify(request, queryset, enable=False)
+
+    batch_user_enable.short_description  = _("Enable selected users")
+    batch_user_disable.short_description = _("Disable selected users")
+
+admin.site.unregister(User)
+admin.site.register(User, ExtendedUserAdmin)
diff --git a/accounts/templates/registration/moderated.html b/accounts/templates/registration/moderated.html
new file mode 100644
index 0000000..e06781b
--- /dev/null
+++ b/accounts/templates/registration/moderated.html
@@ -0,0 +1,22 @@
+{% extends "base.html" %}
+{% load i18n %}
+{% block title %}{{ site.name }} is not available for new user registration currently{% endblock %}
+{% block extra_head %}
+<style type="text/css">
+h1 {
+    margin-top: 4em;
+    text-align: center;
+}
+p.center {
+    text-align: center;
+}
+</style>
+{% endblock %}
+{% block content %}
+<h1>{% trans "Sorry" %} {{ user.username }}!</h1>
+{% blocktrans with site.name as site_name and MEDIA_URL as media and user.email as email %}
+<p class="center"><img src="{{ media }}/img/face-sad.png" alt=""></p>
+<p class="center">{{ site_name }} is not available outside of private alpha currently.</p>
+<p class="center">We will send an email to {{ email }} when our open beta begins.</p>
+{% endblocktrans %}
+{% endblock %}
diff --git a/accounts/views.py b/accounts/views.py
index 184d2c1..e2ba2c3 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -58,6 +58,8 @@ def openid_registration(request, template_name='registration/registration_form.h
             email = registration_form.cleaned_data.get('email')
             if email:
                 user.email = email
+            if settings.MODERATE_NEW_USERS:
+                user.is_active = False
 
             user.save()
             user.get_profile().save()
@@ -65,7 +67,11 @@ def openid_registration(request, template_name='registration/registration_form.h
                 login(request, user)
                 return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
             else:
-                return HttpResponseNotAllowed(_(u'Disabled account'))
+                if not settings.MODERATE_NEW_USERS:
+                    return HttpResponseNotAllowed(_(u'Disabled account'))
+                else:
+                    return render_to_response("registration/moderated.html", {'user': user,},
+                                              context_instance=RequestContext(request))
         else:
             return HttpResponseNotAllowed(_(u'Unknown user'))
 
diff --git a/local_settings.py.in b/local_settings.py.in
index d6e7c7c..ac389f0 100644
--- a/local_settings.py.in
+++ b/local_settings.py.in
@@ -13,3 +13,6 @@ RECAPTCHA_PUBLIC_KEY = ''
 RECAPTCHA_PRIVATE_KEY = ''
 
 EMAIL_PORT = 1025
+
+# Uncomment for limited user access
+#MODERATE_NEW_USERS = True
diff --git a/notes/templates/notes/note_index.html b/notes/templates/notes/note_index.html
index 2f4ce99..bc1ce73 100644
--- a/notes/templates/notes/note_index.html
+++ b/notes/templates/notes/note_index.html
@@ -12,6 +12,7 @@
 {{ block.super }}
 {% user_notes_list request author as all_notes %}
 <div id="sidebar-note-list">
+    {% if enabled %}
     <ul>
 {% for n in all_notes %}
         <li class="note-item{% if n.pinned %} pinned{% endif %}"><a href="{{ n.get_absolute_url }}">{{ n.title|safe }}</a></li>
@@ -25,15 +26,19 @@
         <li id="new-note"><a href="#">New Note...</a></li>
 {% endcomment %}
     </ul>
+    {% endif %}
 </div>
 <div id="sidebar-notebook-list">
+{% if enabled %}
     <h3>{% trans "Notebooks" %}</h3>
     <ul>
 {% user_notebook_list request author as all_notebooks %}
 {% include "notes/notebook_list_snippet.html" %}
+{% endif %}
 {% endblock %}
 
 {% block content %}
+{% if enabled %}
 <table id="content-layout" cellspacing="0" cellpadding="0">
     <tr>
         <td id="note">
@@ -57,4 +62,8 @@
         </td>
     </br>
 </table>
+{% else %}
+<h1>Notice</h1>
+{{ author }}'s account is currently disabled.
+{% endif %}
 {% endblock %}
diff --git a/notes/views.py b/notes/views.py
index 85ffebf..e05e1a1 100644
--- a/notes/views.py
+++ b/notes/views.py
@@ -31,16 +31,18 @@ from snowy import settings
 def note_index(request, username,
                template_name='notes/note_index.html'):
     author = get_object_or_404(User, username=username)
+    enabled = author.is_active
 
     # TODO: retrieve the last open note from the user
     last_modified = Note.objects.user_viewable(request.user, author) \
                                 .order_by('-user_modified')
     if last_modified.count() > 0:
         return HttpResponseRedirect(last_modified[0].get_absolute_url())
-    
+
     # TODO: Instruction page to tell user to either sync or create a new note
     return render_to_response(template_name,
                               {'author': author,
+                               'enabled': enabled,
                                # Django 1.1 does not support == operator, so
                                # we need to do the check here and pass it along
                                'author_is_user': username==request.user.username},
diff --git a/site_media/img/face-sad.png b/site_media/img/face-sad.png
new file mode 100644
index 0000000..16881d0
Binary files /dev/null and b/site_media/img/face-sad.png differ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]