diff --git a/paramecio/citoplasma/httputils.py b/paramecio/citoplasma/httputils.py index 63df909..2919a47 100644 --- a/paramecio/citoplasma/httputils.py +++ b/paramecio/citoplasma/httputils.py @@ -9,7 +9,7 @@ class GetPostFiles: post={} - files=None + files={} @staticmethod def obtain_get(): @@ -28,4 +28,4 @@ class GetPostFiles: @staticmethod def obtain_files(): - GetPostFiles.files=request.files \ No newline at end of file + GetPostFiles.files=request.files diff --git a/paramecio/cromosoma/coreforms.py b/paramecio/cromosoma/coreforms.py index 508ddbc..55fc56c 100644 --- a/paramecio/cromosoma/coreforms.py +++ b/paramecio/cromosoma/coreforms.py @@ -77,4 +77,28 @@ class SelectForm(BaseForm): the_form+='\n' 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() + + + \ No newline at end of file diff --git a/paramecio/cromosoma/extrafields/imagefield.py b/paramecio/cromosoma/extrafields/imagefield.py new file mode 100644 index 0000000..2965aad --- /dev/null +++ b/paramecio/cromosoma/extrafields/imagefield.py @@ -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) + diff --git a/paramecio/cromosoma/webmodel.py b/paramecio/cromosoma/webmodel.py index f03f456..5f9c241 100644 --- a/paramecio/cromosoma/webmodel.py +++ b/paramecio/cromosoma/webmodel.py @@ -104,6 +104,10 @@ class WebModel: # A simple dictionary where post values are saved for use of fields classes 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 @@ -122,6 +126,8 @@ class WebModel: self.fields[field_model.name].model=self self.fields[field_model.name].required=required + + self.files_delete[field_model.name]=field_model.file_related # A method for create the id field. @@ -884,6 +890,10 @@ class PhangoField: # Property that define if make escape in show_formatted 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. diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/imagefieldtest.py b/tests/imagefieldtest.py new file mode 100644 index 0000000..f6b2ae7 --- /dev/null +++ b/tests/imagefieldtest.py @@ -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_\\'")""" + + diff --git a/tests/images/image.jpg b/tests/images/image.jpg new file mode 100644 index 0000000..8e72044 Binary files /dev/null and b/tests/images/image.jpg differ