Fixes in documentation for db model
This commit is contained in:
parent
ae3bf3f6c3
commit
3433b3150a
6 changed files with 159 additions and 2 deletions
|
|
@ -216,7 +216,12 @@ class ForeignKeyField(IntegerField):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
name (str): Name of field
|
name (str): Name of field
|
||||||
|
related_table(str): The table related with this foreign key
|
||||||
|
size (int): The size of the new field in database. By default 11.
|
||||||
|
required (bool): Boolean for define if field is required or not
|
||||||
|
identifier_field (str): The Id field name from related table
|
||||||
|
named_field (str): The field from related table used for identify the row seleted from related table
|
||||||
|
select_field (list): A series of fields names from related
|
||||||
"""
|
"""
|
||||||
|
|
||||||
super(ForeignKeyField, self).__init__(name, size, required)
|
super(ForeignKeyField, self).__init__(name, size, required)
|
||||||
|
|
@ -258,8 +263,16 @@ class ForeignKeyField(IntegerField):
|
||||||
|
|
||||||
|
|
||||||
class BooleanField(IntegerField):
|
class BooleanField(IntegerField):
|
||||||
|
"""Field for boolean values
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, size=1):
|
def __init__(self, name, size=1):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
name (str): Name of field
|
||||||
|
related_table(str): The table related with this foreign key
|
||||||
|
size (int): The size of the new field in database. By default 11.
|
||||||
|
"""
|
||||||
|
|
||||||
required=False
|
required=False
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,30 @@ from html import escape
|
||||||
#Forms para python3
|
#Forms para python3
|
||||||
|
|
||||||
class BaseForm:
|
class BaseForm:
|
||||||
|
"""The class used by all forms classes
|
||||||
|
|
||||||
|
BaseForm is the base class used for all form classes.
|
||||||
|
|
||||||
|
A form class is used for generate simple html forms, how input type, text type, hidden type, etc. PhangoField classes use this forms for generate automatically forms using GenerateAdminClass and others.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
name (str): The html name for this form
|
||||||
|
value (str): The default value of this html form.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
label (str): Label used in functions how show_form that generate a complete html form from a form class list
|
||||||
|
name (str): Name of the html form.
|
||||||
|
default_value (mixed): The default value of the form. Equal to value in typical html form.
|
||||||
|
css (str): Used for add css classes to the html form
|
||||||
|
type (str): Variable used for conventional html forms with html tag <input>
|
||||||
|
field (PhangoField): Field related with this form. Used in PhangoField.
|
||||||
|
required (boolean): If form is required or not, used in functions that generate forms.
|
||||||
|
name_field_id (str): The html id for the html form. Used for html things.
|
||||||
|
help (str): A string with help text, used in functions that generate forms.
|
||||||
|
"""
|
||||||
|
|
||||||
self.label=name
|
self.label=name
|
||||||
self.name=name
|
self.name=name
|
||||||
|
|
@ -21,22 +43,39 @@ class BaseForm:
|
||||||
self.help=''
|
self.help=''
|
||||||
|
|
||||||
def form(self):
|
def form(self):
|
||||||
|
"""Method for returm the html code of the form
|
||||||
|
"""
|
||||||
|
|
||||||
return '<input type="'+self.type+'" class="'+self.css+'" name="'+self.name+'" id="'+self.name_field_id+'" value="'+self.setform(self.default_value)+'" />'
|
return '<input type="'+self.type+'" class="'+self.css+'" name="'+self.name+'" id="'+self.name_field_id+'" value="'+self.setform(self.default_value)+'" />'
|
||||||
|
|
||||||
def show_formatted(self, value):
|
def show_formatted(self, value):
|
||||||
|
"""Method for show the value of form formatted
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (mixed): The value of field form
|
||||||
|
"""
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
#Method for escape value for html input. DON'T CHANGE IF YOU DON'T KNOWN WHAT ARE YOU DOING
|
#Method for escape value for html input. DON'T CHANGE IF YOU DON'T KNOWN WHAT ARE YOU DOING
|
||||||
|
|
||||||
def setform(self, value):
|
def setform(self, value):
|
||||||
|
"""A method for set the value in the form for escape and other things
|
||||||
|
|
||||||
|
Args:
|
||||||
|
value (mixed): The value of field form for set
|
||||||
|
"""
|
||||||
|
|
||||||
value=str(value)
|
value=str(value)
|
||||||
|
|
||||||
return value.replace('"', '"').replace("'", ''')
|
return value.replace('"', '"').replace("'", ''')
|
||||||
|
|
||||||
def change_name(self, new_name):
|
def change_name(self, new_name):
|
||||||
|
"""A method for change the default form html name of the field form
|
||||||
|
|
||||||
|
Args:
|
||||||
|
new_name (str): The new name of the form. Always is finished with _form suffix
|
||||||
|
"""
|
||||||
|
|
||||||
self.name=new_name
|
self.name=new_name
|
||||||
|
|
||||||
|
|
@ -45,6 +84,8 @@ class BaseForm:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
class SimpleTextForm(BaseForm):
|
class SimpleTextForm(BaseForm):
|
||||||
|
"""Form for simple text
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
super().__init__(name, value)
|
super().__init__(name, value)
|
||||||
|
|
@ -56,6 +97,8 @@ class SimpleTextForm(BaseForm):
|
||||||
return super().form()+' '+self.after_text
|
return super().form()+' '+self.after_text
|
||||||
|
|
||||||
class TextForm(BaseForm):
|
class TextForm(BaseForm):
|
||||||
|
"""Form for simple text form
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
super(TextForm, self).__init__(name, value)
|
super(TextForm, self).__init__(name, value)
|
||||||
|
|
@ -65,6 +108,8 @@ class TextForm(BaseForm):
|
||||||
return '<textarea class="'+self.css+'" name="'+self.name+'" id="'+self.name+'_form">'+self.setform(self.default_value)+'</textarea>'
|
return '<textarea class="'+self.css+'" name="'+self.name+'" id="'+self.name+'_form">'+self.setform(self.default_value)+'</textarea>'
|
||||||
|
|
||||||
class PasswordForm(BaseForm):
|
class PasswordForm(BaseForm):
|
||||||
|
"""Form for password forms
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value, show_password=False):
|
def __init__(self, name, value, show_password=False):
|
||||||
super(PasswordForm, self).__init__(name, value)
|
super(PasswordForm, self).__init__(name, value)
|
||||||
|
|
@ -79,6 +124,8 @@ class PasswordForm(BaseForm):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class HiddenForm(BaseForm):
|
class HiddenForm(BaseForm):
|
||||||
|
"""Form for hidden forms
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value):
|
def __init__(self, name, value):
|
||||||
super(HiddenForm, self).__init__(name, value)
|
super(HiddenForm, self).__init__(name, value)
|
||||||
|
|
@ -86,8 +133,16 @@ class HiddenForm(BaseForm):
|
||||||
|
|
||||||
|
|
||||||
class SelectForm(BaseForm):
|
class SelectForm(BaseForm):
|
||||||
|
"""Form for select html form
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value, elements=OrderedDict()):
|
def __init__(self, name, value, elements=OrderedDict()):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
ame (str): The html name for this form
|
||||||
|
value (str): The default value of this html form
|
||||||
|
elements (OrderedDict): An ordered dict with the keys(the form value) and text label. Example, if you have a OrderedDict how {'0': 'Value selected'} in a html select form you have the next result: <select name="name"><option value="0">Value selected</option></selected>
|
||||||
|
"""
|
||||||
super(SelectForm, self).__init__(name, value)
|
super(SelectForm, self).__init__(name, value)
|
||||||
self.arr_select=elements
|
self.arr_select=elements
|
||||||
|
|
||||||
|
|
@ -107,8 +162,20 @@ class SelectForm(BaseForm):
|
||||||
return the_form
|
return the_form
|
||||||
|
|
||||||
class SelectModelForm(SelectForm):
|
class SelectModelForm(SelectForm):
|
||||||
|
"""Form for select html using a webmodel how base for get the data
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, value, model, field_name, field_value, field_parent=None):
|
def __init__(self, name, value, model, field_name, field_value, field_parent=None):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
name (str): The html name for this form
|
||||||
|
value (str): The default value of this html form.
|
||||||
|
arr_select (OrderedDict): Used for save the data from the webmodel
|
||||||
|
model (WebModel): The webmodel used for get the data for arr_select
|
||||||
|
field_name (str): The field used for get the name of every option in select
|
||||||
|
field_value (str): The field used for get the value of every option in select
|
||||||
|
field_parent (int): If the model have parents or children, the value of this argument
|
||||||
|
"""
|
||||||
super(SelectModelForm, self).__init__(name, value)
|
super(SelectModelForm, self).__init__(name, value)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -129,6 +196,10 @@ class SelectModelForm(SelectForm):
|
||||||
|
|
||||||
|
|
||||||
def normal_form(self):
|
def normal_form(self):
|
||||||
|
"""Method for prepare the form hierated from SelectForm class, without parents
|
||||||
|
|
||||||
|
Method for prepare the form hierated from SelectForm class getting data from database using model attribute.
|
||||||
|
"""
|
||||||
|
|
||||||
self.arr_select['']=''
|
self.arr_select['']=''
|
||||||
|
|
||||||
|
|
@ -147,6 +218,10 @@ class SelectModelForm(SelectForm):
|
||||||
return super().form()
|
return super().form()
|
||||||
|
|
||||||
def parent_form(self):
|
def parent_form(self):
|
||||||
|
"""Method for prepare the form hierated from SelectForm class, with parents
|
||||||
|
|
||||||
|
Method for prepare the form hierated from SelectForm class getting data from database using model attribute.
|
||||||
|
"""
|
||||||
|
|
||||||
self.arr_select['']=''
|
self.arr_select['']=''
|
||||||
|
|
||||||
|
|
@ -186,6 +261,8 @@ class SelectModelForm(SelectForm):
|
||||||
|
|
||||||
|
|
||||||
def create_son(self, parent_id, arr_son, separator=''):
|
def create_son(self, parent_id, arr_son, separator=''):
|
||||||
|
"""Recursive method for generate parents and children dictionary
|
||||||
|
"""
|
||||||
|
|
||||||
if parent_id in arr_son:
|
if parent_id in arr_son:
|
||||||
for son in arr_son[parent_id]:
|
for son in arr_son[parent_id]:
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ except:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
|
"""Function for create and update mysql tables using webmodel classes and fields how source.
|
||||||
|
"""
|
||||||
|
|
||||||
connection=WebModel.connection()
|
connection=WebModel.connection()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,14 @@ import traceback
|
||||||
engine=None
|
engine=None
|
||||||
|
|
||||||
class SqlClass:
|
class SqlClass:
|
||||||
|
"""Class used how interface to sqlalchemy for connect to mysql engine
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
cursors_connect (pymysql.cursors.DictCursor): Cursor dict connection to database
|
||||||
|
disable_pool (boolean): If True then not exists mysql pool, if False, use sql pool for better connections.
|
||||||
|
pymsql_install (boolean): If True, pymysql is used how mysqldb classic python module.
|
||||||
|
pool_size (int): The size of the mysql pool.
|
||||||
|
"""
|
||||||
|
|
||||||
cursors_connect=None
|
cursors_connect=None
|
||||||
disable_pool=False
|
disable_pool=False
|
||||||
|
|
@ -19,8 +27,18 @@ class SqlClass:
|
||||||
pool_size=15
|
pool_size=15
|
||||||
|
|
||||||
def __init__(self, connection):
|
def __init__(self, connection):
|
||||||
|
"""
|
||||||
|
Args:
|
||||||
|
connection (dict): A dict with the configurations of SqlClass connection
|
||||||
|
Attributes:
|
||||||
|
error_connection (str): A string where errors are saved
|
||||||
|
connection (dict): A dict with the configurations of SqlClass connection
|
||||||
|
conn (MySQL Connection): A PyMySQL or Mysqldb connection
|
||||||
|
connected (bool): Simple bool for check if was connected to mysql
|
||||||
|
pool_recycle (int): Time limite for recycle the pool by inactivity
|
||||||
|
"""
|
||||||
|
|
||||||
self.max_overflow=-1
|
#self.max_overflow=-1
|
||||||
self.error_connection=""
|
self.error_connection=""
|
||||||
# Data of connection
|
# Data of connection
|
||||||
self.connection=connection
|
self.connection=connection
|
||||||
|
|
@ -32,6 +50,8 @@ class SqlClass:
|
||||||
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
|
"""Method for connect to mysql db using pymysql or mysqldb
|
||||||
|
"""
|
||||||
|
|
||||||
global engine
|
global engine
|
||||||
|
|
||||||
|
|
@ -132,7 +152,13 @@ class SqlClass:
|
||||||
#Make def query more simple if not debugging.
|
#Make def query more simple if not debugging.
|
||||||
|
|
||||||
def query(self, sql_query, arguments=[], name_connection="default"):
|
def query(self, sql_query, arguments=[], name_connection="default"):
|
||||||
|
"""Method for send a sql query to mysql server
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sql_query (str): The sql sentence to execute. For data you should use %s character.
|
||||||
|
arguments (list): The data used in sql sentence. This data substitute the %s characters.
|
||||||
|
name_connection (str): The name of dict element with the configuration of connection, without used in this moment.
|
||||||
|
"""
|
||||||
|
|
||||||
cursor=self.conn.cursor(SqlClass.cursors_connect)
|
cursor=self.conn.cursor(SqlClass.cursors_connect)
|
||||||
|
|
||||||
|
|
@ -194,12 +220,16 @@ class SqlClass:
|
||||||
#return cursor.fetchone()
|
#return cursor.fetchone()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
|
"""Typical method used when class is deleted from memory. Close the connextion if exists.
|
||||||
|
"""
|
||||||
|
|
||||||
if self.conn:
|
if self.conn:
|
||||||
|
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
||||||
def close(self, name_connection="default"):
|
def close(self, name_connection="default"):
|
||||||
|
"""Method used for close the connection if you want close connection and open other.
|
||||||
|
"""
|
||||||
|
|
||||||
if self.conn:
|
if self.conn:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,19 @@ from paramecio2.libraries.i18n import I18n
|
||||||
from flask import request
|
from flask import request
|
||||||
|
|
||||||
class UserModel(WebModel):
|
class UserModel(WebModel):
|
||||||
|
"""Model used with basic things for users login and signup
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name_field_id="id"):
|
def __init__(self, name_field_id="id"):
|
||||||
|
"""
|
||||||
|
Attributes:
|
||||||
|
password_field (str): The name of the password field to check
|
||||||
|
email_field (str): The name of the email field to check
|
||||||
|
username_field (str): The name of the username field to check
|
||||||
|
yes_repeat_password (bool): If True, check password repeat field, if False, no check repeat password field
|
||||||
|
check_user (bool): If True, check if user exists in db table, if False, no check that.
|
||||||
|
check_email (bool): If True, check if email exists in db table, if False, no check that.
|
||||||
|
"""
|
||||||
|
|
||||||
super().__init__(name_field_id)
|
super().__init__(name_field_id)
|
||||||
|
|
||||||
|
|
@ -19,6 +30,11 @@ class UserModel(WebModel):
|
||||||
self.check_email=True
|
self.check_email=True
|
||||||
|
|
||||||
def create_forms(self, arr_fields=[]):
|
def create_forms(self, arr_fields=[]):
|
||||||
|
"""Method for create forms with checking of repeat password
|
||||||
|
|
||||||
|
Args:
|
||||||
|
arr_fields (list): List of fields used for generate the forms list
|
||||||
|
"""
|
||||||
|
|
||||||
# Add password_repeat to forms from the model
|
# Add password_repeat to forms from the model
|
||||||
|
|
||||||
|
|
@ -53,7 +69,14 @@ class UserModel(WebModel):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
||||||
|
"""Method for check all fields of a model for insert or update a row in table db. Special for UserModel class.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dict_values (dict): The dict of values to check
|
||||||
|
external_agent (bool): If True, the query is considered manipulated by external agent and the checks are stricts, if not, checks are not stricts
|
||||||
|
yes_update (bool): If True, the check need be done for update sql sentence, if False, the check is done for insert sql sentence
|
||||||
|
errors_set (str): If insert value, the errors are set for insert sql statement, if update value, then the errors are set for update sql statement.
|
||||||
|
"""
|
||||||
error=0
|
error=0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ import copy
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
class PhangoField:
|
class PhangoField:
|
||||||
|
"""Base class for fields used in WebModel classes
|
||||||
|
|
||||||
|
PhangoField is a class with all elements and variables that you can imagine for a
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, name, size=255, required=False):
|
def __init__(self, name, size=255, required=False):
|
||||||
|
|
||||||
|
|
@ -1099,6 +1103,14 @@ class WebModel:
|
||||||
#Check of all fields in table.
|
#Check of all fields in table.
|
||||||
|
|
||||||
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
def check_all_fields(self, dict_values, external_agent, yes_update=False, errors_set="insert"):
|
||||||
|
"""Method for check all fields of a model for insert or update a row in table db.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dict_values (dict): The dict of values to check
|
||||||
|
external_agent (bool): If True, the query is considered manipulated by external agent and the checks are stricts, if not, checks are not stricts
|
||||||
|
yes_update (bool): If True, the check need be done for update sql sentence, if False, the check is done for insert sql sentence
|
||||||
|
errors_set (str): If insert value, the errors are set for insert sql statement, if update value, then the errors are set for update sql statement.
|
||||||
|
"""
|
||||||
|
|
||||||
fields=[]
|
fields=[]
|
||||||
values=[]
|
values=[]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue