From b58f734064ad3d01aac3c8be2dfbe3fca7fc3de6 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Sat, 11 Jun 2016 05:11:51 +0200 Subject: [PATCH] Added parentfield and new method in phangofield for register webmodel method --- paramecio/citoplasma/adminutils.py | 33 +++++++++++ paramecio/citoplasma/hierarchy_links.py | 57 +++++++++++++++++++ paramecio/cromosoma/corefields.py | 1 + paramecio/cromosoma/coreforms.py | 53 +++++++++++++++-- .../cromosoma/extrafields/parentfield.py | 41 +++++++++++++ paramecio/cromosoma/webmodel.py | 8 +++ 6 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 paramecio/citoplasma/adminutils.py create mode 100644 paramecio/cromosoma/extrafields/parentfield.py diff --git a/paramecio/citoplasma/adminutils.py b/paramecio/citoplasma/adminutils.py new file mode 100644 index 0000000..3d7676b --- /dev/null +++ b/paramecio/citoplasma/adminutils.py @@ -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 diff --git a/paramecio/citoplasma/hierarchy_links.py b/paramecio/citoplasma/hierarchy_links.py index 2921a75..06490c3 100644 --- a/paramecio/citoplasma/hierarchy_links.py +++ b/paramecio/citoplasma/hierarchy_links.py @@ -1,5 +1,7 @@ #/usr/bin/python3 +from paramecio.citoplasma.urls import add_get_parameters + class HierarchyLinks: def __init__(arr_links, t=None): @@ -74,3 +76,58 @@ class HierarchyLinks: 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 '%s' % (add_get_parameters(self.base_url, **args), title) + + def show(self, son_id, separator=' >> '): + + 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) + + + + + + diff --git a/paramecio/cromosoma/corefields.py b/paramecio/cromosoma/corefields.py index e16266d..a30c0ed 100644 --- a/paramecio/cromosoma/corefields.py +++ b/paramecio/cromosoma/corefields.py @@ -116,6 +116,7 @@ class ForeignKeyField(IntegerField): def get_type_sql(self): return 'INT NULL' + class BooleanField(IntegerField): diff --git a/paramecio/cromosoma/coreforms.py b/paramecio/cromosoma/coreforms.py index 30f2026..11d8369 100644 --- a/paramecio/cromosoma/coreforms.py +++ b/paramecio/cromosoma/coreforms.py @@ -88,14 +88,21 @@ class SelectForm(BaseForm): 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) self.arr_select=OrderedDict() self.model=model self.field_name=field_name 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['']='' @@ -107,6 +114,44 @@ class SelectModelForm(SelectForm): 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) diff --git a/paramecio/cromosoma/extrafields/parentfield.py b/paramecio/cromosoma/extrafields/parentfield.py new file mode 100644 index 0000000..07f5141 --- /dev/null +++ b/paramecio/cromosoma/extrafields/parentfield.py @@ -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 diff --git a/paramecio/cromosoma/webmodel.py b/paramecio/cromosoma/webmodel.py index 1be3d6a..1fe50ad 100644 --- a/paramecio/cromosoma/webmodel.py +++ b/paramecio/cromosoma/webmodel.py @@ -140,6 +140,8 @@ class WebModel: self.fields[field_model.name].required=required + self.fields[field_model.name].post_register() + #self.files_delete[field_model.name]=field_model.file_related # A method for create the id field. @@ -992,6 +994,10 @@ class PhangoField: # Extra parameters for the form 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. @@ -1044,6 +1050,8 @@ class PhangoField: self.extra_parameters=parameters + def post_register(self): + pass class PrimaryKeyField(PhangoField):