]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/views/users.py
Adds the option to enable/disable email digests and makes small tweak on the handling...
[osqa.git] / forum / views / users.py
index 6cc079bd14205a2e688c2473ec04b2f38a2c3eef..43464d80dd5a4de035f2c107f7cdec2a3eeae562 100644 (file)
@@ -16,6 +16,7 @@ from django.utils import simplejson
 from django.core.urlresolvers import reverse, NoReverseMatch\r
 from forum.forms import *\r
 from forum.utils.html import sanitize_html\r
+from forum.modules import decorate\r
 from datetime import datetime, date\r
 import decorators\r
 from forum.actions import EditProfileAction, FavoriteAction, BonusRepAction, SuspendAction\r
@@ -75,13 +76,6 @@ def users(request):
         }\r
     }\r
 \r
-def set_new_email(user, new_email, nomessage=False):\r
-    if new_email != user.email:\r
-        user.email = new_email\r
-        user.email_isvalid = False\r
-        user.save()\r
-    #if settings.EMAIL_VALIDATION == 'on':\r
-    #    send_new_email_key(user,nomessage=nomessage)\r
 \r
 @login_required\r
 def edit_user(request, id):\r
@@ -93,7 +87,9 @@ def edit_user(request, id):
         if form.is_valid():\r
             new_email = sanitize_html(form.cleaned_data['email'])\r
 \r
-            set_new_email(user, new_email)\r
+            if new_email != user.email:\r
+                user.email = new_email\r
+                user.email_isvalid = False\r
 \r
             if settings.EDITABLE_SCREEN_NAME:\r
                 user.username = sanitize_html(form.cleaned_data['username'])\r
@@ -108,6 +104,7 @@ def edit_user(request, id):
             user.save()\r
             EditProfileAction(user=user, ip=request.META['REMOTE_ADDR']).save()\r
 \r
+            request.user.message_set.create(message=_("Profile updated."))\r
             return HttpResponseRedirect(user.get_profile_url())\r
     else:\r
         form = EditUserForm(user)\r
@@ -137,7 +134,7 @@ def user_powers(request, id, action, status):
     return HttpResponseRedirect(user.get_profile_url())\r
 \r
 \r
-@decorators.command\r
+@decorate.withfn(decorators.command)\r
 def award_points(request, id):\r
     if (not request.POST) and request.POST.get('points', None):\r
         raise decorators.CommandException(_("Invalid request type"))\r
@@ -150,31 +147,31 @@ def award_points(request, id):
 \r
     extra = dict(message=request.POST.get('message', ''), awarding_user=request.user.id, value=points)\r
 \r
-    BonusRepAction(user=user, extra=extra).save(data=dict(value=points))\r
+    BonusRepAction(user=request.user, extra=extra).save(data=dict(value=points, affected=user))\r
 \r
     return dict(reputation=user.reputation)\r
 \r
 \r
-@decorators.command\r
+@decorate.withfn(decorators.command)\r
 def suspend(request, id):\r
     user = get_object_or_404(User, id=id)\r
 \r
+    if not request.user.is_superuser:\r
+        raise decorators.CommandException(_("Only superusers can suspend other users"))\r
+\r
     if not request.POST:\r
         if user.is_suspended():\r
             suspension = user.suspension\r
-            suspension.cancel(ip=request.META['REMOTE_ADDR'])\r
+            suspension.cancel(user=request.user, ip=request.META['REMOTE_ADDR'])\r
             return decorators.RefreshPageCommand()\r
         else:\r
             return render_to_response('users/suspend_user.html')\r
 \r
-    if not request.user.is_superuser:\r
-        raise decorators.CommandException(_("Only superusers can ban other users"))\r
-\r
     data = {\r
     'bantype': request.POST.get('bantype', 'indefinetly').strip(),\r
     'publicmsg': request.POST.get('publicmsg', _('Bad behaviour')),\r
     'privatemsg': request.POST.get('privatemsg', None) or request.POST.get('publicmsg', ''),\r
-    'suspender': request.user.id\r
+    'suspended': user\r
     }\r
 \r
     if data['bantype'] == 'forxdays':\r
@@ -183,13 +180,14 @@ def suspend(request, id):
         except:\r
             raise decorators.CommandException(_('Invalid numeric argument for the number of days.'))\r
 \r
-    SuspendAction(user=user, ip=request.META['REMOTE_ADDR']).save(data=data)\r
+    SuspendAction(user=request.user, ip=request.META['REMOTE_ADDR']).save(data=data)\r
 \r
     return decorators.RefreshPageCommand()\r
 \r
-def user_view(template, tab_name, tab_title, tab_description, private=False, tabbed=True, weight=500):\r
+\r
+def user_view(template, tab_name, tab_title, tab_description, private=False, tabbed=True, render_to=None, weight=500):\r
     def decorator(fn):\r
-        def decorated(request, id, slug=None):\r
+        def decorated(fn, request, id, slug=None):\r
             user = get_object_or_404(User, id=id)\r
             if private and not (user == request.user or request.user.is_superuser):\r
                 return HttpResponseUnauthorized(request)\r
@@ -213,11 +211,10 @@ def user_view(template, tab_name, tab_title, tab_description, private=False, tab
                     return reverse(fn.__name__, kwargs={'id': vu.id})\r
 \r
             ui.register(ui.PROFILE_TABS, ui.ProfileTab(\r
-                tab_name, tab_title, tab_description,url_getter, private, weight\r
+                tab_name, tab_title, tab_description,url_getter, private, render_to, weight\r
             ))\r
 \r
-        return decorated\r
-\r
+        return decorate.withfn(decorated)(fn)\r
     return decorator\r
 \r
 \r
@@ -250,7 +247,7 @@ def user_profile(request, user):
     "awards": awards,\r
     "total_awards" : len(awards),\r
     }\r
-\r
+    \r
 @user_view('users/recent.html', 'recent', _('recent activity'), _('recent user activity'))\r
 def user_recent(request, user):\r
     activities = user.actions.exclude(\r
@@ -290,26 +287,26 @@ def user_favorites(request, user):
 \r
 @user_view('users/subscriptions.html', 'subscriptions', _('subscription settings'), _('subscriptions'), True, tabbed=False)\r
 def user_subscriptions(request, user):\r
-    if request.method == 'POST':\r
-        form = SubscriptionSettingsForm(request.POST)\r
+    if request.method == 'POST':        \r
+        form = SubscriptionSettingsForm(data=request.POST, instance=user.subscription_settings)\r
 \r
-        if 'notswitch' in request.POST:\r
-            user.subscription_settings.enable_notifications = not user.subscription_settings.enable_notifications\r
-            user.subscription_settings.save()\r
+        if form.is_valid():\r
+            if form.cleaned_data['user'] != user.id:\r
+                return HttpResponseUnauthorized(request)\r
 \r
-            if user.subscription_settings.enable_notifications:\r
-                request.user.message_set.create(message=_('Notifications are now enabled'))\r
-            else:\r
-                request.user.message_set.create(message=_('Notifications are now disabled'))\r
+            if 'notswitch' in request.POST:\r
+                user.subscription_settings.enable_notifications = not user.subscription_settings.enable_notifications\r
+                user.subscription_settings.save()\r
 \r
-        form.is_valid()\r
-        for k, v in form.cleaned_data.items():\r
-            setattr(user.subscription_settings, k, v)\r
+                if user.subscription_settings.enable_notifications:\r
+                    request.user.message_set.create(message=_('Notifications are now enabled'))\r
+                else:\r
+                    request.user.message_set.create(message=_('Notifications are now disabled'))\r
 \r
-        user.subscription_settings.save()\r
-        request.user.message_set.create(message=_('New subscription settings are now saved'))\r
+            form.save()\r
+            request.user.message_set.create(message=_('New subscription settings are now saved'))\r
     else:\r
-        form = SubscriptionSettingsForm(user.subscription_settings.__dict__)\r
+        form = SubscriptionSettingsForm(instance=user.subscription_settings)\r
 \r
     notificatons_on = user.subscription_settings.enable_notifications\r
 \r