Updates to send email comments, included translation, better validation.
authorDerek Moore <derek.k.moore@gmail.com>
Mon, 26 Mar 2012 05:25:54 +0000 (22:25 -0700)
committerDerek Moore <derek.k.moore@gmail.com>
Mon, 26 Mar 2012 05:25:54 +0000 (22:25 -0700)
mediagoblin/edit/forms.py
mediagoblin/edit/views.py
mediagoblin/user_pages/lib.py

index 83600e4d16299c752bcca58f454e455f02915ef1..d310c18f72f0bbe4da3f7f9c4208d1decd7b3f37 100644 (file)
@@ -61,18 +61,14 @@ class EditProfileForm(wtforms.Form):
 class EditAccountForm(wtforms.Form):
     old_password = wtforms.PasswordField(
         _('Old password'),
-        [wtforms.validators.Required()],
         description=_(
             "Enter your old password to prove you own this account."))
     new_password = wtforms.PasswordField(
         _('New password'),
-        [wtforms.validators.Required(),
-         wtforms.validators.Length(min=6, max=30)],
+        [wtforms.validators.Length(min=6, max=30)],
         id="password")
-
     wants_comment_notification = wtforms.BooleanField(
         _('Comment notification?'),
-        [wtforms.validators.Required()],
         description=_("Check this box to be emailed when someone else comments on your media."))
 
 
index 2704a20cba045d171436834c9ba6c5e98000728f..b7954cb5e189d1fc499cbf0a9f20d8f5431d5ffd 100644 (file)
@@ -185,51 +185,51 @@ def edit_profile(request):
 
 @require_active_login
 def edit_account(request):
-    edit_username = request.GET.get('username')
     user = request.user
-
     form = forms.EditAccountForm(request.POST,
-        wants_comment_notification=user.wants_comment_notification)
+        wants_comment_notification=user.get('wants_comment_notification'))
 
     if request.method == 'POST':
-        #save status of comment checkbox first, so user does not need to 
-        #change their password as well.
-        user.wants_comment_notification = request.POST.get(
-            'wants_comment_notification', False) == u'y'
-
-        user.save()
-
-        #check remaining fields for validation
-        if form.validate():
-            password_matches = auth_lib.bcrypt_check_password(
-                request.POST['old_password'],
-                user.pw_hash)
-
-            if (request.POST['old_password'] or \
-                request.POST['new_password']) and not \
-                    password_matches:
-                form.old_password.errors.append(_('Wrong password'))
-
-                return render_to_response(
-                    request,
-                    'mediagoblin/edit/edit_account.html',
-                    {'user': user,
-                     'form': form})
-
-            if password_matches:
-                user.pw_hash = auth_lib.bcrypt_gen_password_hash(
-                    request.POST['new_password'])
-            user.save()
-
-        messages.add_message(request,
-                             messages.SUCCESS,
-                             _("Account settings saved"))
-        return redirect(request,
-                       'mediagoblin.user_pages.user_home',
-                        user=user.username)
+        form_validated = form.validate()
+
+        #if the user has not filled in the new or old password fields
+        if not form.new_password.data and not form.old_password.data:
+            if form.wants_comment_notification.validate(form):
+                user.wants_comment_notification = \
+                    form.wants_comment_notification.data
+                user.save()
+                messages.add_message(request,
+                    messages.SUCCESS,
+                    _("Account settings saved"))
+                return redirect(request,
+                                'mediagoblin.user_pages.user_home',
+                                user=user.username)
+
+        #so the user has filled in one or both of the password fields
+        else:
+            if form_validated:
+                password_matches = auth_lib.bcrypt_check_password(
+                    form.old_password.data,
+                    user.pw_hash)
+                if password_matches:
+                    #the entire form validates and the password matches
+                    user.pw_hash = auth_lib.bcrypt_gen_password_hash(
+                        form.new_password.data)
+                    user.wants_comment_notification = \
+                        form.wants_comment_notification.data
+                    user.save()
+                    messages.add_message(request,
+                        messages.SUCCESS,
+                        _("Account settings saved"))
+                    return redirect(request,
+                                    'mediagoblin.user_pages.user_home',
+                                    user=user.username)
+                else:
+                    form.old_password.errors.append(_('Wrong password'))
 
     return render_to_response(
         request,
         'mediagoblin/edit/edit_account.html',
         {'user': user,
          'form': form})
+
index 00091d7a128ffc0e0a99c96a47603d04aa832d43..79f9a2b2d3fedae574d240ab0d98848b13bc56ea 100644 (file)
@@ -16,6 +16,7 @@
 
 from mediagoblin.tools.mail import send_email
 from mediagoblin.tools.template import render_template
+from mediagoblin.tools.translate import pass_to_ugettext as _
 from mediagoblin import mg_globals
 
 def send_comment_email(user, comment, media, request):
@@ -29,13 +30,12 @@ def send_comment_email(user, comment, media, request):
     - request: the request
     """
 
-    comment_url = u'http://{host}{comment_uri}'.format( 
-            host=request.host,
-            comment_uri=request.urlgen(
-                'mediagoblin.user_pages.media_home.view_comment',
-                comment = comment._id,
-                user = media.get_uploader.username,
-                media = media.slug_or_id) + '#comment')
+    comment_url = request.urlgen(
+                    'mediagoblin.user_pages.media_home.view_comment',
+                    comment = comment._id,
+                    user = media.get_uploader.username,
+                    media = media.slug_or_id,
+                    qualified = True) + '#comment'
 
     comment_author = comment.get_author['username']
 
@@ -49,6 +49,6 @@ def send_comment_email(user, comment, media, request):
     send_email(
         mg_globals.app_config['email_sender_address'],
         [user.email],
-        'GNU MediaGoblin - {comment_author} commented on your post'.format(
-            comment_author=comment_author),
+        'GNU MediaGoblin - {comment_author} '.format(
+            comment_author=comment_author) + _('commented on your post'),
         rendered_email)