paramecio/index.py
This commit is contained in:
parent
6dc5a61699
commit
780a1280b1
6 changed files with 110 additions and 95 deletions
|
|
@ -9,25 +9,18 @@ class SqlClass:
|
||||||
|
|
||||||
mypool=None
|
mypool=None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, connection):
|
||||||
|
|
||||||
self.max_overflow=65
|
self.max_overflow=-1
|
||||||
self.pool_size=45
|
self.pool_size=0
|
||||||
self.error_connection=""
|
self.error_connection=""
|
||||||
self.connection={}
|
# Data of connection
|
||||||
|
self.connection=connection
|
||||||
|
# Sql connection
|
||||||
|
self.conn=None
|
||||||
self.connected=False
|
self.connected=False
|
||||||
self.connection_method=self.connect_to_db_sql
|
#self.connection_method=self.connect_to_db_sql
|
||||||
|
self.caching=False
|
||||||
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"):
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
def getconn():
|
def getconn():
|
||||||
|
|
@ -41,9 +34,9 @@ class SqlClass:
|
||||||
if SqlClass.mypool==None:
|
if SqlClass.mypool==None:
|
||||||
SqlClass.mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size)
|
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
|
self.connected=True
|
||||||
|
|
||||||
|
|
@ -63,13 +56,13 @@ class SqlClass:
|
||||||
#if fetch_type=="ASSOC":
|
#if fetch_type=="ASSOC":
|
||||||
#fetch_type=MySQLdb.cursors.DictCursor
|
#fetch_type=MySQLdb.cursors.DictCursor
|
||||||
|
|
||||||
#with self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor) as cursor:
|
#with self.conn.cursor(MySQLdb.cursors.DictCursor) as cursor:
|
||||||
cursor=self.connection[name_connection].cursor(MySQLdb.cursors.DictCursor)
|
cursor=self.conn.cursor(MySQLdb.cursors.DictCursor)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
cursor.execute(sql_query, arguments)
|
cursor.execute(sql_query, arguments)
|
||||||
self.connection[name_connection].commit()
|
self.conn.commit()
|
||||||
|
|
||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
|
|
@ -95,13 +88,12 @@ class SqlClass:
|
||||||
|
|
||||||
self.close()
|
self.close()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def close(self, name_connection="default"):
|
def close(self, name_connection="default"):
|
||||||
|
|
||||||
if self.connection[name_connection]:
|
if self.conn:
|
||||||
|
|
||||||
self.connection[name_connection].close()
|
self.conn.close()
|
||||||
#self.connection[name_connection]=False
|
#self.conn=False
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,25 +10,18 @@ class SqlClass:
|
||||||
|
|
||||||
mypool=None
|
mypool=None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, connection):
|
||||||
|
|
||||||
self.max_overflow=45
|
self.max_overflow=-1
|
||||||
self.pool_size=30
|
self.pool_size=0
|
||||||
self.error_connection=""
|
self.error_connection=""
|
||||||
self.connection={}
|
# Data of connection
|
||||||
|
self.connection=connection
|
||||||
|
# Sql connection
|
||||||
|
self.conn=None
|
||||||
self.connected=False
|
self.connected=False
|
||||||
self.connection_method=self.connect_to_db_sql
|
#self.connection_method=self.connect_to_db_sql
|
||||||
|
self.caching=False
|
||||||
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"):
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
def getconn():
|
def getconn():
|
||||||
|
|
@ -42,9 +35,9 @@ class SqlClass:
|
||||||
if SqlClass.mypool==None:
|
if SqlClass.mypool==None:
|
||||||
SqlClass.mypool=pool.QueuePool(getconn, max_overflow=self.max_overflow, pool_size=self.pool_size)
|
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
|
self.connected=True
|
||||||
|
|
||||||
|
|
@ -64,13 +57,13 @@ class SqlClass:
|
||||||
#if fetch_type=="ASSOC":
|
#if fetch_type=="ASSOC":
|
||||||
#fetch_type=pymysql.cursors.DictCursor
|
#fetch_type=pymysql.cursors.DictCursor
|
||||||
|
|
||||||
#with self.connection[name_connection].cursor(pymysql.cursors.DictCursor) as cursor:
|
#with self.conn.cursor(pymysql.cursors.DictCursor) as cursor:
|
||||||
cursor=self.connection[name_connection].cursor(pymysql.cursors.DictCursor)
|
cursor=self.conn.cursor(pymysql.cursors.DictCursor)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
cursor.execute(sql_query, arguments)
|
cursor.execute(sql_query, arguments)
|
||||||
self.connection[name_connection].commit()
|
self.conn.commit()
|
||||||
|
|
||||||
return cursor
|
return cursor
|
||||||
|
|
||||||
|
|
@ -96,13 +89,12 @@ class SqlClass:
|
||||||
|
|
||||||
self.close()
|
self.close()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def close(self, name_connection="default"):
|
def close(self, name_connection="default"):
|
||||||
|
|
||||||
if self.connection[name_connection]:
|
if self.conn:
|
||||||
|
|
||||||
self.connection[name_connection].close()
|
self.conn.close()
|
||||||
#self.connection[name_connection]=False
|
#self.conn=False
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ def start():
|
||||||
|
|
||||||
connection=WebModel.connection()
|
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')
|
parser = argparse.ArgumentParser(description='A tool for create tables in databases using models from Cromosoma')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,9 @@ import re
|
||||||
import uuid
|
import uuid
|
||||||
from importlib import import_module, reload
|
from importlib import import_module, reload
|
||||||
from collections import OrderedDict
|
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
|
from paramecio.cromosoma.coreforms import BaseForm, HiddenForm
|
||||||
|
import copy
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
class PhangoField:
|
class PhangoField:
|
||||||
|
|
@ -202,6 +203,8 @@ class PrimaryKeyField(PhangoField):
|
||||||
|
|
||||||
class WebModel:
|
class WebModel:
|
||||||
|
|
||||||
|
__slots__=('sqlclass', 'fields', 'forms')
|
||||||
|
|
||||||
#Globals class variables for internal tasks
|
#Globals class variables for internal tasks
|
||||||
|
|
||||||
arr_sql_index={}
|
arr_sql_index={}
|
||||||
|
|
@ -231,7 +234,7 @@ class WebModel:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def connection():
|
def connection():
|
||||||
|
|
||||||
return SqlClass()
|
return SqlClass(WebModel.connections['default'])
|
||||||
|
|
||||||
# Init the class
|
# Init the class
|
||||||
|
|
||||||
|
|
@ -295,7 +298,7 @@ class WebModel:
|
||||||
|
|
||||||
self.register(primary_key)
|
self.register(primary_key)
|
||||||
|
|
||||||
self.register(PrimaryKeyField(self.name_field_id))
|
#self.register(PrimaryKeyField(self.name_field_id))
|
||||||
|
|
||||||
#self.model[name]=self
|
#self.model[name]=self
|
||||||
|
|
||||||
|
|
@ -323,10 +326,15 @@ class WebModel:
|
||||||
|
|
||||||
self.fields_to_clean=[]
|
self.fields_to_clean=[]
|
||||||
|
|
||||||
|
self.create_fields()
|
||||||
|
|
||||||
# A method where create the new fields of this model
|
# A method where create the new fields of this model
|
||||||
|
|
||||||
def create_fields(self):
|
def create_fields(self):
|
||||||
|
|
||||||
|
#print([i for i in dir(self.__class__) if i[:1] != '_'])
|
||||||
|
#print(dir(self))
|
||||||
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# A method for register the fields
|
# A method for register the fields
|
||||||
|
|
@ -358,16 +366,13 @@ class WebModel:
|
||||||
#raise NameError(sqlclass.error_connection)
|
#raise NameError(sqlclass.error_connection)
|
||||||
|
|
||||||
#self.connection_pool.append(True)
|
#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
|
#WebModel.make_connection=sqlclass.dummy_connect
|
||||||
|
pass
|
||||||
|
|
||||||
def dummy_connect(self, connection):
|
def dummy_connect(self, connection):
|
||||||
return True
|
return True
|
||||||
|
|
@ -648,8 +653,7 @@ class WebModel:
|
||||||
|
|
||||||
self.limit="limit 1"
|
self.limit="limit 1"
|
||||||
|
|
||||||
cursor=self.select(fields_selected, raw_query)
|
with self.select(fields_selected, raw_query) as cursor:
|
||||||
|
|
||||||
self.reset_conditions()
|
self.reset_conditions()
|
||||||
|
|
||||||
row=cursor.fetchone()
|
row=cursor.fetchone()
|
||||||
|
|
@ -657,23 +661,19 @@ class WebModel:
|
||||||
if row==None:
|
if row==None:
|
||||||
row=False
|
row=False
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return row
|
return row
|
||||||
|
|
||||||
def select_a_row_where(self, fields_selected=[], raw_query=0, begin=0):
|
def select_a_row_where(self, fields_selected=[], raw_query=0, begin=0):
|
||||||
|
|
||||||
self.limit="limit "+str(begin)+", 1"
|
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()
|
row=cursor.fetchone()
|
||||||
|
|
||||||
if row==None:
|
if row==None:
|
||||||
row=False
|
row=False
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -699,18 +699,15 @@ class WebModel:
|
||||||
def del_row_id(row):
|
def del_row_id(row):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
cursor=self.select(fields_selected, raw_query)
|
|
||||||
|
|
||||||
results=[] #OrderedDict()
|
results=[] #OrderedDict()
|
||||||
|
|
||||||
|
with self.select(fields_selected, raw_query) as cursor:
|
||||||
for row in cursor:
|
for row in cursor:
|
||||||
|
|
||||||
results.append(row)
|
results.append(row)
|
||||||
|
|
||||||
del_row_id(results)
|
del_row_id(results)
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -748,15 +745,14 @@ class WebModel:
|
||||||
|
|
||||||
sql= "select count(`"+field_to_count+"`) from "+", ".join(tables_to_select)+' '+conditions[0]
|
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
|
||||||
|
|
||||||
|
with self.sqlclass.query(sql, conditions[1], self.connection_id) as cursor:
|
||||||
count=list(cursor.fetchone().values())[0]
|
count=list(cursor.fetchone().values())[0]
|
||||||
|
|
||||||
if self.yes_reset_conditions:
|
if self.yes_reset_conditions:
|
||||||
self.reset_conditions()
|
self.reset_conditions()
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return count
|
return count
|
||||||
|
|
||||||
#+' ORDER BY '+self.order_by+' '+self.limit).strip()
|
#+' ORDER BY '+self.order_by+' '+self.limit).strip()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import os, sys, traceback, inspect, resource
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from bottle import route, get, post, run, default_app, abort, request, response, static_file, load, hook, error
|
from bottle import route, get, post, run, default_app, abort, request, response, static_file, load, hook, error
|
||||||
from settings import config, modules
|
from settings import config, modules
|
||||||
from beaker.middleware import SessionMiddleware
|
#from beaker.middleware import SessionMiddleware
|
||||||
from mimetypes import guess_type
|
from mimetypes import guess_type
|
||||||
from paramecio.cromosoma.webmodel import WebModel
|
from paramecio.cromosoma.webmodel import WebModel
|
||||||
from paramecio.citoplasma.datetime import set_timezone
|
from paramecio.citoplasma.datetime import set_timezone
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,16 @@ class PrivilegesField(corefields.IntegerField):
|
||||||
elif value==2:
|
elif value==2:
|
||||||
return I18n.lang('admin', 'administrator', 'Administrator')
|
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):
|
class UserAdmin(UserModel):
|
||||||
|
|
||||||
#def create_fields(self):
|
#def create_fields(self):
|
||||||
|
|
@ -29,6 +39,31 @@ class UserAdmin(UserModel):
|
||||||
super().__init__(connection)
|
super().__init__(connection)
|
||||||
|
|
||||||
# I can change other fields here, how the name.
|
# 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'))
|
self.register(corefields.CharField('username'))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue