Added hierarchy_links utility

This commit is contained in:
absurdo 2024-03-01 01:04:06 +01:00
parent bdd149749e
commit 60ef008c58
5 changed files with 156 additions and 2 deletions

View file

@ -263,7 +263,8 @@ class SelectModelForm(SelectForm):
arr_son[arr_value[self.field_parent]]=[]
arr_son[arr_value[self.field_parent]].append([arr_value[self.field_value], self.model.fields[self.field_name].show_formatted(arr_value[self.field_name])])
if arr_value[self.field_value]!=self.model.model_id:
arr_son[arr_value[self.field_parent]].append([arr_value[self.field_value], self.model.fields[self.field_name].show_formatted(arr_value[self.field_name])])
self.create_son(0, arr_son)

View file

@ -352,6 +352,7 @@ class WebModel:
sqlclass (SqlClass): A sql_class used for connect to db.
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.
"""
self.cached=WebModel.global_cached
@ -456,6 +457,8 @@ class WebModel:
self.enctype=False
self.model_id=0
self.dummy=0
# A method for add the connection

View file

@ -156,9 +156,14 @@ class GenerateAdminClass:
if post==None or post==False:
if item_id=='0':
self.model.model_id=0
post={}
else:
return ""
else:
self.model.model_id=int(item_id)
url_action=add_get_parameters(self.url, op_admin=2, id=item_id)

View file

@ -0,0 +1,145 @@
#/usr/bin/env python3
#from paramecio.citoplasma.urls import add_get_parameters
from paramecio2.libraries.urls import add_get_parameters
class HierarchyLinks:
def __init__(arr_links, t=None):
self.arr_links=arr_links
self.arr_indexes={}
def update_links(self, link_father, link_son, text):
self.arr_links[link_father][link_son]=text
def calculate_indexes():
#oreach(self.arr_links as $father_link => $arr_child_links)
for father_link, arr_child_links in self.arr_links.items():
#foreach($arr_child_links as $link => $text)
for link, text in self.arr_child_links.items():
self.arr_indexes[link]=father_link
def result(last_link, arr_result=[], yes_last_link=0):
self.calculate_indexes()
if last_link in self.arr_indexes:
father=self.arr_indexes[last_link]
arr_last_link[0]=self.no_link
arr_last_link[1]=self.yes_link
yes_link_func=arr_last_link[yes_last_link]
if father!='':
arr_result.append(self.yes_link_func(last_link, self.arr_links[father][last_link]))
yes_last_link=1
arr_result=self.result(father, arr_result, yes_last_link)
return arr_result
else:
arr_result.append(self.yes_link_func(last_link, self.arr_links[father][last_link]))
return arr_result
return arr_result
def show(link, separator='>>', class_link=''):
arr_result=self.result(link)
arr_result=array_reverse(arr_result)
return ' '+separator+' '.join(arr_result)
def yes_link(link, text):
return '<a href="'+link+'">'+text+'</a>'
def no_link(link, text):
return text
class HierarchyModelLinks:
def __init__(self, model, first_element_title, field_name, field_parent, base_url):
self.model=model
self.field_parent=field_parent
self.field_name=field_name
self.base_url=base_url
self.arr_parent={}
self.arr_son=[]
self.first_element_title=first_element_title
def prepare(self):
conditions=self.model.conditions
with self.model.set_conditions('', []).select([self.model.name_field_id, self.field_name, self.field_parent]) as cur:
for arr_model in cur:
if self.field_parent not in self.arr_parent:
self.arr_parent[arr_model[self.model.name_field_id]]=[]
self.arr_parent[arr_model[self.model.name_field_id]]=[self.model.fields[self.field_name].show_formatted(arr_model[self.field_name]), arr_model[self.field_parent]]
self.model.conditions=conditions
def parents(self, son_id, url_func):
if son_id not in self.arr_parent or son_id==0:
return
self.arr_son.insert(0, url_func(son_id, self.arr_parent[son_id][0]))
self.parents(self.arr_parent[son_id][1], self.url)
def no_url(self, son_id, title):
return title
def url(self, son_id, title):
args={}
args[self.field_parent]=str(son_id)
return '<a href="%s">%s</a>' % (add_get_parameters(self.base_url, **args), title)
def show(self, son_id, separator=' &gt;&gt; '):
try:
son_id=int(son_id)
except:
son_id=0
self.prepare()
self.parents(son_id, self.no_url)
self.arr_son.insert(0, self.url(0, self.first_element_title))
return separator.join(self.arr_son)

View file

@ -13,7 +13,7 @@ if sys.version_info < (3, 9):
# If you install passlib and bcrypt, the password system will use bcrypt by default, if not, will use native crypt libc
setup(name='paramecio2',
version='2.0.32 ',
version='2.0.33 ',
description='Simple Web Framework based in flask and Mako.',
long_description='This framework is a 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 admin sites',
author='Antonio de la Rosa Caballero',