# Path: qiniuyun/models.py
# class ImageAtQiniu(models.Model):
#     fullname = models.CharField(max_length=120,unique=True)
#     
#     def __unicode__(self):
#         return self.fullname
# 
# Path: qiniuyun/backend.py
# class QiniuPush(QiniuStorage):
#     '''
#     根据settings中的QINIU_CONF配置七牛云的密钥、空间、域名等
#     直接调用该类的类方法即可进行文件上传、显示、删除
#     各方法的详细介绍见父类QiniuStorage
#     @auth:ZWJ
#     '''
#     def __init__(self):
#         super(QiniuPush,self).__init__(**settings.QINIU_CONF)
#             
#     @classmethod
#     def put_data(cls,name,data):
#         '''
#         上传内存中的文件流到七牛并返回上传后的url地址
#         example:
#             f=request.FILES['photo']
#             u.headImg=QiniuPush.put_data(f.name, f)
#             u.save() #保存到数据库
#             -----------------
#             #another def 
#             uuu = User.objects.get(username=request.session['user_name'])
#             uuu.headImg=QiniuPush.private_download_url(uuu.headImg) #生成 
#             return render(request, 'account/signup_result.html', {'user': uuu})
#         '''
#         qn=cls()
#         return super(QiniuPush,qn).put_data(name,data)
# 
#     @classmethod
#     def private_download_url(cls,url,expires=7200):
#         '''生成私有资源下载链接'''
#         qn=cls()
#         return super(QiniuPush,qn).private_download_url(url,expires)
#         
#     @classmethod            
#     def put_file(cls,filePath):
#         '''上传本地文件到七牛并返回上传后的url地址'''
#         qn=cls()
#         return super(QiniuPush,qn).put_file(filePath)
#         
#     @classmethod
#     def exists(cls,key):        
#         '''检测七牛云上是否有文件名为key的文件'''
#         qn=cls()
#         return super(QiniuPush,qn).exists(key)  
# 
#     @classmethod
#     def get_url(cls,key): 
#         '''返回七牛云上文件名为key的文件对应的url地址'''   
#         qn=cls()
#         return super(QiniuPush,qn).get_url(key)
#                 
#     @classmethod
#     def delete(cls,key):  
#         '''删除七牛云上文件名为key的文件'''
#         qn=cls()
#         return super(QiniuPush,qn).delete(key) 
#           
#     @classmethod
#     def ls_files(cls,prefix="", limit=100):
#         '''列出七牛云上的文件(前100个)'''
#         qn=cls()
#         return super(QiniuPush,qn).ls_files()


from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from .models import ImageAtQiniu
from qiniuyun.backend import QiniuPush as QP
#coding=utf-8

class UploadForm(forms.Form):
    name = forms.CharField(required=False)
    img = forms.FileField()

def upload(request):
    if request.method == 'POST':
        uf = UploadForm(request.POST, request.FILES)
        if uf.is_valid():
            uname = uf.cleaned_data['name']
            hImg = uf.cleaned_data['img']  
            if not uname.strip():         
                uname=hImg.name            
            u = ImageAtQiniu()
            u.fullname = QP.put_data(uname,hImg)                       
            u.save()
            request.session['img_fullname'] = u.fullname
            return HttpResponseRedirect('/upload/done/')
    else:
        uf = UploadForm()
        uf.img=QP.private_download_url(QP.get_url('default.jpg'))
    return render(request, 'qiniuyun/upload.html', {'uf': uf})

def upload_result(request):
    name=request.session['img_fullname']

# Path: qiniuyun/models.py
# class ImageAtQiniu(models.Model):
#     fullname = models.CharField(max_length=120,unique=True)
#     
#     def __unicode__(self):
#         return self.fullname
# 
# Path: qiniuyun/backend.py
# class QiniuPush(QiniuStorage):
#     '''
#     根据settings中的QINIU_CONF配置七牛云的密钥、空间、域名等
#     直接调用该类的类方法即可进行文件上传、显示、删除
#     各方法的详细介绍见父类QiniuStorage
#     @auth:ZWJ
#     '''
#     def __init__(self):
#         super(QiniuPush,self).__init__(**settings.QINIU_CONF)
#             
#     @classmethod
#     def put_data(cls,name,data):
#         '''
#         上传内存中的文件流到七牛并返回上传后的url地址
#         example:
#             f=request.FILES['photo']
#             u.headImg=QiniuPush.put_data(f.name, f)
#             u.save() #保存到数据库
#             -----------------
#             #another def 
#             uuu = User.objects.get(username=request.session['user_name'])
#             uuu.headImg=QiniuPush.private_download_url(uuu.headImg) #生成 
#             return render(request, 'account/signup_result.html', {'user': uuu})
#         '''
#         qn=cls()
#         return super(QiniuPush,qn).put_data(name,data)
# 
#     @classmethod
#     def private_download_url(cls,url,expires=7200):
#         '''生成私有资源下载链接'''
#         qn=cls()
#         return super(QiniuPush,qn).private_download_url(url,expires)
#         
#     @classmethod            
#     def put_file(cls,filePath):
#         '''上传本地文件到七牛并返回上传后的url地址'''
#         qn=cls()
#         return super(QiniuPush,qn).put_file(filePath)
#         
#     @classmethod
#     def exists(cls,key):        
#         '''检测七牛云上是否有文件名为key的文件'''
#         qn=cls()
#         return super(QiniuPush,qn).exists(key)  
# 
#     @classmethod
#     def get_url(cls,key): 
#         '''返回七牛云上文件名为key的文件对应的url地址'''   
#         qn=cls()
#         return super(QiniuPush,qn).get_url(key)
#                 
#     @classmethod
#     def delete(cls,key):  
#         '''删除七牛云上文件名为key的文件'''
#         qn=cls()
#         return super(QiniuPush,qn).delete(key) 
#           
#     @classmethod
#     def ls_files(cls,prefix="", limit=100):
#         '''列出七牛云上的文件(前100个)'''
#         qn=cls()
#         return super(QiniuPush,qn).ls_files()


# Path: qiniuyun/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django import forms
from .models import ImageAtQiniu
from qiniuyun.backend import QiniuPush as QP

#coding=utf-8

class UploadForm(forms.Form):
    name = forms.CharField(required=False)
    img = forms.FileField()

def upload(request):
    if request.method == 'POST':
        uf = UploadForm(request.POST, request.FILES)
        if uf.is_valid():
            uname = uf.cleaned_data['name']
            hImg = uf.cleaned_data['img']  
            if not uname.strip():         
                uname=hImg.name            
            u = ImageAtQiniu()
            u.fullname = QP.put_data(uname,hImg)                       
            u.save()
            request.session['img_fullname'] = u.fullname
            return HttpResponseRedirect('/upload/done/')
    else:
        uf = UploadForm()
        uf.img=QP.private_download_url(QP.get_url('default.jpg'))
    return render(request, 'qiniuyun/upload.html', {'uf': uf})

def upload_result(request):
    name=request.session['img_fullname']

    img_url=QP.private_download_url(name)