user.get('moo') -> user.moo
[mediagoblin.git] / mediagoblin / edit / views.py
index c656c63f497664cb91ab4a96d52b38c8f3024d53..25a617fd3fdfab3dc537442661a54286ae96e05d 100644 (file)
@@ -33,6 +33,7 @@ from mediagoblin.tools.response import render_to_response, redirect
 from mediagoblin.tools.translate import pass_to_ugettext as _
 from mediagoblin.tools.text import (
     convert_to_tag_list_of_dicts, media_tags_as_string)
+from mediagoblin.tools.url import slugify
 from mediagoblin.db.util import check_media_slug_used, check_collection_slug_used
 
 import mimetypes
@@ -58,22 +59,20 @@ def edit_media(request, media):
     if request.method == 'POST' and form.validate():
         # Make sure there isn't already a MediaEntry with such a slug
         # and userid.
-        slug_used = check_media_slug_used(media.uploader, request.form['slug'],
-                                          media.id)
+        slug = slugify(request.form['slug'])
+        slug_used = check_media_slug_used(media.uploader, slug, media.id)
 
         if slug_used:
             form.slug.errors.append(
                 _(u'An entry with that slug already exists for this user.'))
         else:
-            media.title = unicode(request.form['title'])
-            media.description = unicode(request.form.get('description'))
+            media.title = request.form['title']
+            media.description = request.form.get('description')
             media.tags = convert_to_tag_list_of_dicts(
                                    request.form.get('tags'))
 
             media.license = unicode(request.form.get('license', '')) or None
-
-            media.slug = unicode(request.form['slug'])
-
+            media.slug = slug
             media.save()
 
             return redirect(request,
@@ -192,8 +191,8 @@ def edit_profile(request, url_user=None):
     user = url_user
 
     form = forms.EditProfileForm(request.form,
-        url=user.get('url'),
-        bio=user.get('bio'))
+        url=user.url,
+        bio=user.bio)
 
     if request.method == 'POST' and form.validate():
         user.url = unicode(request.form['url'])
@@ -219,45 +218,42 @@ def edit_profile(request, url_user=None):
 def edit_account(request):
     user = request.user
     form = forms.EditAccountForm(request.form,
-        wants_comment_notification=user.get('wants_comment_notification'))
+        wants_comment_notification=user.wants_comment_notification,
+        license_preference=user.license_preference)
 
     if request.method == 'POST':
         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'))
+        if form_validated and \
+                form.wants_comment_notification.validate(form):
+            user.wants_comment_notification = \
+                form.wants_comment_notification.data
+
+        if form_validated and \
+                form.new_password.data or form.old_password.data:
+            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)
+            else:
+                form.old_password.errors.append(_('Wrong password'))
+
+        if form_validated and \
+                form.license_preference.validate(form):
+            user.license_preference = \
+                form.license_preference.data
+
+        if form_validated and not form.errors:
+            user.save()
+            messages.add_message(request,
+                messages.SUCCESS,
+                _("Account settings saved"))
+            return redirect(request,
+                'mediagoblin.user_pages.user_home',
+                user=user.username)
 
     return render_to_response(
         request,