Added arrayfield field
This commit is contained in:
parent
d796511a50
commit
3f1942cbfb
4 changed files with 80 additions and 3 deletions
46
paramecio/cromosoma/extrafields/arrayfield.py
Normal file
46
paramecio/cromosoma/extrafields/arrayfield.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
from paramecio.cromosoma.webmodel import PhangoField
|
||||||
|
import json
|
||||||
|
|
||||||
|
class ArrayField(PhangoField):
|
||||||
|
|
||||||
|
def __init__(self, name, field_type, required=False):
|
||||||
|
|
||||||
|
super().__init__(name, required)
|
||||||
|
|
||||||
|
self.field_type=field_type
|
||||||
|
|
||||||
|
def check(self, value):
|
||||||
|
|
||||||
|
if type(value).__name__=='str':
|
||||||
|
try:
|
||||||
|
value=json.loads(value)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
|
||||||
|
value=[]
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Sorry, the json array is invalid'
|
||||||
|
|
||||||
|
elif type(value).__name__!='list':
|
||||||
|
|
||||||
|
value=[]
|
||||||
|
self.error=True
|
||||||
|
self.txt_error='Sorry, the json array is invalid'
|
||||||
|
|
||||||
|
for k,v in enumerate(value):
|
||||||
|
|
||||||
|
value[k]=self.field_type.check(v)
|
||||||
|
|
||||||
|
final_value=json.dumps(value)
|
||||||
|
|
||||||
|
final_value=super().check(final_value)
|
||||||
|
|
||||||
|
return final_value
|
||||||
|
|
||||||
|
def get_type_sql(self):
|
||||||
|
|
||||||
|
return 'TEXT NOT NULL DEFAULT ""'
|
||||||
|
|
||||||
|
def show_formatted(self, value):
|
||||||
|
|
||||||
|
return ", ".join(value)
|
||||||
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import os, sys, traceback, inspect
|
import os, sys, traceback, inspect
|
||||||
from importlib import import_module, reload
|
from importlib import import_module
|
||||||
from bottle import route, get, post, run, default_app, abort, request, static_file
|
from bottle import route, get, post, run, default_app, abort, request, static_file
|
||||||
from settings import config
|
from settings import config
|
||||||
from beaker.middleware import SessionMiddleware
|
from beaker.middleware import SessionMiddleware
|
||||||
|
|
|
||||||
4
setup.py
4
setup.py
|
|
@ -11,7 +11,7 @@ if sys.version_info < (3, 3):
|
||||||
#import paramecio
|
#import paramecio
|
||||||
|
|
||||||
setup(name='paramecio',
|
setup(name='paramecio',
|
||||||
version='0.1.0b1',
|
version='0.1.0b2',
|
||||||
description='Fast and simple Framework based in bottle and Mako.',
|
description='Fast and simple Framework based in bottle and Mako.',
|
||||||
long_description='This framework is simple framework used for create web apps. Paramecio is modular and fast. By default have a module called admin that can be used for create ',
|
long_description='This framework is simple framework used for create web apps. Paramecio is modular and fast. By default have a module called admin that can be used for create ',
|
||||||
author='Antonio de la Rosa Caballero',
|
author='Antonio de la Rosa Caballero',
|
||||||
|
|
@ -39,4 +39,4 @@ setup(name='paramecio',
|
||||||
'Programming Language :: Python :: 3.4',
|
'Programming Language :: Python :: 3.4',
|
||||||
'Programming Language :: Python :: 3.5',
|
'Programming Language :: Python :: 3.5',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
31
tests/arrayfieldtest.py
Normal file
31
tests/arrayfieldtest.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
from settings import config
|
||||||
|
from paramecio.cromosoma import corefields
|
||||||
|
from paramecio.cromosoma.extrafields.arrayfield import ArrayField
|
||||||
|
import unittest
|
||||||
|
import json
|
||||||
|
|
||||||
|
class TestFieldMethods(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_i18nfield(self):
|
||||||
|
|
||||||
|
type_field=corefields.IntegerField('value')
|
||||||
|
|
||||||
|
field=ArrayField('field', type_field)
|
||||||
|
|
||||||
|
value=[1,2,5,'trick\'']
|
||||||
|
|
||||||
|
json_encoded=field.check(value)
|
||||||
|
|
||||||
|
self.assertEqual(json_encoded, '["1", "2", "5", "0"]')
|
||||||
|
|
||||||
|
type_field=corefields.CharField('value')
|
||||||
|
|
||||||
|
field=ArrayField('field', type_field)
|
||||||
|
|
||||||
|
value=['trick', 'mytuquito', 25]
|
||||||
|
|
||||||
|
json_encoded=field.check(value)
|
||||||
|
|
||||||
|
self.assertEqual(json_encoded, '["trick", "mytuquito", "25"]')
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue