Properly require files when users submit
authorChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 23 Apr 2011 19:13:33 +0000 (14:13 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sat, 23 Apr 2011 19:13:33 +0000 (14:13 -0500)
mediagoblin/submit/views.py

index 1f55336f0d2e9c806d5c9fa20c19eac8a6814887..926c7011fd38074ae3ea9716bc3079e3187f69df 100644 (file)
@@ -15,6 +15,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+from cgi import FieldStorage
+
 from webob import Response, exc
 from werkzeug.utils import secure_filename
 
@@ -30,38 +32,44 @@ def submit_start(request):
     submit_form = submit_forms.SubmitStartForm(request.POST)
 
     if request.method == 'POST' and submit_form.validate():
-        # create entry and save in database
-        entry = request.db.MediaEntry()
-        entry['title'] = request.POST['title']
-        entry['description'] = request.POST.get(['description'])
-        entry['media_type'] = u'image' # heh
-        entry['uploader'] = request.user
-
-        # Save, just so we can get the entry id for the sake of using
-        # it to generate the file path
-        entry.save(validate=False)
-
-        # Now store generate the queueing related filename
-        queue_filepath = request.app.queue_store.get_unique_filepath(
-            ['media_entries',
-             unicode(request.user['_id']),
-             unicode(entry['_id']),
-             secure_filename(request.POST['file'].filename)])
-
-        # queue appropriately
-        queue_file = request.app.queue_store.get_file(
-            queue_filepath, 'wb')
-
-        with queue_file:
-            queue_file.write(request.POST['file'].file.read())
-
-        # Add queued filename to the entry
-        entry.setdefault('queue_files', []).append(queue_filepath)
-        entry.save(validate=True)
-
-        # redirect
-        return exc.HTTPFound(
-            location=request.urlgen("mediagoblin.submit.success"))
+        if not (request.POST.has_key('file')
+                and isinstance(request.POST['file'], FieldStorage)
+                and request.POST['file'].file):
+            submit_form.file.errors.append(
+                u'You must provide a file.')
+        else:
+            # create entry and save in database
+            entry = request.db.MediaEntry()
+            entry['title'] = request.POST['title']
+            entry['description'] = request.POST.get(['description'])
+            entry['media_type'] = u'image' # heh
+            entry['uploader'] = request.user
+
+            # Save, just so we can get the entry id for the sake of using
+            # it to generate the file path
+            entry.save(validate=False)
+
+            # Now store generate the queueing related filename
+            queue_filepath = request.app.queue_store.get_unique_filepath(
+                ['media_entries',
+                 unicode(request.user['_id']),
+                 unicode(entry['_id']),
+                 secure_filename(request.POST['file'].filename)])
+
+            # queue appropriately
+            queue_file = request.app.queue_store.get_file(
+                queue_filepath, 'wb')
+
+            with queue_file:
+                queue_file.write(request.POST['file'].file.read())
+
+            # Add queued filename to the entry
+            entry.setdefault('queue_files', []).append(queue_filepath)
+            entry.save(validate=True)
+
+            # redirect
+            return exc.HTTPFound(
+                location=request.urlgen("mediagoblin.submit.success"))
 
     # render
     template = request.template_env.get_template(