Fixes in imagefield
This commit is contained in:
parent
913fc268cc
commit
4afa7a311d
2 changed files with 58 additions and 20 deletions
|
|
@ -2,7 +2,10 @@ import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from paramecio.cromosoma.corefields import CharField
|
from paramecio.cromosoma.corefields import CharField
|
||||||
from paramecio.citoplasma.httputils import GetPostFiles
|
from paramecio.citoplasma import httputils
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from bottle import request
|
||||||
try:
|
try:
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
except:
|
except:
|
||||||
|
|
@ -42,21 +45,46 @@ class ImageField(CharField):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def check(self, value):
|
def check(self, value):
|
||||||
|
|
||||||
#GetPostFiles.obtain_files()
|
files_uploaded=request.files
|
||||||
|
|
||||||
field_file=self.name+'_file'
|
field_file=self.name+'_file'
|
||||||
|
|
||||||
if not field_file in GetPostFiles.files:
|
#if not change
|
||||||
|
|
||||||
|
if not field_file in files_uploaded:
|
||||||
|
|
||||||
|
if value=='':
|
||||||
|
|
||||||
|
old_reset=self.model.yes_reset_conditions
|
||||||
|
|
||||||
|
self.model.yes_reset_conditions=False
|
||||||
|
|
||||||
|
with self.model.select([self.name]) as cur:
|
||||||
|
|
||||||
|
for arr_image in cur:
|
||||||
|
|
||||||
|
if arr_image[self.name]!='':
|
||||||
|
os.remove(arr_image[self.name])
|
||||||
|
|
||||||
|
#if arr_image[self.name]!=save_file and arr_image[self.name]!='':
|
||||||
|
|
||||||
|
#value=arr_image[self.name]
|
||||||
|
|
||||||
|
self.model.yes_reset_conditions=old_reset
|
||||||
|
|
||||||
|
return ''
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
self.error=True
|
return self.save_folder+'/'+value
|
||||||
self.txt_error='Error, no exists image file'
|
|
||||||
return ""
|
|
||||||
# Load image file
|
# Load image file
|
||||||
|
|
||||||
file_bytecode=GetPostFiles.files[field_file].file
|
file_bytecode=files_uploaded[field_file].file
|
||||||
|
|
||||||
filename=GetPostFiles.files[field_file].filename
|
filename=files_uploaded[field_file].filename
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
|
@ -75,6 +103,7 @@ class ImageField(CharField):
|
||||||
|
|
||||||
self.error=True
|
self.error=True
|
||||||
self.txt_error='Format is wrong. Requires JPEG, GIF or PNG formats'
|
self.txt_error='Format is wrong. Requires JPEG, GIF or PNG formats'
|
||||||
|
im.close()
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
# Create thumbnails and move file
|
# Create thumbnails and move file
|
||||||
|
|
@ -128,12 +157,13 @@ class ImageField(CharField):
|
||||||
p.mkdir(mode=0o755, parents=True)
|
p.mkdir(mode=0o755, parents=True)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
im.close()
|
||||||
self.error=True
|
self.error=True
|
||||||
|
|
||||||
self.txt_error='Error: cannot create the directory where save the image.Check permissions,'
|
self.txt_error='Error: cannot create the directory where save the image.Check permissions,'
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
#GetPostFiles.files[field_file].save(self.save_folder, overwrite=True)
|
#files_uploaded[field_file].save(self.save_folder, overwrite=True)
|
||||||
|
|
||||||
if os.path.isfile(save_file):
|
if os.path.isfile(save_file):
|
||||||
|
|
||||||
|
|
@ -145,26 +175,34 @@ class ImageField(CharField):
|
||||||
|
|
||||||
if self.model!=None:
|
if self.model!=None:
|
||||||
|
|
||||||
|
#old_conditions=self.model.conditions
|
||||||
|
|
||||||
old_reset=self.model.yes_reset_conditions
|
old_reset=self.model.yes_reset_conditions
|
||||||
|
|
||||||
self.model.yes_reset_conditions=False
|
self.model.yes_reset_conditions=False
|
||||||
|
|
||||||
cur=self.model.select([self.name])
|
with self.model.select([self.name]) as cur:
|
||||||
|
|
||||||
for arr_image in cur:
|
|
||||||
|
|
||||||
if arr_image[self.name]!=save_file:
|
for arr_image in cur:
|
||||||
|
|
||||||
os.remove(arr_image[self.name])
|
if arr_image[self.name]!=save_file and arr_image[self.name]!='':
|
||||||
|
|
||||||
|
os.remove(arr_image[self.name])
|
||||||
|
|
||||||
self.model.yes_reset_conditions=old_reset
|
self.model.yes_reset_conditions=old_reset
|
||||||
|
|
||||||
|
#self.model.conditions=old_conditions
|
||||||
|
|
||||||
|
im.close()
|
||||||
|
|
||||||
return save_file
|
return save_file
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
||||||
|
im.close()
|
||||||
self.error=True
|
self.error=True
|
||||||
self.txt_error='Error: cannot save the image file, Exists directory for save the file?'
|
self.txt_error='Error: cannot save the image file, Exists directory for save the file? '+traceback.format_exc()
|
||||||
|
print(traceback.format_exc())
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def show_formatted(value):
|
def show_formatted(value):
|
||||||
|
|
|
||||||
|
|
@ -470,8 +470,8 @@ class WebModel:
|
||||||
|
|
||||||
except:
|
except:
|
||||||
|
|
||||||
#self.query_error+="\n"+traceback.format_exc()
|
self.query_error+="\n"+traceback.format_exc()
|
||||||
|
#print(traceback.format_exc())
|
||||||
return False
|
return False
|
||||||
|
|
||||||
sql="update `"+self.name+"` SET "+", ".join(update_values)+" "+self.conditions[0]
|
sql="update `"+self.name+"` SET "+", ".join(update_values)+" "+self.conditions[0]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue