Add a license preference field
authorMark Holmquist <mtraceur@member.fsf.org>
Sun, 11 Nov 2012 00:59:37 +0000 (16:59 -0800)
committerSebastian Spaeth <Sebastian@SSpaeth.de>
Thu, 17 Jan 2013 20:34:04 +0000 (21:34 +0100)
This feature is absolutely necessary. Now a user can simply define
their default license and quickly go through a form, as opposed to
stopping to click on the select and choosing the same option over
and over again.

Also added DB migration for the field, so that's working now, too.

Rebased by Sebastian and made the default value to be unicode.

Reviewed-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
mediagoblin/db/migrations.py
mediagoblin/db/models.py
mediagoblin/edit/forms.py
mediagoblin/edit/views.py
mediagoblin/submit/views.py
mediagoblin/templates/mediagoblin/edit/edit_account.html

index 80ec5269a24123c3ddfa388fb2b52e5e8ae73e47..3f43c78928152efabca11325c106145190e70253 100644 (file)
@@ -184,3 +184,15 @@ def fix_CollectionItem_v0_constraint(db_conn):
         pass
 
     db_conn.commit()
+
+
+@RegisterMigration(8, MIGRATIONS)
+def add_license_preference(db):
+    metadata = MetaData(bind=db.bind)
+
+    user_table = Table('core__users', metadata, autoload=True,
+            autoload_with=db.bind)
+
+    col = Column('license_preference', Unicode, default=u'')
+    col.create(user_table)
+    db.commit()
index 782bf8692d37ce916e407e877427bae2242b588d..7e2cc7d20dd831a14ca0172d0e18f2721dcbe1c6 100644 (file)
@@ -63,6 +63,7 @@ class User(Base, UserMixin):
     # Intented to be nullable=False, but migrations would not work for it
     # set to nullable=True implicitly.
     wants_comment_notification = Column(Boolean, default=True)
+    license_preference = Column(Unicode)
     verification_key = Column(Unicode)
     is_admin = Column(Boolean, default=False, nullable=False)
     url = Column(Unicode)
index 293c3bb2c37ae8352b5835dabecbd63a95d0ea3e..2673967bce69e18474c86b6a16d01c7c9fc9b6a7 100644 (file)
@@ -65,8 +65,19 @@ class EditAccountForm(wtforms.Form):
             "Enter your old password to prove you own this account."))
     new_password = wtforms.PasswordField(
         _('New password'),
-        [wtforms.validators.Length(min=6, max=30)],
+        [
+            wtforms.validators.Optional(),
+            wtforms.validators.Length(min=6, max=30)
+        ],
         id="password")
+    license_preference = wtforms.SelectField(
+        _('License preference'),
+        [
+            wtforms.validators.Optional(),
+            wtforms.validators.AnyOf([lic[0] for lic in licenses_as_choices()]),
+        ],
+        choices=licenses_as_choices(),
+        description=_('This will be your default license on upload forms.'))
     wants_comment_notification = wtforms.BooleanField(
         label=_("Email me when others comment on my media"))
 
index c656c63f497664cb91ab4a96d52b38c8f3024d53..88af22d85843b4b7f27d049cafc5ac5f4638ff3e 100644 (file)
@@ -219,45 +219,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.get('wants_comment_notification'),
+        license_preference=user.get('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,
index 2d609b31e2528a729c33a6823b23105c3dd7d603..38adf85f2d1bea384d538729a67aef2a194d8e95 100644 (file)
@@ -41,7 +41,8 @@ def submit_start(request):
     """
     First view for submitting a file.
     """
-    submit_form = submit_forms.SubmitStartForm(request.form)
+    submit_form = submit_forms.SubmitStartForm(request.form,
+        license=request.user.get('license_preference'))
 
     if request.method == 'POST' and submit_form.validate():
         if not ('file' in request.files
index cfeb72816d540903f36ceaf763236d2112157f25..89f559af9053b3cded4d6e5b3fa11773e8f356ec 100644 (file)
         <p>{{ form.wants_comment_notification }}
            {{ form.wants_comment_notification.label }}</p>
       </div>
+      <div class="form_field_input">
+        <p>{{ form.license_preference }}
+           {{ form.license_preference.label }}</p>
+      </div>
       <div class="form_submit_buttons">
         <input type="submit" value="{% trans %}Save changes{% endtrans %}" class="button_form" />
        {{ csrf_token }}