def commonDel(request): """models to delete""" if request.method == "POST": str_model = request.POST.get('model') id = request.POST.get('id') obj_content = ContentType.objects.get(model=str_model) if str_model == 'pictype': # delete pic img = [] img += MyPic.objects.filter(type=id).values_list('img', flat=True) img.append(PicType.objects.get(pk=id).img) keys = Pic.objects.filter(id__in=img).values_list('key', flat=True) # delete remote file qn = SuperQiniu(keys) qn.delMoreFiles() # delete models MyPic.objects.filter(type=id).delete() if str_model == 'pic': MyPic.objects.filter(img=id).delete() PicType.objects.filter(img=id).delete() picture = Pic.objects.get(pk=id) qn = SuperQiniu(picture.key) qn.delFile() # 删除远程图片 if str_model == 'wikitype': # delete wiki Wiki.objects.filter(category=id).delete() obj = obj_content.get_object_for_this_type(pk=id).delete() return HttpResponse('ok')
def setAvatar(request): """更改头像""" context = {} user = request.user context['form'] = AvatarForm() context['pic'] = ['/site_media/avatar/%s.jpg' % str(i) for i in range(1, 36)] if request.method == 'POST': form = AvatarForm(request.POST, request.FILES) if form.is_valid(): avatar = request.FILES['avatar'] qiniu = SuperQiniu(avatar) # 实例化超级七牛类 qiniu.uploadFile() # 上传文件 url = qiniu.downloadFile() # 返回url user.avatar = url user.save() # return HttpResponseRedirect('/manage/success/?url=%s' % str(request.path)) context['form'] = form return render(request, 'manager/user/avatar.html', context)
def setAvatar(request): """更改头像""" context = {} user = request.user context['form'] = AvatarForm() context['pic'] = [ '/site_media/avatar/%s.jpg' % str(i) for i in range(1, 36) ] if request.method == 'POST': form = AvatarForm(request.POST, request.FILES) if form.is_valid(): avatar = request.FILES['avatar'] qiniu = SuperQiniu(avatar) # 实例化超级七牛类 qiniu.uploadFile() # 上传文件 url = qiniu.downloadFile() # 返回url user.avatar = url user.save() # return HttpResponseRedirect('/manage/success/?url=%s' % str(request.path)) context['form'] = form return render(request, 'manager/user/avatar.html', context)
def UploadPic(request): """upload pic to qiniu.""" if request.method == 'POST': img = request.FILES.get('Filedata', None) type = request.POST.get('type', None) if type: qn = SuperQiniu(img, w=800, h=520) else: qn = SuperQiniu(img) qn.uploadFile() remote_url = qn.downloadFile() key = qn.getKey() pic = Pic.objects.create(img=remote_url, key=key) return ajax.ajax_ok({'id': pic.id, 'url': pic.img, 'key': key})
def UploadPic(request): """upload pic to qiniu.""" if request.method == 'POST': img = request.FILES.get('Filedata', None) type = request.POST.get('type', None) if type: qn = SuperQiniu(img, w=800, h=520) else: qn = SuperQiniu(img) qn.uploadFile() remote_url = qn.downloadFile() key = qn.getKey() pic = Pic.objects.create(img=remote_url, key=key) return ajax.ajax_ok({'id':pic.id, 'url':pic.img, 'key':key})