Fixes
This commit is contained in:
parent
20cc7dff36
commit
7eaaa2c411
7 changed files with 253 additions and 2 deletions
|
|
@ -9,7 +9,7 @@ class GetPostFiles:
|
||||||
|
|
||||||
post={}
|
post={}
|
||||||
|
|
||||||
files=None
|
files={}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def obtain_get():
|
def obtain_get():
|
||||||
|
|
@ -28,4 +28,4 @@ class GetPostFiles:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def obtain_files():
|
def obtain_files():
|
||||||
|
|
||||||
GetPostFiles.files=request.files
|
GetPostFiles.files=request.files
|
||||||
|
|
|
||||||
|
|
@ -77,4 +77,28 @@ class SelectForm(BaseForm):
|
||||||
the_form+='</select>\n'
|
the_form+='</select>\n'
|
||||||
|
|
||||||
return the_form
|
return the_form
|
||||||
|
|
||||||
|
class SelectModelForm(SelectForm):
|
||||||
|
|
||||||
|
def __init__(self, name, value):
|
||||||
|
super(SelectModelForm, self).__init__(name, value)
|
||||||
|
self.arr_select=OrderedDict()
|
||||||
|
self.model=None
|
||||||
|
self.field_name=''
|
||||||
|
self.field_value='id'
|
||||||
|
|
||||||
|
def form(self):
|
||||||
|
|
||||||
|
self.arr_select['']=''
|
||||||
|
|
||||||
|
cur=self.model.select([self.field_name, self.field_value])
|
||||||
|
|
||||||
|
for arr_value in cur:
|
||||||
|
|
||||||
|
self.arr_select[arr_value[self.field_value]]=arr_value[self.field_name]
|
||||||
|
|
||||||
|
return super().form()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
154
paramecio/cromosoma/extrafields/imagefield.py
Normal file
154
paramecio/cromosoma/extrafields/imagefield.py
Normal file
|
|
@ -0,0 +1,154 @@
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from paramecio.cromosoma.corefields import CharField
|
||||||
|
from paramecio.citoplasma.httputils import GetPostFiles
|
||||||
|
from PIL import Image
|
||||||
|
#from paramecio.cromosoma.extraforms.fileform import FileForm
|
||||||
|
|
||||||
|
class ImageField(CharField):
|
||||||
|
|
||||||
|
def __init__(self, name, save_folder='media/upload/images', module=None, size=255, required=False):
|
||||||
|
|
||||||
|
super().__init__(name, size, required)
|
||||||
|
|
||||||
|
self.yes_prefix=False
|
||||||
|
|
||||||
|
#self.name_form=FileForm
|
||||||
|
|
||||||
|
self.thumbnail={'mini_': 150}
|
||||||
|
|
||||||
|
self.yes_thumbnail=False
|
||||||
|
|
||||||
|
self.default_quality_thumb=Image.ANTIALIAS
|
||||||
|
|
||||||
|
# Is relative to media folder of paramecio
|
||||||
|
|
||||||
|
#if module!=None:
|
||||||
|
|
||||||
|
self.save_folder=save_folder
|
||||||
|
|
||||||
|
self.file_related=True
|
||||||
|
|
||||||
|
def change_folder(self, folder):
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
def check(self, value):
|
||||||
|
|
||||||
|
#GetPostFiles.obtain_files()
|
||||||
|
|
||||||
|
field_file=self.name+'_file'
|
||||||
|
|
||||||
|
if not field_file in GetPostFiles.files:
|
||||||
|
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Error, no exists image file'
|
||||||
|
return ""
|
||||||
|
# Load image file
|
||||||
|
|
||||||
|
file_bytecode=GetPostFiles.files[field_file].file
|
||||||
|
|
||||||
|
filename=GetPostFiles.files[field_file].filename
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
im=Image.open(file_bytecode)
|
||||||
|
|
||||||
|
except IOError:
|
||||||
|
|
||||||
|
self.error=True
|
||||||
|
|
||||||
|
self.txt_error='Error, file not exists'
|
||||||
|
return ""
|
||||||
|
|
||||||
|
format_image=im.format
|
||||||
|
|
||||||
|
if format_image!='JPEG' and format_image!='GIF' and format_image!='PNG':
|
||||||
|
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Format is wrong. Requires JPEG, GIF or PNG formats'
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# Create thumbnails and move file
|
||||||
|
|
||||||
|
realfilename, ext = os.path.splitext(filename)
|
||||||
|
|
||||||
|
save_file=self.save_folder+'/'+filename
|
||||||
|
|
||||||
|
if self.yes_thumbnail:
|
||||||
|
|
||||||
|
real_width=im.size[0]
|
||||||
|
real_height=im.size[1]
|
||||||
|
|
||||||
|
for name, width_t in self.thumbnail.items():
|
||||||
|
|
||||||
|
im_thumb=im.copy()
|
||||||
|
|
||||||
|
height_t=150
|
||||||
|
|
||||||
|
size=(self.width_t, height_t)
|
||||||
|
|
||||||
|
im_thumb.thumbnail(size, self.default_quality_thumb)
|
||||||
|
im_thumb.save(self.save_folder, "JPEG")
|
||||||
|
|
||||||
|
im_thumb.close()
|
||||||
|
|
||||||
|
pass
|
||||||
|
# Save file
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
#Check if directory exists
|
||||||
|
|
||||||
|
if not os.path.isdir(self.save_folder):
|
||||||
|
|
||||||
|
# Try create if not
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
p=Path(self.save_folder)
|
||||||
|
|
||||||
|
p.mkdir(mode=0o755, parents=True)
|
||||||
|
|
||||||
|
except:
|
||||||
|
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Error: cannot create the directory where save the image.Check permissions,'
|
||||||
|
return ""
|
||||||
|
|
||||||
|
#GetPostFiles.files[field_file].save(self.save_folder, overwrite=True)
|
||||||
|
|
||||||
|
if os.path.isfile(save_file):
|
||||||
|
|
||||||
|
os.remove(save_file)
|
||||||
|
|
||||||
|
im.save(save_file)
|
||||||
|
|
||||||
|
# Delete old files if have a different name
|
||||||
|
|
||||||
|
if self.model!=None:
|
||||||
|
|
||||||
|
old_reset=self.model.yes_reset_conditions
|
||||||
|
|
||||||
|
self.model.yes_reset_conditions=False
|
||||||
|
|
||||||
|
cur=self.model.select([self.name])
|
||||||
|
|
||||||
|
for arr_image in cur:
|
||||||
|
|
||||||
|
os.remove(arr_image[self.name])
|
||||||
|
|
||||||
|
self.model.yes_reset_conditions=old_reset
|
||||||
|
|
||||||
|
return save_file
|
||||||
|
|
||||||
|
except:
|
||||||
|
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Error: cannot save the image file, Exists directory for save the file?'
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def show_formatted(value):
|
||||||
|
|
||||||
|
return os.path.basename(value)
|
||||||
|
|
||||||
|
|
@ -104,6 +104,10 @@ class WebModel:
|
||||||
# A simple dictionary where post values are saved for use of fields classes
|
# A simple dictionary where post values are saved for use of fields classes
|
||||||
|
|
||||||
self.post={}
|
self.post={}
|
||||||
|
|
||||||
|
# A simple dictionary that save the fields that have files related. If i delete the row in database i need delete the files related
|
||||||
|
|
||||||
|
self.files_delete={}
|
||||||
|
|
||||||
# A method where create the new fields of this model
|
# A method where create the new fields of this model
|
||||||
|
|
||||||
|
|
@ -122,6 +126,8 @@ class WebModel:
|
||||||
self.fields[field_model.name].model=self
|
self.fields[field_model.name].model=self
|
||||||
|
|
||||||
self.fields[field_model.name].required=required
|
self.fields[field_model.name].required=required
|
||||||
|
|
||||||
|
self.files_delete[field_model.name]=field_model.file_related
|
||||||
|
|
||||||
# A method for create the id field.
|
# A method for create the id field.
|
||||||
|
|
||||||
|
|
@ -884,6 +890,10 @@ class PhangoField:
|
||||||
# Property that define if make escape in show_formatted
|
# Property that define if make escape in show_formatted
|
||||||
|
|
||||||
self.escape=True
|
self.escape=True
|
||||||
|
|
||||||
|
# File related: if the field have a file related, delete the file
|
||||||
|
|
||||||
|
self.file_related=False
|
||||||
|
|
||||||
# This method is used for describe the new field in a sql language format.
|
# This method is used for describe the new field in a sql language format.
|
||||||
|
|
||||||
|
|
|
||||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
63
tests/imagefieldtest.py
Normal file
63
tests/imagefieldtest.py
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
from bottle import FileUpload
|
||||||
|
from paramecio.cromosoma.webmodel import WebModel
|
||||||
|
from paramecio.cromosoma.extrafields.imagefield import ImageField
|
||||||
|
from paramecio.citoplasma.httputils import GetPostFiles
|
||||||
|
from settings import config
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_imagefield(self):
|
||||||
|
|
||||||
|
f=open('tests/images/image.jpg', 'rb')
|
||||||
|
|
||||||
|
GetPostFiles.files={}
|
||||||
|
|
||||||
|
GetPostFiles.files['image_file']=FileUpload(f, 'image_file', 'image.jpg')
|
||||||
|
|
||||||
|
field=ImageField('image', 'tests/images/uploads', module=None, size=255, required=False)
|
||||||
|
|
||||||
|
field.check('')
|
||||||
|
|
||||||
|
print(field.txt_error)
|
||||||
|
|
||||||
|
self.assertFalse(field.error)
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
"""from settings import config
|
||||||
|
from bottle import FileUpload
|
||||||
|
from paramecio.cromosoma.webmodel import WebModel
|
||||||
|
from paramecio.cromosoma.imagefield import ImageField
|
||||||
|
from paramecio.citoplasma.httputils import GetPostFiles
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
class TestImageFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_image(self):
|
||||||
|
#name, save_folder, module=None, size=255, required=False)
|
||||||
|
|
||||||
|
#FileUpload(fileobj, name, filename
|
||||||
|
x=0
|
||||||
|
pass
|
||||||
|
|
||||||
|
GetPostFiles.files=
|
||||||
|
|
||||||
|
field=ImageField(, 'test/image', module=None, size=255, required=False)
|
||||||
|
|
||||||
|
field.required=True
|
||||||
|
|
||||||
|
field.check('')
|
||||||
|
|
||||||
|
self.assertTrue(field.error)
|
||||||
|
|
||||||
|
field.check('content')
|
||||||
|
|
||||||
|
self.assertFalse(field.error)
|
||||||
|
|
||||||
|
value=field.check("injection_'")
|
||||||
|
|
||||||
|
self.assertEqual(value, "injection_\\'")"""
|
||||||
|
|
||||||
|
|
||||||
BIN
tests/images/image.jpg
Normal file
BIN
tests/images/image.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 546 KiB |
Loading…
Add table
Add a link
Reference in a new issue