Added parentfield and new method in phangofield for register webmodel method

This commit is contained in:
Antonio de la Rosa 2016-06-11 05:11:51 +02:00
parent f0c9014a9a
commit b58f734064
6 changed files with 189 additions and 4 deletions

View file

@ -0,0 +1,33 @@
#!/usr/bin/python3
from collections import OrderedDict
def get_language(s):
s['lang']=s.get('lang', None)
lang_selected=None
if s['lang']!=None:
lang_selected=s['lang']
else:
s['lang']=I18n.default_lang
lang_selected=I18n.default_lang
return lang_selected
def get_menu(modules_admin):
menu=OrderedDict()
for mod in modules_admin:
if type(mod[1]).__name__!='list':
menu[mod[2]]=mod
else:
menu[mod[2]]=mod[0]
for submod in mod[1]:
menu[submod[2]]=submod
return menu

View file

@ -1,5 +1,7 @@
#/usr/bin/python3 #/usr/bin/python3
from paramecio.citoplasma.urls import add_get_parameters
class HierarchyLinks: class HierarchyLinks:
def __init__(arr_links, t=None): def __init__(arr_links, t=None):
@ -74,3 +76,58 @@ class HierarchyLinks:
return 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):
with self.model.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]]=[arr_model[self.field_name], arr_model[self.field_parent]]
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; '):
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

@ -117,6 +117,7 @@ class ForeignKeyField(IntegerField):
return 'INT NULL' return 'INT NULL'
class BooleanField(IntegerField): class BooleanField(IntegerField):
def __init__(self, name, size=1): def __init__(self, name, size=1):

View file

@ -88,14 +88,21 @@ class SelectForm(BaseForm):
class SelectModelForm(SelectForm): class SelectModelForm(SelectForm):
def __init__(self, name, value, model, field_name, field_value): def __init__(self, name, value, model, field_name, field_value, field_parent=None):
super(SelectModelForm, self).__init__(name, value) super(SelectModelForm, self).__init__(name, value)
self.arr_select=OrderedDict() self.arr_select=OrderedDict()
self.model=model self.model=model
self.field_name=field_name self.field_name=field_name
self.field_value=field_value self.field_value=field_value
self.field_parent=field_parent
def form(self): self.form=self.normal_form
if self.field_parent!=None:
self.form=self.parent_form
def normal_form(self):
self.arr_select['']='' self.arr_select['']=''
@ -107,6 +114,44 @@ class SelectModelForm(SelectForm):
return super().form() return super().form()
def parent_form(self):
self.arr_select['']=''
arr_son={}
old_conditions=self.model.conditions
old_limit=self.model.limit
self.model.limit=''
self.model.set_conditions('WHERE 1=1', [])
with self.model.select([self.field_name, self.field_value, self.field_parent], True) as cur:
for arr_value in cur:
#self.arr_select[arr_value[self.field_value]]=arr_value[self.field_name]
if not self.field_parent in arr_son:
arr_son[arr_value[self.field_parent]]=[]
arr_son[arr_value[self.field_parent]].append([arr_value[self.field_value], arr_value[self.field_name]])
self.create_son(0, arr_son)
self.model.conditions=old_conditions
self.model.limit=old_limit
return super().form()
def create_son(self, parent_id, arr_son, separator=''):
if parent_id in arr_son:
for son in arr_son[parent_id]:
self.arr_select[son[0]]=separator+son[1]
if son[0] in arr_son:
separator+='--'
self.create_son(son[0],arr_son, separator)

View file

@ -0,0 +1,41 @@
#!/usr/bin/python3
#from paramecio.cromosoma.webmodel import PhangoField
from paramecio.cromosoma.corefields import IntegerField
from paramecio.cromosoma.coreforms import SelectModelForm
from paramecio.citoplasma.httputils import GetPostFiles
class ParentField(IntegerField):
def __init__(self, name, size=11, required=False, field_name='name'):
super().__init__(name, size, required)
#self.foreignkey=True
self.indexed=True
self.field_name=field_name
def post_register(self):
if self.model!=None:
self.change_form(SelectModelForm, [self.model, self.field_name, self.model.name_field_id, self.name])
def check(self, value):
value=super().check(value)
if self.model!=None:
if self.model.updated==True:
if self.model.name_field_id in self.model.post:
GetPostFiles.obtain_get()
model_id=GetPostFiles.get.get(self.model.name_field_id, '0')
if model_id==value:
self.error=True
self.txt_error='A field cannot be its own father'
value=0
return value
return value

View file

@ -140,6 +140,8 @@ class WebModel:
self.fields[field_model.name].required=required self.fields[field_model.name].required=required
self.fields[field_model.name].post_register()
#self.files_delete[field_model.name]=field_model.file_related #self.files_delete[field_model.name]=field_model.file_related
# A method for create the id field. # A method for create the id field.
@ -993,6 +995,10 @@ class PhangoField:
self.extra_parameters=[] self.extra_parameters=[]
# Template manager for the form if needed
self.t=None
# This method is used for describe the new field in a sql language format. # This method is used for describe the new field in a sql language format.
@ -1044,6 +1050,8 @@ class PhangoField:
self.extra_parameters=parameters self.extra_parameters=parameters
def post_register(self):
pass
class PrimaryKeyField(PhangoField): class PrimaryKeyField(PhangoField):