def test_databases(request): set = request.GET['set'] if set == 'true': f = MediaFile(file_id='testfileid', user_id=1, filename='testfilename', upload_time=datetime.utcnow(), view_count=0) #time.ctime #datetime.datetime f.save() retr = MediaFile.objects.all()[0] file_id = retr.file_id user_id = retr.user_id filename = retr.filename upload_time = retr.upload_time view_count = retr.view_count return render_to_response( 'test-databases.html', { 'file_id': file_id, 'user_id': user_id, 'filename': filename, 'upload_time': upload_time, 'view_count': view_count })
def media_add_ajax(request): """ This is the end point for the ajax file uploads. It will always return "application/json". """ if request.method == 'POST': uploaded_files = [] for key, upload in request.FILES.items(): media_file = MediaFile() media_file.title = request.POST.get('title') media_file.slug = slugify(media_file.title) media_file.caption = request.POST.get('caption') media_file.created_by = request.user media_file.media_file = upload if upload.content_type in ['image/jpeg', 'image/png', 'image/gif']: media_file.thumbnail = upload media_file.thumbnail_medium = upload media_file.save() if media_file.thumbnail == '': thumbnail_url = STATIC_URL + 'img/default_thumbnail.png' else: thumbnail_url = media_file.thumbnail.url uploaded_files.append({ 'file_name': media_file.media_file.name, 'pk': media_file.pk, 'thumbnail_url': thumbnail_url, 'title': media_file.title, }) # Remember, this has to be JSON serializable ret_json = { 'files': uploaded_files, 'success': True, } response = HttpResponse( json.dumps(ret_json), content_type="application/json" ) response['Content-Disposition'] = 'inline; filename="files.json"' return response else: return HttpResponseRedirect(reverse('thathweb.media.views.media_add'))
def media_add_ajax(request): """ This is the end point for the ajax file uploads. It will always return "application/json". """ if request.method == 'POST': uploaded_files = [] for key, upload in request.FILES.items(): media_file = MediaFile() media_file.title = request.POST.get('title') media_file.slug = slugify(media_file.title) media_file.caption = request.POST.get('caption') media_file.created_by = request.user media_file.media_file = upload if upload.content_type in ['image/jpeg', 'image/png', 'image/gif']: media_file.thumbnail = upload media_file.thumbnail_medium = upload media_file.save() if media_file.thumbnail == '': thumbnail_url = STATIC_URL + 'img/default_thumbnail.png' else: thumbnail_url = media_file.thumbnail.url uploaded_files.append({ 'file_name': media_file.media_file.name, 'pk': media_file.pk, 'thumbnail_url': thumbnail_url, 'title': media_file.title, }) # Remember, this has to be JSON serializable ret_json = { 'files': uploaded_files, 'success': True, } response = HttpResponse(json.dumps(ret_json), content_type="application/json") response['Content-Disposition'] = 'inline; filename="files.json"' return response else: return HttpResponseRedirect(reverse('thathweb.media.views.media_add'))
def test_databases(request): set = request.GET['set'] if set == 'true': f = MediaFile(file_id='testfileid', user_id=1, filename='testfilename', upload_time=datetime.utcnow(), view_count=0)#time.ctime #datetime.datetime f.save() retr = MediaFile.objects.all()[0] file_id = retr.file_id user_id = retr.user_id filename = retr.filename upload_time = retr.upload_time view_count = retr.view_count return render_to_response('test-databases.html', {'file_id':file_id,'user_id':user_id,'filename':filename,'upload_time':upload_time,'view_count':view_count})
def upload_form_parameters(bucketname, user_id): file_id = generate_unique_id() # Put together a key s3_key = 'u/%s' % (file_id,) # Expiry date string in ISO8601 GMT format, one hour in the future: expiry = (datetime.utcnow() + timedelta(hours=1)).isoformat()+'Z' logging.debug("Constructing policy string...") # Construct policy string from JSON. This ensures that if the user tries something sneaky such as # altering the hidden form fields, then the upload will be rejected. policy_str = simplejson.dumps({ 'expiration': expiry, 'conditions': [ {'key': s3_key}, {'acl': 'private'}, {'bucket': 's3.mediasnak.com'}, ['starts-with', '$Content-Disposition', 'inline; filename='], ['starts-with', '$Content-Type', ''], {'success_action_redirect': 'http://%s/success' % (os.environ['HTTP_HOST'],)}, ['starts-with', '$x-amz-meta-filename', ''], {'x-amz-meta-userid': user_id} ] }) logging.debug("Encoding base64...") policy = b64encode(policy_str.encode('utf8')) logging.debug("Signing policy...") signature = hmac_sign(policy) # Sign the policy logging.debug("Making database entry...") # Save the file ID in the database, so that we can match it up when the upload is finished # and also ensure it was a valid upload # The filename, upload time and view count will be filled in later (just setting upload time to the current time for now) # User ID may also be checked later file_entry = MediaFile(file_id=file_id, uploaded=False, user_id=user_id, filename='', upload_time=datetime.utcnow(), view_count=0, has_thumb=False) file_entry.save() logging.debug("Returning values...") return { 'key': s3_key, 'aws_id': access_keys.key_id, 'policy': policy, 'signature': signature, 'return_host': os.environ['HTTP_HOST'], 'user_id': user_id }