Fixes in extrafields

This commit is contained in:
Antonio de la Rosa 2024-11-09 01:33:28 +01:00
parent ac32f46ff4
commit 6ca8a624ed
7 changed files with 181 additions and 25 deletions

View file

@ -1,9 +1,35 @@
"""
Parameciofm is a series of wrappers for Flask, mako and others and construct a simple headless cms.
Copyright (C) 2024 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.webmodel import PhangoField,WebModel from paramecio.libraries.db.webmodel import PhangoField,WebModel
import json import json
class ArrayField(PhangoField): class ArrayField(PhangoField):
"""Field for save json arrays with determined values"""
def __init__(self, name, field_type, required=False): def __init__(self, name, field_type, required=False):
"""
Args:
name (str): The name of new field
field_type (PhangoField): The type of PhangoField for save in ArrayField
required (bool): Boolean for define if field is required or not
"""
super().__init__(name, required) super().__init__(name, required)
@ -11,7 +37,9 @@ class ArrayField(PhangoField):
self.error_default='Sorry, the json array is invalid' self.error_default='Sorry, the json array is invalid'
self.set_default='NOT NULL' self.set_default=''
self.type_sql='text'
def check(self, value): def check(self, value):
@ -30,10 +58,17 @@ class ArrayField(PhangoField):
self.error=True self.error=True
self.txt_error='Sorry, the json array is invalid' self.txt_error='Sorry, the json array is invalid'
error=0
if type(self.field_type).__name__!='ArrayField': if type(self.field_type).__name__!='ArrayField':
for k,v in enumerate(value): for k,v in enumerate(value):
value[k]=self.field_type.check(v) value[k]=self.field_type.check(v)
if self.field_type.error:
error+=1
if error>0:
self.error=True
final_value=json.dumps(value) final_value=json.dumps(value)
@ -43,7 +78,7 @@ class ArrayField(PhangoField):
def get_type_sql(self): def get_type_sql(self):
return 'TEXT '+self.set_default return 'JSON'
def show_formatted(self, value): def show_formatted(self, value):

View file

@ -1,7 +1,31 @@
"""
Parameciofm is a series of wrappers for Bottle, mako and others and construct a simple headless cms.
Copyright (C) 2024 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.corefields import IntegerField from paramecio.libraries.db.corefields import IntegerField
try:
from paramecio.libraries.db.extraforms.colorform import ColorForm from paramecio.libraries.db.extraforms.colorform import ColorForm
except:
class ColorForm:
pass
class ColorField(IntegerField): class ColorField(IntegerField):
"""Simple field for save colors in hexadecimal format."""
def __init__(self, name, size=11, required=False): def __init__(self, name, size=11, required=False):
super().__init__(name, size, required) super().__init__(name, size, required)

View file

@ -1,8 +1,33 @@
"""
Parameciofm is a series of wrappers for Bottle, mako and others and construct a simple headless cms.
Copyright (C) 2024 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.corefields import PhangoField from paramecio.libraries.db.corefields import PhangoField
from paramecio.libraries import datetime from paramecio.libraries import datetime
try:
from paramecio.libraries.db.extraforms.dateform import DateForm from paramecio.libraries.db.extraforms.dateform import DateForm
except:
class DateForm:
pass
class DateField(PhangoField): class DateField(PhangoField):
"""Field for use and save dates in YYYYMMDDHHSS format"""
def __init__(self, name, size=255, required=False): def __init__(self, name, size=255, required=False):

View file

@ -1,8 +1,32 @@
"""
Parameciofm is a series of wrappers for Bottle, mako and others and construct a simple headless cms.
Copyright (C) 2024 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.corefields import PhangoField from paramecio.libraries.db.corefields import PhangoField
from paramecio.libraries import datetime from paramecio.libraries import datetime
try:
from paramecio.libraries.db.extraforms.dateform import DateForm from paramecio.libraries.db.extraforms.dateform import DateForm
except:
class DateForm:
pass
class DateTimeField(PhangoField): class DateTimeField(PhangoField):
"""Field for use and save dates in MySQL date format"""
def __init__(self, name, size=255, required=False): def __init__(self, name, size=255, required=False):
@ -14,6 +38,8 @@ class DateTimeField(PhangoField):
self.error_default='Error: Date format invalid' self.error_default='Error: Date format invalid'
self.type_sql='datetime'
def check(self, value): def check(self, value):
if self.utc: if self.utc:
@ -24,13 +50,14 @@ class DateTimeField(PhangoField):
self.error=True self.error=True
self.txt_error=self.error_default self.txt_error=self.error_default
return None
return '0000-00-00 00:00:00'
if value==False: if value==False:
self.error=True self.error=True
self.txt_error=self.error_default self.txt_error=self.error_default
return None return '0000-00-00 00:00:00'
else: else:
""" """
@ -46,19 +73,15 @@ class DateTimeField(PhangoField):
def show_formatted(self, value): def show_formatted(self, value):
# Convert to paramecio value # Convert to paramecio value
if value!=None:
value=str(value) value=str(value)
value=value.replace('-', '').replace(':', '').replace(' ', '') value=value.replace('-', '').replace(':', '').replace(' ', '')
return datetime.format_date(value) return datetime.format_date(value)
else:
return ''
def get_type_sql(self): def get_type_sql(self):
"""Method for return the sql code for this type """Method for return the sql code for this type
""" """
return 'DATETIME NULL' return 'DATETIME NOT NULL'

View file

@ -1,13 +1,35 @@
from paramecio.libraries.db.webmodel import WebModel, PhangoField """
Parameciofm is a series of wrappers for Bottle, mako and others and construct a simple headless cms.
try: Copyright (C) 2024 Antonio de la Rosa Caballero
import ujson as json
except: This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.webmodel import WebModel, PhangoField
import json import json
class DictField(PhangoField): class DictField(PhangoField):
"""Field for save json dicts with determined values"""
def __init__(self, name, field_type, required=False): def __init__(self, name, field_type, required=False):
"""
Args:
name (str): The name of field
field_type (PhangoField): The type of PhangoField for save in ArrayField
required (bool): Boolean for define if field is required or not
"""
super().__init__(name, required) super().__init__(name, required)
@ -17,6 +39,8 @@ class DictField(PhangoField):
self.set_default='NOT NULL' self.set_default='NOT NULL'
self.type_sql='longtext'
def check(self, value): def check(self, value):
if type(value).__name__=='str': if type(value).__name__=='str':
@ -33,20 +57,25 @@ class DictField(PhangoField):
value={} value={}
self.error=True self.error=True
self.txt_error=self.error_default self.txt_error=self.error_default
error=0
for k,v in value.items(): for k,v in value.items():
value[k]=self.field_type.check(v) value[k]=self.field_type.check(v)
if self.field_type.error:
error+=1
final_value=json.dumps(value) final_value=json.dumps(value)
if error>0:
self.error=True
#final_value=WebModel.escape_sql(final_value) #final_value=WebModel.escape_sql(final_value)
return final_value return final_value
def get_type_sql(self): def get_type_sql(self):
return 'TEXT '+self.set_default return 'JSON '+self.set_default
def show_formatted(self, value): def show_formatted(self, value):

View file

@ -1,9 +1,29 @@
"""
Parameciofm is a series of wrappers for bottle, mako and others and construct a simple headless cms.
Copyright (C) 2024 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from paramecio.libraries.db.corefields import CharField from paramecio.libraries.db.corefields import CharField
import re import re
mail_pattern=re.compile(r"\w[\w\.-]*@\w[\w\.-]+\.\w+") mail_pattern=re.compile(r"\w[\w\.-]*@\w[\w\.-]+\.\w+")
class EmailField(CharField): class EmailField(CharField):
"""Field for save and check email addreses"""
def __init__(self, name, size=1024, required=False): def __init__(self, name, size=1024, required=False):

View file

@ -91,7 +91,7 @@ class PTemplate:
templates_loaded={} templates_loaded={}
def __init__(self, environment): def __init__(self, environment, app=None):
"""A class used how shortcuts for Mako template functions. """A class used how shortcuts for Mako template functions.