Commits

Anonymous committed 0f1eaae

fix media upload

  • Participants
  • Parent commits ccd4900

Comments (0)

Files changed (47)

File appengine_django/__init__.pyc

Binary file modified.

File appengine_django/db/__init__.pyc

Binary file modified.

File appengine_django/db/base.pyc

Binary file modified.

File appengine_django/db/creation.pyc

Binary file modified.

File appengine_django/mail.pyc

Binary file modified.

File appengine_django/models.pyc

Binary file modified.

File appengine_django/replacement_imp.pyc

Binary file modified.

File appengine_django/serializer/__init__.pyc

Binary file modified.

File appengine_django/serializer/json.pyc

Binary file modified.

File appengine_django/serializer/python.pyc

Binary file modified.

File appengine_django/serializer/pyyaml.pyc

Binary file modified.

File appengine_django/sessions/backends/__init__.pyc

Binary file modified.

File context/__init__.pyc

Binary file modified.

File context/context_processors.pyc

Binary file modified.

File gaesessions/__init__.pyc

Binary file modified.

File markdown/__init__.pyc

Binary file modified.

File markdown/blockparser.pyc

Binary file modified.

File markdown/blockprocessors.pyc

Binary file modified.

File markdown/etree_loader.pyc

Binary file modified.

File markdown/html4.pyc

Binary file modified.

File markdown/inlinepatterns.pyc

Binary file modified.

File markdown/odict.pyc

Binary file modified.

File markdown/postprocessors.pyc

Binary file modified.

File markdown/preprocessors.pyc

Binary file modified.

File markdown/treeprocessors.pyc

Binary file modified.

File media/models.py

 	title = db.StringProperty()
 	created = db.DateTimeProperty(auto_now_add=True)
 	media = blobstore.BlobReferenceProperty()
-	

File media/views.py

 from django.http import HttpResponseRedirect, Http404, HttpResponse
 from django.shortcuts import render_to_response
 from google.appengine.ext import blobstore
-import cgi
-from models import Media
+from media import models
 from fileform import FileForm
-
-
-def get_uploads(request, field_name=None, populate_post=False):
-    """Get uploads sent to this handler.
-    Args:
-      field_name: Only select uploads that were sent as a specific field.
-      populate_post: Add the non blob fields to request.POST
-    Returns:
-      A list of BlobInfo records corresponding to each upload.
-      Empty list if there are no blob-info records for field_name.
-    """
-    
-    if hasattr(request,'__uploads') == False:
-        request.META['wsgi.input'].seek(0)
-        fields = cgi.FieldStorage(request.META['wsgi.input'], environ=request.META)
-        
-        request.__uploads = {}
-        if populate_post:
-            request.POST = {}
-        
-        for key in fields.keys():
-            field = fields[key]
-            if isinstance(field, cgi.FieldStorage) and 'blob-key' in field.type_options:
-                request.__uploads.setdefault(key, []).append(blobstore.parse_blob_info(field))
-            elif populate_post:
-                request.POST[key] = field.value
-    if field_name:
-        try:
-            return list(request.__uploads[field_name])
-        except KeyError:
-            return []
-    else:
-        results = []
-        for uploads in request.__uploads.itervalues():
-            results += uploads
-        return results
-
+from utilities import blob_helper
 
 def upload(request):
-	form = None
-	if request.method =='POST':
-		media_blobs = get_uploads(request, field_name='media', populate_post=True)
-		form = FileForm(request.POST)
-		if form.is_valid() and len(media_blobs) == 1:
-			media_item = Media(name=form.cleaned_data['name'],
-								media=media_blobs[0])
-			media_item.put()
-			return HttpResponse('success')
-		
-		if len(media_blobs) == 0:
-			request.session['upload_error'] = "Media file is required"
-		
-		form = FileForm(request.POST)
-            
-	if form is None: form = FileForm()
-	upload_url = blobstore.create_upload_url('/media/upload')
-	return render_to_response('admin/upload.html', {'upload_url':upload_url,
+    form = None
+    if request.method =='POST':
+        blob_info = blob_helper.get_uploads(request, field_name='media', populate_post=True)
+        form = FileForm(request.POST)
+        if form.is_valid() and len(blob_info) == 1:
+            media_item = models.Media(title=form.cleaned_data['title'],
+                                   media=blob_info[0])
+            media_item.put()
+            return HttpResponseRedirect('/posts/')
+
+        if len(media_blobs) == 0:
+            request.session['upload_error'] = "Media file is required"
+
+        form = FileForm(request.POST)
+
+    if form is None: form = FileForm()
+    upload_url = blobstore.create_upload_url('/media/upload/')
+    return render_to_response('admin/upload.html', {'upload_url':upload_url,
 													'upload_error': request.session.pop('upload_error', None),
 													'form': form})

File pages/__init__.pyc

Binary file modified.

File pages/contactform.pyc

Binary file modified.

File pages/models.pyc

Binary file modified.

File pages/pageform.pyc

Binary file modified.

File pages/views.pyc

Binary file modified.

File posts/__init__.pyc

Binary file modified.

File posts/models.pyc

Binary file modified.

File posts/postform.pyc

Binary file modified.

File posts/views.pyc

Binary file modified.

File pygments/__init__.pyc

Binary file modified.

File pygments/util.pyc

Binary file modified.

File resources/css/theme.css

 }
 
 #main-content .content{
-	padding:5px;
+	padding:10px;
 	margin-right:5px;
 	background: rgba(255,255,255,0.7);
 	-moz-border-radius:6px;

File settings.pyc

Binary file modified.

File templates/admin/upload.html

 {% block content %}
 	<form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
 	{% if upload_error %}<span>{{ upload_error }}</span>{% endif %}
-	
+
 	{{ form.as_p }}
 	<p>
 	<label for="fileupload">Upload Media :</label>
-	<input type="file" name="media"><br>
+	<input type="file" name="media"/><br>
 	</p>
 	<p>
 	<input class="submit" type="submit" name="submit" value="Upload">

File urls.pyc

Binary file modified.

File utilities/appengine.py

-from django.http import HttpResponsePermanentRedirect
-from google.appengine.api import users
-
-def loginrequired(func):
-    def redirect_to_login(request):
-        return HttpResponsePermanentRedirect(users.create_login_url(request.get_full_path()))
-
-    user = users.get_current_user()
-    if user:
-        return func
-    else:
-        return redirect_to_login

File utilities/auth_helper.py

+from django.http import HttpResponsePermanentRedirect
+from google.appengine.api import users
+
+def loginrequired(func):
+    def redirect_to_login(request):
+        return HttpResponsePermanentRedirect(users.create_login_url(request.get_full_path()))
+
+    user = users.get_current_user()
+    if user:
+        return func
+    else:
+        return redirect_to_login

File utilities/blob_helper.py

+import cgi
+from google.appengine.ext import blobstore
+
+
+def get_uploads(request, field_name=None, populate_post=False):
+    """Get uploads sent to this handler.
+    Args:
+      field_name: Only select uploads that were sent as a specific field.
+      populate_post: Add the non blob fields to request.POST
+    Returns:
+      A list of BlobInfo records corresponding to each upload.
+      Empty list if there are no blob-info records for field_name.
+    """
+    if hasattr(request,'__uploads') == False:
+        request.META['wsgi.input'].seek(0)
+        fields = cgi.FieldStorage(request.META['wsgi.input'], environ=request.META)
+
+        request.__uploads = {}
+        if populate_post:
+            request.POST = {}
+
+        for key in fields.keys():
+            field = fields[key]
+            if isinstance(field, cgi.FieldStorage) and 'blob-key' in field.type_options:
+                request.__uploads.setdefault(key, []).append(blobstore.parse_blob_info(field))
+            elif populate_post:
+                request.POST[key] = field.value
+    if field_name:
+        try:
+            return list(request.__uploads[field_name])
+        except KeyError:
+            return []
+    else:
+        results = []
+        for uploads in request.__uploads.itervalues():
+            results += uploads
+        return results
+
+
+
+def send_blob(request, blob_key_or_info, content_type=None, save_as=None):
+    """Send a blob-response based on a blob_key.
+
+    Sets the correct response header for serving a blob.  If BlobInfo
+    is provided and no content_type specified, will set request content type
+    to BlobInfo's content type.
+
+    Args:
+      blob_key_or_info: BlobKey or BlobInfo record to serve.
+      content_type: Content-type to override when known.
+      save_as: If True, and BlobInfo record is provided, use BlobInfos
+        filename to save-as.  If string is provided, use string as filename.
+        If None or False, do not send as attachment.
+
+      Raises:
+        ValueError on invalid save_as parameter.
+    """
+
+    CONTENT_DISPOSITION_FORMAT = 'attachment; filename="%s"'
+    if isinstance(blob_key_or_info, blobstore.BlobInfo):
+      blob_key = blob_key_or_info.key()
+      blob_info = blob_key_or_info
+    else:
+      blob_key = blob_key_or_info
+      blob_info = None
+
+    logging.debug(blob_info)
+    response = HttpResponse()
+    response[blobstore.BLOB_KEY_HEADER] = str(blob_key)
+
+    if content_type:
+      if isinstance(content_type, unicode):
+        content_type = content_type.encode('utf-8')
+      response['Content-Type'] = content_type
+    else:
+      del response['Content-Type']
+
+    def send_attachment(filename):
+      if isinstance(filename, unicode):
+        filename = filename.encode('utf-8')
+      response['Content-Disposition'] = (CONTENT_DISPOSITION_FORMAT % filename)
+
+    if save_as:
+      if isinstance(save_as, basestring):
+        send_attachment(save_as)
+      elif blob_info and save_as is True:
+        send_attachment(blob_info.filename)
+      else:
+        if not blob_info:
+          raise ValueError('Expected BlobInfo value for blob_key_or_info.')
+        else:
+          raise ValueError('Unexpected value for save_as')
+
+    return response

File utilities/log_helper.py

+def log_traceback(exception, args):
+    import sys, traceback, logging
+    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
+    logging.debug(exception)
+    logging.debug(args)
+    for tb in traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback):
+        logging.debug(tb)

File utilities/logging.py

-def log_traceback(exception, args):
-    import sys, traceback, logging
-    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
-    logging.debug(exception)
-    logging.debug(args)
-    for tb in traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback):
-        logging.debug(tb)