From 780a1280b13b7beb11eb45dd31653b3ac51da747 Mon Sep 17 00:00:00 2001 From: Antonio de la Rosa Date: Tue, 13 Dec 2016 23:47:59 +0100 Subject: [PATCH] paramecio/index.py --- paramecio/cromosoma/databases/mysqldb.py | 42 +++++------- paramecio/cromosoma/databases/pymysql.py | 42 +++++------- paramecio/cromosoma/dbadmin.py | 2 +- paramecio/cromosoma/webmodel.py | 82 +++++++++++------------- paramecio/index.py | 2 +- paramecio/modules/admin/models/admin.py | 35 ++++++++++ 6 files changed, 110 insertions(+), 95 deletions(-) diff --git a/paramecio/cromosoma/databases/mysqldb.py b/paramecio/cromosoma/databases/mysqldb.py index a558ba4..7ddc18e 100644 --- a/paramecio/cromosoma/databases/mysqldb.py +++ b/paramecio/cromosoma/databases/mysqldb.py @@ -9,25 +9,18 @@ class SqlClass: mypool=None - def __init__(self): + def __init__(self, connection): - self.max_overflow=65 - self.pool_size=45 + self.max_overflow=-1 + self.pool_size=0 self.error_connection="" - self.connection={} + # Data of connection + self.connection=connection + # Sql connection + self.conn=None self.connected=False - self.connection_method=self.connect_to_db_sql - - def dummy_connect(self, connection, name_connection="default"): - pass - - def connect_to_db(self, connection, name_connection="default"): - - self.connection_method(connection, name_connection) - - self.connection_method=self.dummy_connect - - def connect_to_db_sql(self, connection, name_connection="default"): + #self.connection_method=self.connect_to_db_sql + self.caching=False try: def getconn(): @@ -41,9 +34,9 @@ class SqlClass: if SqlClass.mypool==None: SqlClass.mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size) - self.connection[name_connection]=SqlClass.mypool.connect() + self.conn=SqlClass.mypool.connect() - self.connection[name_connection].ping(True) + self.conn.ping(True) self.connected=True @@ -63,13 +56,13 @@ class SqlClass: #if fetch_type=="ASSOC": #fetch_type=MySQLdb.cursors.DictCursor - #with self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor) as cursor: - cursor=self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor) + #with self.conn.cursor(MySQLdb.cursors.DictCursor) as cursor: + cursor=self.conn.cursor(MySQLdb.cursors.DictCursor) try: cursor.execute(sql_query, arguments) - self.connection[name_connection].commit() + self.conn.commit() return cursor @@ -95,13 +88,12 @@ class SqlClass: self.close() """ - def close(self, name_connection="default"): - if self.connection[name_connection]: + if self.conn: - self.connection[name_connection].close() - #self.connection[name_connection]=False + self.conn.close() + #self.conn=False pass diff --git a/paramecio/cromosoma/databases/pymysql.py b/paramecio/cromosoma/databases/pymysql.py index 3c71718..74f61f7 100644 --- a/paramecio/cromosoma/databases/pymysql.py +++ b/paramecio/cromosoma/databases/pymysql.py @@ -10,25 +10,18 @@ class SqlClass: mypool=None - def __init__(self): + def __init__(self, connection): - self.max_overflow=45 - self.pool_size=30 + self.max_overflow=-1 + self.pool_size=0 self.error_connection="" - self.connection={} + # Data of connection + self.connection=connection + # Sql connection + self.conn=None self.connected=False - self.connection_method=self.connect_to_db_sql - - def dummy_connect(self, connection, name_connection="default"): - pass - - def connect_to_db(self, connection, name_connection="default"): - - self.connection_method(connection, name_connection) - - self.connection_method=self.dummy_connect - - def connect_to_db_sql(self, connection, name_connection="default"): + #self.connection_method=self.connect_to_db_sql + self.caching=False try: def getconn(): @@ -42,9 +35,9 @@ class SqlClass: if SqlClass.mypool==None: SqlClass.mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size) - self.connection[name_connection]=SqlClass.mypool.connect() + self.conn=SqlClass.mypool.connect() - self.connection[name_connection].ping(True) + self.conn.ping(True) self.connected=True @@ -64,13 +57,13 @@ class SqlClass: #if fetch_type=="ASSOC": #fetch_type=pymysql.cursors.DictCursor - #with self.connection[name_connection].cursor(pymysql.cursors.DictCursor) as cursor: - cursor=self.connection[name_connection].cursor(pymysql.cursors.DictCursor) + #with self.conn.cursor(pymysql.cursors.DictCursor) as cursor: + cursor=self.conn.cursor(pymysql.cursors.DictCursor) try: cursor.execute(sql_query, arguments) - self.connection[name_connection].commit() + self.conn.commit() return cursor @@ -96,13 +89,12 @@ class SqlClass: self.close() """ - def close(self, name_connection="default"): - if self.connection[name_connection]: + if self.conn: - self.connection[name_connection].close() - #self.connection[name_connection]=False + self.conn.close() + #self.conn=False pass diff --git a/paramecio/cromosoma/dbadmin.py b/paramecio/cromosoma/dbadmin.py index b5b0cbc..03ee7a3 100644 --- a/paramecio/cromosoma/dbadmin.py +++ b/paramecio/cromosoma/dbadmin.py @@ -18,7 +18,7 @@ def start(): connection=WebModel.connection() - connection.connect_to_db(WebModel.connections['default']) + #connection.connect_to_db(WebModel.connections['default']) parser = argparse.ArgumentParser(description='A tool for create tables in databases using models from Cromosoma') diff --git a/paramecio/cromosoma/webmodel.py b/paramecio/cromosoma/webmodel.py index c5b4c94..507cfc8 100644 --- a/paramecio/cromosoma/webmodel.py +++ b/paramecio/cromosoma/webmodel.py @@ -5,8 +5,9 @@ import re import uuid from importlib import import_module, reload from collections import OrderedDict -from paramecio.cromosoma.databases.mysqldb import SqlClass +from paramecio.cromosoma.databases.pymysql import SqlClass from paramecio.cromosoma.coreforms import BaseForm, HiddenForm +import copy import traceback class PhangoField: @@ -202,6 +203,8 @@ class PrimaryKeyField(PhangoField): class WebModel: + __slots__=('sqlclass', 'fields', 'forms') + #Globals class variables for internal tasks arr_sql_index={} @@ -231,7 +234,7 @@ class WebModel: @staticmethod def connection(): - return SqlClass() + return SqlClass(WebModel.connections['default']) # Init the class @@ -295,7 +298,7 @@ class WebModel: self.register(primary_key) - self.register(PrimaryKeyField(self.name_field_id)) + #self.register(PrimaryKeyField(self.name_field_id)) #self.model[name]=self @@ -322,11 +325,16 @@ class WebModel: self.sqlclass=sqlclass self.fields_to_clean=[] - + + self.create_fields() + # A method where create the new fields of this model def create_fields(self): + #print([i for i in dir(self.__class__) if i[:1] != '_']) + #print(dir(self)) + pass # A method for register the fields @@ -358,16 +366,13 @@ class WebModel: #raise NameError(sqlclass.error_connection) #self.connection_pool.append(True) - """ - if not sqlclass.connected: - if WebModel.make_connection(self.connections[self.connection_id])==False: - raise NameError(sqlclass.error_connection) - """ - if self.sqlclass.connect_to_db(self.connections[self.connection_id])==False: - raise NameError(sqlclass.error_connection) + + #if self.sqlclass.connect_to_db(self.connections[self.connection_id])==False: + # raise NameError(sqlclass.error_connection) #WebModel.make_connection=sqlclass.dummy_connect + pass def dummy_connect(self, connection): return True @@ -648,16 +653,13 @@ class WebModel: self.limit="limit 1" - cursor=self.select(fields_selected, raw_query) - - self.reset_conditions() - - row=cursor.fetchone() - - if row==None: - row=False - - cursor.close() + with self.select(fields_selected, raw_query) as cursor: + self.reset_conditions() + + row=cursor.fetchone() + + if row==None: + row=False return row @@ -665,14 +667,12 @@ class WebModel: self.limit="limit "+str(begin)+", 1" - cursor=self.select(fields_selected, raw_query) + with self.select(fields_selected, raw_query) as cursor: - row=cursor.fetchone() - - if row==None: - row=False - - cursor.close() + row=cursor.fetchone() + + if row==None: + row=False return row @@ -699,17 +699,14 @@ class WebModel: def del_row_id(row): pass - cursor=self.select(fields_selected, raw_query) - results=[] #OrderedDict() - for row in cursor: + with self.select(fields_selected, raw_query) as cursor: + for row in cursor: + + results.append(row) - results.append(row) - - del_row_id(results) - - cursor.close() + del_row_id(results) return results @@ -748,14 +745,13 @@ class WebModel: sql= "select count(`"+field_to_count+"`) from "+", ".join(tables_to_select)+' '+conditions[0] - cursor=self.sqlclass.query(sql, conditions[1], self.connection_id) + count=0 - count=list(cursor.fetchone().values())[0] - - if self.yes_reset_conditions: - self.reset_conditions() - - cursor.close() + with self.sqlclass.query(sql, conditions[1], self.connection_id) as cursor: + count=list(cursor.fetchone().values())[0] + + if self.yes_reset_conditions: + self.reset_conditions() return count diff --git a/paramecio/index.py b/paramecio/index.py index 68e6218..7214327 100644 --- a/paramecio/index.py +++ b/paramecio/index.py @@ -2,7 +2,7 @@ import os, sys, traceback, inspect, resource from importlib import import_module from bottle import route, get, post, run, default_app, abort, request, response, static_file, load, hook, error from settings import config, modules -from beaker.middleware import SessionMiddleware +#from beaker.middleware import SessionMiddleware from mimetypes import guess_type from paramecio.cromosoma.webmodel import WebModel from paramecio.citoplasma.datetime import set_timezone diff --git a/paramecio/modules/admin/models/admin.py b/paramecio/modules/admin/models/admin.py index bd46130..4363a7d 100644 --- a/paramecio/modules/admin/models/admin.py +++ b/paramecio/modules/admin/models/admin.py @@ -21,6 +21,16 @@ class PrivilegesField(corefields.IntegerField): elif value==2: return I18n.lang('admin', 'administrator', 'Administrator') +_username=corefields.CharField('username') +_password=PasswordField('password') +_email=EmailField('email') +_token_recovery=corefields.CharField('token_recovery') +_token_login=corefields.CharField('token_login') +_privileges=PrivilegesField('privileges') +_lang=LangField('lang', 20) +_disabled=corefields.BooleanField('disabled') +_num_tries=corefields.IntegerField('num_tries', 1) + class UserAdmin(UserModel): #def create_fields(self): @@ -29,6 +39,31 @@ class UserAdmin(UserModel): super().__init__(connection) # I can change other fields here, how the name. + """ + self.register(_username) + + self.fields['username'].required=True + + self.register(_password) + + self.fields['password'].required=True + + self.register(_email) + + self.fields['email'].required=True + + self.register(_token_recovery) + + self.register(_token_login) + + self.register(_privileges) + + self.register(_lang) + + self.register(_disabled) + + self.register(_num_tries) + """ self.register(corefields.CharField('username'))