# Copyright 2010 Hudarsono <http://blog.hudarsono.me>## This file is part of MeBlog.## MeBlog is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## MeBlog is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with MeBlog. If not, see <http://www.gnu.org/licenses/>.importurllibfromdjango.httpimportHttpResponseRedirect,Http404,HttpResponsefromdjango.shortcutsimportrender_to_responsefromdjango.confimportsettingsfromdjango.views.decorators.csrfimportcsrf_exemptfromgoogle.appengine.extimportblobstorefromgoogle.appengine.apiimportmemcachefrommediaimportmodelsfromfileformimportFileFormfromutilitiesimportblob_helperfromutilities.auth_helperimportlogin_requiredADMIN_PAGESIZE=settings.PAGESIZE@login_requireddeflistMedia(request):ifrequest.GET.get('page'):page=int(request.GET.get('page'))else:page=1offset=ADMIN_PAGESIZE*(page-1)ifmemcache.get('mediapage-'+str(page)):list=memcache.get('mediapage-'+str(page))else:list=models.Media.all().order('-created').fetch(ADMIN_PAGESIZE,offset)memcache.set('mediapage-'+str(page),list)# check if there is next pageoffset=ADMIN_PAGESIZE*(page)next_page=models.Media.all().order('-created').fetch(ADMIN_PAGESIZE,offset)ifnext_page:p_next=page+1else:p_next=Nonepaging={'prev':page-1,'next':p_next}returnrender_to_response('admin/medialist.html',{'list':list,'paging':paging})@login_requireddefdelMedia(request,key):blob_key=str(urllib.unquote(key))blobstore.delete(blob_key)memcache.delete('mediapage-1')returnHttpResponseRedirect('/media/')@csrf_exempt@login_requireddefupload(request):form=Noneifrequest.method=='POST':blob_info=blob_helper.get_uploads(request,field_name='media',populate_post=True)form=FileForm(request.POST)ifform.is_valid()andlen(blob_info)==1:media_item=models.Media(title=form.cleaned_data['title'],media=blob_info[0],filename=blob_info[0].filename,filesize=blob_info[0].size,type=blob_info[0].content_type)media_item.put()memcache.flush_all()returnHttpResponseRedirect('/media/')iflen(media_blobs)==0:request.session['upload_error']="Media file is required"form=FileForm(request.POST)ifformisNone:form=FileForm()upload_url=blobstore.create_upload_url('/media/upload/')returnrender_to_response('admin/upload.html',{'upload_url':upload_url,'upload_error':request.session.pop('upload_error',None),'form':form})defserve(request,key):media=models.Media.get(key)returnblob_helper.send_blob(request,media.media,save_as=False)defdownload(request,key):media=models.Media.get(key)returnblob_helper.send_blob(request,media.media,save_as=True)