Exemplo n.º 1
0
def file_upload(request,item_type,item_id):
	ctx = {}
	ctx['system_para'] = System_Para.get_default_system_parameters()
	ctx['action_url'] = '/file-upload/' + item_type + '/' + item_id + "/"
	ctx['file_delete_url'] = '/file-delete/' + item_type
	ctx['host_item_id'] = item_id
	if request.method == 'GET':
		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
				ctx['item'] = item
				try:
					ctx['image_list'] = Product_Images.objects.filter(product=item).order_by('create_time').reverse()
					if item_type == 'product_album':
						ctx['image_list'] = Album.objects.filter(item_type=item_type,item_id=item.id).order_by('create_time').reverse()
				except:
					ctx['image_list'] = []
			except:
				raise Http404
		elif item_type == 'article':
			raise Http404
		else:
			raise Http404
		return render(request,System_Config.get_template_name() + '/file_upload.html',ctx)
	else:
		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id)
			#加入到对象的图片列表中去
			if item_type == 'product':
				pi = Product_Images.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],product=item)
			else:
				ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id)
		elif item_type == 'article':
			try:
				item = Article.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id)
			ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id)
		else:
			raise Http404
		#判断是否是从CKEDITER传上来的
		if 'CKEditorFuncNum' in request.GET:
			logger.debug('请求来自CKEDITER.')
			script = '<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("' + request.GET['CKEditorFuncNum'] + '","' + filenames['image_url'] + '");</script>';
			logger.debug('返回的script: %s' % [script])
			return HttpResponse(script,content_type='text/html;charset=UTF-8')
		return redirect('/file-upload/' + item_type + '/' + item_id + "/")
Exemplo n.º 2
0
def upload_file(request):
    if request.method == 'POST':
        #form = UploadFileForm(request.POST, request.FILES)
        #if form.is_valid():
        filenames = handle_uploaded_file(request.FILES['file'], 'product',
                                         '52')

        if filenames:
            lines = []
            lines.append('<p>大图的文件名:%(image)s</p>' % filenames)
            lines.append('<p>缩略图的文件名:%(thumb)s</p>' % filenames)
            lines.append('<p>大图的URL:%(image_url)s</p>' % filenames)
            lines.append('<p>缩略图的URL:%(thumb_url)s</p>' % filenames)
            return HttpResponse(''.join(lines))
        else:
            return HttpResponse('/faild')
    else:
        raise Http404
Exemplo n.º 3
0
def file_upload(request, item_type, item_id):
    ctx = {}
    ctx['system_para'] = System_Para.get_default_system_parameters()
    ctx['action_url'] = '/file-upload/' + item_type + '/' + item_id + "/"
    ctx['file_delete_url'] = '/file-delete/' + item_type
    ctx['host_item_id'] = item_id
    if request.method == 'GET':
        if item_type == 'product' or item_type == 'product_album':
            try:
                item = Product.objects.get(id=item_id)
                ctx['item'] = item
                try:
                    ctx['image_list'] = Product_Images.objects.filter(
                        product=item).order_by('create_time').reverse()
                    if item_type == 'product_album':
                        ctx['image_list'] = Album.objects.filter(
                            item_type=item_type,
                            item_id=item.id).order_by('create_time').reverse()
                except:
                    ctx['image_list'] = []
            except:
                raise Http404
        elif item_type == 'article':
            raise Http404
        else:
            raise Http404
        return render(request,
                      System_Config.get_template_name() + '/file_upload.html',
                      ctx)
    else:
        if item_type == 'product' or item_type == 'product_album':
            try:
                item = Product.objects.get(id=item_id)
            except:
                raise Http404
            filenames = handle_uploaded_file(request.FILES['upload'],
                                             item_type, item_id)
            #加入到对象的图片列表中去
            if item_type == 'product':
                pi = Product_Images.objects.create(
                    image=filenames['image_url'],
                    thumb=filenames['thumb_url'],
                    product=item)
            else:
                ai = Album.objects.create(image=filenames['image_url'],
                                          thumb=filenames['thumb_url'],
                                          item_type=item_type,
                                          item_id=item.id)
        elif item_type == 'article':
            try:
                item = Article.objects.get(id=item_id)
            except:
                raise Http404
            filenames = handle_uploaded_file(request.FILES['upload'],
                                             item_type, item_id)
            ai = Album.objects.create(image=filenames['image_url'],
                                      thumb=filenames['thumb_url'],
                                      item_type=item_type,
                                      item_id=item.id)
        else:
            raise Http404
        #判断是否是从CKEDITER传上来的
        if 'CKEditorFuncNum' in request.GET:
            logger.debug('请求来自CKEDITER.')
            script = '<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("' + request.GET[
                'CKEditorFuncNum'] + '","' + filenames[
                    'image_url'] + '");</script>'
            logger.debug('返回的script: %s' % [script])
            return HttpResponse(script, content_type='text/html;charset=UTF-8')
        return redirect('/file-upload/' + item_type + '/' + item_id + "/")
Exemplo n.º 4
0
def file_upload(request,item_type,item_id):
	ctx = {}
	ctx['action_url'] = '/admin/file-upload/' + item_type + '/' + item_id + "/"
	ctx['file_delete_url'] = '/file-delete/' + item_type
	ctx['host_item_id'] = item_id
	
	if request.method == 'GET':
		ctx['item_type'] = item_type
		ctx['extra_info'] = request.GET.get('extra-info')
		return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)
	else:
		ctx['result_message'] = '文件上传成功'
		
		
		result_extra = {}
		ctx['result_extra'] = json.dumps(result_extra)
		
		manual_name = request.POST.get('manual_name','noname')	
		same_name_handle = request.POST.get('same_name_handle','reject')
		alt_value = request.POST.get('alt_value','')
		filename_type = request.POST.get('filename_type','random')
		href = request.POST.get('href','')
		sort = request.POST.get('sort','0')
		extra_info = request.POST.get('extra_info','')
	
		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
			except:
				raise Http404
			

			logger.debug("filename_type:%s" % filename_type)
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,filename_type,manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)
				#return HttpResponse(filenames['upload_error_msg'])
			
			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']
			
			#加入到对象的图片列表中去
			is_show = request.POST.get('is_show_in_product_detail',False)
			
			if item_type == 'product':
				pi = Product_Images.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],product=item,sort=sort,is_show_in_product_detail=is_show,alt_value=alt_value,file_name=real_name,thumb_name = thumb_name,path=file_path)
				'''
				如果改商品原来没有图片,则自动把第一张作为主图
				'''
				if not item.image:
					item.image = pi.image
					item.thumb = pi.thumb
					item.save()
					
				result_extra['img_id'] = pi.id	
			else:
				ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id,alt_value=alt_value,file_name=real_name,thumb_name = thumb_name,sort=sort,path=file_path)
				
				result_extra['img_id'] = ai.id
		elif item_type == 'article':
			try:
				item = Article.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,filename_type,manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)				
		
			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']
		
			logger.debug('Upload success!!!')
			ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id,alt_value=alt_value,file_name=real_name,thumb_name = thumb_name,sort=sort,path=file_path)
			'''
			如果改文章原来没有图片,则自动把第一张作为主图
			'''
			if not item.image:
				item.image = ai.image
				item.thumb = ai.thumb
				item.save()
			result_extra['img_id'] = ai.id
			logger.debug('ai success!!!')
			
		elif item_type == 'attribute':
			try:
				item = Attribute_Group.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,request.POST['filename_type'],manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)
			
			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']
			
			ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id,alt_value=alt_value,file_name=real_name,thumb_name = thumb_name,sort=sort,path=file_path)
			result_extra['img_id'] = ai.id
			logger.info('Attribute_Group image upload success')
		elif item_type == 'slider':
			try:
				item = Slider.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,request.POST['filename_type'],manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)
			
			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']
			
			ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id,alt_value=alt_value,href=href,file_name=real_name,thumb_name = thumb_name,sort=sort,path=file_path)
			result_extra['img_id'] = ai.id
			logger.info('Slider image upload success')	
		
		else:
			raise Http404
			
		logger.info('come in')	
		#判断是否是从CKEDITER传上来的
		if 'CKEditorFuncNum' in request.GET:
			logger.debug('请求来自CKEDITER.')
			script = '<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("' + request.GET['CKEditorFuncNum'] + '","' + filenames['image_url'] + '");</script>';
			logger.debug('返回的script: %s' % [script])
			return HttpResponse(script,content_type='text/html;charset=UTF-8')
			
		return_url = '/admin/file-upload/' + item_type + '/' + item_id + "/"
		if 'return_url' in request.POST:
			return_url = request.POST.get('return_url')
			return redirect(return_url)
			
		result_extra['extra_info'] = extra_info	
		ctx['result_extra'] = json.dumps(result_extra)
		return TemplateResponse(request,System_Config.get_template_name('admin') + '/file_upload.html',ctx)
Exemplo n.º 5
0
def file_upload(request,item_type,item_id):
	ctx = {}
	ctx['system_para'] = System_Para.get_default_system_parameters()
	ctx['action_url'] = '/admin/file-upload/' + item_type + '/' + item_id + "/"
	ctx['file_delete_url'] = '/file-delete/' + item_type
	ctx['host_item_id'] = item_id
	if request.method == 'GET':
		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
				ctx['item'] = item
				try:
					ctx['image_list'] = Product_Images.objects.filter(product=item).order_by('create_time').reverse()
					if item_type == 'product_album':
						ctx['image_list'] = Album.objects.filter(item_type=item_type,item_id=item.id).order_by('create_time').reverse()
				except:
					ctx['image_list'] = []
			except:
				raise Http404
		elif item_type == 'article':
			try:
				item = Article.objects.get(id=item_id)
				ctx['item'] = item
				try:
					ctx['image_list'] = Album.objects.filter(item_type=item_type,item_id=item.id).order_by('create_time').reverse()
				except:
					ctx['image_list'] = []
			except:
				raise Http404
		else:
			raise Http404
		return render(request,'admin/file_upload.html',ctx)
	else:
		
		manual_name = request.POST.get('manual_name','noname')	
		same_name_handle = request.POST.get('same_name_handle','reject')
	
		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
			except:
				raise Http404
			

			logger.debug("filename_type:%s" % request.POST['filename_type'])
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,request.POST['filename_type'],manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				return HttpResponse(filenames['upload_error_msg'])
				
			#加入到对象的图片列表中去
			sort = request.POST.get('sort','0')
			is_show = request.POST.get('is_show_in_product_detail',False)
			
			if item_type == 'product':
				pi = Product_Images.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],product=item,sort=sort,is_show_in_product_detail=is_show)
			else:
				ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id)
		elif item_type == 'article':
			try:
				item = Article.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'],item_type,item_id,request.POST['filename_type'],manual_name,same_name_handle)
			if filenames['upload_result'] == False:
				return HttpResponse(filenames['upload_error_msg'])			
		
			logger.debug('Upload success!!!')
			ai = Album.objects.create(image=filenames['image_url'],thumb=filenames['thumb_url'],item_type=item_type,item_id=item.id)
			logger.debug('ai success!!!')
		else:
			raise Http404
		#判断是否是从CKEDITER传上来的
		if 'CKEditorFuncNum' in request.GET:
			logger.debug('请求来自CKEDITER.')
			script = '<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("' + request.GET['CKEditorFuncNum'] + '","' + filenames['image_url'] + '");</script>';
			logger.debug('返回的script: %s' % [script])
			return HttpResponse(script,content_type='text/html;charset=UTF-8')
			
		return_url = '/admin/file-upload/' + item_type + '/' + item_id + "/"
		if 'return_url' in request.POST:
			return_url = request.POST.get('return_url')
		return redirect(return_url)
Exemplo n.º 6
0
def file_upload(request, item_type, item_id):
	ctx = {}
	ctx['action_url'] = '/admin/file-upload/' + item_type + '/' + item_id + "/"
	ctx['file_delete_url'] = '/file-delete/' + item_type
	ctx['host_item_id'] = item_id

	if request.method == 'GET':
		ctx['item_type'] = item_type
		ctx['extra_info'] = request.GET.get('extra-info')
		return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)
	else:
		ctx['result_message'] = '文件上传成功'

		result_extra = {}
		ctx['result_extra'] = json.dumps(result_extra)

		manual_name = request.POST.get('manual_name', 'noname')
		same_name_handle = request.POST.get('same_name_handle', 'reject')
		alt_value = request.POST.get('alt_value', '')
		filename_type = request.POST.get('filename_type', 'random')
		href = request.POST.get('href', '')
		sort = request.POST.get('sort', '0')
		extra_info = request.POST.get('extra_info', '')
		
		#长度和高度的限制,给自动缩放用,自动缩放是否启用,没有配置,默认为不缩放,或者最长边不超过1280

		if item_type == 'product' or item_type == 'product_album':
			try:
				item = Product.objects.get(id=item_id)
			except:
				raise Http404	
				
			logger.debug("filename_type:%s" % filename_type)
			filenames = handle_uploaded_file(request.FILES['upload'], item_type, item_id, filename_type, manual_name,
											 same_name_handle,get_scale_para('product'))
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)
			# return HttpResponse(filenames['upload_error_msg'])

			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']

			# 加入到对象的图片列表中去
			is_show = request.POST.get('is_show_in_product_detail', False)

			if item_type == 'product':
				pi = Product_Images.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'],
												   product=item, sort=sort, is_show_in_product_detail=is_show,
												   alt_value=alt_value, file_name=real_name, thumb_name=thumb_name,
												   path=file_path)
				'''
				如果改商品原来没有图片,则自动把第一张作为主图
				'''
				if not item.image:
					item.image = pi.image
					item.thumb = pi.thumb
					item.save()

				result_extra['img_id'] = pi.id
			else:
				ai = Album.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'],
										  item_type=item_type, item_id=item.id, alt_value=alt_value,
										  file_name=real_name, thumb_name=thumb_name, sort=sort, path=file_path)

				result_extra['img_id'] = ai.id
		elif item_type == 'article':
			try:
				item = Article.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'], item_type, item_id, filename_type, manual_name,
											 same_name_handle,get_scale_para('article'))
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)

			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']

			logger.debug('Upload success!!!')
			ai = Album.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'], item_type=item_type,
									  item_id=item.id, alt_value=alt_value, file_name=real_name, thumb_name=thumb_name,
									  sort=sort, path=file_path)
			'''
			如果改文章原来没有图片,则自动把第一张作为主图
			'''
			if not item.image:
				item.image = ai.image
				item.thumb = ai.thumb
				item.save()
			result_extra['img_id'] = ai.id
			logger.debug('ai success!!!')

		elif item_type == 'attribute':
			try:
				item = Attribute_Group.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'], item_type, item_id, request.POST['filename_type'],
											 manual_name, same_name_handle,get_scale_para('attribute'))
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)

			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']

			ai = Album.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'], item_type=item_type,
									  item_id=item.id, alt_value=alt_value, file_name=real_name, thumb_name=thumb_name,
									  sort=sort, path=file_path)
			result_extra['img_id'] = ai.id
			logger.info('Attribute_Group image upload success')
		elif item_type == 'slider':
			try:
				item = Slider.objects.get(id=item_id)
			except:
				raise Http404
			filenames = handle_uploaded_file(request.FILES['upload'], item_type, item_id, request.POST['filename_type'],
											 manual_name, same_name_handle,get_scale_para('slider'))
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)

			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']

			ai = Album.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'], item_type=item_type,
									  item_id=item.id, alt_value=alt_value, href=href, file_name=real_name,
									  thumb_name=thumb_name, sort=sort, path=file_path)
			result_extra['img_id'] = ai.id
			logger.info('Slider image upload success')
		elif item_type == 'attachment':
			try:
				item = Product.objects.get(id=item_id)
			except:
				raise Http404

			logger.debug("filename_type:%s" % filename_type)

			upload = request.FILES.get('upload', None)
			upload2 = request.FILES.get('upload2', None)

			if not upload2:
				upload2 = upload

			filenames = handle_uploaded_attachment_file(upload, upload2, item_type, item_id,
												 filename_type, manual_name,
												 same_name_handle)
			if filenames['upload_result'] == False:
				ctx['result_message'] = filenames['upload_error_msg']
				return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)
			real_name = filenames['real_name']
			thumb_name = filenames['real_thumb']
			file_path = filenames['real_path']

			ai = Album.objects.create(image=filenames['image_url'], thumb=filenames['thumb_url'],
									  item_type=item_type,
									  item_id=item.id, alt_value=alt_value, file_name=real_name, thumb_name=thumb_name,
									  sort=sort, path=file_path)

			result_extra['img_id'] = ai.id
		else:
			raise Http404

		logger.info('come in')
		# 判断是否是从CKEDITER传上来的
		if 'CKEditorFuncNum' in request.GET:
			logger.info('从CKEDITER')
			logger.debug('请求来自CKEDITER.')
			script = '<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction("' + request.GET[
				'CKEditorFuncNum'] + '","' + filenames['image_url'] + '");</script>';
			logger.debug('返回的script: %s' % [script])
			return HttpResponse(script, content_type='text/html;charset=UTF-8')

		return_url = '/admin/file-upload/' + item_type + '/' + item_id + "/"
		if 'return_url' in request.POST:
			return_url = request.POST.get('return_url')
			return redirect(return_url)

		result_extra['extra_info'] = extra_info
		ctx['result_extra'] = json.dumps(result_extra)
		return TemplateResponse(request, System_Config.get_template_name('admin') + '/file_upload.html', ctx)