modern #2

Merged
absurdo merged 38 commits from modern into master 2025-03-15 00:00:11 +00:00
7 changed files with 119 additions and 55 deletions
Showing only changes of commit 6b0ca95ac0 - Show all commits

View file

@ -48,6 +48,8 @@ def start():
#lang_t=re.compile("\${lang\('("+module_base+"?)',\s+'(.*?)',\s+'(.*?)'\)\}")
lang_t=re.compile("lang\('("+module_base+"?)',\s+'(.*?)',\s+'(.*?)'\)")
lang_s=re.compile("slang\('(.*?)',\s+'(.*?)'\)"
if not os.path.isdir(path):
print("Error: directory to scan doesn't exists")

View file

@ -178,23 +178,29 @@ def start():
print("--Adding indexes and constraints for the new table "+table)
for k_field, index in WebModel.arr_sql_index[table].items():
print("---Added index to "+k_field)
connection.query(index)
if table in WebModel.arr_sql_index:
for k_set, index_set in WebModel.arr_sql_set_index[table].items():
for k_field, index in WebModel.arr_sql_index[table].items():
print("---Added index to "+k_field)
connection.query(index)
if index_set!="":
connection.query(index_set)
print("---Added constraint to "+k_set)
if table in WebModel.arr_sql_set_index:
for k_set, index_set in WebModel.arr_sql_set_index[table].items():
if index_set!="":
connection.query(index_set)
print("---Added constraint to "+k_set)
print("--Adding uniques elements for the new table")
for k_field, unique_set in WebModel.arr_sql_unique[table].items():
if table in WebModel.arr_sql_unique:
if unique_set!="":
connection.query(unique_set)
print("---Added unique to "+unique_set)
for k_field, unique_set in WebModel.arr_sql_unique[table].items():
if unique_set!="":
connection.query(unique_set)
print("---Added unique to "+unique_set)
#See if changes exists

View file

@ -356,6 +356,7 @@ class WebModel:
show_formatted (bool): If True, by default all fields are showed with formatted value using show_formatted method of PhangoField classes and children in select method. If False, raw value is showed.
enctype (bool): If True, forms generated using this model are prepared for enctype=multipart/form-data A.K.A. upload files.
model_id (int): Variable where the actual row from model selected can be saved for different things.
field_quote(str): The field delimiter. In Mariadb is `, in PostgreSQL in the future is \"
"""
self.cached=WebModel.global_cached
@ -464,6 +465,8 @@ class WebModel:
self.dummy=0
self.field_quote='`'
# A method for add the connection
def conn(self, sqlclass):
@ -480,7 +483,11 @@ class WebModel:
self.conditions=["WHERE 1=1", []]
self.order_by="ORDER BY `"+self.name+"`.`id` ASC"
name_table=self.field_quote+self.name+self.field_quote
field_id=self.field_quote+"id"+self.field_quote
self.order_by="ORDER BY "+name_table+"."+field_id+" ASC"
self.limit=""
@ -495,7 +502,11 @@ class WebModel:
self.name=name
self.order_by="ORDER BY `"+self.name+"`.`id` ASC"
name_table=self.field_quote+self.name+self.field_quote
field_id=self.field_quote+"id"+self.field_quote
self.order_by="ORDER BY "+name_table+"."+field_id+" ASC"
# A method where create the new fields of this model
@ -624,7 +635,11 @@ class WebModel:
arr_str=['%s' for x in range(c)]
sql="insert into `"+self.name+"` (`"+"`, `".join(fields)+"`) VALUES ("+", ".join(arr_str)+")"
join_fields="`, `".join(fields)
join_values=", ".join(arr_str)
sql="insert into `"+self.name+self.field_quote+" ("+self.field_quote+join_fields+self.field_quote+") VALUES ("+join_values+")"
cursor=self.query(sql, values, self.connection_id)
@ -692,7 +707,9 @@ class WebModel:
#print(traceback.format_exc())
return False
sql="update `"+self.name+"` SET "+", ".join(update_values)+" "+self.conditions[0]
field_name=self.field_quote+self.name+self.field_quote
sql="update "+field_name+" SET "+", ".join(update_values)+" "+self.conditions[0]
cursor=self.query(sql, values+self.conditions[1], self.connection_id)
@ -768,7 +785,7 @@ class WebModel:
#First table selecction
tables_to_select=['`'+self.name+'`']
tables_to_select=[self.field_quote+self.name+self.field_quote]
keys=list(self.fields.keys())
@ -801,21 +818,21 @@ class WebModel:
else:
arr_repeat_field[self.fields[field].table_name]=0
table_name=self.fields[field].table_name+'` as `'+self.fields[field].table_name+str(arr_repeat_field[self.fields[field].table_name])
table_name=self.fields[field].table_name+self.field_quote+' as '+self.field_quote+self.fields[field].table_name+str(arr_repeat_field[self.fields[field].table_name])
final_table_name=self.fields[field].table_name+str(arr_repeat_field[self.fields[field].table_name])
# The name with its alias of this related table model
tables_to_select.append('`'+table_name+'`')
tables_to_select.append(self.field_quote+table_name+self.field_quote)
# Add field from related table
# as "+table_name+"_"+self.fields[field].named_field
extra_fields.append("`"+final_table_name+"`.`"+self.fields[field].named_field+"` as "+field)
extra_fields.append(self.field_quote+final_table_name+self.field_quote+"."+self.field_quote+self.fields[field].named_field+self.field_quote+" as "+field)
# Add a condition to sql query for join the two tables.
conditions[0]+=" AND `"+final_table_name+"`.`"+self.fields[field].identifier_field+"`=`"+self.name+"`.`"+field+"`"
conditions[0]+=" AND "+self.field_quote+final_table_name+self.field_quote+"."+self.field_quote+self.fields[field].identifier_field+self.field_quote+"="+self.field_quote+self.name+self.field_quote+"."+self.field_quote+field+self.field_quote
# Add extra fields from related table from select_fields ForeignKeyField class member
@ -828,11 +845,11 @@ class WebModel:
# Check if extra_field is ForeignKeyField, if yes, call this function recursively.
extra_fields.append("`"+final_table_name+"`.`"+extra_field+"` as `"+field+"_"+extra_field+"`")
extra_fields.append(self.field_quote+final_table_name+self.field_quote+"."+self.field_quote+extra_field+self.field_quote+" as "+self.field_quote+field+"_"+extra_field+self.field_quote)
else:
# Add normal field to sql query
final_fields.append("`"+self.name+"`.`"+field+"`")
final_fields.append(self.field_quote+self.name+self.field_quote+"."+self.field_quote+field+self.field_quote)
#if len(new_fields)>0:
#self.fields.update(new_fields)
@ -890,7 +907,7 @@ class WebModel:
id (int): The id of the row to search.
"""
self.conditions=['WHERE `'+self.name_field_id+'`=%s', [id]]
self.conditions=['WHERE '+self.field_quote+self.name_field_id+self.field_quote+'=%s', [id]]
count=self.select_count(self.name_field_id)
@ -916,7 +933,7 @@ class WebModel:
row (dict): Returns dict with the row values.
"""
self.conditions=['WHERE `'+self.name+'`.`'+self.name_field_id+'`=%s', [id]]
self.conditions=['WHERE '+self.field_quote+self.name+self.field_quote+'.'+self.field_quote+self.name_field_id+self.field_quote+'=%s', [id]]
self.limit="limit 1"
@ -1086,7 +1103,7 @@ class WebModel:
#First table selecction
tables_to_select=['`'+self.name+'`']
tables_to_select=[self.field_quote+self.name+self.field_quote]
fields=list(self.fields.keys())
@ -1100,13 +1117,13 @@ class WebModel:
table_name=self.fields[field].table_name
tables_to_select.append('`'+table_name+'`')
tables_to_select.append(self.field_quote+table_name+self.field_quote)
# Add a condition to sql query for join the two tables.
conditions[0]+=" AND `"+table_name+"`.`"+self.fields[field].identifier_field+"`=`"+self.name+"`.`"+field+"`"
conditions[0]+=" AND "+self.field_quote+table_name+self.field_quote+"."+self.field_quote+self.fields[field].identifier_field+self.field_quote+"="+self.field_quote+self.name+self.field_quote+"."+self.field_quote+field+self.field_quote
sql= "select count(`"+field_to_count+"`) from "+", ".join(tables_to_select)+' '+conditions[0]
sql= "select count("+self.field_quote+field_to_count+self.field_quote+") from "+", ".join(tables_to_select)+' '+conditions[0]
count=0
@ -1135,7 +1152,7 @@ class WebModel:
#+' '+self.order_by+' '+self.limit
sql=("delete from `"+self.name+"` "+self.conditions[0]).strip()
sql=("delete from "+self.field_quote+self.name+self.field_quote+" "+self.conditions[0]).strip()
result=self.query(sql, self.conditions[1], self.connection_id)
@ -1271,13 +1288,13 @@ class WebModel:
fields=self.fields
for field, data in fields.items():
table_fields.append('`'+field+'` '+data.get_type_sql())
table_fields.append(self.field_quote+field+self.field_quote+' '+data.get_type_sql())
#Check if indexed
if fields[field].indexed==True:
self.arr_sql_index[self.name][field]='CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+'(`'+field+'`);'
self.arr_sql_index[self.name][field]='CREATE INDEX '+self.field_quote+'index_'+self.name+'_'+field+self.field_quote+' ON '+self.name+'('+self.field_quote+field+self.field_quote+');'
self.arr_sql_set_index[self.name][field]=""
@ -1285,20 +1302,20 @@ class WebModel:
if fields[field].unique==True:
self.arr_sql_unique[self.name][field]='ALTER TABLE `'+self.name+'` ADD UNIQUE (`'+field+'`)'
self.arr_sql_unique[self.name][field]='ALTER TABLE '+self.field_quote+self.name+self.field_quote+' ADD UNIQUE ('+self.field_quote+field+self.field_quote+')'
self.arr_sql_set_unique[self.name][field]=""
if type(fields[field]).__name__=="ForeignKeyField":
self.arr_sql_index[self.name][field]='CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+'(`'+field+'`);'
self.arr_sql_index[self.name][field]='CREATE INDEX '+self.field_quote+'index_'+self.name+'_'+field+self.field_quote+' ON '+self.name+'('+self.field_quote+field+self.field_quote+');'
table_related=fields[field].table_name
id_table_related=fields[field].table_id
self.arr_sql_set_index[self.name][field]='ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE CASCADE ON UPDATE CASCADE;'
self.arr_sql_set_index[self.name][field]='ALTER TABLE '+self.field_quote+self.name+self.field_quote+' ADD CONSTRAINT '+self.field_quote+field+'_'+self.name+'IDX'+self.field_quote+' FOREIGN KEY ( '+self.field_quote+field+self.field_quote+' ) REFERENCES '+self.field_quote+table_related+self.field_quote+' ('+self.field_quote+id_table_related+self.field_quote+') ON DELETE CASCADE ON UPDATE CASCADE;'
return "create table `"+self.name+"` (\n"+",\n".join(table_fields)+"\n) DEFAULT CHARSET=utf8;";
return "create table "+self.field_quote+self.name+self.field_quote+" (\n"+",\n".join(table_fields)+"\n) DEFAULT CHARSET=utf8;";
def update_table(self, fields_to_add, fields_to_modify, fields_to_add_index, fields_to_add_constraint, fields_to_add_unique, fields_to_delete_index, fields_to_delete_unique, fields_to_delete_constraint, fields_to_delete):
@ -1308,40 +1325,40 @@ class WebModel:
print("---Deleting index from "+field+" in "+self.name)
self.query('DROP INDEX `index_'+self.name+'_'+field+'` ON '+self.name, [], self.connection_id)
self.query('DROP INDEX '+self.field_quote+'index_'+self.name+'_'+field+self.field_quote+' ON '+self.name, [], self.connection_id)
for field in fields_to_delete_unique:
print("---Deleting unique from "+field+" in "+self.name)
self.query('DROP INDEX `'+field+'` ON '+self.name, [], self.connection_id)
self.query('DROP INDEX '+self.field_quote+field+self.field_quote+' ON '+self.field_quote+self.name+self.field_quote, [], self.connection_id)
for field in fields_to_delete_constraint:
print("---Deleting foreignkey from "+field+" in "+self.name)
self.query('ALTER TABLE `'+self.name+'` DROP FOREIGN KEY '+field+'_'+self.name+'IDX', [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' DROP FOREIGN KEY '+field+'_'+self.name+'IDX', [], self.connection_id)
for field in fields_to_delete:
print("---Deleting "+field+" from "+self.name)
self.query('ALTER TABLE `'+self.name+'` DROP `'+field+'`', [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' DROP '+self.field_quote+field+self.field_quote, [], self.connection_id)
#Deleting indexes and constraints.
#Obtain new fields
for field in fields_to_modify:
print("---Updating "+field+" in "+self.name)
self.query('ALTER TABLE `'+self.name+'` MODIFY `'+field+'` '+self.fields[field].get_type_sql(), [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' MODIFY '+self.field_quote+field+self.field_quote+' '+self.fields[field].get_type_sql(), [], self.connection_id)
for field in fields_to_add:
print("---Adding "+field+" in "+self.name)
self.query('ALTER TABLE `'+self.name+'` ADD `'+field+'` '+self.fields[field].get_type_sql(), [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' ADD '+self.field_quote+field+self.field_quote+' '+self.fields[field].get_type_sql(), [], self.connection_id)
for field in fields_to_add_index:
print("---Adding index to "+field+" in "+self.name)
self.query('CREATE INDEX `index_'+self.name+'_'+field+'` ON '+self.name+' (`'+field+'`);', [], self.connection_id)
self.query('CREATE INDEX '+self.field_quote+'index_'+self.name+'_'+field+self.field_quote+' ON '+self.name+' ('+self.field_quote+field+self.field_quote+');', [], self.connection_id)
for field in fields_to_add_constraint:
@ -1351,13 +1368,13 @@ class WebModel:
id_table_related=self.fields[field].table_id
self.query('ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE CASCADE ON UPDATE CASCADE;', [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' ADD CONSTRAINT '+self.field_quote+field+'_'+self.name+'IDX'+self.field_quote+' FOREIGN KEY ( '+self.field_quote+field+self.field_quote+' ) REFERENCES '+self.field_quote+table_related+self.field_quote+' ('+self.field_quote+id_table_related+self.field_quote+') ON DELETE CASCADE ON UPDATE CASCADE;', [], self.connection_id)
for field in fields_to_add_unique:
print("---Adding unique to "+field+" in "+self.name)
self.query('ALTER TABLE `'+self.name+'` ADD UNIQUE (`'+field+'`)', [], self.connection_id)
self.query('ALTER TABLE '+self.field_quote+self.name+self.field_quote+' ADD UNIQUE ('+self.field_quote+field+self.field_quote+')', [], self.connection_id)
@ -1370,7 +1387,7 @@ class WebModel:
sql_str (str): Return the sql query for drop the table represented by this model
"""
return self.query('DROP TABLE '+self.name, [], self.connection_id)
return self.query('DROP TABLE '+self.field_quote+self.name+self.field_quote, [], self.connection_id)
#Return an array with all fields
@ -1410,7 +1427,7 @@ class WebModel:
error=False
if yes_update==True:
f_update=lambda field, value: "`"+field+"`=%s"
f_update=lambda field, value: self.field_quote+field+self.field_quote+"=%s"
else:
f_update=lambda field, value: ""
@ -1697,6 +1714,6 @@ class QueryModel(WebModel):
self.label_general=self.name
self.order_by="ORDER BY `"+self.name+"`.`id` ASC"
self.order_by="ORDER BY "+self.field_quote+self.name+self.field_quote+"."+self.field_quote+"id"+self.field_quote+" ASC"

View file

@ -49,6 +49,8 @@ class GenerateAdminClass:
self.url_redirect=self.url
self.pre_update=None
self.post_update=None
self.text_home=I18n.lang('common', 'home', 'Home')
@ -126,6 +128,9 @@ class GenerateAdminClass:
title_edit=I18n.lang('common', 'edit_new_item', 'Edit item')
self.model.conditions=['WHERE `'+self.model.name+'`.`'+self.model.name_field_id+'`=%s', [getpostfiles.get['id']]]
if self.pre_update:
getpostfiles.post=self.pre_update(self, getpostfiles.post)
if insert_row(getpostfiles.post):
set_flash_message(I18n.lang('common', 'task_successful', 'Task successful'))
@ -209,6 +214,8 @@ class GenerateConfigClass:
self.template_insert='utils/insertform.phtml'
self.pre_update=None
self.post_update=None
self.text_home=I18n.lang('common', 'home', 'Home')

View file

@ -106,6 +106,36 @@ class I18n:
l={}
def __init__(self, module):
self.module=module
def slang(self, symbol, text_default, lang=None):
"""Method for get a string from selected language but object oriented
Method for get a string from selected language but object oriented
Args:
symbol (str): The symbol used for identify the text string.
text_default (str): The text default used. You have use how base for translations.
"""
return I18n.lang(self.module, symbol, text_default, lang)
def tlang(self, text_default, lang=None):
"""Method for get a string from selected language but object oriented and using module and symbol by default
Method for get a string from selected language but object oriented and using module and symbol by default
Args:
symbol (str): The symbol used for identify the text string.
text_default (str): The text default used. You have use how base for translations.
"""
symbol=text_default[:60]
return I18n.lang(self.module, symbol, text_default, lang)
#@staticmethod
#def set_lang(code_lang):
# if default_lang

View file

@ -192,6 +192,12 @@ class PTemplate:
self.add_filter(self._)
self.i18n=I18n(base_name)
self.add_filter(self.i18n.slang)
self.add_filter(self.i18n.tlang)
def _(self, text):
return self.l.gettext(text)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from paramecio.libraries.keyutils import create_key_encrypt, create_key_encrypt_256, create_key
from oslo_concurrency import lockutils
#from oslo_concurrency import lockutils
try:
@ -211,10 +211,6 @@ else:
def generate_session(session={}, max_age=None):
#secret=URLSafeSerializer(config.key_encrypt)
#session=secret.dumps(session)
token=create_key(30).replace('/', '#')
s={'token': token}
@ -276,7 +272,7 @@ else:
return s
@lockutils.synchronized('not_thread_safe')
#@lockutils.synchronized('not_thread_safe')
def save_session(token, session, create_file=False):
file_session=config.session_opts['session.data_dir']+'/'+token+'_session'