Example #1
0
def upload_photo(request,album_id):
    """上传照片"""
    schools = get_schools(request.user)
    album = Album.objects.get(pk=album_id,site__school__in=schools)
    if request.method == 'POST':
        f = request.FILES["img"]
        chunks = int(request.POST.get('chunks'))
        if chunks == 1:
            form = PhotoForm(request.POST,request.FILES)
            if form.is_valid():
                photo = Photo()
                photo.img = f
                photo.creator = request.user
                photo.album = album
                photo.is_show = True
                photo.save()
                if photo.id:
                    data = json.dumps({'status':1,'desc':"ok"})
                    return HttpResponse(data)
        else:
            chunk = int(request.POST.get('chunk'))
            file_id = request.POST.get('file_id')
            name = request.POST.get('name')
            
            file_path = FILE_PATH + '/temp/' + str(file_id)
            print file_path,'ppppppppppppppppppppp'
            fp = open(file_path,"a+b")
            fp.write(f.read())
            fp.close()
            
            if chunk + 1  == chunks:
                filename = 'photo/' + str(file_id) + '.' +  name.split('.')[-1].lower()
                try:
                    URL('http://' + SITE_INFO.domain + reverse('cron_make_large_img')).post_async(filename=filename,file_path=file_path)
                except:
                    fr = open(file_path,"rb")
                    content = fr.read()
                    fr.close()
                    if os.path.isfile(file_path):
                        os.remove(file_path)
                    AliyunStorage(). _put_file(filename, content)
                    
                photo = Photo()
                photo.img = filename
                photo.creator = request.user
                photo.album = album
                photo.is_show = True
                photo.save()
                data = json.dumps({'status':1,'desc':"ok"})
                return HttpResponse(data)

            else:
                return HttpResponse(json.dumps({'status':1,'desc':"ok"}))
            data = json.dumps({'status':0,'desc':"error"})
            return HttpResponse(data) 
    return redirect(reverse('oa_album_detail',kwargs={'album_id':album.id}))
Example #2
0
def update_photo(request,photo_id):
    """更新相片"""
    schools = get_schools(request.user)
    photo = get_object_or_404(Photo,id=photo_id,album__site__school__in=schools)
    if request.method == 'POST':
        form = PhotoForm(request.POST,instance=photo)
        if form.is_valid():
            form.save()
            messages.success(request, u'修改成功')
    return redirect(reverse('oa_album_detail',kwargs={'album_id':photo.album_id}))