diff --git a/paramecio2/libraries/db/corefields.py b/paramecio2/libraries/db/corefields.py index a72861a..d34a4f0 100644 --- a/paramecio2/libraries/db/corefields.py +++ b/paramecio2/libraries/db/corefields.py @@ -38,6 +38,9 @@ class IntegerField(PhangoField): super(IntegerField, self).__init__(name, size, required) self.default_value=0 + + + self.type_sql='int({})'.format(self.size) def check(self, value): @@ -82,6 +85,11 @@ class BigIntegerField(IntegerField): """ + def __init__(self, name, size=11, required=False): + + super().__init__(name, size, required) + self.type_sql='bigint({})'.format(self.size) + def get_type_sql(self): """Method for return the sql code for this type @@ -107,6 +115,7 @@ class FloatField(PhangoField): self.error_default="The value is zero" self.default_value=0 + self.type_sql='float'.format(self.size) def check(self, value): @@ -147,6 +156,11 @@ class FloatField(PhangoField): class DecimalField(FloatField): """PhangoField field for Decimals fields.""" + def __init__(self, name, size=11, required=False): + + super().__init__(name, size, required) + self.type_sql='decimal(20,2)' + def get_type_sql(self): return 'DECIMAL(20, 2) NOT NULL DEFAULT "0"' @@ -154,6 +168,11 @@ class DecimalField(FloatField): class DoubleField(FloatField): """PhangoField field for Double fields.""" + def __init__(self, name, size=11, required=False): + + super().__init__(name, size, required) + self.type_sql='double' + def get_type_sql(self): return 'DOUBLE NOT NULL DEFAULT "0"' @@ -182,6 +201,8 @@ class TextField(PhangoField): super().__init__(name, 11, required) + self.type_sql='text' + self.set_default='NOT NULL' def get_type_sql(self): @@ -197,7 +218,21 @@ class LongTextField(TextField): Class used for text fields, use LONGTEXT sql type for the this field. """ + + def __init__(self, name, required=False): + """Init TextField class different to standard PhangoField + Args: + name (str): The name of new field + required (bool): Boolean for define if field is required or not + + Attributes: + set_default (str): Set if the value es NOT NULL or not + """ + + super().__init__(name, required) + self.type_sql='longtext' + def get_type_sql(self): """Method for return the sql code for this type @@ -339,6 +374,8 @@ class BooleanField(IntegerField): self.default_error="Need 0 or 1 value" self.default_value=0 + self.type_sql='tinyint(1)' + def check(self, value): self.error=False diff --git a/paramecio2/libraries/db/dbadmin.py b/paramecio2/libraries/db/dbadmin.py index 625c6fb..a2d39f2 100644 --- a/paramecio2/libraries/db/dbadmin.py +++ b/paramecio2/libraries/db/dbadmin.py @@ -130,6 +130,32 @@ def start(): new_tables=[x for x in tables if x not in table_exists] + # Get foreignkeys + + # SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA='catalogdev_db' AND information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' ; + + foreignkey_fields={} + + #| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE | + #+--------------------+-------------------+-----------------------------------------+---------------+-------------------+-----------------+ + #| def | catalogdev_db | product_id_attributesIDX | catalogdev_db | attributes | FOREIGN KEY | + + + #WebModel.connections + + db_name=WebModel.connections['default']['db'] + + with connection.query('SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA=%s AND information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = %s', [db_name, 'FOREIGN KEY']) as cursor: + + for row in cursor: + if not row['TABLE_NAME'] in foreignkey_fields: + foreignkey_fields[row['TABLE_NAME']]=[] + + foreignkey_fields[row['TABLE_NAME']]=row['CONSTRAINT_NAME'].replace('_{}IDX'.format(row['TABLE_NAME']), '') + + pass + + #If don't want order #new_tables=set(tables)-set(table_exists) @@ -187,6 +213,23 @@ def start(): print(Style.BRIGHT+"Checking old versions of model for find changes...") for table in tables: + + #print(table) + + table_fields={table: {}} + + # Field | Type | Null | Key | Default | Extra | + #| id | int(11) | NO | PRI | NULL | auto_increment | + + with connection.query('describe %s' % table) as cursor: + #all_fields=cursor.fetchall() + #print(all_fields) + + for row in cursor: + table_fields[table][row['Field']]={'type': row['Type'], 'key': row['Key']} + pass + #print(table_fields) + #connection.query("") #Check if new table @@ -209,7 +252,8 @@ def start(): for f, v in WebModel.model[table].fields.items(): - if not f in WebModel.model[old_table].fields: + #if not f in WebModel.model[old_table].fields: + if not f in table_fields[table]: fields_to_add.append(f) @@ -253,13 +297,15 @@ def start(): #Add index - if v.indexed==True and v_old.indexed==False: + #if v.indexed==True and v_old.indexed==False: + if v.indexed==True and table_fields[table][f]['key']!='MUL': fields_to_add_index.append(f) changes+=1 - if v.indexed==False and v_old.indexed==True: + #if v.indexed==False and v_old.indexed==True: + if v.indexed==False and table_fields[table][f]['key']=='MUL' and v.foreignkey==False: fields_to_delete_index.append(f) @@ -267,13 +313,15 @@ def start(): #Add unique - if v.unique==True and v_old.unique==False: + #if v.unique==True and v_old.unique==False: + if v.unique==True and table_fields[table][f]['key']!='UNI': fields_to_add_unique.append(f) changes+=1 - if v.unique==False and v_old.unique==True: + #if v.unique==False and v_old.unique==True: + if v.unique==False and table_fields[table][f]['key']=='UNI': fields_to_delete_unique.append(f) @@ -281,29 +329,43 @@ def start(): #Add constraint - if v.foreignkey==True and v_old.foreignkey==False: + #if v.foreignkey==True and v_old.foreignkey==False: + if v.foreignkey==True and table_fields[table][f]['key']!='MUL': fields_to_add_constraint.append(f) changes+=1 - if v.foreignkey==False and v_old.foreignkey==True: + #if v.foreignkey==False and v_old.foreignkey==True: + if v.foreignkey==False and table_fields[table][f]['key']=='MUL': - fields_to_delete_constraint.append(f) + if table in foreignkey_fields: - changes+=1 - - for f, v in WebModel.model[old_table].fields.items(): + if f in foreignkey_fields[table]: + + fields_to_delete_constraint.append(f) + + changes+=1 + + # Clean fields + + #for f, v in WebModel.model[old_table].fields.items(): + + for f, v in table_fields[table].items(): if not f in WebModel.model[table].fields: #Add constraint - if v.foreignkey==True: - - fields_to_delete_constraint.append(f) - - changes+=1 + #if v.foreignkey==True: + + if table in foreignkey_fields: + + if f in foreignkey_fields[table]: + + fields_to_delete_constraint.append(f) + + changes+=1 fields_to_delete.append(f) diff --git a/paramecio2/libraries/db/extrafields/arrayfield.py b/paramecio2/libraries/db/extrafields/arrayfield.py index 7283242..b8c99c9 100644 --- a/paramecio2/libraries/db/extrafields/arrayfield.py +++ b/paramecio2/libraries/db/extrafields/arrayfield.py @@ -38,6 +38,8 @@ class ArrayField(PhangoField): self.error_default='Sorry, the json array is invalid' self.set_default='NOT NULL' + + self.type_sql='text' def check(self, value): diff --git a/paramecio2/libraries/db/extrafields/datetimefield.py b/paramecio2/libraries/db/extrafields/datetimefield.py index b31e158..79eb4da 100644 --- a/paramecio2/libraries/db/extrafields/datetimefield.py +++ b/paramecio2/libraries/db/extrafields/datetimefield.py @@ -37,6 +37,8 @@ class DateTimeField(PhangoField): self.utc=False self.error_default='Error: Date format invalid' + + self.type_sql='datetime' def check(self, value): diff --git a/paramecio2/libraries/db/extrafields/dictfield.py b/paramecio2/libraries/db/extrafields/dictfield.py index 474f468..118dccb 100644 --- a/paramecio2/libraries/db/extrafields/dictfield.py +++ b/paramecio2/libraries/db/extrafields/dictfield.py @@ -38,6 +38,8 @@ class DictField(PhangoField): self.error_default='Sorry, the json dict is invalid' self.set_default='NOT NULL' + + self.type_sql='longtext' def check(self, value): @@ -73,7 +75,7 @@ class DictField(PhangoField): def get_type_sql(self): - return 'TEXT '+self.set_default + return 'JSON '+self.set_default def show_formatted(self, value): diff --git a/paramecio2/libraries/db/extrafields/i18nfield.py b/paramecio2/libraries/db/extrafields/i18nfield.py index 772f34a..116db21 100644 --- a/paramecio2/libraries/db/extrafields/i18nfield.py +++ b/paramecio2/libraries/db/extrafields/i18nfield.py @@ -50,6 +50,8 @@ class I18nField(PhangoField): arr_i18n={i:'' for i in I18n.dict_i18n} self.default_value=json.dumps(arr_i18n) + + self.type_sql='longtext' def change_form(self, form): self.extra_parameters=[form] diff --git a/paramecio2/libraries/db/extrafields/jsonfield.py b/paramecio2/libraries/db/extrafields/jsonfield.py index ba65efb..d7e1fdd 100644 --- a/paramecio2/libraries/db/extrafields/jsonfield.py +++ b/paramecio2/libraries/db/extrafields/jsonfield.py @@ -44,6 +44,8 @@ class JsonField(PhangoField): self.set_default='NOT NULL' + self.type_sql='longtext' + def check(self, value): if type(value).__name__=='str': @@ -75,7 +77,7 @@ class JsonField(PhangoField): def get_type_sql(self): - return 'LONGTEXT '+self.set_default + return 'JSON '+self.set_default def show_formatted(self, value): diff --git a/paramecio2/libraries/db/webmodel.py b/paramecio2/libraries/db/webmodel.py index ee29cdb..869429b 100644 --- a/paramecio2/libraries/db/webmodel.py +++ b/paramecio2/libraries/db/webmodel.py @@ -145,6 +145,8 @@ class PhangoField: # Value used for help strings in tooltips in forms self.help='' + + self.type_sql='varchar({})'.format(self.size) def get_type_sql(self): """This method is used for describe the new field in a sql language format.""" @@ -222,6 +224,7 @@ class PrimaryKeyField(PhangoField): self.name_form=HiddenForm self.required=False self.error_default="The value is zero" + self.type_sql='int({})'.format(self.size) def check(self, value): diff --git a/paramecio2/libraries/mtemplates.py b/paramecio2/libraries/mtemplates.py index b628083..9b9f0b8 100644 --- a/paramecio2/libraries/mtemplates.py +++ b/paramecio2/libraries/mtemplates.py @@ -21,7 +21,7 @@ along with this program. If not, see . #import gettext from mako.template import Template -from flask import session, url_for +from flask import url_for as url_for_flask from mako.lookup import TemplateLookup from os import path try: @@ -37,6 +37,12 @@ from paramecio2.libraries.i18n import I18n, PGetText from paramecio2.libraries.urls import make_url, make_media_url, add_get_parameters from paramecio2.libraries.formsutils import csrf_token +framework='flask' + +if hasattr(config, 'framework'): + framework=config.framework + + """ def _(text): @@ -91,7 +97,7 @@ class PTemplate: templates_loaded={} - def __init__(self, environment): + def __init__(self, environment, url_for_function=None): """A class used how shortcuts for Mako template functions. This class is used to have a set of shortcuts and hooks to Mako templates functions and methods over a series of default options. @@ -117,11 +123,22 @@ class PTemplate: self.add_filter(I18n.lang) - #self.add_filter(make_url) + self.add_filter(make_url) self.add_filter(make_media_url) + + if not url_for_function: + + url_for=url_for_flask - self.add_filter(url_for) + self.add_filter(url_for) + + else: + + url_for=url_for_function + + self.add_filter(url_for) + self.add_filter(csrf_token)