From dcadcf0ffb605a4130502bca9ae98d838ff94910 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 20:13:40 +0100 Subject: [PATCH 01/63] Added pyDAL from repository, adapted web2py code --- .gitmodules | 3 + gluon/__init__.py | 3 +- gluon/compileapp.py | 6 +- gluon/contrib/__init__.py | 5 + gluon/contrib/pydal | 1 + gluon/dal.py | 20 + gluon/dal/__init__.py | 4 - gluon/dal/_compat.py | 21 - gluon/dal/_globals.py | 13 - gluon/dal/_load.py | 313 ---- gluon/dal/adapters/__init__.py | 60 - gluon/dal/adapters/base.py | 1876 ---------------------- gluon/dal/adapters/couchdb.py | 202 --- gluon/dal/adapters/cubrid.py | 54 - gluon/dal/adapters/db2.py | 105 -- gluon/dal/adapters/firebird.py | 182 --- gluon/dal/adapters/google.py | 621 -------- gluon/dal/adapters/imap.py | 1034 ------------ gluon/dal/adapters/informix.py | 134 -- gluon/dal/adapters/ingres.py | 147 -- gluon/dal/adapters/mongo.py | 575 ------- gluon/dal/adapters/mssql.py | 513 ------ gluon/dal/adapters/mysql.py | 140 -- gluon/dal/adapters/oracle.py | 191 --- gluon/dal/adapters/postgres.py | 420 ----- gluon/dal/adapters/sapdb.py | 97 -- gluon/dal/adapters/sqlite.py | 280 ---- gluon/dal/adapters/teradata.py | 76 - gluon/dal/base.py | 1095 ------------- gluon/dal/connection.py | 116 -- gluon/dal/helpers/__init__.py | 0 gluon/dal/helpers/classes.py | 298 ---- gluon/dal/helpers/methods.py | 342 ---- gluon/dal/helpers/regex.py | 22 - gluon/dal/objects.py | 2704 -------------------------------- gluon/main.py | 6 +- gluon/scheduler.py | 4 +- gluon/shell.py | 4 +- gluon/sql.py | 7 +- gluon/sqlhtml.py | 11 +- gluon/tools.py | 64 +- gluon/validators.py | 8 +- gluon/widget.py | 2 +- 43 files changed, 87 insertions(+), 11692 deletions(-) create mode 100644 .gitmodules create mode 160000 gluon/contrib/pydal create mode 100644 gluon/dal.py delete mode 100644 gluon/dal/__init__.py delete mode 100644 gluon/dal/_compat.py delete mode 100644 gluon/dal/_globals.py delete mode 100644 gluon/dal/_load.py delete mode 100644 gluon/dal/adapters/__init__.py delete mode 100644 gluon/dal/adapters/base.py delete mode 100644 gluon/dal/adapters/couchdb.py delete mode 100644 gluon/dal/adapters/cubrid.py delete mode 100644 gluon/dal/adapters/db2.py delete mode 100644 gluon/dal/adapters/firebird.py delete mode 100644 gluon/dal/adapters/google.py delete mode 100644 gluon/dal/adapters/imap.py delete mode 100644 gluon/dal/adapters/informix.py delete mode 100644 gluon/dal/adapters/ingres.py delete mode 100644 gluon/dal/adapters/mongo.py delete mode 100644 gluon/dal/adapters/mssql.py delete mode 100644 gluon/dal/adapters/mysql.py delete mode 100644 gluon/dal/adapters/oracle.py delete mode 100644 gluon/dal/adapters/postgres.py delete mode 100644 gluon/dal/adapters/sapdb.py delete mode 100644 gluon/dal/adapters/sqlite.py delete mode 100644 gluon/dal/adapters/teradata.py delete mode 100644 gluon/dal/base.py delete mode 100644 gluon/dal/connection.py delete mode 100644 gluon/dal/helpers/__init__.py delete mode 100644 gluon/dal/helpers/classes.py delete mode 100644 gluon/dal/helpers/methods.py delete mode 100644 gluon/dal/helpers/regex.py delete mode 100644 gluon/dal/objects.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..7e2aea49 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "gluon/contrib/pydal"] + path = gluon/contrib/pydal + url = https://github.com/web2py/pydal.git diff --git a/gluon/__init__.py b/gluon/__init__.py index 457b061d..baf54a5b 100644 --- a/gluon/__init__.py +++ b/gluon/__init__.py @@ -11,8 +11,9 @@ Web2Py framework modules """ __all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_LIST_OF_EMAILS', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_JSON', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'LOAD', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STRONG', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML', 'redirect', 'current', 'embed64'] - + from globals import current +from contrib import pydal from html import * from validators import * from http import redirect, HTTP diff --git a/gluon/compileapp.py b/gluon/compileapp.py index e12bb200..c8cd2356 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -26,7 +26,7 @@ from gluon.fileutils import mktree, listdir, read_file, write_file from gluon.myregex import regex_expose, regex_longcomments from gluon.languages import translator from gluon.dal import DAL, Field -from gluon.dal.base import BaseAdapter +from gluon.pydal.base import BaseAdapter from gluon.sqlhtml import SQLFORM, SQLTABLE from gluon.cache import Cache from gluon.globals import current, Response @@ -407,7 +407,7 @@ def build_environment(request, response, session, store_current=True): # Enable standard conditional models (i.e., /*.py, /[controller]/*.py, and # /[controller]/[function]/*.py) response.models_to_run = [ - r'^\w+\.py$', + r'^\w+\.py$', r'^%s/\w+\.py$' % request.controller, r'^%s/%s/\w+\.py$' % (request.controller, request.function) ] @@ -514,7 +514,7 @@ def compile_controllers(folder): for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function - filename = pjoin(folder, 'compiled', + filename = pjoin(folder, 'compiled', 'controllers.%s.%s.py' % (fname[:-3],function)) write_file(filename, command) save_pyc(filename) diff --git a/gluon/contrib/__init__.py b/gluon/contrib/__init__.py index 8b137891..195e3d0a 100644 --- a/gluon/contrib/__init__.py +++ b/gluon/contrib/__init__.py @@ -1 +1,6 @@ +import os +import sys +sys.path.append(os.path.join(os.path.abspath(__file__), "pydal")) + +import pydal diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal new file mode 160000 index 00000000..d8865533 --- /dev/null +++ b/gluon/contrib/pydal @@ -0,0 +1 @@ +Subproject commit d886553357bc45c75402d619c09cdee54e378dc9 diff --git a/gluon/dal.py b/gluon/dal.py new file mode 100644 index 00000000..4844be57 --- /dev/null +++ b/gluon/dal.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +from gluon.pydal import DAL as pyDAL +from gluon.pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon + + +from gluon import serializers as w2p_serializers +from gluon import validators as w2p_validators +from gluon.utils import web2py_uuid +from gluon import sqlhtml + + +class DAL(pyDAL): + serializers = w2p_serializers + validators = w2p_validators + uuid = web2py_uuid + representers = { + 'rows_render': sqlhtml.represent, + 'rows_xml': sqlhtml.SQLTABLE + } diff --git a/gluon/dal/__init__.py b/gluon/dal/__init__.py deleted file mode 100644 index 5b149590..00000000 --- a/gluon/dal/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -from .base import DAL -from .objects import Field -from .helpers.classes import SQLCustomType -from .helpers.methods import geoPoint, geoLine, geoPolygon diff --git a/gluon/dal/_compat.py b/gluon/dal/_compat.py deleted file mode 100644 index c38b1220..00000000 --- a/gluon/dal/_compat.py +++ /dev/null @@ -1,21 +0,0 @@ -import sys -import hashlib -import os - -PY2 = sys.version_info[0] == 2 - -if PY2: - import cPickle as pickle - import cStringIO as StringIO - import copy_reg as copyreg - hashlib_md5 = hashlib.md5 -else: - import pickle - from io import StringIO - import copyreg - hashlib_md5 = lambda s: hashlib.md5(bytes(s,'utf8')) - -pjoin = os.path.join -exists = os.path.exists -ogetattr = object.__getattribute__ -osetattr = object.__setattr__ diff --git a/gluon/dal/_globals.py b/gluon/dal/_globals.py deleted file mode 100644 index 6eaea4d1..00000000 --- a/gluon/dal/_globals.py +++ /dev/null @@ -1,13 +0,0 @@ -import threading -import logging - -GLOBAL_LOCKER = threading.RLock() -THREAD_LOCAL = threading.local() - -LOGGER = logging.getLogger("web2py.dal") - -DEFAULT = lambda: None - -def IDENTITY(x): return x -def OR(a,b): return a|b -def AND(a,b): return a&b diff --git a/gluon/dal/_load.py b/gluon/dal/_load.py deleted file mode 100644 index 103340b8..00000000 --- a/gluon/dal/_load.py +++ /dev/null @@ -1,313 +0,0 @@ -# -*- coding: utf-8 -*- - -import decimal -import re - -from ._globals import LOGGER - - -# verify presence of web2py modules -try: - from collections import OrderedDict -except: - from gluon.contrib.ordereddict import OrderedDict - -try: - from gluon.utils import web2py_uuid -except (ImportError, SystemError): - import uuid - def web2py_uuid(): return str(uuid.uuid4()) - -try: - import portalocker - have_portalocker = True -except ImportError: - portalocker = None - have_portalocker = False - -try: - from gluon import serializers - have_serializers = True - simplejson = None -except ImportError: - serializers = None - have_serializers = False - try: - import json as simplejson - except ImportError: - try: - import gluon.contrib.simplejson as simplejson - except ImportError: - simplejson = None - - -# list of drivers will be built on the fly -# and lists only what is available -DRIVERS = [] - -try: - from new import classobj - from google.appengine.ext import db as gae - from google.appengine.ext import ndb - from google.appengine.api import namespace_manager, rdbms - from google.appengine.api.datastore_types import Key ### for belongs on ID - from google.appengine.ext.db.polymodel import PolyModel - from google.appengine.ext.ndb.polymodel import PolyModel as NDBPolyModel - DRIVERS.append('google') -except ImportError: - classobj = None - gae = None - ndb = None - namespace_manager = rdbms = None - Key = None - PolyModel = NDBPolyModel = None - -if not 'google' in DRIVERS: - - try: - from pysqlite2 import dbapi2 as sqlite2 - DRIVERS.append('sqlite2') - except ImportError: - LOGGER.debug('no SQLite drivers pysqlite2.dbapi2') - - try: - from sqlite3 import dbapi2 as sqlite3 - DRIVERS.append('sqlite3') - except ImportError: - LOGGER.debug('no SQLite drivers sqlite3') - - try: - # first try contrib driver, then from site-packages (if installed) - try: - import gluon.contrib.pymysql as pymysql - # monkeypatch pymysql because they havent fixed the bug: - # https://github.com/petehunt/PyMySQL/issues/86 - pymysql.ESCAPE_REGEX = re.compile("'") - pymysql.ESCAPE_MAP = {"'": "''"} - # end monkeypatch - except ImportError: - import pymysql - DRIVERS.append('pymysql') - except ImportError: - LOGGER.debug('no MySQL driver pymysql') - - try: - import MySQLdb - DRIVERS.append('MySQLdb') - except ImportError: - LOGGER.debug('no MySQL driver MySQLDB') - - try: - import mysql.connector as mysqlconnector - DRIVERS.append("mysqlconnector") - except ImportError: - LOGGER.debug("no driver mysql.connector") - - try: - import psycopg2 - from psycopg2.extensions import adapt as psycopg2_adapt - DRIVERS.append('psycopg2') - except ImportError: - psycopg2_adapt = None - LOGGER.debug('no PostgreSQL driver psycopg2') - - try: - # first try contrib driver, then from site-packages (if installed) - try: - import gluon.contrib.pg8000.dbapi as pg8000 - except ImportError: - import pg8000.dbapi as pg8000 - DRIVERS.append('pg8000') - except ImportError: - LOGGER.debug('no PostgreSQL driver pg8000') - - try: - import cx_Oracle - DRIVERS.append('cx_Oracle') - except ImportError: - cx_Oracle = None - LOGGER.debug('no Oracle driver cx_Oracle') - - try: - try: - import pyodbc - except ImportError: - try: - import gluon.contrib.pypyodbc as pyodbc - except Exception, e: - raise ImportError(str(e)) - DRIVERS.append('pyodbc') - #DRIVERS.append('DB2(pyodbc)') - #DRIVERS.append('Teradata(pyodbc)') - #DRIVERS.append('Ingres(pyodbc)') - except ImportError: - pyodbc = None - LOGGER.debug('no MSSQL/DB2/Teradata/Ingres driver pyodbc') - - try: - import ibm_db_dbi - DRIVERS.append('ibm_db_dbi') - except ImportError: - LOGGER.debug('no DB2 driver ibm_db_dbi') - - try: - import Sybase - DRIVERS.append('Sybase') - except ImportError: - LOGGER.debug('no Sybase driver') - - try: - import kinterbasdb - DRIVERS.append('kinterbasdb') - #DRIVERS.append('Firebird(kinterbasdb)') - except ImportError: - LOGGER.debug('no Firebird/Interbase driver kinterbasdb') - - try: - import fdb - DRIVERS.append('fdb') - except ImportError: - LOGGER.debug('no Firebird driver fdb') - - try: - import firebirdsql - DRIVERS.append('firebirdsql') - except ImportError: - LOGGER.debug('no Firebird driver firebirdsql') - - try: - import informixdb - DRIVERS.append('informixdb') - LOGGER.warning('Informix support is experimental') - except ImportError: - LOGGER.debug('no Informix driver informixdb') - - try: - import sapdb - DRIVERS.append('sapdb') - LOGGER.warning('SAPDB support is experimental') - except ImportError: - LOGGER.debug('no SAP driver sapdb') - - try: - import cubriddb - DRIVERS.append('cubriddb') - LOGGER.warning('Cubrid support is experimental') - except ImportError: - LOGGER.debug('no Cubrid driver cubriddb') - - try: - from com.ziclix.python.sql import zxJDBC - import java.sql - # Try sqlite jdbc driver from http://www.zentus.com/sqlitejdbc/ - from org.sqlite import JDBC # required by java.sql; ensure we have it - zxJDBC_sqlite = java.sql.DriverManager - DRIVERS.append('zxJDBC') - #DRIVERS.append('SQLite(zxJDBC)') - LOGGER.warning('zxJDBC support is experimental') - is_jdbc = True - except ImportError: - LOGGER.debug('no SQLite/PostgreSQL driver zxJDBC') - is_jdbc = False - - try: - import couchdb - DRIVERS.append('couchdb') - except ImportError: - couchdb = None - LOGGER.debug('no Couchdb driver couchdb') - - try: - import pymongo - DRIVERS.append('pymongo') - except: - LOGGER.debug('no MongoDB driver pymongo') - - try: - import imaplib - DRIVERS.append('imaplib') - except: - LOGGER.debug('no IMAP driver imaplib') - - GAEDecimalProperty = None - NDBDecimalProperty = None -else: - is_jdbc = False - - class GAEDecimalProperty(gae.Property): - """ - GAE decimal implementation - """ - data_type = decimal.Decimal - - def __init__(self, precision, scale, **kwargs): - super(GAEDecimalProperty, self).__init__(self, **kwargs) - d = '1.' - for x in range(scale): - d += '0' - self.round = decimal.Decimal(d) - - def get_value_for_datastore(self, model_instance): - value = super(GAEDecimalProperty, self)\ - .get_value_for_datastore(model_instance) - if value is None or value == '': - return None - else: - return str(value) - - def make_value_from_datastore(self, value): - if value is None or value == '': - return None - else: - return decimal.Decimal(value).quantize(self.round) - - def validate(self, value): - value = super(GAEDecimalProperty, self).validate(value) - if value is None or isinstance(value, decimal.Decimal): - return value - elif isinstance(value, basestring): - return decimal.Decimal(value) - raise gae.BadValueError("Property %s must be a Decimal or string."\ - % self.name) - - #TODO Needs more testing - class NDBDecimalProperty(ndb.StringProperty): - """ - NDB decimal implementation - """ - data_type = decimal.Decimal - - def __init__(self, precision, scale, **kwargs): - d = '1.' - for x in range(scale): - d += '0' - self.round = decimal.Decimal(d) - - def _to_base_type(self, value): - if value is None or value == '': - return None - else: - return str(value) - - def _from_base_type(self, value): - if value is None or value == '': - return None - else: - return decimal.Decimal(value).quantize(self.round) - - def _validate(self, value): - if value is None or isinstance(value, decimal.Decimal): - return value - elif isinstance(value, basestring): - return decimal.Decimal(value) - raise TypeError("Property %s must be a Decimal or string."\ - % self._name) - - psycopg2_adapt = None - cx_Oracle = None - pyodbc = None - couchdb = None - - -def get_driver(name): - return globals().get(name) diff --git a/gluon/dal/adapters/__init__.py b/gluon/dal/adapters/__init__.py deleted file mode 100644 index 1bb4211f..00000000 --- a/gluon/dal/adapters/__init__.py +++ /dev/null @@ -1,60 +0,0 @@ -# -*- coding: utf-8 -*- -from .sqlite import SQLiteAdapter, SpatiaLiteAdapter, JDBCSQLiteAdapter -from .mysql import MySQLAdapter -from .postgres import PostgreSQLAdapter, NewPostgreSQLAdapter, JDBCPostgreSQLAdapter -from .oracle import OracleAdapter -from .mssql import MSSQLAdapter, MSSQL2Adapter, MSSQL3Adapter, MSSQL4Adapter, \ - VerticaAdapter, SybaseAdapter -from .firebird import FireBirdAdapter -from .informix import InformixAdapter, InformixSEAdapter -from .db2 import DB2Adapter -from .teradata import TeradataAdapter -from .ingres import IngresAdapter, IngresUnicodeAdapter -from .sapdb import SAPDBAdapter -from .cubrid import CubridAdapter -from .google import GoogleDatastoreAdapter, GoogleSQLAdapter -from .couchdb import CouchDBAdapter -from .mongo import MongoDBAdapter -from .imap import IMAPAdapter - - -ADAPTERS = { - 'sqlite': SQLiteAdapter, - 'spatialite': SpatiaLiteAdapter, - 'sqlite:memory': SQLiteAdapter, - 'spatialite:memory': SpatiaLiteAdapter, - 'mysql': MySQLAdapter, - 'postgres': PostgreSQLAdapter, - 'postgres:psycopg2': PostgreSQLAdapter, - 'postgres:pg8000': PostgreSQLAdapter, - 'postgres2:psycopg2': NewPostgreSQLAdapter, - 'postgres2:pg8000': NewPostgreSQLAdapter, - 'oracle': OracleAdapter, - 'mssql': MSSQLAdapter, - 'mssql2': MSSQL2Adapter, - 'mssql3': MSSQL3Adapter, - 'mssql4' : MSSQL4Adapter, - 'vertica': VerticaAdapter, - 'sybase': SybaseAdapter, - 'db2:ibm_db_dbi': DB2Adapter, - 'db2:pyodbc': DB2Adapter, - 'teradata': TeradataAdapter, - 'informix': InformixAdapter, - 'informix-se': InformixSEAdapter, - 'firebird': FireBirdAdapter, - 'firebird_embedded': FireBirdAdapter, - 'ingres': IngresAdapter, - 'ingresu': IngresUnicodeAdapter, - 'sapdb': SAPDBAdapter, - 'cubrid': CubridAdapter, - 'jdbc:sqlite': JDBCSQLiteAdapter, - 'jdbc:sqlite:memory': JDBCSQLiteAdapter, - 'jdbc:postgres': JDBCPostgreSQLAdapter, - 'gae': GoogleDatastoreAdapter, # discouraged, for backward compatibility - 'google:datastore': GoogleDatastoreAdapter, - 'google:datastore+ndb': GoogleDatastoreAdapter, - 'google:sql': GoogleSQLAdapter, - 'couchdb': CouchDBAdapter, - 'mongodb': MongoDBAdapter, - 'imap': IMAPAdapter -} diff --git a/gluon/dal/adapters/base.py b/gluon/dal/adapters/base.py deleted file mode 100644 index aad58af9..00000000 --- a/gluon/dal/adapters/base.py +++ /dev/null @@ -1,1876 +0,0 @@ -# -*- coding: utf-8 -*- -import re -import os -import sys -import locale -import datetime -import decimal -import logging -import copy -import time -import base64 -import types - -from .._compat import pjoin, exists, pickle, hashlib_md5 -from .._globals import IDENTITY, LOGGER -from .._load import DRIVERS, get_driver, have_portalocker, portalocker, have_serializers, \ - serializers, simplejson, gae -from ..connection import ConnectionPool -from ..objects import Expression, Field, Query, Table, Row, FieldVirtual, \ - FieldMethod, LazyReferenceGetter, LazySet, VirtualCommand, Rows -from ..helpers.regex import REGEX_NO_GREEDY_ENTITY_NAME, REGEX_TYPE, REGEX_SELECT_AS_PARSER -from ..helpers.methods import xorify, use_common_filters, bar_encode, \ - bar_decode_integer, bar_decode_string -from ..helpers.classes import SQLCustomType, SQLALL, Reference, RecordUpdater, RecordDeleter - - -TIMINGSSIZE = 100 -CALLABLETYPES = (types.LambdaType, types.FunctionType, - types.BuiltinFunctionType, - types.MethodType, types.BuiltinMethodType) -SELECT_ARGS = set( - ('orderby', 'groupby', 'limitby','required', 'cache', 'left', - 'distinct', 'having', 'join','for_update', 'processor','cacheable', 'orderby_on_limitby')) - - -class AdapterMeta(type): - """Metaclass to support manipulation of adapter classes. - - At the moment is used to intercept `entity_quoting` argument passed to DAL. - """ - - def __call__(cls, *args, **kwargs): - entity_quoting = kwargs.get('entity_quoting', False) - if 'entity_quoting' in kwargs: - del kwargs['entity_quoting'] - - obj = super(AdapterMeta, cls).__call__(*args, **kwargs) - if not entity_quoting: - quot = obj.QUOTE_TEMPLATE = '%s' - regex_ent = r'(\w+)' - else: - quot = obj.QUOTE_TEMPLATE - regex_ent = REGEX_NO_GREEDY_ENTITY_NAME - obj.REGEX_TABLE_DOT_FIELD = re.compile(r'^' + \ - quot % regex_ent + \ - r'\.' + \ - quot % regex_ent + \ - r'$') - - return obj - - -class BaseAdapter(ConnectionPool): - - __metaclass__ = AdapterMeta - - driver_auto_json = [] - driver = None - driver_name = None - drivers = () # list of drivers from which to pick - connection = None - commit_on_alter_table = False - support_distributed_transaction = False - uploads_in_blob = False - can_select_for_update = True - dbpath = None - folder = None - connector = lambda *args, **kwargs: None # __init__ should override this - - TRUE = 'T' - FALSE = 'F' - T_SEP = ' ' - QUOTE_TEMPLATE = '"%s"' - - - types = { - 'boolean': 'CHAR(1)', - 'string': 'CHAR(%(length)s)', - 'text': 'TEXT', - 'json': 'TEXT', - 'password': 'CHAR(%(length)s)', - 'blob': 'BLOB', - 'upload': 'CHAR(%(length)s)', - 'integer': 'INTEGER', - 'bigint': 'INTEGER', - 'float':'DOUBLE', - 'double': 'DOUBLE', - 'decimal': 'DOUBLE', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'INTEGER PRIMARY KEY AUTOINCREMENT', - 'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'TEXT', - 'list:string': 'TEXT', - 'list:reference': 'TEXT', - # the two below are only used when DAL(...bigint_id=True) and replace 'id','reference' - 'big-id': 'INTEGER PRIMARY KEY AUTOINCREMENT', - 'big-reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT "FK_%(constraint_name)s" FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - def isOperationalError(self,exception): - if not hasattr(self.driver, "OperationalError"): - return None - return isinstance(exception, self.driver.OperationalError) - - def isProgrammingError(self,exception): - if not hasattr(self.driver, "ProgrammingError"): - return None - return isinstance(exception, self.driver.ProgrammingError) - - def id_query(self, table): - pkeys = getattr(table,'_primarykey',None) - if pkeys: - return table[pkeys[0]] != None - else: - return table._id != None - - def adapt(self, obj): - return "'%s'" % obj.replace("'", "''") - - def smart_adapt(self, obj): - if isinstance(obj,(int,float)): - return str(obj) - return self.adapt(str(obj)) - - def file_exists(self, filename): - #to be used ONLY for files that on GAE may not be on filesystem - return exists(filename) - - def file_open(self, filename, mode='rb', lock=True): - #to be used ONLY for files that on GAE may not be on filesystem - if have_portalocker and lock: - fileobj = portalocker.LockedFile(filename,mode) - else: - fileobj = open(filename,mode) - return fileobj - - def file_close(self, fileobj): - #to be used ONLY for files that on GAE may not be on filesystem - if fileobj: - fileobj.close() - - def file_delete(self, filename): - os.unlink(filename) - - def find_driver(self,adapter_args,uri=None): - self.adapter_args = adapter_args - if getattr(self,'driver',None) != None: - return - drivers_available = [driver for driver in self.drivers - if driver in DRIVERS] - if uri: - items = uri.split('://',1)[0].split(':') - request_driver = items[1] if len(items)>1 else None - else: - request_driver = None - request_driver = request_driver or adapter_args.get('driver') - if request_driver: - if request_driver in drivers_available: - self.driver_name = request_driver - #self.driver = globals().get(request_driver) - self.driver = get_driver(request_driver) - else: - raise RuntimeError("driver %s not available" % request_driver) - elif drivers_available: - self.driver_name = drivers_available[0] - #self.driver = globals().get(self.driver_name) - self.driver = get_driver(self.driver_name) - else: - raise RuntimeError("no driver available %s" % str(self.drivers)) - - def log(self, message, table=None): - """ Logs migrations - - It will not log changes if logfile is not specified. Defaults - to sql.log - """ - - isabs = None - logfilename = self.adapter_args.get('logfile','sql.log') - writelog = bool(logfilename) - if writelog: - isabs = os.path.isabs(logfilename) - - if table and table._dbt and writelog and self.folder: - if isabs: - table._loggername = logfilename - else: - table._loggername = pjoin(self.folder, logfilename) - logfile = self.file_open(table._loggername, 'a') - logfile.write(message) - self.file_close(logfile) - - - def __init__(self, db,uri,pool_size=0, folder=None, db_codec='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={},do_connect=True, after_connection=None): - self.db = db - self.dbengine = "None" - self.uri = uri - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - class Dummy(object): - lastrowid = 1 - def __getattr__(self, value): - return lambda *a, **b: [] - self.connection = Dummy() - self.cursor = Dummy() - - - def sequence_name(self,tablename): - return self.QUOTE_TEMPLATE % ('%s_sequence' % tablename) - - def trigger_name(self,tablename): - return '%s_sequence' % tablename - - def varquote(self,name): - return name - - def create_table(self, table, - migrate=True, - fake_migrate=False, - polymodel=None): - db = table._db - fields = [] - # PostGIS geo fields are added after the table has been created - postcreation_fields = [] - sql_fields = {} - sql_fields_aux = {} - TFK = {} - tablename = table._tablename - sortable = 0 - types = self.types - for field in table: - sortable += 1 - field_name = field.name - field_type = field.type - if isinstance(field_type,SQLCustomType): - ftype = field_type.native or field_type.type - elif field_type.startswith('reference'): - referenced = field_type[10:].strip() - if referenced == '.': - referenced = tablename - constraint_name = self.constraint_name(tablename, field_name) - # if not '.' in referenced \ - # and referenced != tablename \ - # and hasattr(table,'_primarykey'): - # ftype = types['integer'] - #else: - try: - rtable = db[referenced] - rfield = rtable._id - rfieldname = rfield.name - rtablename = referenced - except (KeyError, ValueError, AttributeError), e: - LOGGER.debug('Error: %s' % e) - try: - rtablename,rfieldname = referenced.split('.') - rtable = db[rtablename] - rfield = rtable[rfieldname] - except Exception, e: - LOGGER.debug('Error: %s' %e) - raise KeyError('Cannot resolve reference %s in %s definition' % (referenced, table._tablename)) - - # must be PK reference or unique - if getattr(rtable, '_primarykey', None) and rfieldname in rtable._primarykey or \ - rfield.unique: - ftype = types[rfield.type[:9]] % \ - dict(length=rfield.length) - # multicolumn primary key reference? - if not rfield.unique and len(rtable._primarykey)>1: - # then it has to be a table level FK - if rtablename not in TFK: - TFK[rtablename] = {} - TFK[rtablename][rfieldname] = field_name - else: - ftype = ftype + \ - types['reference FK'] % dict( - constraint_name = constraint_name, # should be quoted - foreign_key = rtable.sqlsafe + ' (' + rfield.sqlsafe_name + ')', - table_name = table.sqlsafe, - field_name = field.sqlsafe_name, - on_delete_action=field.ondelete) - else: - # make a guess here for circular references - if referenced in db: - id_fieldname = db[referenced]._id.sqlsafe_name - elif referenced == tablename: - id_fieldname = table._id.sqlsafe_name - else: #make a guess - id_fieldname = self.QUOTE_TEMPLATE % 'id' - #gotcha: the referenced table must be defined before - #the referencing one to be able to create the table - #Also if it's not recommended, we can still support - #references to tablenames without rname to make - #migrations and model relationship work also if tables - #are not defined in order - if referenced == tablename: - real_referenced = db[referenced].sqlsafe - else: - real_referenced = (referenced in db - and db[referenced].sqlsafe - or referenced) - rfield = db[referenced]._id - ftype = types[field_type[:9]] % dict( - index_name = self.QUOTE_TEMPLATE % (field_name+'__idx'), - field_name = field.sqlsafe_name, - constraint_name = self.QUOTE_TEMPLATE % constraint_name, - foreign_key = '%s (%s)' % (real_referenced, rfield.sqlsafe_name), - on_delete_action=field.ondelete) - elif field_type.startswith('list:reference'): - ftype = types[field_type[:14]] - elif field_type.startswith('decimal'): - precision, scale = map(int,field_type[8:-1].split(',')) - ftype = types[field_type[:7]] % \ - dict(precision=precision,scale=scale) - elif field_type.startswith('geo'): - if not hasattr(self,'srid'): - raise RuntimeError('Adapter does not support geometry') - srid = self.srid - geotype, parms = field_type[:-1].split('(') - if not geotype in types: - raise SyntaxError( - 'Field: unknown field type: %s for %s' \ - % (field_type, field_name)) - ftype = types[geotype] - if self.dbengine == 'postgres' and geotype == 'geometry': - if self.ignore_field_case is True: - field_name = field_name.lower() - # parameters: schema, srid, dimension - dimension = 2 # GIS.dimension ??? - parms = parms.split(',') - if len(parms) == 3: - schema, srid, dimension = parms - elif len(parms) == 2: - schema, srid = parms - else: - schema = parms[0] - ftype = "SELECT AddGeometryColumn ('%%(schema)s', '%%(tablename)s', '%%(fieldname)s', %%(srid)s, '%s', %%(dimension)s);" % types[geotype] - ftype = ftype % dict(schema=schema, - tablename=tablename, - fieldname=field_name, srid=srid, - dimension=dimension) - postcreation_fields.append(ftype) - elif not field_type in types: - raise SyntaxError('Field: unknown field type: %s for %s' % \ - (field_type, field_name)) - else: - ftype = types[field_type]\ - % dict(length=field.length) - if not field_type.startswith('id') and \ - not field_type.startswith('reference'): - if field.notnull: - ftype += ' NOT NULL' - else: - ftype += self.ALLOW_NULL() - if field.unique: - ftype += ' UNIQUE' - if field.custom_qualifier: - ftype += ' %s' % field.custom_qualifier - - # add to list of fields - sql_fields[field_name] = dict( - length=field.length, - unique=field.unique, - notnull=field.notnull, - sortable=sortable, - type=str(field_type), - sql=ftype) - - if field.notnull and not field.default is None: - # Caveat: sql_fields and sql_fields_aux - # differ for default values. - # sql_fields is used to trigger migrations and sql_fields_aux - # is used for create tables. - # The reason is that we do not want to trigger - # a migration simply because a default value changes. - not_null = self.NOT_NULL(field.default, field_type) - ftype = ftype.replace('NOT NULL', not_null) - sql_fields_aux[field_name] = dict(sql=ftype) - # Postgres - PostGIS: - # geometry fields are added after the table has been created, not now - if not (self.dbengine == 'postgres' and \ - field_type.startswith('geom')): - fields.append('%s %s' % (field.sqlsafe_name, ftype)) - other = ';' - - # backend-specific extensions to fields - if self.dbengine == 'mysql': - if not hasattr(table, "_primarykey"): - fields.append('PRIMARY KEY (%s)' % (self.QUOTE_TEMPLATE % table._id.name)) - engine = self.adapter_args.get('engine','InnoDB') - other = ' ENGINE=%s CHARACTER SET utf8;' % engine - - fields = ',\n '.join(fields) - for rtablename in TFK: - rfields = TFK[rtablename] - pkeys = [self.QUOTE_TEMPLATE % pk for pk in db[rtablename]._primarykey] - fkeys = [self.QUOTE_TEMPLATE % rfields[k].name for k in pkeys ] - fields = fields + ',\n ' + \ - types['reference TFK'] % dict( - table_name = table.sqlsafe, - field_name=', '.join(fkeys), - foreign_table = table.sqlsafe, - foreign_key = ', '.join(pkeys), - on_delete_action = field.ondelete) - - table_rname = table.sqlsafe - - if getattr(table,'_primarykey',None): - query = "CREATE TABLE %s(\n %s,\n %s) %s" % \ - (table.sqlsafe, fields, - self.PRIMARY_KEY(', '.join([self.QUOTE_TEMPLATE % pk for pk in table._primarykey])),other) - else: - query = "CREATE TABLE %s(\n %s\n)%s" % \ - (table.sqlsafe, fields, other) - - if self.uri.startswith('sqlite:///') \ - or self.uri.startswith('spatialite:///'): - path_encoding = sys.getfilesystemencoding() \ - or locale.getdefaultlocale()[1] or 'utf8' - dbpath = self.uri[9:self.uri.rfind('/')]\ - .decode('utf8').encode(path_encoding) - else: - dbpath = self.folder - - if not migrate: - return query - elif self.uri.startswith('sqlite:memory')\ - or self.uri.startswith('spatialite:memory'): - table._dbt = None - elif isinstance(migrate, str): - table._dbt = pjoin(dbpath, migrate) - else: - table._dbt = pjoin( - dbpath, '%s_%s.table' % (table._db._uri_hash, tablename)) - - if not table._dbt or not self.file_exists(table._dbt): - if table._dbt: - self.log('timestamp: %s\n%s\n' - % (datetime.datetime.today().isoformat(), - query), table) - if not fake_migrate: - self.create_sequence_and_triggers(query,table) - table._db.commit() - # Postgres geom fields are added now, - # after the table has been created - for query in postcreation_fields: - self.execute(query) - table._db.commit() - if table._dbt: - tfile = self.file_open(table._dbt, 'w') - pickle.dump(sql_fields, tfile) - self.file_close(tfile) - if fake_migrate: - self.log('faked!\n', table) - else: - self.log('success!\n', table) - else: - tfile = self.file_open(table._dbt, 'r') - try: - sql_fields_old = pickle.load(tfile) - except EOFError: - self.file_close(tfile) - raise RuntimeError('File %s appears corrupted' % table._dbt) - self.file_close(tfile) - if sql_fields != sql_fields_old: - self.migrate_table( - table, - sql_fields, sql_fields_old, - sql_fields_aux, None, - fake_migrate=fake_migrate - ) - return query - - def migrate_table( - self, - table, - sql_fields, - sql_fields_old, - sql_fields_aux, - logfile, - fake_migrate=False, - ): - - # logfile is deprecated (moved to adapter.log method) - db = table._db - db._migrated.append(table._tablename) - tablename = table._tablename - def fix(item): - k,v=item - if not isinstance(v,dict): - v=dict(type='unknown',sql=v) - if self.ignore_field_case is not True: return k, v - return k.lower(),v - # make sure all field names are lower case to avoid - # migrations because of case cahnge - sql_fields = dict(map(fix,sql_fields.iteritems())) - sql_fields_old = dict(map(fix,sql_fields_old.iteritems())) - sql_fields_aux = dict(map(fix,sql_fields_aux.iteritems())) - if db._debug: - logging.debug('migrating %s to %s' % (sql_fields_old,sql_fields)) - - keys = sql_fields.keys() - for key in sql_fields_old: - if not key in keys: - keys.append(key) - new_add = self.concat_add(tablename) - - metadata_change = False - sql_fields_current = copy.copy(sql_fields_old) - for key in keys: - query = None - if not key in sql_fields_old: - sql_fields_current[key] = sql_fields[key] - if self.dbengine in ('postgres',) and \ - sql_fields[key]['type'].startswith('geometry'): - # 'sql' == ftype in sql - query = [ sql_fields[key]['sql'] ] - else: - query = ['ALTER TABLE %s ADD %s %s;' % \ - (table.sqlsafe, key, - sql_fields_aux[key]['sql'].replace(', ', new_add))] - metadata_change = True - elif self.dbengine in ('sqlite', 'spatialite'): - if key in sql_fields: - sql_fields_current[key] = sql_fields[key] - metadata_change = True - elif not key in sql_fields: - del sql_fields_current[key] - ftype = sql_fields_old[key]['type'] - if (self.dbengine in ('postgres',) and - ftype.startswith('geometry')): - geotype, parms = ftype[:-1].split('(') - schema = parms.split(',')[0] - query = [ "SELECT DropGeometryColumn ('%(schema)s', "+ - "'%(table)s', '%(field)s');" % - dict(schema=schema, table=tablename, field=key,) ] - elif self.dbengine in ('firebird',): - query = ['ALTER TABLE %s DROP %s;' % - (self.QUOTE_TEMPLATE % tablename, self.QUOTE_TEMPLATE % key)] - else: - query = ['ALTER TABLE %s DROP COLUMN %s;' % - (self.QUOTE_TEMPLATE % tablename, self.QUOTE_TEMPLATE % key)] - metadata_change = True - elif sql_fields[key]['sql'] != sql_fields_old[key]['sql'] \ - and not (key in table.fields and - isinstance(table[key].type, SQLCustomType)) \ - and not sql_fields[key]['type'].startswith('reference')\ - and not sql_fields[key]['type'].startswith('double')\ - and not sql_fields[key]['type'].startswith('id'): - sql_fields_current[key] = sql_fields[key] - t = tablename - tt = sql_fields_aux[key]['sql'].replace(', ', new_add) - if self.dbengine in ('firebird',): - drop_expr = 'ALTER TABLE %s DROP %s;' - else: - drop_expr = 'ALTER TABLE %s DROP COLUMN %s;' - key_tmp = key + '__tmp' - query = ['ALTER TABLE %s ADD %s %s;' % (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key_tmp, tt), - 'UPDATE %s SET %s=%s;' % - (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key_tmp, self.QUOTE_TEMPLATE % key), - drop_expr % (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key), - 'ALTER TABLE %s ADD %s %s;' % - (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key, tt), - 'UPDATE %s SET %s=%s;' % - (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key, self.QUOTE_TEMPLATE % key_tmp), - drop_expr % (self.QUOTE_TEMPLATE % t, self.QUOTE_TEMPLATE % key_tmp)] - metadata_change = True - elif sql_fields[key]['type'] != sql_fields_old[key]['type']: - sql_fields_current[key] = sql_fields[key] - metadata_change = True - - if query: - self.log('timestamp: %s\n' - % datetime.datetime.today().isoformat(), table) - db['_lastsql'] = '\n'.join(query) - for sub_query in query: - self.log(sub_query + '\n', table) - if fake_migrate: - if db._adapter.commit_on_alter_table: - self.save_dbt(table,sql_fields_current) - self.log('faked!\n', table) - else: - self.execute(sub_query) - # Caveat: mysql, oracle and firebird - # do not allow multiple alter table - # in one transaction so we must commit - # partial transactions and - # update table._dbt after alter table. - if db._adapter.commit_on_alter_table: - db.commit() - self.save_dbt(table,sql_fields_current) - self.log('success!\n', table) - - elif metadata_change: - self.save_dbt(table,sql_fields_current) - - if metadata_change and not (query and db._adapter.commit_on_alter_table): - db.commit() - self.save_dbt(table,sql_fields_current) - self.log('success!\n', table) - - def save_dbt(self,table, sql_fields_current): - tfile = self.file_open(table._dbt, 'w') - pickle.dump(sql_fields_current, tfile) - self.file_close(tfile) - - def LOWER(self, first): - return 'LOWER(%s)' % self.expand(first) - - def UPPER(self, first): - return 'UPPER(%s)' % self.expand(first) - - def COUNT(self, first, distinct=None): - return ('COUNT(%s)' if not distinct else 'COUNT(DISTINCT %s)') \ - % self.expand(first) - - def EXTRACT(self, first, what): - return "EXTRACT(%s FROM %s)" % (what, self.expand(first)) - - def EPOCH(self, first): - return self.EXTRACT(first, 'epoch') - - def LENGTH(self, first): - return "LENGTH(%s)" % self.expand(first) - - def AGGREGATE(self, first, what): - return "%s(%s)" % (what, self.expand(first)) - - def JOIN(self): - return 'JOIN' - - def LEFT_JOIN(self): - return 'LEFT JOIN' - - def RANDOM(self): - return 'Random()' - - def NOT_NULL(self, default, field_type): - return 'NOT NULL DEFAULT %s' % self.represent(default,field_type) - - def COALESCE(self, first, second): - expressions = [self.expand(first)]+[self.expand(e) for e in second] - return 'COALESCE(%s)' % ','.join(expressions) - - def COALESCE_ZERO(self, first): - return 'COALESCE(%s,0)' % self.expand(first) - - def RAW(self, first): - return first - - def ALLOW_NULL(self): - return '' - - def SUBSTRING(self, field, parameters): - return 'SUBSTR(%s,%s,%s)' % (self.expand(field), parameters[0], parameters[1]) - - def PRIMARY_KEY(self, key): - return 'PRIMARY KEY(%s)' % key - - def _drop(self, table, mode): - return ['DROP TABLE %s;' % table.sqlsafe] - - def drop(self, table, mode=''): - db = table._db - queries = self._drop(table, mode) - for query in queries: - if table._dbt: - self.log(query + '\n', table) - self.execute(query) - db.commit() - del db[table._tablename] - del db.tables[db.tables.index(table._tablename)] - db._remove_references_to(table) - if table._dbt: - self.file_delete(table._dbt) - self.log('success!\n', table) - - def _insert(self, table, fields): - table_rname = table.sqlsafe - if fields: - keys = ','.join(f.sqlsafe_name for f, v in fields) - values = ','.join(self.expand(v, f.type) for f, v in fields) - return 'INSERT INTO %s(%s) VALUES (%s);' % (table_rname, keys, values) - else: - return self._insert_empty(table) - - def _insert_empty(self, table): - return 'INSERT INTO %s DEFAULT VALUES;' % (table.sqlsafe) - - def insert(self, table, fields): - query = self._insert(table,fields) - try: - self.execute(query) - except Exception: - e = sys.exc_info()[1] - if hasattr(table,'_on_insert_error'): - return table._on_insert_error(table,fields,e) - raise e - if hasattr(table, '_primarykey'): - mydict = dict([(k[0].name, k[1]) for k in fields if k[0].name in table._primarykey]) - if mydict != {}: - return mydict - id = self.lastrowid(table) - if hasattr(table, '_primarykey') and len(table._primarykey) == 1: - id = {table._primarykey[0]: id} - if not isinstance(id, (int, long)): - return id - rid = Reference(id) - (rid._table, rid._record) = (table, None) - return rid - - def bulk_insert(self, table, items): - return [self.insert(table,item) for item in items] - - def NOT(self, first): - return '(NOT %s)' % self.expand(first) - - def AND(self, first, second): - return '(%s AND %s)' % (self.expand(first), self.expand(second)) - - def OR(self, first, second): - return '(%s OR %s)' % (self.expand(first), self.expand(second)) - - def BELONGS(self, first, second): - if isinstance(second, str): - return '(%s IN (%s))' % (self.expand(first), second[:-1]) - if not second: - return '(1=0)' - items = ','.join(self.expand(item, first.type) for item in second) - return '(%s IN (%s))' % (self.expand(first), items) - - def REGEXP(self, first, second): - """Regular expression operator""" - raise NotImplementedError - - def LIKE(self, first, second): - """Case sensitive like operator""" - return '(%s LIKE %s)' % (self.expand(first), - self.expand(second, 'string')) - - def ILIKE(self, first, second): - """Case insensitive like operator""" - return '(LOWER(%s) LIKE %s)' % (self.expand(first), - self.expand(second, 'string').lower()) - - def STARTSWITH(self, first, second): - return '(%s LIKE %s)' % (self.expand(first), - self.expand(second+'%', 'string')) - - def ENDSWITH(self, first, second): - return '(%s LIKE %s)' % (self.expand(first), - self.expand('%'+second, 'string')) - - def CONTAINS(self, first, second, case_sensitive=True): - if first.type in ('string','text', 'json'): - if isinstance(second,Expression): - second = Expression(None,self.CONCAT('%',Expression( - None,self.REPLACE(second,('%','%%'))),'%')) - else: - second = '%'+str(second).replace('%','%%')+'%' - elif first.type.startswith('list:'): - if isinstance(second,Expression): - second = Expression(None,self.CONCAT( - '%|',Expression(None,self.REPLACE( - Expression(None,self.REPLACE( - second,('%','%%'))),('|','||'))),'|%')) - else: - second = '%|'+str(second).replace('%','%%')\ - .replace('|','||')+'|%' - op = case_sensitive and self.LIKE or self.ILIKE - return op(first,second) - - def EQ(self, first, second=None): - if second is None: - return '(%s IS NULL)' % self.expand(first) - return '(%s = %s)' % (self.expand(first), - self.expand(second, first.type)) - - def NE(self, first, second=None): - if second is None: - return '(%s IS NOT NULL)' % self.expand(first) - return '(%s <> %s)' % (self.expand(first), - self.expand(second, first.type)) - - def LT(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s < None" % first) - return '(%s < %s)' % (self.expand(first), - self.expand(second,first.type)) - - def LE(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s <= None" % first) - return '(%s <= %s)' % (self.expand(first), - self.expand(second,first.type)) - - def GT(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s > None" % first) - return '(%s > %s)' % (self.expand(first), - self.expand(second,first.type)) - - def GE(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s >= None" % first) - return '(%s >= %s)' % (self.expand(first), - self.expand(second,first.type)) - - def is_numerical_type(self, ftype): - return ftype in ('integer','boolean','double','bigint') or \ - ftype.startswith('decimal') - - def REPLACE(self, first, (second, third)): - return 'REPLACE(%s,%s,%s)' % (self.expand(first,'string'), - self.expand(second,'string'), - self.expand(third,'string')) - - def CONCAT(self, *items): - return '(%s)' % ' || '.join(self.expand(x,'string') for x in items) - - def ADD(self, first, second): - if self.is_numerical_type(first.type) or isinstance(first.type, Field): - return '(%s + %s)' % (self.expand(first), - self.expand(second, first.type)) - else: - return self.CONCAT(first, second) - - def SUB(self, first, second): - return '(%s - %s)' % (self.expand(first), - self.expand(second, first.type)) - - def MUL(self, first, second): - return '(%s * %s)' % (self.expand(first), - self.expand(second, first.type)) - - def DIV(self, first, second): - return '(%s / %s)' % (self.expand(first), - self.expand(second, first.type)) - - def MOD(self, first, second): - return '(%s %% %s)' % (self.expand(first), - self.expand(second, first.type)) - - def AS(self, first, second): - return '%s AS %s' % (self.expand(first), second) - - def ON(self, first, second): - table_rname = self.table_alias(first) - if use_common_filters(second): - second = self.common_filter(second,[first._tablename]) - return ('%s ON %s') % (self.expand(table_rname), self.expand(second)) - - def INVERT(self, first): - return '%s DESC' % self.expand(first) - - def COMMA(self, first, second): - return '%s, %s' % (self.expand(first), self.expand(second)) - - def CAST(self, first, second): - return 'CAST(%s AS %s)' % (first, second) - - def expand(self, expression, field_type=None, colnames=False): - if isinstance(expression, Field): - et = expression.table - if not colnames: - table_rname = et._ot and self.QUOTE_TEMPLATE % et._tablename or et._rname or self.QUOTE_TEMPLATE % et._tablename - out = '%s.%s' % (table_rname, expression._rname or (self.QUOTE_TEMPLATE % (expression.name))) - else: - out = '%s.%s' % (self.QUOTE_TEMPLATE % et._tablename, self.QUOTE_TEMPLATE % expression.name) - if field_type == 'string' and not expression.type in ( - 'string','text','json','password'): - out = self.CAST(out, self.types['text']) - return out - elif isinstance(expression, (Expression, Query)): - first = expression.first - second = expression.second - op = expression.op - optional_args = expression.optional_args or {} - if not second is None: - out = op(first, second, **optional_args) - elif not first is None: - out = op(first,**optional_args) - elif isinstance(op, str): - if op.endswith(';'): - op=op[:-1] - out = '(%s)' % op - else: - out = op() - return out - elif field_type: - return str(self.represent(expression,field_type)) - elif isinstance(expression,(list,tuple)): - return ','.join(self.represent(item,field_type) \ - for item in expression) - elif isinstance(expression, bool): - return '1' if expression else '0' - else: - return str(expression) - - def table_alias(self, tbl): - if not isinstance(tbl, Table): - tbl = self.db[tbl] - return tbl.sqlsafe_alias - - - def alias(self, table, alias): - """ - Given a table object, makes a new table object - with alias name. - """ - other = copy.copy(table) - other['_ot'] = other._ot or other.sqlsafe - other['ALL'] = SQLALL(other) - other['_tablename'] = alias - for fieldname in other.fields: - other[fieldname] = copy.copy(other[fieldname]) - other[fieldname]._tablename = alias - other[fieldname].tablename = alias - other[fieldname].table = other - table._db[alias] = other - return other - - def _truncate(self, table, mode=''): - return ['TRUNCATE TABLE %s %s;' % (table.sqlsafe, mode or '')] - - def truncate(self, table, mode= ' '): - # Prepare functions "write_to_logfile" and "close_logfile" - try: - queries = table._db._adapter._truncate(table, mode) - for query in queries: - self.log(query + '\n', table) - self.execute(query) - self.log('success!\n', table) - finally: - pass - - def _update(self, tablename, query, fields): - if query: - if use_common_filters(query): - query = self.common_filter(query, [tablename]) - sql_w = ' WHERE ' + self.expand(query) - else: - sql_w = '' - sql_v = ','.join(['%s=%s' % (field.sqlsafe_name, - self.expand(value, field.type)) \ - for (field, value) in fields]) - tablename = self.db[tablename].sqlsafe - return 'UPDATE %s SET %s%s;' % (tablename, sql_v, sql_w) - - def update(self, tablename, query, fields): - sql = self._update(tablename, query, fields) - try: - self.execute(sql) - except Exception: - e = sys.exc_info()[1] - table = self.db[tablename] - if hasattr(table,'_on_update_error'): - return table._on_update_error(table,query,fields,e) - raise e - try: - return self.cursor.rowcount - except: - return None - - def _delete(self, tablename, query): - if query: - if use_common_filters(query): - query = self.common_filter(query, [tablename]) - sql_w = ' WHERE ' + self.expand(query) - else: - sql_w = '' - tablename = self.db[tablename].sqlsafe - return 'DELETE FROM %s%s;' % (tablename, sql_w) - - def delete(self, tablename, query): - sql = self._delete(tablename, query) - self.execute(sql) - try: - counter = self.cursor.rowcount - except: - counter = None - return counter - - def get_table(self, query): - tablenames = self.tables(query) - if len(tablenames)==1: - return tablenames[0] - elif len(tablenames)<1: - raise RuntimeError("No table selected") - else: - raise RuntimeError("Too many tables selected") - - def expand_all(self, fields, tablenames): - db = self.db - new_fields = [] - append = new_fields.append - for item in fields: - if isinstance(item,SQLALL): - new_fields += item._table - elif isinstance(item,str): - m = self.REGEX_TABLE_DOT_FIELD.match(item) - if m: - tablename,fieldname = m.groups() - append(db[tablename][fieldname]) - else: - append(Expression(db,lambda item=item:item)) - else: - append(item) - # ## if no fields specified take them all from the requested tables - if not new_fields: - for table in tablenames: - for field in db[table]: - append(field) - return new_fields - - def _select(self, query, fields, attributes): - tables = self.tables - for key in set(attributes.keys())-SELECT_ARGS: - raise SyntaxError('invalid select attribute: %s' % key) - args_get = attributes.get - tablenames = tables(query) - tablenames_for_common_filters = tablenames - for field in fields: - if isinstance(field, basestring): - m = self.REGEX_TABLE_DOT_FIELD.match(field) - if m: - tn,fn = m.groups() - field = self.db[tn][fn] - for tablename in tables(field): - if not tablename in tablenames: - tablenames.append(tablename) - - if len(tablenames) < 1: - raise SyntaxError('Set: no tables selected') - def colexpand(field): - return self.expand(field, colnames=True) - self._colnames = map(colexpand, fields) - def geoexpand(field): - if isinstance(field.type,str) and field.type.startswith('geo') and isinstance(field, Field): - field = field.st_astext() - return self.expand(field) - sql_f = ', '.join(map(geoexpand, fields)) - sql_o = '' - sql_s = '' - left = args_get('left', False) - inner_join = args_get('join', False) - distinct = args_get('distinct', False) - groupby = args_get('groupby', False) - orderby = args_get('orderby', False) - having = args_get('having', False) - limitby = args_get('limitby', False) - orderby_on_limitby = args_get('orderby_on_limitby', True) - for_update = args_get('for_update', False) - if self.can_select_for_update is False and for_update is True: - raise SyntaxError('invalid select attribute: for_update') - if distinct is True: - sql_s += 'DISTINCT' - elif distinct: - sql_s += 'DISTINCT ON (%s)' % distinct - if inner_join: - icommand = self.JOIN() - if not isinstance(inner_join, (tuple, list)): - inner_join = [inner_join] - ijoint = [t._tablename for t in inner_join - if not isinstance(t,Expression)] - ijoinon = [t for t in inner_join if isinstance(t, Expression)] - itables_to_merge={} #issue 490 - [itables_to_merge.update( - dict.fromkeys(tables(t))) for t in ijoinon] - ijoinont = [t.first._tablename for t in ijoinon] - [itables_to_merge.pop(t) for t in ijoinont - if t in itables_to_merge] #issue 490 - iimportant_tablenames = ijoint + ijoinont + itables_to_merge.keys() - iexcluded = [t for t in tablenames - if not t in iimportant_tablenames] - if left: - join = attributes['left'] - command = self.LEFT_JOIN() - if not isinstance(join, (tuple, list)): - join = [join] - joint = [t._tablename for t in join - if not isinstance(t, Expression)] - joinon = [t for t in join if isinstance(t, Expression)] - #patch join+left patch (solves problem with ordering in left joins) - tables_to_merge={} - [tables_to_merge.update( - dict.fromkeys(tables(t))) for t in joinon] - joinont = [t.first._tablename for t in joinon] - [tables_to_merge.pop(t) for t in joinont if t in tables_to_merge] - tablenames_for_common_filters = [t for t in tablenames - if not t in joinont ] - important_tablenames = joint + joinont + tables_to_merge.keys() - excluded = [t for t in tablenames - if not t in important_tablenames ] - else: - excluded = tablenames - - if use_common_filters(query): - query = self.common_filter(query,tablenames_for_common_filters) - sql_w = ' WHERE ' + self.expand(query) if query else '' - - JOIN = ' CROSS JOIN ' - - if inner_join and not left: - # Wrap table references with parenthesis (approach 1) - # sql_t = ', '.join([self.table_alias(t) for t in iexcluded + \ - # itables_to_merge.keys()]) - # sql_t = '(%s)' % sql_t - # or approach 2: Use 'JOIN' instead comma: - sql_t = JOIN.join([self.table_alias(t) - for t in iexcluded + itables_to_merge.keys()]) - for t in ijoinon: - sql_t += ' %s %s' % (icommand, t) - elif not inner_join and left: - sql_t = JOIN.join([self.table_alias(t) for t in excluded + \ - tables_to_merge.keys()]) - if joint: - sql_t += ' %s %s' % (command, - ','.join([t for t in joint])) - for t in joinon: - sql_t += ' %s %s' % (command, t) - elif inner_join and left: - all_tables_in_query = set(important_tablenames + \ - iimportant_tablenames + \ - tablenames) - tables_in_joinon = set(joinont + ijoinont) - tables_not_in_joinon = \ - all_tables_in_query.difference(tables_in_joinon) - sql_t = JOIN.join([self.table_alias(t) for t in tables_not_in_joinon]) - for t in ijoinon: - sql_t += ' %s %s' % (icommand, t) - if joint: - sql_t += ' %s %s' % (command, - ','.join([t for t in joint])) - for t in joinon: - sql_t += ' %s %s' % (command, t) - else: - sql_t = ', '.join(self.table_alias(t) for t in tablenames) - if groupby: - if isinstance(groupby, (list, tuple)): - groupby = xorify(groupby) - sql_o += ' GROUP BY %s' % self.expand(groupby) - if having: - sql_o += ' HAVING %s' % attributes['having'] - if orderby: - if isinstance(orderby, (list, tuple)): - orderby = xorify(orderby) - if str(orderby) == '': - sql_o += ' ORDER BY %s' % self.RANDOM() - else: - sql_o += ' ORDER BY %s' % self.expand(orderby) - if (limitby and not groupby and tablenames and orderby_on_limitby and not orderby): - sql_o += ' ORDER BY %s' % ', '.join( - [self.db[t].sqlsafe + '.' + self.db[t][x].sqlsafe_name for t in tablenames for x in ( - hasattr(self.db[t], '_primarykey') and self.db[t]._primarykey - or ['_id'] - ) - ] - ) - # oracle does not support limitby - sql = self.select_limitby(sql_s, sql_f, sql_t, sql_w, sql_o, limitby) - if for_update and self.can_select_for_update is True: - sql = sql.rstrip(';') + ' FOR UPDATE;' - return sql - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_o += ' LIMIT %i OFFSET %i' % (lmax - lmin, lmin) - return 'SELECT %s %s FROM %s%s%s;' % \ - (sql_s, sql_f, sql_t, sql_w, sql_o) - - def _fetchall(self): - return self.cursor.fetchall() - - def _select_aux(self,sql,fields,attributes): - args_get = attributes.get - cache = args_get('cache',None) - if not cache: - self.execute(sql) - rows = self._fetchall() - else: - (cache_model, time_expire) = cache - key = self.uri + '/' + sql + '/rows' - if len(key)>200: key = hashlib_md5(key).hexdigest() - def _select_aux2(): - self.execute(sql) - return self._fetchall() - rows = cache_model(key,_select_aux2,time_expire) - if isinstance(rows,tuple): - rows = list(rows) - limitby = args_get('limitby', None) or (0,) - rows = self.rowslice(rows,limitby[0],None) - processor = args_get('processor',self.parse) - cacheable = args_get('cacheable',False) - return processor(rows,fields,self._colnames,cacheable=cacheable) - - def select(self, query, fields, attributes): - """ - Always returns a Rows object, possibly empty. - """ - sql = self._select(query, fields, attributes) - cache = attributes.get('cache', None) - if cache and attributes.get('cacheable',False): - del attributes['cache'] - (cache_model, time_expire) = cache - key = self.uri + '/' + sql - if len(key)>200: key = hashlib_md5(key).hexdigest() - args = (sql,fields,attributes) - return cache_model( - key, - lambda self=self,args=args:self._select_aux(*args), - time_expire) - else: - return self._select_aux(sql,fields,attributes) - - def _count(self, query, distinct=None): - tablenames = self.tables(query) - if query: - if use_common_filters(query): - query = self.common_filter(query, tablenames) - sql_w = ' WHERE ' + self.expand(query) - else: - sql_w = '' - sql_t = ','.join(self.table_alias(t) for t in tablenames) - if distinct: - if isinstance(distinct,(list, tuple)): - distinct = xorify(distinct) - sql_d = self.expand(distinct) - return 'SELECT count(DISTINCT %s) FROM %s%s;' % \ - (sql_d, sql_t, sql_w) - return 'SELECT count(*) FROM %s%s;' % (sql_t, sql_w) - - def count(self, query, distinct=None): - self.execute(self._count(query, distinct)) - return self.cursor.fetchone()[0] - - def tables(self, *queries): - tables = set() - for query in queries: - if isinstance(query, Field): - tables.add(query.tablename) - elif isinstance(query, (Expression, Query)): - if not query.first is None: - tables = tables.union(self.tables(query.first)) - if not query.second is None: - tables = tables.union(self.tables(query.second)) - return list(tables) - - def commit(self): - if self.connection: - return self.connection.commit() - - def rollback(self): - if self.connection: - return self.connection.rollback() - - def close_connection(self): - if self.connection: - r = self.connection.close() - self.connection = None - return r - - def distributed_transaction_begin(self, key): - return - - def prepare(self, key): - if self.connection: self.connection.prepare() - - def commit_prepared(self, key): - if self.connection: self.connection.commit() - - def rollback_prepared(self, key): - if self.connection: self.connection.rollback() - - def concat_add(self, tablename): - return ', ADD ' - - def constraint_name(self, table, fieldname): - return '%s_%s__constraint' % (table,fieldname) - - def create_sequence_and_triggers(self, query, table, **args): - self.execute(query) - - - def log_execute(self, *a, **b): - if not self.connection: raise ValueError(a[0]) - if not self.connection: return None - command = a[0] - if hasattr(self,'filter_sql_command'): - command = self.filter_sql_command(command) - if self.db._debug: - LOGGER.debug('SQL: %s' % command) - self.db._lastsql = command - t0 = time.time() - ret = self.cursor.execute(command, *a[1:], **b) - self.db._timings.append((command,time.time()-t0)) - del self.db._timings[:-TIMINGSSIZE] - return ret - - def execute(self, *a, **b): - return self.log_execute(*a, **b) - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if isinstance(obj, CALLABLETYPES): - obj = obj() - if isinstance(fieldtype, SQLCustomType): - value = fieldtype.encoder(obj) - if value and fieldtype.type in ('string','text', 'json'): - return self.adapt(value) - return value or 'NULL' - if isinstance(obj, (Expression, Field)): - return str(obj) - if field_is_type('list:'): - if not obj: - obj = [] - elif not isinstance(obj, (list, tuple)): - obj = [obj] - if field_is_type('list:string'): - obj = map(str,obj) - else: - obj = map(int,[o for o in obj if o != '']) - # we don't want to bar_encode json objects - if isinstance(obj, (list, tuple)) and (not fieldtype == "json"): - obj = bar_encode(obj) - if obj is None: - return 'NULL' - if obj == '' and not fieldtype[:2] in ['st', 'te', 'js', 'pa', 'up']: - return 'NULL' - r = self.represent_exceptions(obj, fieldtype) - if not r is None: - return r - if fieldtype == 'boolean': - if obj and not str(obj)[:1].upper() in '0F': - return self.smart_adapt(self.TRUE) - else: - return self.smart_adapt(self.FALSE) - if fieldtype == 'id' or fieldtype == 'integer': - return str(long(obj)) - if field_is_type('decimal'): - return str(obj) - elif field_is_type('reference'): # reference - # check for tablename first - referenced = fieldtype[9:].strip() - if referenced in self.db.tables: - return str(long(obj)) - p = referenced.partition('.') - if p[2] != '': - try: - ftype = self.db[p[0]][p[2]].type - return self.represent(obj, ftype) - except (ValueError, KeyError): - return repr(obj) - elif isinstance(obj, (Row, Reference)): - return str(obj['id']) - return str(long(obj)) - elif fieldtype == 'double': - return repr(float(obj)) - if isinstance(obj, unicode): - obj = obj.encode(self.db_codec) - if fieldtype == 'blob': - obj = base64.b64encode(str(obj)) - elif fieldtype == 'date': - if isinstance(obj, (datetime.date, datetime.datetime)): - obj = obj.isoformat()[:10] - else: - obj = str(obj) - elif fieldtype == 'datetime': - if isinstance(obj, datetime.datetime): - obj = obj.isoformat(self.T_SEP)[:19] - elif isinstance(obj, datetime.date): - obj = obj.isoformat()[:10]+self.T_SEP+'00:00:00' - else: - obj = str(obj) - elif fieldtype == 'time': - if isinstance(obj, datetime.time): - obj = obj.isoformat()[:10] - else: - obj = str(obj) - elif fieldtype == 'json': - if not 'dumps' in self.driver_auto_json: - # always pass a string JSON string - if have_serializers: - obj = serializers.json(obj) - elif simplejson: - obj = simplejson.dumps(obj) - else: - raise RuntimeError("missing simplejson") - if not isinstance(obj,bytes): - obj = bytes(obj) - try: - obj.decode(self.db_codec) - except: - obj = obj.decode('latin1').encode(self.db_codec) - return self.adapt(obj) - - def represent_exceptions(self, obj, fieldtype): - return None - - def lastrowid(self, table): - return None - - def rowslice(self, rows, minimum=0, maximum=None): - """ - By default this function does nothing; - overload when db does not do slicing. - """ - return rows - - def parse_value(self, value, field_type, blob_decode=True): - if field_type != 'blob' and isinstance(value, str): - try: - value = value.decode(self.db._db_codec) - except Exception: - pass - if isinstance(value, unicode): - value = value.encode('utf-8') - if isinstance(field_type, SQLCustomType): - value = field_type.decoder(value) - if not isinstance(field_type, str) or value is None: - return value - elif field_type in ('string', 'text', 'password', 'upload', 'dict'): - return value - elif field_type.startswith('geo'): - return value - elif field_type == 'blob' and not blob_decode: - return value - else: - key = REGEX_TYPE.match(field_type).group(0) - return self.parsemap[key](value,field_type) - - def parse_reference(self, value, field_type): - referee = field_type[10:].strip() - if not '.' in referee: - value = Reference(value) - value._table, value._record = self.db[referee], None - return value - - def parse_boolean(self, value, field_type): - return value == self.TRUE or str(value)[:1].lower() == 't' - - def parse_date(self, value, field_type): - if isinstance(value, datetime.datetime): - return value.date() - if not isinstance(value, (datetime.date,datetime.datetime)): - (y, m, d) = map(int, str(value)[:10].strip().split('-')) - value = datetime.date(y, m, d) - return value - - def parse_time(self, value, field_type): - if not isinstance(value, datetime.time): - time_items = map(int,str(value)[:8].strip().split(':')[:3]) - if len(time_items) == 3: - (h, mi, s) = time_items - else: - (h, mi, s) = time_items + [0] - value = datetime.time(h, mi, s) - return value - - def parse_datetime(self, value, field_type): - if not isinstance(value, datetime.datetime): - value = str(value) - date_part,time_part,timezone = value[:10],value[11:19],value[19:] - if '+' in timezone: - ms,tz = timezone.split('+') - h,m = tz.split(':') - dt = datetime.timedelta(seconds=3600*int(h)+60*int(m)) - elif '-' in timezone: - ms,tz = timezone.split('-') - h,m = tz.split(':') - dt = -datetime.timedelta(seconds=3600*int(h)+60*int(m)) - else: - dt = None - (y, m, d) = map(int,date_part.split('-')) - time_parts = time_part and time_part.split(':')[:3] or (0,0,0) - while len(time_parts)<3: time_parts.append(0) - time_items = map(int,time_parts) - (h, mi, s) = time_items - value = datetime.datetime(y, m, d, h, mi, s) - if dt: - value = value + dt - return value - - def parse_blob(self, value, field_type): - return base64.b64decode(str(value)) - - def parse_decimal(self, value, field_type): - decimals = int(field_type[8:-1].split(',')[-1]) - if self.dbengine in ('sqlite', 'spatialite'): - value = ('%.' + str(decimals) + 'f') % value - if not isinstance(value, decimal.Decimal): - value = decimal.Decimal(str(value)) - return value - - def parse_list_integers(self, value, field_type): - if not isinstance(self, NoSQLAdapter): - value = bar_decode_integer(value) - return value - - def parse_list_references(self, value, field_type): - if not isinstance(self, NoSQLAdapter): - value = bar_decode_integer(value) - return [self.parse_reference(r, field_type[5:]) for r in value] - - def parse_list_strings(self, value, field_type): - if not isinstance(self, NoSQLAdapter): - value = bar_decode_string(value) - return value - - def parse_id(self, value, field_type): - return long(value) - - def parse_integer(self, value, field_type): - return long(value) - - def parse_double(self, value, field_type): - return float(value) - - def parse_json(self, value, field_type): - if not 'loads' in self.driver_auto_json: - if not isinstance(value, basestring): - raise RuntimeError('json data not a string') - if isinstance(value, unicode): - value = value.encode('utf-8') - if have_serializers: - value = serializers.loads_json(value) - elif simplejson: - value = simplejson.loads(value) - else: - raise RuntimeError("missing simplejson") - return value - - def build_parsemap(self): - self.parsemap = { - 'id':self.parse_id, - 'integer':self.parse_integer, - 'bigint':self.parse_integer, - 'float':self.parse_double, - 'double':self.parse_double, - 'reference':self.parse_reference, - 'boolean':self.parse_boolean, - 'date':self.parse_date, - 'time':self.parse_time, - 'datetime':self.parse_datetime, - 'blob':self.parse_blob, - 'decimal':self.parse_decimal, - 'json':self.parse_json, - 'list:integer':self.parse_list_integers, - 'list:reference':self.parse_list_references, - 'list:string':self.parse_list_strings, - } - - def parse(self, rows, fields, colnames, blob_decode=True, - cacheable = False): - from .google import GoogleDatastoreAdapter - db = self.db - virtualtables = [] - new_rows = [] - tmps = [] - for colname in colnames: - col_m = self.REGEX_TABLE_DOT_FIELD.match(colname) - if not col_m: - tmps.append(None) - else: - tablename, fieldname = col_m.groups() - table = db[tablename] - field = table[fieldname] - ft = field.type - tmps.append((tablename, fieldname, table, field, ft)) - for (i,row) in enumerate(rows): - new_row = Row() - for (j,colname) in enumerate(colnames): - value = row[j] - tmp = tmps[j] - if tmp: - (tablename,fieldname,table,field,ft) = tmp - colset = new_row.get(tablename, None) - if colset is None: - colset = new_row[tablename] = Row() - if tablename not in virtualtables: - virtualtables.append(tablename) - value = self.parse_value(value,ft,blob_decode) - if field.filter_out: - value = field.filter_out(value) - colset[fieldname] = value - - # for backward compatibility - if ft=='id' and fieldname!='id' and \ - not 'id' in table.fields: - colset['id'] = value - - if ft == 'id' and not cacheable: - # temporary hack to deal with - # GoogleDatastoreAdapter - # references - if isinstance(self, GoogleDatastoreAdapter): - id = value.key.id() if self.use_ndb else value.key().id_or_name() - colset[fieldname] = id - colset.gae_item = value - else: - id = value - colset.update_record = RecordUpdater(colset,table,id) - colset.delete_record = RecordDeleter(table,id) - if table._db._lazy_tables: - colset['__get_lazy_reference__'] = LazyReferenceGetter(table, id) - for rfield in table._referenced_by: - referee_link = db._referee_name and \ - db._referee_name % dict( - table=rfield.tablename,field=rfield.name) - if referee_link and not referee_link in colset: - colset[referee_link] = LazySet(rfield,id) - else: - if not '_extra' in new_row: - new_row['_extra'] = Row() - new_row['_extra'][colname] = \ - self.parse_value(value, - fields[j].type,blob_decode) - new_column_name = \ - REGEX_SELECT_AS_PARSER.search(colname) - if not new_column_name is None: - column_name = new_column_name.groups(0) - setattr(new_row,column_name[0],value) - new_rows.append(new_row) - rowsobj = Rows(db, new_rows, colnames, rawrows=rows) - - - for tablename in virtualtables: - table = db[tablename] - fields_virtual = [(f,v) for (f,v) in table.iteritems() - if isinstance(v,FieldVirtual)] - fields_lazy = [(f,v) for (f,v) in table.iteritems() - if isinstance(v,FieldMethod)] - if fields_virtual or fields_lazy: - for row in rowsobj.records: - box = row[tablename] - for f,v in fields_virtual: - try: - box[f] = v.f(row) - except AttributeError: - pass # not enough fields to define virtual field - for f,v in fields_lazy: - try: - box[f] = (v.handler or VirtualCommand)(v.f,row) - except AttributeError: - pass # not enough fields to define virtual field - - ### old style virtual fields - for item in table.virtualfields: - try: - rowsobj = rowsobj.setvirtualfields(**{tablename:item}) - except (KeyError, AttributeError): - # to avoid breaking virtualfields when partial select - pass - return rowsobj - - def common_filter(self, query, tablenames): - tenant_fieldname = self.db._request_tenant - - for tablename in tablenames: - table = self.db[tablename] - - # deal with user provided filters - if table._common_filter != None: - query = query & table._common_filter(query) - - # deal with multi_tenant filters - if tenant_fieldname in table: - default = table[tenant_fieldname].default - if not default is None: - newquery = table[tenant_fieldname] == default - if query is None: - query = newquery - else: - query = query & newquery - return query - - def CASE(self,query,t,f): - def represent(x): - types = {type(True):'boolean',type(0):'integer',type(1.0):'double'} - if x is None: return 'NULL' - elif isinstance(x,Expression): return str(x) - else: return self.represent(x,types.get(type(x),'string')) - return Expression(self.db,'CASE WHEN %s THEN %s ELSE %s END' % \ - (self.expand(query),represent(t),represent(f))) - - def sqlsafe_table(self, tablename, ot=None): - if ot is not None: - return ('%s AS ' + self.QUOTE_TEMPLATE) % (ot, tablename) - return self.QUOTE_TEMPLATE % tablename - - def sqlsafe_field(self, fieldname): - return self.QUOTE_TEMPLATE % fieldname - - -class NoSQLAdapter(BaseAdapter): - can_select_for_update = False - QUOTE_TEMPLATE = '%s' - - @staticmethod - def to_unicode(obj): - if isinstance(obj, str): - return obj.decode('utf8') - elif not isinstance(obj, unicode): - return unicode(obj) - return obj - - def id_query(self, table): - return table._id > 0 - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if isinstance(obj, CALLABLETYPES): - obj = obj() - if isinstance(fieldtype, SQLCustomType): - return fieldtype.encoder(obj) - if isinstance(obj, (Expression, Field)): - raise SyntaxError("non supported on GAE") - if self.dbengine == 'google:datastore': - if isinstance(fieldtype, gae.Property): - return obj - is_string = isinstance(fieldtype,str) - is_list = is_string and field_is_type('list:') - if is_list: - if not obj: - obj = [] - if not isinstance(obj, (list, tuple)): - obj = [obj] - if obj == '' and not \ - (is_string and fieldtype[:2] in ['st','te', 'pa','up']): - return None - if not obj is None: - if isinstance(obj, list) and not is_list: - obj = [self.represent(o, fieldtype) for o in obj] - elif fieldtype in ('integer','bigint','id'): - obj = long(obj) - elif fieldtype == 'double': - obj = float(obj) - elif is_string and field_is_type('reference'): - if isinstance(obj, (Row, Reference)): - obj = obj['id'] - obj = long(obj) - elif fieldtype == 'boolean': - if obj and not str(obj)[0].upper() in '0F': - obj = True - else: - obj = False - elif fieldtype == 'date': - if not isinstance(obj, datetime.date): - (y, m, d) = map(int,str(obj).strip().split('-')) - obj = datetime.date(y, m, d) - elif isinstance(obj,datetime.datetime): - (y, m, d) = (obj.year, obj.month, obj.day) - obj = datetime.date(y, m, d) - elif fieldtype == 'time': - if not isinstance(obj, datetime.time): - time_items = map(int,str(obj).strip().split(':')[:3]) - if len(time_items) == 3: - (h, mi, s) = time_items - else: - (h, mi, s) = time_items + [0] - obj = datetime.time(h, mi, s) - elif fieldtype == 'datetime': - if not isinstance(obj, datetime.datetime): - (y, m, d) = map(int,str(obj)[:10].strip().split('-')) - time_items = map(int,str(obj)[11:].strip().split(':')[:3]) - while len(time_items)<3: - time_items.append(0) - (h, mi, s) = time_items - obj = datetime.datetime(y, m, d, h, mi, s) - elif fieldtype == 'blob': - pass - elif fieldtype == 'json': - if isinstance(obj, basestring): - obj = self.to_unicode(obj) - if have_serializers: - obj = serializers.loads_json(obj) - elif simplejson: - obj = simplejson.loads(obj) - else: - raise RuntimeError("missing simplejson") - elif is_string and field_is_type('list:string'): - return map(self.to_unicode,obj) - elif is_list: - return map(int,obj) - else: - obj = self.to_unicode(obj) - return obj - - def _insert(self,table,fields): - return 'insert %s in %s' % (fields, table) - - def _count(self,query,distinct=None): - return 'count %s' % repr(query) - - def _select(self,query,fields,attributes): - return 'select %s where %s' % (repr(fields), repr(query)) - - def _delete(self,tablename, query): - return 'delete %s where %s' % (repr(tablename),repr(query)) - - def _update(self,tablename,query,fields): - return 'update %s (%s) where %s' % (repr(tablename), - repr(fields),repr(query)) - - def commit(self): - """ - remember: no transactions on many NoSQL - """ - pass - - def rollback(self): - """ - remember: no transactions on many NoSQL - """ - pass - - def close_connection(self): - """ - remember: no transactions on many NoSQL - """ - pass - - - # these functions should never be called! - def OR(self,first,second): raise SyntaxError("Not supported") - def AND(self,first,second): raise SyntaxError("Not supported") - def AS(self,first,second): raise SyntaxError("Not supported") - def ON(self,first,second): raise SyntaxError("Not supported") - def STARTSWITH(self,first,second=None): raise SyntaxError("Not supported") - def ENDSWITH(self,first,second=None): raise SyntaxError("Not supported") - def ADD(self,first,second): raise SyntaxError("Not supported") - def SUB(self,first,second): raise SyntaxError("Not supported") - def MUL(self,first,second): raise SyntaxError("Not supported") - def DIV(self,first,second): raise SyntaxError("Not supported") - def LOWER(self,first): raise SyntaxError("Not supported") - def UPPER(self,first): raise SyntaxError("Not supported") - def EXTRACT(self,first,what): raise SyntaxError("Not supported") - def LENGTH(self, first): raise SyntaxError("Not supported") - def AGGREGATE(self,first,what): raise SyntaxError("Not supported") - def LEFT_JOIN(self): raise SyntaxError("Not supported") - def RANDOM(self): raise SyntaxError("Not supported") - def SUBSTRING(self,field,parameters): raise SyntaxError("Not supported") - def PRIMARY_KEY(self,key): raise SyntaxError("Not supported") - def ILIKE(self,first,second): raise SyntaxError("Not supported") - def drop(self,table,mode): raise SyntaxError("Not supported") - def alias(self,table,alias): raise SyntaxError("Not supported") - def migrate_table(self,*a,**b): raise SyntaxError("Not supported") - def distributed_transaction_begin(self,key): raise SyntaxError("Not supported") - def prepare(self,key): raise SyntaxError("Not supported") - def commit_prepared(self,key): raise SyntaxError("Not supported") - def rollback_prepared(self,key): raise SyntaxError("Not supported") - def concat_add(self,table): raise SyntaxError("Not supported") - def constraint_name(self, table, fieldname): raise SyntaxError("Not supported") - def create_sequence_and_triggers(self, query, table, **args): pass - def log_execute(self,*a,**b): raise SyntaxError("Not supported") - def execute(self,*a,**b): raise SyntaxError("Not supported") - def represent_exceptions(self, obj, fieldtype): raise SyntaxError("Not supported") - def lastrowid(self,table): raise SyntaxError("Not supported") - def rowslice(self,rows,minimum=0,maximum=None): raise SyntaxError("Not supported") diff --git a/gluon/dal/adapters/couchdb.py b/gluon/dal/adapters/couchdb.py deleted file mode 100644 index e2720e91..00000000 --- a/gluon/dal/adapters/couchdb.py +++ /dev/null @@ -1,202 +0,0 @@ -# -*- coding: utf-8 -*- -import datetime - -from .._globals import IDENTITY -from .._load import serializers, couchdb, web2py_uuid -from ..objects import Field, Query -from ..helpers.classes import SQLALL -from ..helpers.methods import uuid2int -from .base import BaseAdapter, NoSQLAdapter, SELECT_ARGS - - -class CouchDBAdapter(NoSQLAdapter): - drivers = ('couchdb',) - - uploads_in_blob = True - types = { - 'boolean': bool, - 'string': str, - 'text': str, - 'json': str, - 'password': str, - 'blob': str, - 'upload': str, - 'integer': long, - 'bigint': long, - 'float': float, - 'double': float, - 'date': datetime.date, - 'time': datetime.time, - 'datetime': datetime.datetime, - 'id': long, - 'reference': long, - 'list:string': list, - 'list:integer': list, - 'list:reference': list, - } - - def file_exists(self, filename): pass - def file_open(self, filename, mode='rb', lock=True): pass - def file_close(self, fileobj): pass - - def expand(self,expression,field_type=None): - if isinstance(expression,Field): - if expression.type=='id': - return "%s._id" % expression.tablename - return BaseAdapter.expand(self,expression,field_type) - - def AND(self,first,second): - return '(%s && %s)' % (self.expand(first),self.expand(second)) - - def OR(self,first,second): - return '(%s || %s)' % (self.expand(first),self.expand(second)) - - def EQ(self,first,second): - if second is None: - return '(%s == null)' % self.expand(first) - return '(%s == %s)' % (self.expand(first),self.expand(second,first.type)) - - def NE(self,first,second): - if second is None: - return '(%s != null)' % self.expand(first) - return '(%s != %s)' % (self.expand(first),self.expand(second,first.type)) - - def COMMA(self,first,second): - return '%s + %s' % (self.expand(first),self.expand(second)) - - def represent(self, obj, fieldtype): - value = NoSQLAdapter.represent(self, obj, fieldtype) - if fieldtype=='id': - return repr(str(long(value))) - elif fieldtype in ('date','time','datetime','boolean'): - return serializers.json(value) - return repr(not isinstance(value,unicode) and value \ - or value and value.encode('utf8')) - - def __init__(self,db,uri='couchdb://127.0.0.1:5984', - pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.uri = uri - if do_connect: self.find_driver(adapter_args) - self.dbengine = 'couchdb' - self.folder = folder - db['_lastsql'] = '' - self.db_codec = 'UTF-8' - self._after_connection = after_connection - self.pool_size = pool_size - - url='http://'+uri[10:] - def connector(url=url,driver_args=driver_args): - return self.driver.Server(url,**driver_args) - self.reconnect(connector,cursor=False) - - def create_table(self, table, migrate=True, fake_migrate=False, polymodel=None): - if migrate: - try: - self.connection.create(table._tablename) - except: - pass - - def insert(self,table,fields): - id = uuid2int(web2py_uuid()) - ctable = self.connection[table._tablename] - values = dict((k.name,self.represent(v,k.type)) for k,v in fields) - values['_id'] = str(id) - ctable.save(values) - return id - - def _select(self,query,fields,attributes): - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - for key in set(attributes.keys())-SELECT_ARGS: - raise SyntaxError('invalid select attribute: %s' % key) - new_fields=[] - for item in fields: - if isinstance(item,SQLALL): - new_fields += item._table - else: - new_fields.append(item) - def uid(fd): - return fd=='id' and '_id' or fd - def get(row,fd): - return fd=='id' and long(row['_id']) or row.get(fd,None) - fields = new_fields - tablename = self.get_table(query) - fieldnames = [f.name for f in (fields or self.db[tablename])] - colnames = ['%s.%s' % (tablename,k) for k in fieldnames] - fields = ','.join(['%s.%s' % (tablename,uid(f)) for f in fieldnames]) - fn="(function(%(t)s){if(%(query)s)emit(%(order)s,[%(fields)s]);})" %\ - dict(t=tablename, - query=self.expand(query), - order='%s._id' % tablename, - fields=fields) - return fn, colnames - - def select(self,query,fields,attributes): - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - fn, colnames = self._select(query,fields,attributes) - tablename = colnames[0].split('.')[0] - ctable = self.connection[tablename] - rows = [cols['value'] for cols in ctable.query(fn)] - processor = attributes.get('processor',self.parse) - return processor(rows,fields,colnames,False) - - def delete(self,tablename,query): - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - if query.first.type=='id' and query.op==self.EQ: - id = query.second - tablename = query.first.tablename - assert(tablename == query.first.tablename) - ctable = self.connection[tablename] - try: - del ctable[str(id)] - return 1 - except couchdb.http.ResourceNotFound: - return 0 - else: - tablename = self.get_table(query) - rows = self.select(query,[self.db[tablename]._id],{}) - ctable = self.connection[tablename] - for row in rows: - del ctable[str(row.id)] - return len(rows) - - def update(self,tablename,query,fields): - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - if query.first.type=='id' and query.op==self.EQ: - id = query.second - tablename = query.first.tablename - ctable = self.connection[tablename] - try: - doc = ctable[str(id)] - for key,value in fields: - doc[key.name] = self.represent(value,self.db[tablename][key.name].type) - ctable.save(doc) - return 1 - except couchdb.http.ResourceNotFound: - return 0 - else: - tablename = self.get_table(query) - rows = self.select(query,[self.db[tablename]._id],{}) - ctable = self.connection[tablename] - table = self.db[tablename] - for row in rows: - doc = ctable[str(row.id)] - for key,value in fields: - doc[key.name] = self.represent(value,table[key.name].type) - ctable.save(doc) - return len(rows) - - def count(self,query,distinct=None): - if distinct: - raise RuntimeError("COUNT DISTINCT not supported") - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - tablename = self.get_table(query) - rows = self.select(query,[self.db[tablename]._id],{}) - return len(rows) diff --git a/gluon/dal/adapters/cubrid.py b/gluon/dal/adapters/cubrid.py deleted file mode 100644 index 51c2ccbf..00000000 --- a/gluon/dal/adapters/cubrid.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: utf-8 -*- -import re - -from .._globals import IDENTITY -from .mysql import MySQLAdapter - - -class CubridAdapter(MySQLAdapter): - drivers = ('cubriddb',) - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P[^?]+)(\?set_encoding=(?P\w+))?$') - - def __init__(self, db, uri, pool_size=0, folder=None, db_codec='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "cubrid" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = int(m.group('port') or '30000') - user = credential_decoder(user) - passwd = credential_decoder(password) - def connector(host=host,port=port,db=db, - user=user,passwd=passwd,driver_args=driver_args): - return self.driver.connect(host,port,db,user,passwd,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.execute('SET FOREIGN_KEY_CHECKS=1;') - self.execute("SET sql_mode='NO_BACKSLASH_ESCAPES';") - diff --git a/gluon/dal/adapters/db2.py b/gluon/dal/adapters/db2.py deleted file mode 100644 index fd5ca520..00000000 --- a/gluon/dal/adapters/db2.py +++ /dev/null @@ -1,105 +0,0 @@ -# -*- coding: utf-8 -*- -import base64 -import datetime - -from .._globals import IDENTITY -from .base import BaseAdapter - - -class DB2Adapter(BaseAdapter): - drivers = ('ibm_db_dbi', 'pyodbc') - - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'CLOB', - 'json': 'CLOB', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BLOB', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'REAL', - 'double': 'DOUBLE', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL', - 'reference': 'INT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'CLOB', - 'list:string': 'CLOB', - 'list:reference': 'CLOB', - 'big-id': 'BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY NOT NULL', - 'big-reference': 'BIGINT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - def LEFT_JOIN(self): - return 'LEFT OUTER JOIN' - - def RANDOM(self): - return 'RAND()' - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_o += ' FETCH FIRST %i ROWS ONLY' % lmax - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def represent_exceptions(self, obj, fieldtype): - if fieldtype == 'blob': - obj = base64.b64encode(str(obj)) - return "BLOB('%s')" % obj - elif fieldtype == 'datetime': - if isinstance(obj, datetime.datetime): - obj = obj.isoformat()[:19].replace('T','-').replace(':','.') - elif isinstance(obj, datetime.date): - obj = obj.isoformat()[:10]+'-00.00.00' - return "'%s'" % obj - return None - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "db2" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://', 1)[1] - - def connector(cnxn=ruri,driver_args=driver_args): - if self.driver_name == 'ibm_db_dbi': - vars = cnxn.split(";") - cnxn = {} - for var in vars: - v = var.split('=') - cnxn[v[0].lower()] = v[1] - return self.driver.connect(cnxn['dsn'], cnxn['uid'], cnxn['pwd'], **driver_args) - else: - return self.driver.connect(cnxn, **driver_args) - - self.connector = connector - if do_connect: self.reconnect() - - def execute(self,command,placeholders=None): - if command[-1:]==';': - command = command[:-1] - if placeholders: - return self.log_execute(command, placeholders) - return self.log_execute(command) - - def lastrowid(self,table): - self.execute('SELECT DISTINCT IDENTITY_VAL_LOCAL() FROM %s;' % table) - return long(self.cursor.fetchone()[0]) - - def rowslice(self,rows,minimum=0,maximum=None): - if maximum is None: - return rows[minimum:] - return rows[minimum:maximum] diff --git a/gluon/dal/adapters/firebird.py b/gluon/dal/adapters/firebird.py deleted file mode 100644 index aebeaa5d..00000000 --- a/gluon/dal/adapters/firebird.py +++ /dev/null @@ -1,182 +0,0 @@ -# -*- coding: utf-8 -*- -import re - -from .._globals import IDENTITY -from ..objects import Expression -from .base import BaseAdapter - - -class FireBirdAdapter(BaseAdapter): - drivers = ('kinterbasdb','firebirdsql','fdb','pyodbc') - - commit_on_alter_table = False - support_distributed_transaction = True - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'BLOB SUB_TYPE 1', - 'json': 'BLOB SUB_TYPE 1', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BLOB SUB_TYPE 0', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INTEGER', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'DOUBLE PRECISION', - 'decimal': 'DECIMAL(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'INTEGER PRIMARY KEY', - 'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'BLOB SUB_TYPE 1', - 'list:string': 'BLOB SUB_TYPE 1', - 'list:reference': 'BLOB SUB_TYPE 1', - 'big-id': 'BIGINT PRIMARY KEY', - 'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - def sequence_name(self,tablename): - return ('genid_' + self.QUOTE_TEMPLATE) % tablename - - def trigger_name(self,tablename): - return 'trg_id_%s' % tablename - - def RANDOM(self): - return 'RAND()' - - def EPOCH(self, first): - return "DATEDIFF(second, '1970-01-01 00:00:00', %s)" % self.expand(first) - - def NOT_NULL(self,default,field_type): - return 'DEFAULT %s NOT NULL' % self.represent(default,field_type) - - def SUBSTRING(self,field,parameters): - return 'SUBSTRING(%s from %s for %s)' % (self.expand(field), parameters[0], parameters[1]) - - def LENGTH(self, first): - return "CHAR_LENGTH(%s)" % self.expand(first) - - def CONTAINS(self,first,second,case_sensitive=False): - if first.type.startswith('list:'): - second = Expression(None,self.CONCAT('|',Expression( - None,self.REPLACE(second,('|','||'))),'|')) - return '(%s CONTAINING %s)' % (self.expand(first), - self.expand(second, 'string')) - - def _drop(self,table,mode): - sequence_name = table._sequence_name - return ['DROP TABLE %s %s;' % (table.sqlsafe, mode), 'DROP GENERATOR %s;' % sequence_name] - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_s = ' FIRST %i SKIP %i %s' % (lmax - lmin, lmin, sql_s) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def _truncate(self,table,mode = ''): - return ['DELETE FROM %s;' % table._tablename, - 'SET GENERATOR %s TO 0;' % table._sequence_name] - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P.+?)(\?set_encoding=(?P\w+))?$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "firebird" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError("Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - port = int(m.group('port') or 3050) - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - charset = m.group('charset') or 'UTF8' - driver_args.update(dsn='%s/%s:%s' % (host,port,db), - user = credential_decoder(user), - password = credential_decoder(password), - charset = charset) - - def connector(driver_args=driver_args): - return self.driver.connect(**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def create_sequence_and_triggers(self, query, table, **args): - tablename = table._tablename - sequence_name = table._sequence_name - trigger_name = table._trigger_name - self.execute(query) - self.execute('create generator %s;' % sequence_name) - self.execute('set generator %s to 0;' % sequence_name) - self.execute('create trigger %s for %s active before insert position 0 as\nbegin\nif(new.id is null) then\nbegin\nnew.id = gen_id(%s, 1);\nend\nend;' % (trigger_name, tablename, sequence_name)) - - def lastrowid(self,table): - sequence_name = table._sequence_name - self.execute('SELECT gen_id(%s, 0) FROM rdb$database' % sequence_name) - return long(self.cursor.fetchone()[0]) - - -class FireBirdEmbeddedAdapter(FireBirdAdapter): - drivers = ('kinterbasdb','firebirdsql','fdb','pyodbc') - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\?]+)(\?set_encoding=(?P\w+))?$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "firebird" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - pathdb = m.group('path') - if not pathdb: - raise SyntaxError('Path required') - charset = m.group('charset') - if not charset: - charset = 'UTF8' - host = '' - driver_args.update(host=host, - database=pathdb, - user=credential_decoder(user), - password=credential_decoder(password), - charset=charset) - - def connector(driver_args=driver_args): - return self.driver.connect(**driver_args) - self.connector = connector - if do_connect: self.reconnect() diff --git a/gluon/dal/adapters/google.py b/gluon/dal/adapters/google.py deleted file mode 100644 index 86c3ef3c..00000000 --- a/gluon/dal/adapters/google.py +++ /dev/null @@ -1,621 +0,0 @@ -# -*- coding: utf-8 -*- -import os -import re - -from .._compat import pjoin -from .._globals import IDENTITY, LOGGER, THREAD_LOCAL -from .._load import classobj, gae, ndb, NDBDecimalProperty, GAEDecimalProperty, \ - namespace_manager, Key, NDBPolyModel, PolyModel, rdbms, have_serializers, \ - serializers, simplejson -from ..objects import Table, Field, Expression, Query -from ..helpers.classes import SQLCustomType, SQLALL, Reference, UseDatabaseStoredFile -from ..helpers.methods import use_common_filters, xorify -from .base import NoSQLAdapter -from .mysql import MySQLAdapter - - -class GoogleSQLAdapter(UseDatabaseStoredFile, MySQLAdapter): - uploads_in_blob = True - - REGEX_URI = re.compile('^(?P.*)/(?P.*)$') - - def __init__(self, db, uri='google:sql://realm:domain/database', - pool_size=0, folder=None, db_codec='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - - self.db = db - self.dbengine = "mysql" - self.uri = uri - self.pool_size = pool_size - self.db_codec = db_codec - self._after_connection = after_connection - if do_connect: self.find_driver(adapter_args, uri) - self.folder = folder or pjoin('$HOME',THREAD_LOCAL.folder.split( - os.sep+'applications'+os.sep,1)[1]) - ruri = uri.split("://")[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError("Invalid URI string in SQLDB: %s" % self.uri) - instance = credential_decoder(m.group('instance')) - self.dbstring = db = credential_decoder(m.group('db')) - driver_args['instance'] = instance - if not 'charset' in driver_args: - driver_args['charset'] = 'utf8' - self.createdb = createdb = adapter_args.get('createdb',True) - if not createdb: - driver_args['database'] = db - def connector(driver_args=driver_args): - return rdbms.connect(**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - if self.createdb: - # self.execute('DROP DATABASE %s' % self.dbstring) - self.execute('CREATE DATABASE IF NOT EXISTS %s' % self.dbstring) - self.execute('USE %s' % self.dbstring) - self.execute("SET FOREIGN_KEY_CHECKS=1;") - self.execute("SET sql_mode='NO_BACKSLASH_ESCAPES';") - - def execute(self, command, *a, **b): - return self.log_execute(command.decode('utf8'), *a, **b) - - def find_driver(self,adapter_args,uri=None): - self.adapter_args = adapter_args - self.driver = "google" - - -class GAEF(object): - def __init__(self,name,op,value,apply): - self.name=name=='id' and '__key__' or name - self.op=op - self.value=value - self.apply=apply - def __repr__(self): - return '(%s %s %s:%s)' % (self.name, self.op, repr(self.value), type(self.value)) - - -class GoogleDatastoreAdapter(NoSQLAdapter): - """ - NDB: - - You can enable NDB by using adapter_args:: - - db = DAL('google:datastore', adapter_args={'ndb_settings':ndb_settings, 'use_ndb':True}) - - ndb_settings is optional and can be used for per model caching settings. - It must be a dict in this form:: - - ndb_settings = {:{:}} - - See: https://developers.google.com/appengine/docs/python/ndb/cache - """ - - MAX_FETCH_LIMIT = 1000000 - uploads_in_blob = True - types = {} - # reconnect is not required for Datastore dbs - reconnect = lambda *args, **kwargs: None - - def file_exists(self, filename): pass - def file_open(self, filename, mode='rb', lock=True): pass - def file_close(self, fileobj): pass - - REGEX_NAMESPACE = re.compile('.*://(?P.+)') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.use_ndb = adapter_args.get('use_ndb',uri.startswith('google:datastore+ndb')) - if self.use_ndb is True: - self.types.update({ - 'boolean': ndb.BooleanProperty, - 'string': (lambda **kwargs: ndb.StringProperty(**kwargs)), - 'text': ndb.TextProperty, - 'json': ndb.TextProperty, - 'password': ndb.StringProperty, - 'blob': ndb.BlobProperty, - 'upload': ndb.StringProperty, - 'integer': ndb.IntegerProperty, - 'bigint': ndb.IntegerProperty, - 'float': ndb.FloatProperty, - 'double': ndb.FloatProperty, - 'decimal': NDBDecimalProperty, - 'date': ndb.DateProperty, - 'time': ndb.TimeProperty, - 'datetime': ndb.DateTimeProperty, - 'id': None, - 'reference': ndb.IntegerProperty, - 'list:string': (lambda **kwargs: ndb.StringProperty(repeated=True,default=None, **kwargs)), - 'list:integer': (lambda **kwargs: ndb.IntegerProperty(repeated=True,default=None, **kwargs)), - 'list:reference': (lambda **kwargs: ndb.IntegerProperty(repeated=True,default=None, **kwargs)), - }) - else: - self.types.update({ - 'boolean': gae.BooleanProperty, - 'string': (lambda **kwargs: gae.StringProperty(multiline=True, **kwargs)), - 'text': gae.TextProperty, - 'json': gae.TextProperty, - 'password': gae.StringProperty, - 'blob': gae.BlobProperty, - 'upload': gae.StringProperty, - 'integer': gae.IntegerProperty, - 'bigint': gae.IntegerProperty, - 'float': gae.FloatProperty, - 'double': gae.FloatProperty, - 'decimal': GAEDecimalProperty, - 'date': gae.DateProperty, - 'time': gae.TimeProperty, - 'datetime': gae.DateTimeProperty, - 'id': None, - 'reference': gae.IntegerProperty, - 'list:string': (lambda **kwargs: gae.StringListProperty(default=None, **kwargs)), - 'list:integer': (lambda **kwargs: gae.ListProperty(int,default=None, **kwargs)), - 'list:reference': (lambda **kwargs: gae.ListProperty(int,default=None, **kwargs)), - }) - self.db = db - self.uri = uri - self.dbengine = 'google:datastore' - self.folder = folder - db['_lastsql'] = '' - self.db_codec = 'UTF-8' - self._after_connection = after_connection - self.pool_size = 0 - match = self.REGEX_NAMESPACE.match(uri) - if match: - namespace_manager.set_namespace(match.group('namespace')) - self.keyfunc = (self.use_ndb and ndb.Key) or Key.from_path - - self.ndb_settings = None - if 'ndb_settings' in adapter_args: - self.ndb_settings = adapter_args['ndb_settings'] - - def parse_id(self, value, field_type): - return value - - def represent(self, obj, fieldtype): - if fieldtype == "json": - if have_serializers: - return serializers.json(obj) - elif simplejson: - return simplejson.dumps(obj) - else: - raise Exception("Could not dump json object (missing json library)") - else: - return NoSQLAdapter.represent(self, obj, fieldtype) - - def create_table(self,table,migrate=True,fake_migrate=False, polymodel=None): - myfields = {} - for field in table: - if isinstance(polymodel,Table) and field.name in polymodel.fields(): - continue - attr = {} - if isinstance(field.custom_qualifier, dict): - #this is custom properties to add to the GAE field declartion - attr = field.custom_qualifier - field_type = field.type - if isinstance(field_type, SQLCustomType): - ftype = self.types[field_type.native or field_type.type](**attr) - elif isinstance(field_type, ((self.use_ndb and ndb.Property) or gae.Property)): - ftype = field_type - elif field_type.startswith('id'): - continue - elif field_type.startswith('decimal'): - precision, scale = field_type[7:].strip('()').split(',') - precision = int(precision) - scale = int(scale) - dec_cls = (self.use_ndb and NDBDecimalProperty) or GAEDecimalProperty - ftype = dec_cls(precision, scale, **attr) - elif field_type.startswith('reference'): - if field.notnull: - attr = dict(required=True) - ftype = self.types[field_type[:9]](**attr) - elif field_type.startswith('list:reference'): - if field.notnull: - attr['required'] = True - ftype = self.types[field_type[:14]](**attr) - elif field_type.startswith('list:'): - ftype = self.types[field_type](**attr) - elif not field_type in self.types\ - or not self.types[field_type]: - raise SyntaxError('Field: unknown field type: %s' % field_type) - else: - ftype = self.types[field_type](**attr) - myfields[field.name] = ftype - if not polymodel: - model_cls = (self.use_ndb and ndb.Model) or gae.Model - table._tableobj = classobj(table._tablename, (model_cls, ), myfields) - if self.use_ndb: - # Set NDB caching variables - if self.ndb_settings and (table._tablename in self.ndb_settings): - for k, v in self.ndb_settings.iteritems(): - setattr(table._tableobj, k, v) - elif polymodel==True: - pm_cls = (self.use_ndb and NDBPolyModel) or PolyModel - table._tableobj = classobj(table._tablename, (pm_cls, ), myfields) - elif isinstance(polymodel,Table): - table._tableobj = classobj(table._tablename, (polymodel._tableobj, ), myfields) - else: - raise SyntaxError("polymodel must be None, True, a table or a tablename") - return None - - def expand(self,expression,field_type=None): - if isinstance(expression,Field): - if expression.type in ('text', 'blob', 'json'): - raise SyntaxError('AppEngine does not index by: %s' % expression.type) - return expression.name - elif isinstance(expression, (Expression, Query)): - if not expression.second is None: - return expression.op(expression.first, expression.second) - elif not expression.first is None: - return expression.op(expression.first) - else: - return expression.op() - elif field_type: - return self.represent(expression,field_type) - elif isinstance(expression,(list,tuple)): - return ','.join([self.represent(item,field_type) for item in expression]) - else: - return str(expression) - - ### TODO from gql.py Expression - def AND(self,first,second): - a = self.expand(first) - b = self.expand(second) - if b[0].name=='__key__' and a[0].name!='__key__': - return b+a - return a+b - - def EQ(self,first,second=None): - if isinstance(second, Key): - return [GAEF(first.name,'=',second,lambda a,b:a==b)] - return [GAEF(first.name,'=',self.represent(second,first.type),lambda a,b:a==b)] - - def NE(self,first,second=None): - if first.type != 'id': - return [GAEF(first.name,'!=',self.represent(second,first.type),lambda a,b:a!=b)] - else: - if not second is None: - second = self.keyfunc(first._tablename, long(second)) - return [GAEF(first.name,'!=',second,lambda a,b:a!=b)] - - def LT(self,first,second=None): - if first.type != 'id': - return [GAEF(first.name,'<',self.represent(second,first.type),lambda a,b:a',self.represent(second,first.type),lambda a,b:a>b)] - else: - second = self.keyfunc(first._tablename, long(second)) - return [GAEF(first.name,'>',second,lambda a,b:a>b)] - - def GE(self,first,second=None): - if first.type != 'id': - return [GAEF(first.name,'>=',self.represent(second,first.type),lambda a,b:a>=b)] - else: - second = self.keyfunc(first._tablename, long(second)) - return [GAEF(first.name,'>=',second,lambda a,b:a>=b)] - - def INVERT(self,first): - return '-%s' % first.name - - def COMMA(self,first,second): - return '%s, %s' % (self.expand(first),self.expand(second)) - - def BELONGS(self,first,second=None): - if not isinstance(second,(list, tuple, set)): - raise SyntaxError("Not supported") - if not self.use_ndb: - if isinstance(second,set): - second = list(second) - if first.type == 'id': - second = [self.keyfunc(first._tablename, int(i)) for i in second] - return [GAEF(first.name,'in',second,lambda a,b:a in b)] - - def CONTAINS(self,first,second,case_sensitive=False): - # silently ignoring: GAE can only do case sensitive matches! - if not first.type.startswith('list:'): - raise SyntaxError("Not supported") - return [GAEF(first.name,'=',self.expand(second,first.type[5:]),lambda a,b:b in a)] - - def NOT(self,first): - nops = { self.EQ: self.NE, - self.NE: self.EQ, - self.LT: self.GE, - self.GT: self.LE, - self.LE: self.GT, - self.GE: self.LT} - if not isinstance(first,Query): - raise SyntaxError("Not suported") - nop = nops.get(first.op,None) - if not nop: - raise SyntaxError("Not suported %s" % first.op.__name__) - first.op = nop - return self.expand(first) - - def truncate(self,table,mode): - self.db(self.db._adapter.id_query(table)).delete() - - GAE_FILTER_OPTIONS = { - '=': lambda q, t, p, v: q.filter(getattr(t,p) == v), - '>': lambda q, t, p, v: q.filter(getattr(t,p) > v), - '<': lambda q, t, p, v: q.filter(getattr(t,p) < v), - '<=': lambda q, t, p, v: q.filter(getattr(t,p) <= v), - '>=': lambda q, t, p, v: q.filter(getattr(t,p) >= v), - '!=': lambda q, t, p, v: q.filter(getattr(t,p) != v), - 'in': lambda q, t, p, v: q.filter(getattr(t,p).IN(v)), - } - - def filter(self, query, tableobj, prop, op, value): - return self.GAE_FILTER_OPTIONS[op](query, tableobj, prop, value) - - def select_raw(self,query,fields=None,attributes=None,count_only=False): - db = self.db - fields = fields or [] - attributes = attributes or {} - args_get = attributes.get - new_fields = [] - - for item in fields: - if isinstance(item,SQLALL): - new_fields += item._table - else: - new_fields.append(item) - - fields = new_fields - if query: - tablename = self.get_table(query) - elif fields: - tablename = fields[0].tablename - query = db._adapter.id_query(fields[0].table) - else: - raise SyntaxError("Unable to determine a tablename") - - if query: - if use_common_filters(query): - query = self.common_filter(query,[tablename]) - - #tableobj is a GAE/NDB Model class (or subclass) - tableobj = db[tablename]._tableobj - filters = self.expand(query) - - projection = None - if len(db[tablename].fields) == len(fields): - #getting all fields, not a projection query - projection = None - elif args_get('projection') == True: - projection = [] - for f in fields: - if f.type in ['text', 'blob', 'json']: - raise SyntaxError( - "text and blob field types not allowed in projection queries") - else: - projection.append(f.name) - - elif args_get('filterfields') is True: - projection = [] - for f in fields: - projection.append(f.name) - - # real projection's can't include 'id'. - # it will be added to the result later - query_projection = [ - p for p in projection if \ - p != db[tablename]._id.name] if projection and \ - args_get('projection') == True\ - else None - - cursor = args_get('reusecursor') - cursor = cursor if isinstance(cursor, str) else None - if self.use_ndb: - qo = ndb.QueryOptions(projection=query_projection, cursor=cursor) - items = tableobj.query(default_options=qo) - else: - items = gae.Query(tableobj, projection=query_projection, cursor=cursor) - - for filter in filters: - if (args_get('projection') == True and - filter.name in query_projection and - filter.op in ('=', '<=', '>=')): - raise SyntaxError("projection fields cannot have equality filters") - if filter.name=='__key__' and filter.op=='>' and filter.value==0: - continue - elif filter.name=='__key__' and filter.op=='=': - if filter.value==0: - items = [] - elif isinstance(filter.value, (self.use_ndb and ndb.Key) or Key): - # key qeuries return a class instance, - # can't use projection - # extra values will be ignored in post-processing later - item = filter.value.get() if self.use_ndb else tableobj.get(filter.value) - items = [item] if item else [] - else: - # key qeuries return a class instance, - # can't use projection - # extra values will be ignored in post-processing later - item = tableobj.get_by_id(filter.value) - items = [item] if item else [] - elif isinstance(items,list): # i.e. there is a single record! - items = [i for i in items if filter.apply( - getattr(item,filter.name),filter.value)] - else: - if filter.name=='__key__' and filter.op != 'in': - items.order(tableobj._key) if self.use_ndb else items.order('__key__') - if self.use_ndb: - items = self.filter(items, tableobj, filter.name, filter.op, filter.value) - else: - items = items.filter('%s %s' % (filter.name,filter.op), filter.value) - - if count_only: - items = [len(items) if isinstance(items,list) else items.count()] - elif not isinstance(items,list): - query = items - if args_get('left', None): - raise SyntaxError('Set: no left join in appengine') - if args_get('groupby', None): - raise SyntaxError('Set: no groupby in appengine') - orderby = args_get('orderby', False) - if orderby: - ### THIS REALLY NEEDS IMPROVEMENT !!! - if isinstance(orderby, (list, tuple)): - orderby = xorify(orderby) - if isinstance(orderby,Expression): - orderby = self.expand(orderby) - orders = orderby.split(', ') - for order in orders: - if self.use_ndb: - #TODO There must be a better way - def make_order(o): - s = str(o) - desc = s[0] == '-' - s = (desc and s[1:]) or s - return (desc and -getattr(tableobj, s)) or getattr(tableobj, s) - _order = {'-id':-tableobj._key,'id':tableobj._key}.get(order) - if _order is None: - _order = make_order(order) - query = query.order(_order) - else: - order={'-id':'-__key__','id':'__key__'}.get(order,order) - query = query.order(order) - - if args_get('limitby', None): - (lmin, lmax) = attributes['limitby'] - limit, fetch_args = lmax-lmin, {'offset':lmin,'keys_only':True} - - if self.use_ndb: - keys, cursor, more = query.fetch_page(limit,**fetch_args) - items = ndb.get_multi(keys) - else: - keys = query.fetch(limit, **fetch_args) - items = gae.get(keys) - cursor = query.cursor() - #cursor is only useful if there was a limit and we didn't return - # all results - if args_get('reusecursor'): - db['_lastcursor'] = cursor - else: - # if a limit is not specified, always return an iterator - rows = query - - return (items, tablename, projection or db[tablename].fields) - - def select(self,query,fields,attributes): - """ - This is the GAE version of select. Some notes to consider: - - db['_lastsql'] is not set because there is not SQL statement string - for a GAE query - - 'nativeRef' is a magical fieldname used for self references on GAE - - optional attribute 'projection' when set to True will trigger - use of the GAE projection queries. note that there are rules for - what is accepted imposed by GAE: each field must be indexed, - projection queries cannot contain blob or text fields, and you - cannot use == and also select that same field. - see https://developers.google.com/appengine/docs/python/datastore/queries#Query_Projection - - optional attribute 'filterfields' when set to True web2py will only - parse the explicitly listed fields into the Rows object, even though - all fields are returned in the query. This can be used to reduce - memory usage in cases where true projection queries are not - usable. - - optional attribute 'reusecursor' allows use of cursor with queries - that have the limitby attribute. Set the attribute to True for the - first query, set it to the value of db['_lastcursor'] to continue - a previous query. The user must save the cursor value between - requests, and the filters must be identical. It is up to the user - to follow google's limitations: - https://developers.google.com/appengine/docs/python/datastore/queries#Query_Cursors - """ - - (items, tablename, fields) = self.select_raw(query,fields,attributes) - # self.db['_lastsql'] = self._select(query,fields,attributes) - rows = [[(t==self.db[tablename]._id.name and item) or \ - (t=='nativeRef' and item) or getattr(item, t) \ - for t in fields] for item in items] - colnames = ['%s.%s' % (tablename, t) for t in fields] - processor = attributes.get('processor',self.parse) - return processor(rows,fields,colnames,False) - - def parse_list_integers(self, value, field_type): - return value[:] if self.use_ndb else value - - def parse_list_strings(self, value, field_type): - return value[:] if self.use_ndb else value - - def count(self,query,distinct=None,limit=None): - if distinct: - raise RuntimeError("COUNT DISTINCT not supported") - (items, tablename, fields) = self.select_raw(query,count_only=True) - return items[0] - - def delete(self,tablename, query): - """ - This function was changed on 2010-05-04 because according to - http://code.google.com/p/googleappengine/issues/detail?id=3119 - GAE no longer supports deleting more than 1000 records. - """ - # self.db['_lastsql'] = self._delete(tablename,query) - (items, tablename, fields) = self.select_raw(query) - # items can be one item or a query - if not isinstance(items,list): - #use a keys_only query to ensure that this runs as a datastore - # small operations - leftitems = items.fetch(1000, keys_only=True) - counter = 0 - while len(leftitems): - counter += len(leftitems) - if self.use_ndb: - ndb.delete_multi(leftitems) - else: - gae.delete(leftitems) - leftitems = items.fetch(1000, keys_only=True) - else: - counter = len(items) - if self.use_ndb: - ndb.delete_multi([item.key for item in items]) - else: - gae.delete(items) - return counter - - def update(self,tablename,query,update_fields): - # self.db['_lastsql'] = self._update(tablename,query,update_fields) - (items, tablename, fields) = self.select_raw(query) - counter = 0 - for item in items: - for field, value in update_fields: - setattr(item, field.name, self.represent(value,field.type)) - item.put() - counter += 1 - LOGGER.info(str(counter)) - return counter - - def insert(self,table,fields): - dfields=dict((f.name,self.represent(v,f.type)) for f,v in fields) - # table._db['_lastsql'] = self._insert(table,fields) - tmp = table._tableobj(**dfields) - tmp.put() - key = tmp.key if self.use_ndb else tmp.key() - rid = Reference(key.id()) - (rid._table, rid._record, rid._gaekey) = (table, None, key) - return rid - - def bulk_insert(self,table,items): - parsed_items = [] - for item in items: - dfields=dict((f.name,self.represent(v,f.type)) for f,v in item) - parsed_items.append(table._tableobj(**dfields)) - if self.use_ndb: - ndb.put_multi(parsed_items) - else: - gae.put(parsed_items) - return True diff --git a/gluon/dal/adapters/imap.py b/gluon/dal/adapters/imap.py deleted file mode 100644 index a63ac61b..00000000 --- a/gluon/dal/adapters/imap.py +++ /dev/null @@ -1,1034 +0,0 @@ -# -*- coding: utf-8 -*- -import datetime -import re -import sys - -from .._globals import IDENTITY, GLOBAL_LOCKER, LOGGER -from ..connection import ConnectionPool -from ..objects import Field, Query, Expression -from ..helpers.classes import SQLALL -from ..helpers.methods import use_common_filters -from .base import NoSQLAdapter - - -class IMAPAdapter(NoSQLAdapter): - - """ IMAP server adapter - - This class is intended as an interface with - email IMAP servers to perform simple queries in the - web2py DAL query syntax, so email read, search and - other related IMAP mail services (as those implemented - by brands like Google(r), and Yahoo!(r) - can be managed from web2py applications. - - The code uses examples by Yuji Tomita on this post: - http://yuji.wordpress.com/2011/06/22/python-imaplib-imap-example-with-gmail/#comment-1137 - and is based in docs for Python imaplib, python email - and email IETF's (i.e. RFC2060 and RFC3501) - - This adapter was tested with a small set of operations with Gmail(r). Other - services requests could raise command syntax and response data issues. - - It creates its table and field names "statically", - meaning that the developer should leave the table and field - definitions to the DAL instance by calling the adapter's - .define_tables() method. The tables are defined with the - IMAP server mailbox list information. - - .define_tables() returns a dictionary mapping dal tablenames - to the server mailbox names with the following structure: - - {: str } - - Here is a list of supported fields: - - =========== ============== =========== - Field Type Description - =========== ============== =========== - uid string - answered boolean Flag - created date - content list:string A list of dict text or html parts - to string - cc string - bcc string - size integer the amount of octets of the message* - deleted boolean Flag - draft boolean Flag - flagged boolean Flag - sender string - recent boolean Flag - seen boolean Flag - subject string - mime string The mime header declaration - email string The complete RFC822 message (*) - attachments list Each non text part as dict - encoding string The main detected encoding - =========== ============== =========== - - (*) At the application side it is measured as the length of the RFC822 - message string - - WARNING: As row id's are mapped to email sequence numbers, - make sure your imap client web2py app does not delete messages - during select or update actions, to prevent - updating or deleting different messages. - Sequence numbers change whenever the mailbox is updated. - To avoid this sequence numbers issues, it is recommended the use - of uid fields in query references (although the update and delete - in separate actions rule still applies). - :: - - # This is the code recommended to start imap support - # at the app's model: - - imapdb = DAL("imap://user:password@server:port", pool_size=1) # port 993 for ssl - imapdb.define_tables() - - Here is an (incomplete) list of possible imap commands:: - - # Count today's unseen messages - # smaller than 6000 octets from the - # inbox mailbox - - q = imapdb.INBOX.seen == False - q &= imapdb.INBOX.created == datetime.date.today() - q &= imapdb.INBOX.size < 6000 - unread = imapdb(q).count() - - # Fetch last query messages - rows = imapdb(q).select() - - # it is also possible to filter query select results with limitby and - # sequences of mailbox fields - - set.select(, limitby=(, )) - - # Mark last query messages as seen - messages = [row.uid for row in rows] - seen = imapdb(imapdb.INBOX.uid.belongs(messages)).update(seen=True) - - # Delete messages in the imap database that have mails from mr. Gumby - - deleted = 0 - for mailbox in imapdb.tables - deleted += imapdb(imapdb[mailbox].sender.contains("gumby")).delete() - - # It is possible also to mark messages for deletion instead of ereasing them - # directly with set.update(deleted=True) - - - # This object give access - # to the adapter auto mailbox - # mapped names (which native - # mailbox has what table name) - - imapdb.mailboxes # tablename, server native name pairs - - # To retrieve a table native mailbox name use: - imapdb..mailbox - - ### New features v2.4.1: - - # Declare mailboxes statically with tablename, name pairs - # This avoids the extra server names retrieval - - imapdb.define_tables({"inbox": "INBOX"}) - - # Selects without content/attachments/email columns will only - # fetch header and flags - - imapdb(q).select(imapdb.INBOX.sender, imapdb.INBOX.subject) - - """ - drivers = ('imaplib',) - types = { - 'string': str, - 'text': str, - 'date': datetime.date, - 'datetime': datetime.datetime, - 'id': long, - 'boolean': bool, - 'integer': int, - 'bigint': long, - 'blob': str, - 'list:string': str - } - - dbengine = 'imap' - - REGEX_URI = re.compile('^(?P[^:]+)(\:(?P[^@]*))?@(?P[^\:@]+)(\:(?P[0-9]+))?$') - - def __init__(self, - db, - uri, - pool_size=0, - folder=None, - db_codec ='UTF-8', - credential_decoder=IDENTITY, - driver_args={}, - adapter_args={}, - do_connect=True, - after_connection=None): - - # db uri: user@example.com:password@imap.server.com:123 - # TODO: max size adapter argument for preventing large mail transfers - - self.db = db - self.uri = uri - if do_connect: self.find_driver(adapter_args) - self.pool_size=pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.credential_decoder = credential_decoder - self.driver_args = driver_args - self.adapter_args = adapter_args - self.mailbox_size = None - self.static_names = None - self.charset = sys.getfilesystemencoding() - # imap class - self.imap4 = None - uri = uri.split("://")[1] - - """ MESSAGE is an identifier for sequence number""" - - self.flags = {'deleted': '\\Deleted', 'draft': '\\Draft', - 'flagged': '\\Flagged', 'recent': '\\Recent', - 'seen': '\\Seen', 'answered': '\\Answered'} - self.search_fields = { - 'id': 'MESSAGE', 'created': 'DATE', - 'uid': 'UID', 'sender': 'FROM', - 'to': 'TO', 'cc': 'CC', - 'bcc': 'BCC', 'content': 'TEXT', - 'size': 'SIZE', 'deleted': '\\Deleted', - 'draft': '\\Draft', 'flagged': '\\Flagged', - 'recent': '\\Recent', 'seen': '\\Seen', - 'subject': 'SUBJECT', 'answered': '\\Answered', - 'mime': None, 'email': None, - 'attachments': None - } - - db['_lastsql'] = '' - - m = self.REGEX_URI.match(uri) - user = m.group('user') - password = m.group('password') - host = m.group('host') - port = int(m.group('port')) - over_ssl = False - if port==993: - over_ssl = True - - driver_args.update(host=host,port=port, password=password, user=user) - def connector(driver_args=driver_args): - # it is assumed sucessful authentication alLways - # TODO: support direct connection and login tests - if over_ssl: - self.imap4 = self.driver.IMAP4_SSL - else: - self.imap4 = self.driver.IMAP4 - connection = self.imap4(driver_args["host"], driver_args["port"]) - data = connection.login(driver_args["user"], driver_args["password"]) - - # static mailbox list - connection.mailbox_names = None - - # dummy cursor function - connection.cursor = lambda : True - - return connection - - self.db.define_tables = self.define_tables - self.connector = connector - if do_connect: self.reconnect() - - def reconnect(self, f=None, cursor=True): - """ - IMAP4 Pool connection method - - imap connection lacks of self cursor command. - A custom command should be provided as a replacement - for connection pooling to prevent uncaught remote session - closing - - """ - if getattr(self, 'connection', None) is not None: - return - if f is None: - f = self.connector - - if not self.pool_size: - self.connection = f() - self.cursor = cursor and self.connection.cursor() - else: - POOLS = ConnectionPool.POOLS - uri = self.uri - while True: - GLOBAL_LOCKER.acquire() - if not uri in POOLS: - POOLS[uri] = [] - if POOLS[uri]: - self.connection = POOLS[uri].pop() - GLOBAL_LOCKER.release() - self.cursor = cursor and self.connection.cursor() - if self.cursor and self.check_active_connection: - try: - # check if connection is alive or close it - result, data = self.connection.list() - except: - # Possible connection reset error - # TODO: read exception class - self.connection = f() - break - else: - GLOBAL_LOCKER.release() - self.connection = f() - self.cursor = cursor and self.connection.cursor() - break - self.after_connection_hook() - - def get_last_message(self, tablename): - last_message = None - # request mailbox list to the server if needed. - if not isinstance(self.connection.mailbox_names, dict): - self.get_mailboxes() - try: - result = self.connection.select( - self.connection.mailbox_names[tablename]) - last_message = int(result[1][0]) - # Last message must be a positive integer - if last_message == 0: - last_message = 1 - except (IndexError, ValueError, TypeError, KeyError): - e = sys.exc_info()[1] - LOGGER.debug("Error retrieving the last mailbox" + - " sequence number. %s" % str(e)) - return last_message - - def get_uid_bounds(self, tablename): - if not isinstance(self.connection.mailbox_names, dict): - self.get_mailboxes() - # fetch first and last messages - # return (first, last) messages uid's - last_message = self.get_last_message(tablename) - result, data = self.connection.uid("search", None, "(ALL)") - uid_list = data[0].strip().split() - if len(uid_list) <= 0: - return None - else: - return (uid_list[0], uid_list[-1]) - - def convert_date(self, date, add=None, imf=False): - if add is None: - add = datetime.timedelta() - """ Convert a date object to a string - with d-Mon-Y style for IMAP or the inverse - case - - add adds to the date object - """ - months = [None, "JAN","FEB","MAR","APR","MAY","JUN", - "JUL", "AUG","SEP","OCT","NOV","DEC"] - if isinstance(date, basestring): - # Prevent unexpected date response format - try: - if "," in date: - dayname, datestring = date.split(",") - else: - dayname, datestring = None, date - date_list = datestring.strip().split() - year = int(date_list[2]) - month = months.index(date_list[1].upper()) - day = int(date_list[0]) - hms = map(int, date_list[3].split(":")) - return datetime.datetime(year, month, day, - hms[0], hms[1], hms[2]) + add - except (ValueError, AttributeError, IndexError), e: - LOGGER.error("Could not parse date text: %s. %s" % - (date, e)) - return None - elif isinstance(date, (datetime.date, datetime.datetime)): - if imf: date_format = "%a, %d %b %Y %H:%M:%S %z" - else: date_format = "%d-%b-%Y" - return (date + add).strftime(date_format) - else: - return None - - @staticmethod - def header_represent(f, r): - from email.header import decode_header - text, encoding = decode_header(f)[0] - if encoding: - text = text.decode(encoding).encode('utf-8') - return text - - def encode_text(self, text, charset, errors="replace"): - """ convert text for mail to unicode""" - if text is None: - text = "" - else: - if isinstance(text, str): - if charset is None: - text = unicode(text, "utf-8", errors) - else: - text = unicode(text, charset, errors) - else: - raise Exception("Unsupported mail text type %s" % type(text)) - return text.encode("utf-8") - - def get_charset(self, message): - charset = message.get_content_charset() - return charset - - def get_mailboxes(self): - """ Query the mail database for mailbox names """ - if self.static_names: - # statically defined mailbox names - self.connection.mailbox_names = self.static_names - return self.static_names.keys() - - mailboxes_list = self.connection.list() - self.connection.mailbox_names = dict() - mailboxes = list() - x = 0 - for item in mailboxes_list[1]: - x = x + 1 - item = item.strip() - if not "NOSELECT" in item.upper(): - sub_items = item.split("\"") - sub_items = [sub_item for sub_item in sub_items \ - if len(sub_item.strip()) > 0] - # mailbox = sub_items[len(sub_items) -1] - mailbox = sub_items[-1].strip() - # remove unwanted characters and store original names - # Don't allow leading non alphabetic characters - mailbox_name = re.sub('^[_0-9]*', '', re.sub('[^_\w]','',re.sub('[/ ]','_',mailbox))) - mailboxes.append(mailbox_name) - self.connection.mailbox_names[mailbox_name] = mailbox - - return mailboxes - - def get_query_mailbox(self, query): - nofield = True - tablename = None - attr = query - while nofield: - if hasattr(attr, "first"): - attr = attr.first - if isinstance(attr, Field): - return attr.tablename - elif isinstance(attr, Query): - pass - else: - return None - else: - return None - return tablename - - def is_flag(self, flag): - if self.search_fields.get(flag, None) in self.flags.values(): - return True - else: - return False - - def define_tables(self, mailbox_names=None): - """ - Auto create common IMAP fileds - - This function creates fields definitions "statically" - meaning that custom fields as in other adapters should - not be supported and definitions handled on a service/mode - basis (local syntax for Gmail(r), Ymail(r) - - Returns a dictionary with tablename, server native mailbox name - pairs. - """ - if mailbox_names: - # optional statically declared mailboxes - self.static_names = mailbox_names - else: - self.static_names = None - if not isinstance(self.connection.mailbox_names, dict): - self.get_mailboxes() - - names = self.connection.mailbox_names.keys() - - for name in names: - self.db.define_table("%s" % name, - Field("uid", writable=False), - Field("created", "datetime", writable=False), - Field("content", "text", writable=False), - Field("to", writable=False), - Field("cc", writable=False), - Field("bcc", writable=False), - Field("sender", writable=False), - Field("size", "integer", writable=False), - Field("subject", writable=False), - Field("mime", writable=False), - Field("email", "text", writable=False, readable=False), - Field("attachments", "text", writable=False, readable=False), - Field("encoding", writable=False), - Field("answered", "boolean"), - Field("deleted", "boolean"), - Field("draft", "boolean"), - Field("flagged", "boolean"), - Field("recent", "boolean", writable=False), - Field("seen", "boolean") - ) - - # Set a special _mailbox attribute for storing - # native mailbox names - self.db[name].mailbox = \ - self.connection.mailbox_names[name] - - # decode quoted printable - self.db[name].to.represent = self.db[name].cc.represent = \ - self.db[name].bcc.represent = self.db[name].sender.represent = \ - self.db[name].subject.represent = self.header_represent - - # Set the db instance mailbox collections - self.db.mailboxes = self.connection.mailbox_names - return self.db.mailboxes - - def create_table(self, *args, **kwargs): - # not implemented - # but required by DAL - pass - - def select(self, query, fields, attributes): - """ Searches and Fetches records and return web2py rows - """ - # move this statement elsewhere (upper-level) - if use_common_filters(query): - query = self.common_filter(query, [self.get_query_mailbox(query),]) - - import email - # get records from imap server with search + fetch - # convert results to a dictionary - tablename = None - fetch_results = list() - - if isinstance(query, Query): - tablename = self.get_table(query) - mailbox = self.connection.mailbox_names.get(tablename, None) - if mailbox is None: - raise ValueError("Mailbox name not found: %s" % mailbox) - else: - # select with readonly - result, selected = self.connection.select(mailbox, True) - if result != "OK": - raise Exception("IMAP error: %s" % selected) - self.mailbox_size = int(selected[0]) - search_query = "(%s)" % str(query).strip() - search_result = self.connection.uid("search", None, search_query) - # Normal IMAP response OK is assumed (change this) - if search_result[0] == "OK": - # For "light" remote server responses just get the first - # ten records (change for non-experimental implementation) - # However, light responses are not guaranteed with this - # approach, just fewer messages. - limitby = attributes.get('limitby', None) - messages_set = search_result[1][0].split() - # descending order - messages_set.reverse() - if limitby is not None: - # TODO: orderby, asc/desc, limitby from complete message set - messages_set = messages_set[int(limitby[0]):int(limitby[1])] - - # keep the requests small for header/flags - if any([(field.name in ["content", "size", - "attachments", "email"]) for - field in fields]): - imap_fields = "(RFC822 FLAGS)" - else: - imap_fields = "(RFC822.HEADER FLAGS)" - - if len(messages_set) > 0: - # create fetch results object list - # fetch each remote message and store it in memmory - # (change to multi-fetch command syntax for faster - # transactions) - for uid in messages_set: - # fetch the RFC822 message body - typ, data = self.connection.uid("fetch", uid, imap_fields) - if typ == "OK": - fr = {"message": int(data[0][0].split()[0]), - "uid": long(uid), - "email": email.message_from_string(data[0][1]), - "raw_message": data[0][1]} - fr["multipart"] = fr["email"].is_multipart() - # fetch flags for the message - fr["flags"] = self.driver.ParseFlags(data[1]) - fetch_results.append(fr) - else: - # error retrieving the message body - raise Exception("IMAP error retrieving the body: %s" % data) - else: - raise Exception("IMAP search error: %s" % search_result[1]) - elif isinstance(query, (Expression, basestring)): - raise NotImplementedError() - else: - raise TypeError("Unexpected query type") - - imapqry_dict = {} - imapfields_dict = {} - - if len(fields) == 1 and isinstance(fields[0], SQLALL): - allfields = True - elif len(fields) == 0: - allfields = True - else: - allfields = False - if allfields: - colnames = ["%s.%s" % (tablename, field) for field in self.search_fields.keys()] - else: - colnames = ["%s.%s" % (tablename, field.name) for field in fields] - - for k in colnames: - imapfields_dict[k] = k - - imapqry_list = list() - imapqry_array = list() - for fr in fetch_results: - attachments = [] - content = [] - size = 0 - n = int(fr["message"]) - item_dict = dict() - message = fr["email"] - uid = fr["uid"] - charset = self.get_charset(message) - flags = fr["flags"] - raw_message = fr["raw_message"] - # Return messages data mapping static fields - # and fetched results. Mapping should be made - # outside the select function (with auxiliary - # instance methods) - - # pending: search flags states trough the email message - # instances for correct output - - # preserve subject encoding (ASCII/quoted printable) - - if "%s.id" % tablename in colnames: - item_dict["%s.id" % tablename] = n - if "%s.created" % tablename in colnames: - item_dict["%s.created" % tablename] = self.convert_date(message["Date"]) - if "%s.uid" % tablename in colnames: - item_dict["%s.uid" % tablename] = uid - if "%s.sender" % tablename in colnames: - # If there is no encoding found in the message header - # force utf-8 replacing characters (change this to - # module's defaults). Applies to .sender, .to, .cc and .bcc fields - item_dict["%s.sender" % tablename] = message["From"] - if "%s.to" % tablename in colnames: - item_dict["%s.to" % tablename] = message["To"] - if "%s.cc" % tablename in colnames: - if "Cc" in message.keys(): - item_dict["%s.cc" % tablename] = message["Cc"] - else: - item_dict["%s.cc" % tablename] = "" - if "%s.bcc" % tablename in colnames: - if "Bcc" in message.keys(): - item_dict["%s.bcc" % tablename] = message["Bcc"] - else: - item_dict["%s.bcc" % tablename] = "" - if "%s.deleted" % tablename in colnames: - item_dict["%s.deleted" % tablename] = "\\Deleted" in flags - if "%s.draft" % tablename in colnames: - item_dict["%s.draft" % tablename] = "\\Draft" in flags - if "%s.flagged" % tablename in colnames: - item_dict["%s.flagged" % tablename] = "\\Flagged" in flags - if "%s.recent" % tablename in colnames: - item_dict["%s.recent" % tablename] = "\\Recent" in flags - if "%s.seen" % tablename in colnames: - item_dict["%s.seen" % tablename] = "\\Seen" in flags - if "%s.subject" % tablename in colnames: - item_dict["%s.subject" % tablename] = message["Subject"] - if "%s.answered" % tablename in colnames: - item_dict["%s.answered" % tablename] = "\\Answered" in flags - if "%s.mime" % tablename in colnames: - item_dict["%s.mime" % tablename] = message.get_content_type() - if "%s.encoding" % tablename in colnames: - item_dict["%s.encoding" % tablename] = charset - - # Here goes the whole RFC822 body as an email instance - # for controller side custom processing - # The message is stored as a raw string - # >> email.message_from_string(raw string) - # returns a Message object for enhanced object processing - if "%s.email" % tablename in colnames: - # WARNING: no encoding performed (raw message) - item_dict["%s.email" % tablename] = raw_message - - # Size measure as suggested in a Velocity Reviews post - # by Tim Williams: "how to get size of email attachment" - # Note: len() and server RFC822.SIZE reports doesn't match - # To retrieve the server size for representation would add a new - # fetch transaction to the process - for part in message.walk(): - maintype = part.get_content_maintype() - if ("%s.attachments" % tablename in colnames) or \ - ("%s.content" % tablename in colnames): - payload = part.get_payload(decode=True) - if payload: - filename = part.get_filename() - values = {"mime": part.get_content_type()} - if ((filename or not "text" in maintype) and - ("%s.attachments" % tablename in colnames)): - values.update({"payload": payload, - "filename": filename, - "encoding": part.get_content_charset(), - "disposition": part["Content-Disposition"]}) - attachments.append(values) - elif (("text" in maintype) and - ("%s.content" % tablename in colnames)): - values.update({"text": self.encode_text(payload, - self.get_charset(part))}) - content.append(values) - - if "%s.size" % tablename in colnames: - if part is not None: - size += len(str(part)) - item_dict["%s.content" % tablename] = content - item_dict["%s.attachments" % tablename] = attachments - item_dict["%s.size" % tablename] = size - imapqry_list.append(item_dict) - - # extra object mapping for the sake of rows object - # creation (sends an array or lists) - for item_dict in imapqry_list: - imapqry_array_item = list() - for fieldname in colnames: - imapqry_array_item.append(item_dict[fieldname]) - imapqry_array.append(imapqry_array_item) - - # parse result and return a rows object - colnames = colnames - processor = attributes.get('processor',self.parse) - return processor(imapqry_array, fields, colnames) - - def insert(self, table, fields): - def add_payload(message, obj): - payload = Message() - encoding = obj.get("encoding", "utf-8") - if encoding and (encoding.upper() in - ("BASE64", "7BIT", "8BIT", "BINARY")): - payload.add_header("Content-Transfer-Encoding", encoding) - else: - payload.set_charset(encoding) - mime = obj.get("mime", None) - if mime: - payload.set_type(mime) - if "text" in obj: - payload.set_payload(obj["text"]) - elif "payload" in obj: - payload.set_payload(obj["payload"]) - if "filename" in obj and obj["filename"]: - payload.add_header("Content-Disposition", - "attachment", filename=obj["filename"]) - message.attach(payload) - - mailbox = table.mailbox - d = dict(((k.name, v) for k, v in fields)) - date_time = d.get("created") or datetime.datetime.now() - struct_time = date_time.timetuple() - if len(d) > 0: - message = d.get("email", None) - attachments = d.get("attachments", []) - content = d.get("content", []) - flags = " ".join(["\\%s" % flag.capitalize() for flag in - ("answered", "deleted", "draft", "flagged", - "recent", "seen") if d.get(flag, False)]) - if not message: - from email.message import Message - mime = d.get("mime", None) - charset = d.get("encoding", None) - message = Message() - message["from"] = d.get("sender", "") - message["subject"] = d.get("subject", "") - message["date"] = self.convert_date(date_time, imf=True) - - if mime: - message.set_type(mime) - if charset: - message.set_charset(charset) - for item in ("to", "cc", "bcc"): - value = d.get(item, "") - if isinstance(value, basestring): - message[item] = value - else: - message[item] = ";".join([i for i in - value]) - if (not message.is_multipart() and - (not message.get_content_type().startswith( - "multipart"))): - if isinstance(content, basestring): - message.set_payload(content) - elif len(content) > 0: - message.set_payload(content[0]["text"]) - else: - [add_payload(message, c) for c in content] - [add_payload(message, a) for a in attachments] - message = message.as_string() - - result, data = self.connection.append(mailbox, flags, struct_time, message) - if result == "OK": - uid = int(re.findall("\d+", str(data))[-1]) - return self.db(table.uid==uid).select(table.id).first().id - else: - raise Exception("IMAP message append failed: %s" % data) - else: - raise NotImplementedError("IMAP empty insert is not implemented") - - def update(self, tablename, query, fields): - # TODO: the adapter should implement an .expand method - commands = list() - rowcount = 0 - if use_common_filters(query): - query = self.common_filter(query, [tablename,]) - mark = [] - unmark = [] - if query: - for item in fields: - field = item[0] - name = field.name - value = item[1] - if self.is_flag(name): - flag = self.search_fields[name] - if (value is not None) and (flag != "\\Recent"): - if value: - mark.append(flag) - else: - unmark.append(flag) - result, data = self.connection.select( - self.connection.mailbox_names[tablename]) - string_query = "(%s)" % query - result, data = self.connection.search(None, string_query) - store_list = [item.strip() for item in data[0].split() - if item.strip().isdigit()] - # build commands for marked flags - for number in store_list: - result = None - if len(mark) > 0: - commands.append((number, "+FLAGS", "(%s)" % " ".join(mark))) - if len(unmark) > 0: - commands.append((number, "-FLAGS", "(%s)" % " ".join(unmark))) - - for command in commands: - result, data = self.connection.store(*command) - if result == "OK": - rowcount += 1 - else: - raise Exception("IMAP storing error: %s" % data) - return rowcount - - def count(self,query,distinct=None): - counter = 0 - tablename = self.get_query_mailbox(query) - if query and tablename is not None: - if use_common_filters(query): - query = self.common_filter(query, [tablename,]) - result, data = self.connection.select(self.connection.mailbox_names[tablename]) - string_query = "(%s)" % query - result, data = self.connection.search(None, string_query) - store_list = [item.strip() for item in data[0].split() if item.strip().isdigit()] - counter = len(store_list) - return counter - - def delete(self, tablename, query): - counter = 0 - if query: - if use_common_filters(query): - query = self.common_filter(query, [tablename,]) - result, data = self.connection.select(self.connection.mailbox_names[tablename]) - string_query = "(%s)" % query - result, data = self.connection.search(None, string_query) - store_list = [item.strip() for item in data[0].split() if item.strip().isdigit()] - for number in store_list: - result, data = self.connection.store(number, "+FLAGS", "(\\Deleted)") - if result == "OK": - counter += 1 - else: - raise Exception("IMAP store error: %s" % data) - if counter > 0: - result, data = self.connection.expunge() - return counter - - def BELONGS(self, first, second): - result = None - name = self.search_fields[first.name] - if name == "MESSAGE": - values = [str(val) for val in second if str(val).isdigit()] - result = "%s" % ",".join(values).strip() - - elif name == "UID": - values = [str(val) for val in second if str(val).isdigit()] - result = "UID %s" % ",".join(values).strip() - - else: - raise Exception("Operation not supported") - # result = "(%s %s)" % (self.expand(first), self.expand(second)) - return result - - def CONTAINS(self, first, second, case_sensitive=False): - # silently ignore, only case sensitive - result = None - name = self.search_fields[first.name] - - if name in ("FROM", "TO", "SUBJECT", "TEXT"): - result = "%s \"%s\"" % (name, self.expand(second)) - else: - if first.name in ("cc", "bcc"): - result = "%s \"%s\"" % (first.name.upper(), self.expand(second)) - elif first.name == "mime": - result = "HEADER Content-Type \"%s\"" % self.expand(second) - else: - raise Exception("Operation not supported") - return result - - def GT(self, first, second): - result = None - name = self.search_fields[first.name] - if name == "MESSAGE": - last_message = self.get_last_message(first.tablename) - result = "%d:%d" % (int(self.expand(second)) + 1, last_message) - elif name == "UID": - # GT and LT may not return - # expected sets depending on - # the uid format implemented - try: - pedestal, threshold = self.get_uid_bounds(first.tablename) - except TypeError: - e = sys.exc_info()[1] - LOGGER.debug("Error requesting uid bounds: %s", str(e)) - return "" - try: - lower_limit = int(self.expand(second)) + 1 - except (ValueError, TypeError): - e = sys.exc_info()[1] - raise Exception("Operation not supported (non integer UID)") - result = "UID %s:%s" % (lower_limit, threshold) - elif name == "DATE": - result = "SINCE %s" % self.convert_date(second, add=datetime.timedelta(1)) - elif name == "SIZE": - result = "LARGER %s" % self.expand(second) - else: - raise Exception("Operation not supported") - return result - - def GE(self, first, second): - result = None - name = self.search_fields[first.name] - if name == "MESSAGE": - last_message = self.get_last_message(first.tablename) - result = "%s:%s" % (self.expand(second), last_message) - elif name == "UID": - # GT and LT may not return - # expected sets depending on - # the uid format implemented - try: - pedestal, threshold = self.get_uid_bounds(first.tablename) - except TypeError: - e = sys.exc_info()[1] - LOGGER.debug("Error requesting uid bounds: %s", str(e)) - return "" - lower_limit = self.expand(second) - result = "UID %s:%s" % (lower_limit, threshold) - elif name == "DATE": - result = "SINCE %s" % self.convert_date(second) - else: - raise Exception("Operation not supported") - return result - - def LT(self, first, second): - result = None - name = self.search_fields[first.name] - if name == "MESSAGE": - result = "%s:%s" % (1, int(self.expand(second)) - 1) - elif name == "UID": - try: - pedestal, threshold = self.get_uid_bounds(first.tablename) - except TypeError: - e = sys.exc_info()[1] - LOGGER.debug("Error requesting uid bounds: %s", str(e)) - return "" - try: - upper_limit = int(self.expand(second)) - 1 - except (ValueError, TypeError): - e = sys.exc_info()[1] - raise Exception("Operation not supported (non integer UID)") - result = "UID %s:%s" % (pedestal, upper_limit) - elif name == "DATE": - result = "BEFORE %s" % self.convert_date(second) - elif name == "SIZE": - result = "SMALLER %s" % self.expand(second) - else: - raise Exception("Operation not supported") - return result - - def LE(self, first, second): - result = None - name = self.search_fields[first.name] - if name == "MESSAGE": - result = "%s:%s" % (1, self.expand(second)) - elif name == "UID": - try: - pedestal, threshold = self.get_uid_bounds(first.tablename) - except TypeError: - e = sys.exc_info()[1] - LOGGER.debug("Error requesting uid bounds: %s", str(e)) - return "" - upper_limit = int(self.expand(second)) - result = "UID %s:%s" % (pedestal, upper_limit) - elif name == "DATE": - result = "BEFORE %s" % self.convert_date(second, add=datetime.timedelta(1)) - else: - raise Exception("Operation not supported") - return result - - def NE(self, first, second=None): - if (second is None) and isinstance(first, Field): - # All records special table query - if first.type == "id": - return self.GE(first, 1) - result = self.NOT(self.EQ(first, second)) - result = result.replace("NOT NOT", "").strip() - return result - - def EQ(self,first,second): - name = self.search_fields[first.name] - result = None - if name is not None: - if name == "MESSAGE": - # query by message sequence number - result = "%s" % self.expand(second) - elif name == "UID": - result = "UID %s" % self.expand(second) - elif name == "DATE": - result = "ON %s" % self.convert_date(second) - - elif name in self.flags.values(): - if second: - result = "%s" % (name.upper()[1:]) - else: - result = "NOT %s" % (name.upper()[1:]) - else: - raise Exception("Operation not supported") - else: - raise Exception("Operation not supported") - return result - - def AND(self, first, second): - result = "%s %s" % (self.expand(first), self.expand(second)) - return result - - def OR(self, first, second): - result = "OR %s %s" % (self.expand(first), self.expand(second)) - return "%s" % result.replace("OR OR", "OR") - - def NOT(self, first): - result = "NOT %s" % self.expand(first) - return result diff --git a/gluon/dal/adapters/informix.py b/gluon/dal/adapters/informix.py deleted file mode 100644 index cd7a06ca..00000000 --- a/gluon/dal/adapters/informix.py +++ /dev/null @@ -1,134 +0,0 @@ -# -*- coding: utf-8 -*- -import datetime -import re - -from .._globals import IDENTITY -from .base import BaseAdapter - - -class InformixAdapter(BaseAdapter): - drivers = ('informixdb',) - - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'BLOB SUB_TYPE 1', - 'json': 'BLOB SUB_TYPE 1', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BLOB SUB_TYPE 0', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INTEGER', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'DOUBLE PRECISION', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'CHAR(8)', - 'datetime': 'DATETIME', - 'id': 'SERIAL', - 'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'BLOB SUB_TYPE 1', - 'list:string': 'BLOB SUB_TYPE 1', - 'list:reference': 'BLOB SUB_TYPE 1', - 'big-id': 'BIGSERIAL', - 'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': 'REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s CONSTRAINT FK_%(table_name)s_%(field_name)s', - 'reference TFK': 'FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s CONSTRAINT TFK_%(table_name)s_%(field_name)s', - } - - def RANDOM(self): - return 'Random()' - - def NOT_NULL(self,default,field_type): - return 'DEFAULT %s NOT NULL' % self.represent(default,field_type) - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - fetch_amt = lmax - lmin - dbms_version = int(self.connection.dbms_version.split('.')[0]) - if lmin and (dbms_version >= 10): - # Requires Informix 10.0+ - sql_s += ' SKIP %d' % (lmin, ) - if fetch_amt and (dbms_version >= 9): - # Requires Informix 9.0+ - sql_s += ' FIRST %d' % (fetch_amt, ) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def represent_exceptions(self, obj, fieldtype): - if fieldtype == 'date': - if isinstance(obj, (datetime.date, datetime.datetime)): - obj = obj.isoformat()[:10] - else: - obj = str(obj) - return "to_date('%s','%%Y-%%m-%%d')" % obj - elif fieldtype == 'datetime': - if isinstance(obj, datetime.datetime): - obj = obj.isoformat()[:19].replace('T',' ') - elif isinstance(obj, datetime.date): - obj = obj.isoformat()[:10]+' 00:00:00' - else: - obj = str(obj) - return "to_date('%s','%%Y-%%m-%%d %%H:%%M:%%S')" % obj - return None - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P.+)$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "informix" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - user = credential_decoder(user) - password = credential_decoder(password) - dsn = '%s@%s' % (db,host) - driver_args.update(user=user,password=password,autocommit=True) - def connector(dsn=dsn,driver_args=driver_args): - return self.driver.connect(dsn,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def execute(self,command): - if command[-1:]==';': - command = command[:-1] - return self.log_execute(command) - - def lastrowid(self,table): - return self.cursor.sqlerrd[1] - - -class InformixSEAdapter(InformixAdapter): - """ work in progress """ - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - return 'SELECT %s %s FROM %s%s%s;' % \ - (sql_s, sql_f, sql_t, sql_w, sql_o) - - def rowslice(self,rows,minimum=0,maximum=None): - if maximum is None: - return rows[minimum:] - return rows[minimum:maximum] diff --git a/gluon/dal/adapters/ingres.py b/gluon/dal/adapters/ingres.py deleted file mode 100644 index 8c5f752e..00000000 --- a/gluon/dal/adapters/ingres.py +++ /dev/null @@ -1,147 +0,0 @@ -# -*- coding: utf-8 -*- - -from .._globals import IDENTITY -from .._load import pyodbc -from .base import BaseAdapter - -# NOTE invalid database object name (ANSI-SQL wants -# this form of name to be a delimited identifier) -INGRES_SEQNAME='ii***lineitemsequence' - - -class IngresAdapter(BaseAdapter): - drivers = ('pyodbc',) - - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'CLOB', - 'json': 'CLOB', - 'password': 'VARCHAR(%(length)s)', ## Not sure what this contains utf8 or nvarchar. Or even bytes? - 'blob': 'BLOB', - 'upload': 'VARCHAR(%(length)s)', ## FIXME utf8 or nvarchar... or blob? what is this type? - 'integer': 'INTEGER4', # or int8... - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT8', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'ANSIDATE', - 'time': 'TIME WITHOUT TIME ZONE', - 'datetime': 'TIMESTAMP WITHOUT TIME ZONE', - 'id': 'int not null unique with default next value for %s' % INGRES_SEQNAME, - 'reference': 'INT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'CLOB', - 'list:string': 'CLOB', - 'list:reference': 'CLOB', - 'big-id': 'bigint not null unique with default next value for %s' % INGRES_SEQNAME, - 'big-reference': 'BIGINT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', ## FIXME TODO - } - - def LEFT_JOIN(self): - return 'LEFT OUTER JOIN' - - def RANDOM(self): - return 'RANDOM()' - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - fetch_amt = lmax - lmin - if fetch_amt: - sql_s += ' FIRST %d ' % (fetch_amt, ) - if lmin: - # Requires Ingres 9.2+ - sql_o += ' OFFSET %d' % (lmin, ) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "ingres" - self._driver = pyodbc - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - connstr = uri.split(':', 1)[1] - # Simple URI processing - connstr = connstr.lstrip() - while connstr.startswith('/'): - connstr = connstr[1:] - if '=' in connstr: - # Assume we have a regular ODBC connection string and just use it - ruri = connstr - else: - # Assume only (local) dbname is passed in with OS auth - database_name = connstr - default_driver_name = 'Ingres' - vnode = '(local)' - servertype = 'ingres' - ruri = 'Driver={%s};Server=%s;Database=%s' % (default_driver_name, vnode, database_name) - def connector(cnxn=ruri,driver_args=driver_args): - return self.driver.connect(cnxn,**driver_args) - - self.connector = connector - - # TODO if version is >= 10, set types['id'] to Identity column, see http://community.actian.com/wiki/Using_Ingres_Identity_Columns - if do_connect: self.reconnect() - - def create_sequence_and_triggers(self, query, table, **args): - # post create table auto inc code (if needed) - # modify table to btree for performance.... - # Older Ingres releases could use rule/trigger like Oracle above. - if hasattr(table,'_primarykey'): - modify_tbl_sql = 'modify %s to btree unique on %s' % \ - (table._tablename, - ', '.join(["'%s'" % x for x in table.primarykey])) - self.execute(modify_tbl_sql) - else: - tmp_seqname='%s_iisq' % table._tablename - query=query.replace(INGRES_SEQNAME, tmp_seqname) - self.execute('create sequence %s' % tmp_seqname) - self.execute(query) - self.execute('modify %s to btree unique on %s' % (table._tablename, 'id')) - - - def lastrowid(self,table): - tmp_seqname='%s_iisq' % table - self.execute('select current value for %s' % tmp_seqname) - return long(self.cursor.fetchone()[0]) # don't really need int type cast here... - - -class IngresUnicodeAdapter(IngresAdapter): - - drivers = ('pyodbc',) - - types = { - 'boolean': 'CHAR(1)', - 'string': 'NVARCHAR(%(length)s)', - 'text': 'NCLOB', - 'json': 'NCLOB', - 'password': 'NVARCHAR(%(length)s)', ## Not sure what this contains utf8 or nvarchar. Or even bytes? - 'blob': 'BLOB', - 'upload': 'VARCHAR(%(length)s)', ## FIXME utf8 or nvarchar... or blob? what is this type? - 'integer': 'INTEGER4', # or int8... - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT8', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'ANSIDATE', - 'time': 'TIME WITHOUT TIME ZONE', - 'datetime': 'TIMESTAMP WITHOUT TIME ZONE', - 'id': 'INTEGER4 not null unique with default next value for %s'% INGRES_SEQNAME, - 'reference': 'INTEGER4, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'NCLOB', - 'list:string': 'NCLOB', - 'list:reference': 'NCLOB', - 'big-id': 'BIGINT not null unique with default next value for %s'% INGRES_SEQNAME, - 'big-reference': 'BIGINT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', ## FIXME TODO - } diff --git a/gluon/dal/adapters/mongo.py b/gluon/dal/adapters/mongo.py deleted file mode 100644 index fd5aaeea..00000000 --- a/gluon/dal/adapters/mongo.py +++ /dev/null @@ -1,575 +0,0 @@ -# -*- coding: utf-8 -*- -import datetime -import logging -import re - -from .._globals import IDENTITY -from ..objects import Table, Query, Field, Expression -from ..helpers.classes import SQLALL -from ..helpers.methods import xorify -from .base import NoSQLAdapter - -class MongoDBAdapter(NoSQLAdapter): - drivers = ('pymongo',) - driver_auto_json = ['loads','dumps'] - - uploads_in_blob = False - - types = { - 'boolean': bool, - 'string': str, - 'text': str, - 'json': str, - 'password': str, - 'blob': str, - 'upload': str, - 'integer': long, - 'bigint': long, - 'float': float, - 'double': float, - 'date': datetime.date, - 'time': datetime.time, - 'datetime': datetime.datetime, - 'id': long, - 'reference': long, - 'list:string': list, - 'list:integer': list, - 'list:reference': list, - } - - error_messages = {"javascript_needed": "This must yet be replaced" + - " with javascript in order to work."} - - def __init__(self,db,uri='mongodb://127.0.0.1:5984/db', - pool_size=0, folder=None, db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - - self.db = db - self.uri = uri - if do_connect: self.find_driver(adapter_args) - import random - from bson.objectid import ObjectId - from bson.son import SON - import pymongo.uri_parser - - m = pymongo.uri_parser.parse_uri(uri) - - self.SON = SON - self.ObjectId = ObjectId - self.random = random - - self.dbengine = 'mongodb' - self.folder = folder - db['_lastsql'] = '' - self.db_codec = 'UTF-8' - self._after_connection = after_connection - self.pool_size = pool_size - #this is the minimum amount of replicates that it should wait - # for on insert/update - self.minimumreplication = adapter_args.get('minimumreplication',0) - # by default all inserts and selects are performand asynchronous, - # but now the default is - # synchronous, except when overruled by either this default or - # function parameter - self.safe = adapter_args.get('safe',True) - # load user setting for uploads in blob storage - self.uploads_in_blob = adapter_args.get('uploads_in_blob', False) - - if isinstance(m,tuple): - m = {"database" : m[1]} - if m.get('database') is None: - raise SyntaxError("Database is required!") - - def connector(uri=self.uri,m=m): - # Connection() is deprecated - if hasattr(self.driver, "MongoClient"): - Connection = self.driver.MongoClient - else: - Connection = self.driver.Connection - return Connection(uri)[m.get('database')] - - self.reconnect(connector,cursor=False) - - def object_id(self, arg=None): - """ Convert input to a valid Mongodb ObjectId instance - - self.object_id("") -> ObjectId (not unique) instance """ - if not arg: - arg = 0 - if isinstance(arg, basestring): - # we assume an integer as default input - rawhex = len(arg.replace("0x", "").replace("L", "")) == 24 - if arg.isdigit() and (not rawhex): - arg = int(arg) - elif arg == "": - arg = int("0x%sL" % \ - "".join([self.random.choice("0123456789abcdef") \ - for x in range(24)]), 0) - elif arg.isalnum(): - if not arg.startswith("0x"): - arg = "0x%s" % arg - try: - arg = int(arg, 0) - except ValueError, e: - raise ValueError( - "invalid objectid argument string: %s" % e) - else: - raise ValueError("Invalid objectid argument string. " + - "Requires an integer or base 16 value") - elif isinstance(arg, self.ObjectId): - return arg - - if not isinstance(arg, (int, long)): - raise TypeError("object_id argument must be of type " + - "ObjectId or an objectid representable integer") - hexvalue = hex(arg)[2:].rstrip('L').zfill(24) - return self.ObjectId(hexvalue) - - def parse_reference(self, value, field_type): - # here we have to check for ObjectID before base parse - if isinstance(value, self.ObjectId): - value = long(str(value), 16) - return super(MongoDBAdapter, - self).parse_reference(value, field_type) - - def parse_id(self, value, field_type): - if isinstance(value, self.ObjectId): - value = long(str(value), 16) - return super(MongoDBAdapter, - self).parse_id(value, field_type) - - def represent(self, obj, fieldtype): - # the base adatpter does not support MongoDB ObjectId - if isinstance(obj, self.ObjectId): - value = obj - else: - value = NoSQLAdapter.represent(self, obj, fieldtype) - # reference types must be convert to ObjectID - if fieldtype =='date': - if value is None: - return value - # this piece of data can be stripped off based on the fieldtype - t = datetime.time(0, 0, 0) - # mongodb doesn't has a date object and so it must datetime, - # string or integer - return datetime.datetime.combine(value, t) - elif fieldtype == 'time': - if value is None: - return value - # this piece of data can be stripped of based on the fieldtype - d = datetime.date(2000, 1, 1) - # mongodb doesn't has a time object and so it must datetime, - # string or integer - return datetime.datetime.combine(d, value) - elif fieldtype == "blob": - if value is None: - return value - from bson import Binary - if not isinstance(value, Binary): - if not isinstance(value, basestring): - return Binary(str(value)) - return Binary(value) - return value - elif (isinstance(fieldtype, basestring) and - fieldtype.startswith('list:')): - if fieldtype.startswith('list:reference'): - newval = [] - for v in value: - newval.append(self.object_id(v)) - return newval - return value - elif ((isinstance(fieldtype, basestring) and - fieldtype.startswith("reference")) or - (isinstance(fieldtype, Table)) or fieldtype=="id"): - value = self.object_id(value) - return value - - def create_table(self, table, migrate=True, fake_migrate=False, - polymodel=None, isCapped=False): - if isCapped: - raise RuntimeError("Not implemented") - - def count(self, query, distinct=None, snapshot=True): - if distinct: - raise RuntimeError("COUNT DISTINCT not supported") - if not isinstance(query,Query): - raise SyntaxError("Not Supported") - tablename = self.get_table(query) - return long(self.select(query,[self.db[tablename]._id], {}, - count=True,snapshot=snapshot)['count']) - # Maybe it would be faster if we just implemented the pymongo - # .count() function which is probably quicker? - # therefor call __select() connection[table].find(query).count() - # Since this will probably reduce the return set? - - def expand(self, expression, field_type=None): - if isinstance(expression, Query): - # any query using 'id':= - # set name as _id (as per pymongo/mongodb primary key) - # convert second arg to an objectid field - # (if its not already) - # if second arg is 0 convert to objectid - if isinstance(expression.first,Field) and \ - ((expression.first.type == 'id') or \ - ("reference" in expression.first.type)): - if expression.first.type == 'id': - expression.first.name = '_id' - # cast to Mongo ObjectId - if isinstance(expression.second, (tuple, list, set)): - expression.second = [self.object_id(item) for - item in expression.second] - else: - expression.second = self.object_id(expression.second) - result = expression.op(expression.first, expression.second) - - if isinstance(expression, Field): - if expression.type=='id': - result = "_id" - else: - result = expression.name - elif isinstance(expression, (Expression, Query)): - if not expression.second is None: - result = expression.op(expression.first, expression.second) - elif not expression.first is None: - result = expression.op(expression.first) - elif not isinstance(expression.op, str): - result = expression.op() - else: - result = expression.op - elif field_type: - result = self.represent(expression,field_type) - elif isinstance(expression,(list,tuple)): - result = [self.represent(item,field_type) for - item in expression] - else: - result = expression - return result - - def drop(self, table, mode=''): - ctable = self.connection[table._tablename] - ctable.drop() - - def truncate(self, table, mode, safe=None): - if safe == None: - safe=self.safe - ctable = self.connection[table._tablename] - ctable.remove(None, safe=True) - - def select(self, query, fields, attributes, count=False, - snapshot=False): - mongofields_dict = self.SON() - mongoqry_dict = {} - new_fields, mongosort_list = [], [] - # try an orderby attribute - orderby = attributes.get('orderby', False) - limitby = attributes.get('limitby', False) - # distinct = attributes.get('distinct', False) - if 'for_update' in attributes: - logging.warn('mongodb does not support for_update') - for key in set(attributes.keys())-set(('limitby', - 'orderby','for_update')): - if attributes[key] is not None: - logging.warn('select attribute not implemented: %s' % key) - if limitby: - limitby_skip, limitby_limit = limitby[0], int(limitby[1]) - else: - limitby_skip = limitby_limit = 0 - if orderby: - if isinstance(orderby, (list, tuple)): - orderby = xorify(orderby) - # !!!! need to add 'random' - for f in self.expand(orderby).split(','): - if f.startswith('-'): - mongosort_list.append((f[1:], -1)) - else: - mongosort_list.append((f, 1)) - for item in fields: - if isinstance(item, SQLALL): - new_fields += item._table - else: - new_fields.append(item) - fields = new_fields - if isinstance(query,Query): - tablename = self.get_table(query) - elif len(fields) != 0: - tablename = fields[0].tablename - else: - raise SyntaxError("The table name could not be found in " + - "the query nor from the select statement.") - mongoqry_dict = self.expand(query) - fields = fields or self.db[tablename] - for field in fields: - mongofields_dict[field.name] = 1 - ctable = self.connection[tablename] - if count: - return {'count' : ctable.find( - mongoqry_dict, mongofields_dict, - skip=limitby_skip, limit=limitby_limit, - sort=mongosort_list, snapshot=snapshot).count()} - else: - # pymongo cursor object - mongo_list_dicts = ctable.find(mongoqry_dict, - mongofields_dict, skip=limitby_skip, - limit=limitby_limit, sort=mongosort_list, - snapshot=snapshot) - rows = [] - # populate row in proper order - # Here we replace ._id with .id to follow the standard naming - colnames = [] - newnames = [] - for field in fields: - colname = str(field) - colnames.append(colname) - tablename, fieldname = colname.split(".") - if fieldname == "_id": - # Mongodb reserved uuid key - field.name = "id" - newnames.append(".".join((tablename, field.name))) - - for record in mongo_list_dicts: - row=[] - for colname in colnames: - tablename, fieldname = colname.split(".") - # switch to Mongo _id uuids for retrieving - # record id's - if fieldname == "id": fieldname = "_id" - if fieldname in record: - value = record[fieldname] - else: - value = None - row.append(value) - rows.append(row) - processor = attributes.get('processor', self.parse) - result = processor(rows, fields, newnames, False) - return result - - def insert(self, table, fields, safe=None): - """Safe determines whether a asynchronous request is done or a - synchronous action is done - For safety, we use by default synchronous requests""" - - values = dict() - if safe is None: - safe = self.safe - ctable = self.connection[table._tablename] - for k, v in fields: - if not k.name in ["id", "safe"]: - fieldname = k.name - fieldtype = table[k.name].type - values[fieldname] = self.represent(v, fieldtype) - - ctable.insert(values, safe=safe) - return long(str(values['_id']), 16) - - def update(self, tablename, query, fields, safe=None): - if safe == None: - safe = self.safe - # return amount of adjusted rows or zero, but no exceptions - # @ related not finding the result - if not isinstance(query, Query): - raise RuntimeError("Not implemented") - amount = self.count(query, False) - if not isinstance(query, Query): - raise SyntaxError("Not Supported") - filter = None - if query: - filter = self.expand(query) - # do not try to update id fields to avoid backend errors - modify = {'$set': dict((k.name, self.represent(v, k.type)) for - k, v in fields if (not k.name in ("_id", "id")))} - try: - result = self.connection[tablename].update(filter, - modify, multi=True, safe=safe) - if safe: - try: - # if result count is available fetch it - return result["n"] - except (KeyError, AttributeError, TypeError): - return amount - else: - return amount - except Exception, e: - # TODO Reverse update query to verifiy that the query succeded - raise RuntimeError("uncaught exception when updating rows: %s" % e) - - def delete(self, tablename, query, safe=None): - if safe is None: - safe = self.safe - amount = 0 - amount = self.count(query, False) - if not isinstance(query, Query): - raise RuntimeError("query type %s is not supported" % \ - type(query)) - filter = self.expand(query) - self.connection[tablename].remove(filter, safe=safe) - return amount - - def bulk_insert(self, table, items): - return [self.insert(table,item) for item in items] - - ## OPERATORS - def INVERT(self, first): - #print "in invert first=%s" % first - return '-%s' % self.expand(first) - - # TODO This will probably not work:( - def NOT(self, first): - return {'$not': self.expand(first)} - - def AND(self,first,second): - # pymongo expects: .find({'$and': [{'x':'1'}, {'y':'2'}]}) - return {'$and': [self.expand(first),self.expand(second)]} - - def OR(self,first,second): - # pymongo expects: .find({'$or': [{'name':'1'}, {'name':'2'}]}) - return {'$or': [self.expand(first),self.expand(second)]} - - def BELONGS(self, first, second): - if isinstance(second, str): - return {self.expand(first) : {"$in" : [ second[:-1]]} } - elif second==[] or second==() or second==set(): - return {1:0} - items = [self.expand(item, first.type) for item in second] - return {self.expand(first) : {"$in" : items} } - - def EQ(self,first,second=None): - result = {} - result[self.expand(first)] = self.expand(second) - return result - - def NE(self, first, second=None): - result = {} - result[self.expand(first)] = {'$ne': self.expand(second)} - return result - - def LT(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s < None" % first) - result = {} - result[self.expand(first)] = {'$lt': self.expand(second)} - return result - - def LE(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s <= None" % first) - result = {} - result[self.expand(first)] = {'$lte': self.expand(second)} - return result - - def GT(self,first,second): - result = {} - result[self.expand(first)] = {'$gt': self.expand(second)} - return result - - def GE(self,first,second=None): - if second is None: - raise RuntimeError("Cannot compare %s >= None" % first) - result = {} - result[self.expand(first)] = {'$gte': self.expand(second)} - return result - - def ADD(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '%s + %s' % (self.expand(first), - self.expand(second, first.type)) - - def SUB(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '(%s - %s)' % (self.expand(first), - self.expand(second, first.type)) - - def MUL(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '(%s * %s)' % (self.expand(first), - self.expand(second, first.type)) - - def DIV(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '(%s / %s)' % (self.expand(first), - self.expand(second, first.type)) - - def MOD(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '(%s %% %s)' % (self.expand(first), - self.expand(second, first.type)) - - def AS(self, first, second): - raise NotImplementedError(self.error_messages["javascript_needed"]) - return '%s AS %s' % (self.expand(first), second) - - # We could implement an option that simulates a full featured SQL - # database. But I think the option should be set explicit or - # implemented as another library. - def ON(self, first, second): - raise NotImplementedError("This is not possible in NoSQL" + - " but can be simulated with a wrapper.") - return '%s ON %s' % (self.expand(first), self.expand(second)) - - # BLOW ARE TWO IMPLEMENTATIONS OF THE SAME FUNCITONS - # WHICH ONE IS BEST? - - def COMMA(self, first, second): - return '%s, %s' % (self.expand(first), self.expand(second)) - - def LIKE(self, first, second): - #escaping regex operators? - return {self.expand(first): ('%s' % \ - self.expand(second, 'string').replace('%','/'))} - - def ILIKE(self, first, second): - val = second if isinstance(second,self.ObjectId) else { - '$regex': second.replace('%', ''), '$options': 'i'} - return {self.expand(first): val} - - def STARTSWITH(self, first, second): - #escaping regex operators? - return {self.expand(first): ('/^%s/' % \ - self.expand(second, 'string'))} - - def ENDSWITH(self, first, second): - #escaping regex operators? - return {self.expand(first): ('/%s^/' % \ - self.expand(second, 'string'))} - - def CONTAINS(self, first, second, case_sensitive=False): - # silently ignore, only case sensitive - # There is a technical difference, but mongodb doesn't support - # that, but the result will be the same - val = second if isinstance(second,self.ObjectId) else \ - {'$regex':".*" + re.escape(self.expand(second, 'string')) + ".*"} - return {self.expand(first) : val} - - def LIKE(self, first, second): - import re - return {self.expand(first): {'$regex': \ - re.escape(self.expand(second, - 'string')).replace('%','.*')}} - - #TODO verify full compatibilty with official SQL Like operator - def STARTSWITH(self, first, second): - #TODO Solve almost the same problem as with endswith - import re - return {self.expand(first): {'$regex' : '^' + - re.escape(self.expand(second, - 'string'))}} - - #TODO verify full compatibilty with official SQL Like operator - def ENDSWITH(self, first, second): - #escaping regex operators? - #TODO if searched for a name like zsa_corbitt and the function - # is endswith('a') then this is also returned. - # Aldo it end with a t - import re - return {self.expand(first): {'$regex': \ - re.escape(self.expand(second, 'string')) + '$'}} - - #TODO verify full compatibilty with official oracle contains operator - def CONTAINS(self, first, second, case_sensitive=False): - # silently ignore, only case sensitive - #There is a technical difference, but mongodb doesn't support - # that, but the result will be the same - #TODO contains operators need to be transformed to Regex - return {self.expand(first) : {'$regex': \ - ".*" + re.escape(self.expand(second, 'string')) + ".*"}} - diff --git a/gluon/dal/adapters/mssql.py b/gluon/dal/adapters/mssql.py deleted file mode 100644 index ee3cafcc..00000000 --- a/gluon/dal/adapters/mssql.py +++ /dev/null @@ -1,513 +0,0 @@ -# -*- coding: utf-8 -*- -import re -import sys - -from .._globals import IDENTITY, LOGGER -from ..helpers.methods import varquote_aux -from .base import BaseAdapter - - -class MSSQLAdapter(BaseAdapter): - drivers = ('pyodbc',) - T_SEP = 'T' - - QUOTE_TEMPLATE = '"%s"' - - types = { - 'boolean': 'BIT', - 'string': 'VARCHAR(%(length)s)', - 'text': 'TEXT', - 'json': 'TEXT', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'IMAGE', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATETIME', - 'time': 'CHAR(8)', - 'datetime': 'DATETIME', - 'id': 'INT IDENTITY PRIMARY KEY', - 'reference': 'INT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'TEXT', - 'list:string': 'TEXT', - 'list:reference': 'TEXT', - 'geometry': 'geometry', - 'geography': 'geography', - 'big-id': 'BIGINT IDENTITY PRIMARY KEY', - 'big-reference': 'BIGINT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - def concat_add(self,tablename): - return '; ALTER TABLE %s ADD ' % tablename - - def varquote(self,name): - return varquote_aux(name,'[%s]') - - def EXTRACT(self,field,what): - return "DATEPART(%s,%s)" % (what, self.expand(field)) - - def LEFT_JOIN(self): - return 'LEFT OUTER JOIN' - - def RANDOM(self): - return 'NEWID()' - - def ALLOW_NULL(self): - return ' NULL' - - def CAST(self, first, second): - return first # apparently no cast necessary in MSSQL - - def SUBSTRING(self,field,parameters): - return 'SUBSTRING(%s,%s,%s)' % (self.expand(field), parameters[0], parameters[1]) - - def PRIMARY_KEY(self,key): - return 'PRIMARY KEY CLUSTERED (%s)' % key - - def AGGREGATE(self, first, what): - if what == 'LENGTH': - what = 'LEN' - return "%s(%s)" % (what, self.expand(first)) - - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_s += ' TOP %i' % lmax - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - TRUE = 1 - FALSE = 0 - - REGEX_DSN = re.compile('^(?P.+)$') - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P[^\?]+)(\?(?P.*))?$') - REGEX_ARGPATTERN = re.compile('(?P[^=]+)=(?P[^&]*)') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, srid=4326, - after_connection=None): - self.db = db - self.dbengine = "mssql" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.srid = srid - self.find_or_make_work_folder() - # ## read: http://bytes.com/groups/python/460325-cx_oracle-utf8 - ruri = uri.split('://',1)[1] - if '@' not in ruri: - try: - m = self.REGEX_DSN.match(ruri) - if not m: - raise SyntaxError( - 'Parsing uri string(%s) has no result' % self.uri) - dsn = m.group('dsn') - if not dsn: - raise SyntaxError('DSN required') - except SyntaxError: - e = sys.exc_info()[1] - LOGGER.error('NdGpatch error') - raise e - # was cnxn = 'DSN=%s' % dsn - cnxn = dsn - else: - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = m.group('port') or '1433' - # Parse the optional url name-value arg pairs after the '?' - # (in the form of arg1=value1&arg2=value2&...) - # Default values (drivers like FreeTDS insist on uppercase parameter keys) - argsdict = { 'DRIVER':'{SQL Server}' } - urlargs = m.group('urlargs') or '' - for argmatch in self.REGEX_ARGPATTERN.finditer(urlargs): - argsdict[str(argmatch.group('argkey')).upper()] = argmatch.group('argvalue') - urlargs = ';'.join(['%s=%s' % (ak, av) for (ak, av) in argsdict.iteritems()]) - cnxn = 'SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s;%s' \ - % (host, port, db, user, password, urlargs) - def connector(cnxn=cnxn,driver_args=driver_args): - return self.driver.connect(cnxn,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def lastrowid(self,table): - #self.execute('SELECT @@IDENTITY;') - self.execute('SELECT SCOPE_IDENTITY();') - return long(self.cursor.fetchone()[0]) - - def rowslice(self,rows,minimum=0,maximum=None): - if maximum is None: - return rows[minimum:] - return rows[minimum:maximum] - - def EPOCH(self, first): - return "DATEDIFF(second, '1970-01-01 00:00:00', %s)" % self.expand(first) - - def CONCAT(self, *items): - return '(%s)' % ' + '.join(self.expand(x,'string') for x in items) - - # GIS Spatial Extensions - - # No STAsGeoJSON in MSSQL - - def ST_ASTEXT(self, first): - return '%s.STAsText()' %(self.expand(first)) - - def ST_CONTAINS(self, first, second): - return '%s.STContains(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - def ST_DISTANCE(self, first, second): - return '%s.STDistance(%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_EQUALS(self, first, second): - return '%s.STEquals(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - def ST_INTERSECTS(self, first, second): - return '%s.STIntersects(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - def ST_OVERLAPS(self, first, second): - return '%s.STOverlaps(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - # no STSimplify in MSSQL - - def ST_TOUCHES(self, first, second): - return '%s.STTouches(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - def ST_WITHIN(self, first, second): - return '%s.STWithin(%s)=1' %(self.expand(first), self.expand(second, first.type)) - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if field_is_type('geometry'): - srid = 0 # MS SQL default srid for geometry - geotype, parms = fieldtype[:-1].split('(') - if parms: - srid = parms - return "geometry::STGeomFromText('%s',%s)" %(obj, srid) - elif fieldtype == 'geography': - srid = 4326 # MS SQL default srid for geography - geotype, parms = fieldtype[:-1].split('(') - if parms: - srid = parms - return "geography::STGeomFromText('%s',%s)" %(obj, srid) -# else: -# raise SyntaxError('Invalid field type %s' %fieldtype) - return "geometry::STGeomFromText('%s',%s)" %(obj, srid) - return BaseAdapter.represent(self, obj, fieldtype) - - -class MSSQL3Adapter(MSSQLAdapter): - """Experimental support for pagination in MSSQL - - Requires MSSQL >= 2005, uses `ROW_NUMBER()` - """ - - types = { - 'boolean': 'BIT', - 'string': 'VARCHAR(%(length)s)', - 'text': 'VARCHAR(MAX)', - 'json': 'VARCHAR(MAX)', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'IMAGE', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATETIME', - 'time': 'TIME(7)', - 'datetime': 'DATETIME', - 'id': 'INT IDENTITY PRIMARY KEY', - 'reference': 'INT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'VARCHAR(MAX)', - 'list:string': 'VARCHAR(MAX)', - 'list:reference': 'VARCHAR(MAX)', - 'geometry': 'geometry', - 'geography': 'geography', - 'big-id': 'BIGINT IDENTITY PRIMARY KEY', - 'big-reference': 'BIGINT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - if lmin == 0: - sql_s += ' TOP %i' % lmax - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - lmin += 1 - sql_o_inner = sql_o[sql_o.find('ORDER BY ')+9:] - sql_g_inner = sql_o[:sql_o.find('ORDER BY ')] - sql_f_outer = ['f_%s' % f for f in range(len(sql_f.split(',')))] - sql_f_inner = [f for f in sql_f.split(',')] - sql_f_iproxy = ['%s AS %s' % (o, n) for (o, n) in zip(sql_f_inner, sql_f_outer)] - sql_f_iproxy = ', '.join(sql_f_iproxy) - sql_f_oproxy = ', '.join(sql_f_outer) - return 'SELECT %s %s FROM (SELECT %s ROW_NUMBER() OVER (ORDER BY %s) AS w_row, %s FROM %s%s%s) TMP WHERE w_row BETWEEN %i AND %s;' % (sql_s,sql_f_oproxy,sql_s,sql_f,sql_f_iproxy,sql_t,sql_w,sql_g_inner,lmin,lmax) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s,sql_f,sql_t,sql_w,sql_o) - def rowslice(self,rows,minimum=0,maximum=None): - return rows - - -class MSSQL4Adapter(MSSQLAdapter): - """Support for "native" pagination - - Requires MSSQL >= 2012, uses `OFFSET ... ROWS ... FETCH NEXT ... ROWS ONLY` - """ - - types = { - 'boolean': 'BIT', - 'string': 'VARCHAR(%(length)s)', - 'text': 'VARCHAR(MAX)', - 'json': 'VARCHAR(MAX)', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'IMAGE', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATETIME', - 'time': 'TIME(7)', - 'datetime': 'DATETIME', - 'id': 'INT IDENTITY PRIMARY KEY', - 'reference': 'INT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'VARCHAR(MAX)', - 'list:string': 'VARCHAR(MAX)', - 'list:reference': 'VARCHAR(MAX)', - 'geometry': 'geometry', - 'geography': 'geography', - 'big-id': 'BIGINT IDENTITY PRIMARY KEY', - 'big-reference': 'BIGINT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - if lmin == 0: - #top is still slightly faster, especially because - #web2py's default to fetch references is to not specify - #an orderby clause - sql_s += ' TOP %i' % lmax - else: - if not sql_o: - #if there is no orderby, we can't use the brand new statements - #that being said, developer chose its own poison, so be it random - sql_o += ' ORDER BY %s' % self.RANDOM() - sql_o += ' OFFSET %i ROWS FETCH NEXT %i ROWS ONLY' % (lmin, lmax - lmin) - return 'SELECT %s %s FROM %s%s%s;' % \ - (sql_s, sql_f, sql_t, sql_w, sql_o) - - def rowslice(self,rows,minimum=0,maximum=None): - return rows - - -class MSSQL2Adapter(MSSQLAdapter): - drivers = ('pyodbc',) - - types = { - 'boolean': 'CHAR(1)', - 'string': 'NVARCHAR(%(length)s)', - 'text': 'NTEXT', - 'json': 'NTEXT', - 'password': 'NVARCHAR(%(length)s)', - 'blob': 'IMAGE', - 'upload': 'NVARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATETIME', - 'time': 'CHAR(8)', - 'datetime': 'DATETIME', - 'id': 'INT IDENTITY PRIMARY KEY', - 'reference': 'INT, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'NTEXT', - 'list:string': 'NTEXT', - 'list:reference': 'NTEXT', - 'big-id': 'BIGINT IDENTITY PRIMARY KEY', - 'big-reference': 'BIGINT, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - def represent(self, obj, fieldtype): - value = BaseAdapter.represent(self, obj, fieldtype) - if fieldtype in ('string','text', 'json') and value[:1]=="'": - value = 'N'+value - return value - - def execute(self,a): - return self.log_execute(a.decode('utf8')) - - -class VerticaAdapter(MSSQLAdapter): - drivers = ('pyodbc',) - T_SEP = ' ' - - types = { - 'boolean': 'BOOLEAN', - 'string': 'VARCHAR(%(length)s)', - 'text': 'BYTEA', - 'json': 'VARCHAR(%(length)s)', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BYTEA', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'DOUBLE PRECISION', - 'decimal': 'DECIMAL(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'DATETIME', - 'id': 'IDENTITY', - 'reference': 'INT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'BYTEA', - 'list:string': 'BYTEA', - 'list:reference': 'BYTEA', - 'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - - def EXTRACT(self, first, what): - return "DATE_PART('%s', TIMESTAMP %s)" % (what, self.expand(first)) - - def _truncate(self, table, mode=''): - tablename = table._tablename - return ['TRUNCATE %s %s;' % (tablename, mode or '')] - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_o += ' LIMIT %i OFFSET %i' % (lmax - lmin, lmin) - return 'SELECT %s %s FROM %s%s%s;' % \ - (sql_s, sql_f, sql_t, sql_w, sql_o) - - def lastrowid(self,table): - self.execute('SELECT LAST_INSERT_ID();') - return long(self.cursor.fetchone()[0]) - - def execute(self, a): - return self.log_execute(a) - - -class SybaseAdapter(MSSQLAdapter): - drivers = ('Sybase',) - - types = { - 'boolean': 'BIT', - 'string': 'CHAR VARYING(%(length)s)', - 'text': 'TEXT', - 'json': 'TEXT', - 'password': 'CHAR VARYING(%(length)s)', - 'blob': 'IMAGE', - 'upload': 'CHAR VARYING(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATETIME', - 'time': 'CHAR(8)', - 'datetime': 'DATETIME', - 'id': 'INT IDENTITY PRIMARY KEY', - 'reference': 'INT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'TEXT', - 'list:string': 'TEXT', - 'list:reference': 'TEXT', - 'geometry': 'geometry', - 'geography': 'geography', - 'big-id': 'BIGINT IDENTITY PRIMARY KEY', - 'big-reference': 'BIGINT NULL, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, srid=4326, - after_connection=None): - self.db = db - self.dbengine = "sybase" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.srid = srid - self.find_or_make_work_folder() - # ## read: http://bytes.com/groups/python/460325-cx_oracle-utf8 - ruri = uri.split('://',1)[1] - if '@' not in ruri: - try: - m = self.REGEX_DSN.match(ruri) - if not m: - raise SyntaxError( - 'Parsing uri string(%s) has no result' % self.uri) - dsn = m.group('dsn') - if not dsn: - raise SyntaxError('DSN required') - except SyntaxError: - e = sys.exc_info()[1] - LOGGER.error('NdGpatch error') - raise e - else: - m = self.REGEX_URI.match(uri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = m.group('port') or '1433' - - dsn = 'sybase:host=%s:%s;dbname=%s' % (host,port,db) - - driver_args.update(user = credential_decoder(user), - password = credential_decoder(password)) - - def connector(dsn=dsn,driver_args=driver_args): - return self.driver.connect(dsn,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - diff --git a/gluon/dal/adapters/mysql.py b/gluon/dal/adapters/mysql.py deleted file mode 100644 index 58c35080..00000000 --- a/gluon/dal/adapters/mysql.py +++ /dev/null @@ -1,140 +0,0 @@ -# -*- coding: utf-8 -*- -import re - -from .._globals import IDENTITY -from ..helpers.methods import varquote_aux -from .base import BaseAdapter - - -class MySQLAdapter(BaseAdapter): - drivers = ('MySQLdb','pymysql', 'mysqlconnector') - - commit_on_alter_table = True - support_distributed_transaction = True - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'LONGTEXT', - 'json': 'LONGTEXT', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'LONGBLOB', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'DOUBLE', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'DATETIME', - 'id': 'INT AUTO_INCREMENT NOT NULL', - 'reference': 'INT, INDEX %(index_name)s (%(field_name)s), FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'LONGTEXT', - 'list:string': 'LONGTEXT', - 'list:reference': 'LONGTEXT', - 'big-id': 'BIGINT AUTO_INCREMENT NOT NULL', - 'big-reference': 'BIGINT, INDEX %(index_name)s (%(field_name)s), FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT `FK_%(constraint_name)s` FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - QUOTE_TEMPLATE = "`%s`" - - def varquote(self,name): - return varquote_aux(name,'`%s`') - - def RANDOM(self): - return 'RAND()' - - def SUBSTRING(self,field,parameters): - return 'SUBSTRING(%s,%s,%s)' % (self.expand(field), - parameters[0], parameters[1]) - - def EPOCH(self, first): - return "UNIX_TIMESTAMP(%s)" % self.expand(first) - - def CONCAT(self, *items): - return 'CONCAT(%s)' % ','.join(self.expand(x,'string') for x in items) - - def REGEXP(self,first,second): - return '(%s REGEXP %s)' % (self.expand(first), - self.expand(second,'string')) - - def CAST(self, first, second): - if second=='LONGTEXT': second = 'CHAR' - return 'CAST(%s AS %s)' % (first, second) - - def _drop(self,table,mode): - # breaks db integrity but without this mysql does not drop table - table_rname = table.sqlsafe - return ['SET FOREIGN_KEY_CHECKS=0;','DROP TABLE %s;' % table_rname, - 'SET FOREIGN_KEY_CHECKS=1;'] - - def _insert_empty(self, table): - return 'INSERT INTO %s VALUES (DEFAULT);' % (table.sqlsafe) - - def distributed_transaction_begin(self,key): - self.execute('XA START;') - - def prepare(self,key): - self.execute("XA END;") - self.execute("XA PREPARE;") - - def commit_prepared(self,key): - self.execute("XA COMMIT;") - - def rollback_prepared(self,key): - self.execute("XA ROLLBACK;") - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P[^?]+)(\?set_encoding=(?P\w+))?$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "mysql" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError( - "Invalid URI string in DAL: %s" % self.uri) - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = int(m.group('port') or '3306') - charset = m.group('charset') or 'utf8' - driver_args.update(db=db, - user=credential_decoder(user), - passwd=credential_decoder(password), - host=host, - port=port, - charset=charset) - - - def connector(driver_args=driver_args): - return self.driver.connect(**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.execute('SET FOREIGN_KEY_CHECKS=1;') - self.execute("SET sql_mode='NO_BACKSLASH_ESCAPES';") - - def lastrowid(self,table): - self.execute('select last_insert_id();') - return int(self.cursor.fetchone()[0]) diff --git a/gluon/dal/adapters/oracle.py b/gluon/dal/adapters/oracle.py deleted file mode 100644 index e00b9fea..00000000 --- a/gluon/dal/adapters/oracle.py +++ /dev/null @@ -1,191 +0,0 @@ -# -*- coding: utf-8 -*- -import base64 -import datetime -import re - -from .._globals import IDENTITY -from .._load import cx_Oracle -from .base import BaseAdapter - -class OracleAdapter(BaseAdapter): - drivers = ('cx_Oracle',) - - commit_on_alter_table = False - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR2(%(length)s)', - 'text': 'CLOB', - 'json': 'CLOB', - 'password': 'VARCHAR2(%(length)s)', - 'blob': 'CLOB', - 'upload': 'VARCHAR2(%(length)s)', - 'integer': 'INT', - 'bigint': 'NUMBER', - 'float': 'FLOAT', - 'double': 'BINARY_DOUBLE', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'CHAR(8)', - 'datetime': 'DATE', - 'id': 'NUMBER PRIMARY KEY', - 'reference': 'NUMBER, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'CLOB', - 'list:string': 'CLOB', - 'list:reference': 'CLOB', - 'big-id': 'NUMBER PRIMARY KEY', - 'big-reference': 'NUMBER, CONSTRAINT %(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT FK_%(constraint_name)s FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT FK_%(foreign_table)s_PK FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - } - - - def trigger_name(self,tablename): - return '%s_trigger' % tablename - - def LEFT_JOIN(self): - return 'LEFT OUTER JOIN' - - def RANDOM(self): - return 'dbms_random.value' - - def NOT_NULL(self,default,field_type): - return 'DEFAULT %s NOT NULL' % self.represent(default,field_type) - - def REGEXP(self, first, second): - return 'REGEXP_LIKE(%s, %s)' % (self.expand(first), - self.expand(second, 'string')) - - def _drop(self,table,mode): - sequence_name = table._sequence_name - return ['DROP TABLE %s %s;' % (table.sqlsafe, mode), 'DROP SEQUENCE %s;' % sequence_name] - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - if len(sql_w) > 1: - sql_w_row = sql_w + ' AND w_row > %i' % lmin - else: - sql_w_row = 'WHERE w_row > %i' % lmin - return 'SELECT %s %s FROM (SELECT w_tmp.*, ROWNUM w_row FROM (SELECT %s FROM %s%s%s) w_tmp WHERE ROWNUM<=%i) %s %s %s;' % (sql_s, sql_f, sql_f, sql_t, sql_w, sql_o, lmax, sql_t, sql_w_row, sql_o) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def constraint_name(self, tablename, fieldname): - constraint_name = BaseAdapter.constraint_name(self, tablename, fieldname) - if len(constraint_name)>30: - constraint_name = '%s_%s__constraint' % (tablename[:10], fieldname[:7]) - return constraint_name - - def represent_exceptions(self, obj, fieldtype): - if fieldtype == 'blob': - obj = base64.b64encode(str(obj)) - return ":CLOB('%s')" % obj - elif fieldtype == 'date': - if isinstance(obj, (datetime.date, datetime.datetime)): - obj = obj.isoformat()[:10] - else: - obj = str(obj) - return "to_date('%s','yyyy-mm-dd')" % obj - elif fieldtype == 'datetime': - if isinstance(obj, datetime.datetime): - obj = obj.isoformat()[:19].replace('T',' ') - elif isinstance(obj, datetime.date): - obj = obj.isoformat()[:10]+' 00:00:00' - else: - obj = str(obj) - return "to_date('%s','yyyy-mm-dd hh24:mi:ss')" % obj - return None - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "oracle" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - if not 'threaded' in driver_args: - driver_args['threaded']=True - def connector(uri=ruri,driver_args=driver_args): - return self.driver.connect(uri,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.execute("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';") - self.execute("ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS';") - - oracle_fix = re.compile("[^']*('[^']*'[^']*)*\:(?PCLOB\('([^']+|'')*'\))") - - def execute(self, command, args=None): - args = args or [] - i = 1 - while True: - m = self.oracle_fix.match(command) - if not m: - break - command = command[:m.start('clob')] + str(i) + command[m.end('clob'):] - args.append(m.group('clob')[6:-2].replace("''", "'")) - i += 1 - if command[-1:]==';': - command = command[:-1] - return self.log_execute(command, args) - - def create_sequence_and_triggers(self, query, table, **args): - tablename = table._rname or table._tablename - id_name = table._id.name - sequence_name = table._sequence_name - trigger_name = table._trigger_name - self.execute(query) - self.execute('CREATE SEQUENCE %s START WITH 1 INCREMENT BY 1 NOMAXVALUE MINVALUE -1;' % sequence_name) - self.execute(""" - CREATE OR REPLACE TRIGGER %(trigger_name)s BEFORE INSERT ON %(tablename)s FOR EACH ROW - DECLARE - curr_val NUMBER; - diff_val NUMBER; - PRAGMA autonomous_transaction; - BEGIN - IF :NEW.%(id)s IS NOT NULL THEN - EXECUTE IMMEDIATE 'SELECT %(sequence_name)s.nextval FROM dual' INTO curr_val; - diff_val := :NEW.%(id)s - curr_val - 1; - IF diff_val != 0 THEN - EXECUTE IMMEDIATE 'alter sequence %(sequence_name)s increment by '|| diff_val; - EXECUTE IMMEDIATE 'SELECT %(sequence_name)s.nextval FROM dual' INTO curr_val; - EXECUTE IMMEDIATE 'alter sequence %(sequence_name)s increment by 1'; - END IF; - END IF; - SELECT %(sequence_name)s.nextval INTO :NEW.%(id)s FROM DUAL; - END; - """ % dict(trigger_name=trigger_name, tablename=tablename, - sequence_name=sequence_name,id=id_name)) - - def lastrowid(self,table): - sequence_name = table._sequence_name - self.execute('SELECT %s.currval FROM dual;' % sequence_name) - return long(self.cursor.fetchone()[0]) - - #def parse_value(self, value, field_type, blob_decode=True): - # if blob_decode and isinstance(value, cx_Oracle.LOB): - # try: - # value = value.read() - # except self.driver.ProgrammingError: - # # After a subsequent fetch the LOB value is not valid anymore - # pass - # return BaseAdapter.parse_value(self, value, field_type, blob_decode) - - def _fetchall(self): - if any(x[1]==cx_Oracle.LOB for x in self.cursor.description): - return [tuple([(c.read() if type(c) == cx_Oracle.LOB else c) \ - for c in r]) for r in self.cursor] - else: - return self.cursor.fetchall() - - def sqlsafe_table(self, tablename, ot=None): - if ot is not None: - return (self.QUOTE_TEMPLATE + ' ' \ - + self.QUOTE_TEMPLATE) % (ot, tablename) - return self.QUOTE_TEMPLATE % tablename diff --git a/gluon/dal/adapters/postgres.py b/gluon/dal/adapters/postgres.py deleted file mode 100644 index 7e81d6c8..00000000 --- a/gluon/dal/adapters/postgres.py +++ /dev/null @@ -1,420 +0,0 @@ -# -*- coding: utf-8 -*- -import re - -from .._load import psycopg2_adapt -from .._globals import IDENTITY, LOGGER -from ..helpers.methods import varquote_aux -from .base import BaseAdapter - - -class PostgreSQLAdapter(BaseAdapter): - drivers = ('psycopg2','pg8000') - - QUOTE_TEMPLATE = '"%s"' - - support_distributed_transaction = True - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'TEXT', - 'json': 'TEXT', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BYTEA', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INTEGER', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT8', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'SERIAL PRIMARY KEY', - 'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'TEXT', - 'list:string': 'TEXT', - 'list:reference': 'TEXT', - 'geometry': 'GEOMETRY', - 'geography': 'GEOGRAPHY', - 'big-id': 'BIGSERIAL PRIMARY KEY', - 'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference FK': ', CONSTRAINT "FK_%(constraint_name)s" FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'reference TFK': ' CONSTRAINT "FK_%(foreign_table)s_PK" FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s) ON DELETE %(on_delete_action)s', - - } - - - def varquote(self,name): - return varquote_aux(name,'"%s"') - - def adapt(self,obj): - if self.driver_name == 'psycopg2': - return psycopg2_adapt(obj).getquoted() - elif self.driver_name == 'pg8000': - return "'%s'" % str(obj).replace("%","%%").replace("'","''") - else: - return "'%s'" % str(obj).replace("'","''") - - def sequence_name(self,table): - return self.QUOTE_TEMPLATE % (table + '_id_seq') - - def RANDOM(self): - return 'RANDOM()' - - def ADD(self, first, second): - t = first.type - if t in ('text','string','password', 'json', 'upload','blob'): - return '(%s || %s)' % (self.expand(first), self.expand(second, t)) - else: - return '(%s + %s)' % (self.expand(first), self.expand(second, t)) - - def distributed_transaction_begin(self,key): - return - - def prepare(self,key): - self.execute("PREPARE TRANSACTION '%s';" % key) - - def commit_prepared(self,key): - self.execute("COMMIT PREPARED '%s';" % key) - - def rollback_prepared(self,key): - self.execute("ROLLBACK PREPARED '%s';" % key) - - def create_sequence_and_triggers(self, query, table, **args): - # following lines should only be executed if table._sequence_name does not exist - # self.execute('CREATE SEQUENCE %s;' % table._sequence_name) - # self.execute("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT NEXTVAL('%s');" \ - # % (table._tablename, table._fieldname, table._sequence_name)) - self.execute(query) - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:@]+)(\:(?P[0-9]+))?/(?P[^\?]+)(\?sslmode=(?P.+))?$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, srid=4326, - after_connection=None): - self.db = db - self.dbengine = "postgres" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.srid = srid - self.find_or_make_work_folder() - self._last_insert = None # for INSERT ... RETURNING ID - - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError("Invalid URI string in DAL") - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = m.group('port') or '5432' - sslmode = m.group('sslmode') - if sslmode: - msg = ("dbname='%s' user='%s' host='%s' " - "port=%s password='%s' sslmode='%s'") \ - % (db, user, host, port, password, sslmode) - else: - msg = ("dbname='%s' user='%s' host='%s' " - "port=%s password='%s'") \ - % (db, user, host, port, password) - # choose diver according uri - if self.driver: - self.__version__ = "%s %s" % (self.driver.__name__, - self.driver.__version__) - else: - self.__version__ = None - def connector(msg=msg,driver_args=driver_args): - return self.driver.connect(msg,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.connection.set_client_encoding('UTF8') - self.execute("SET standard_conforming_strings=on;") - self.try_json() - - def _insert(self, table, fields): - table_rname = table.sqlsafe - if fields: - keys = ','.join(f.sqlsafe_name for f, v in fields) - values = ','.join(self.expand(v, f.type) for f, v in fields) - if table._id: - self._last_insert = (table._id, 1) - return 'INSERT INTO %s(%s) VALUES (%s) RETURNING %s;' % ( - table_rname, keys, values, table._id.name) - else: - self._last_insert = None - return 'INSERT INTO %s(%s) VALUES (%s);' % (table_rname, keys, values) - else: - self._last_insert - return self._insert_empty(table) - - def lastrowid(self, table=None): - if self._last_insert: - return int(self.cursor.fetchone()[0]) - else: - self.execute("select lastval()") - return int(self.cursor.fetchone()[0]) - - def try_json(self): - # check JSON data type support - # (to be added to after_connection) - - # until pg8000 supports json, leave this commented - #if self.driver_name == "pg8000": - # supports_json = self.connection.server_version >= "9.2.0" - - if (self.driver_name == "psycopg2" and - self.driver.__version__ >= "2.0.12"): - supports_json = self.connection.server_version >= 90200 - elif self.driver_name == "zxJDBC": - supports_json = self.connection.dbversion >= "9.2.0" - else: - supports_json = None - if supports_json: - self.types["json"] = "JSON" - if (self.driver_name == "psycopg2" and - self.driver.__version__ >= '2.5.0'): - self.driver_auto_json = ['loads'] - else: - LOGGER.debug("Your database version does not support the JSON" - " data type (using TEXT instead)") - - def LIKE(self,first,second): - args = (self.expand(first), self.expand(second,'string')) - if not first.type in ('string', 'text', 'json'): - return '(%s LIKE %s)' % ( - self.CAST(args[0], 'CHAR(%s)' % first.length), args[1]) - else: - return '(%s LIKE %s)' % args - - def ILIKE(self,first,second): - args = (self.expand(first), self.expand(second,'string')) - if not first.type in ('string', 'text', 'json'): - return '(%s LIKE %s)' % ( - self.CAST(args[0], 'CHAR(%s)' % first.length), args[1]) - else: - return '(%s ILIKE %s)' % args - - def REGEXP(self,first,second): - return '(%s ~ %s)' % (self.expand(first), - self.expand(second,'string')) - - # GIS functions - - def ST_ASGEOJSON(self, first, second): - """ - http://postgis.org/docs/ST_AsGeoJSON.html - """ - return 'ST_AsGeoJSON(%s,%s,%s,%s)' %(second['version'], - self.expand(first), second['precision'], second['options']) - - def ST_ASTEXT(self, first): - """ - http://postgis.org/docs/ST_AsText.html - """ - return 'ST_AsText(%s)' %(self.expand(first)) - - def ST_X(self, first): - """ - http://postgis.org/docs/ST_X.html - """ - return 'ST_X(%s)' %(self.expand(first)) - - def ST_Y(self, first): - """ - http://postgis.org/docs/ST_Y.html - """ - return 'ST_Y(%s)' %(self.expand(first)) - - def ST_CONTAINS(self, first, second): - """ - http://postgis.org/docs/ST_Contains.html - """ - return 'ST_Contains(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_DISTANCE(self, first, second): - """ - http://postgis.org/docs/ST_Distance.html - """ - return 'ST_Distance(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_EQUALS(self, first, second): - """ - http://postgis.org/docs/ST_Equals.html - """ - return 'ST_Equals(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_INTERSECTS(self, first, second): - """ - http://postgis.org/docs/ST_Intersects.html - """ - return 'ST_Intersects(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_OVERLAPS(self, first, second): - """ - http://postgis.org/docs/ST_Overlaps.html - """ - return 'ST_Overlaps(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_SIMPLIFY(self, first, second): - """ - http://postgis.org/docs/ST_Simplify.html - """ - return 'ST_Simplify(%s,%s)' %(self.expand(first), self.expand(second, 'double')) - - def ST_TOUCHES(self, first, second): - """ - http://postgis.org/docs/ST_Touches.html - """ - return 'ST_Touches(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_WITHIN(self, first, second): - """ - http://postgis.org/docs/ST_Within.html - """ - return 'ST_Within(%s,%s)' %(self.expand(first), self.expand(second, first.type)) - - def ST_DWITHIN(self, first, (second, third)): - """ - http://postgis.org/docs/ST_DWithin.html - """ - return 'ST_DWithin(%s,%s,%s)' %(self.expand(first), - self.expand(second, first.type), - self.expand(third, 'double')) - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if field_is_type('geo'): - srid = 4326 # postGIS default srid for geometry - geotype, parms = fieldtype[:-1].split('(') - parms = parms.split(',') - if len(parms) >= 2: - schema, srid = parms[:2] - if field_is_type('geometry'): - value = "ST_GeomFromText('%s',%s)" %(obj, srid) - elif field_is_type('geography'): - value = "ST_GeogFromText('SRID=%s;%s')" %(srid, obj) -# else: -# raise SyntaxError('Invalid field type %s' %fieldtype) - return value - return BaseAdapter.represent(self, obj, fieldtype) - - def _drop(self, table, mode='restrict'): - if mode not in ['restrict', 'cascade', '']: - raise ValueError('Invalid mode: %s' % mode) - return ['DROP TABLE ' + table.sqlsafe + ' ' + str(mode) + ';'] - -class NewPostgreSQLAdapter(PostgreSQLAdapter): - drivers = ('psycopg2','pg8000') - - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'TEXT', - 'json': 'TEXT', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BYTEA', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INTEGER', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'FLOAT8', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'SERIAL PRIMARY KEY', - 'reference': 'INTEGER REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'BIGINT[]', - 'list:string': 'TEXT[]', - 'list:reference': 'BIGINT[]', - 'geometry': 'GEOMETRY', - 'geography': 'GEOGRAPHY', - 'big-id': 'BIGSERIAL PRIMARY KEY', - 'big-reference': 'BIGINT REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - def parse_list_integers(self, value, field_type): - return value - - def parse_list_references(self, value, field_type): - return [self.parse_reference(r, field_type[5:]) for r in value] - - def parse_list_strings(self, value, field_type): - return value - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if field_is_type('list:'): - if not obj: - obj = [] - elif not isinstance(obj, (list, tuple)): - obj = [obj] - if field_is_type('list:string'): - obj = map(str,obj) - else: - obj = map(int,obj) - return 'ARRAY[%s]' % ','.join(repr(item) for item in obj) - return BaseAdapter.represent(self, obj, fieldtype) - - -class JDBCPostgreSQLAdapter(PostgreSQLAdapter): - drivers = ('zxJDBC',) - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:/]+)(\:(?P[0-9]+))?/(?P.+)$') - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None ): - self.db = db - self.dbengine = "postgres" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError("Invalid URI string in DAL") - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - port = m.group('port') or '5432' - msg = ('jdbc:postgresql://%s:%s/%s' % (host, port, db), user, password) - def connector(msg=msg,driver_args=driver_args): - return self.driver.connect(*msg,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.connection.set_client_encoding('UTF8') - self.execute('BEGIN;') - self.execute("SET CLIENT_ENCODING TO 'UNICODE';") - self.try_json() diff --git a/gluon/dal/adapters/sapdb.py b/gluon/dal/adapters/sapdb.py deleted file mode 100644 index a696bf65..00000000 --- a/gluon/dal/adapters/sapdb.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- coding: utf-8 -*- -import re - -from .._globals import IDENTITY -from .base import BaseAdapter - - -class SAPDBAdapter(BaseAdapter): - drivers = ('sapdb',) - - support_distributed_transaction = False - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'LONG', - 'json': 'LONG', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'LONG', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'FLOAT', - 'double': 'DOUBLE PRECISION', - 'decimal': 'FIXED(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - 'id': 'INT PRIMARY KEY', - 'reference': 'INT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - 'list:integer': 'LONG', - 'list:string': 'LONG', - 'list:reference': 'LONG', - 'big-id': 'BIGINT PRIMARY KEY', - 'big-reference': 'BIGINT, FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_key)s ON DELETE %(on_delete_action)s', - } - - def sequence_name(self,table): - return (self.QUOTE_TEMPLATE + '_id_Seq') % table - - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - if len(sql_w) > 1: - sql_w_row = sql_w + ' AND w_row > %i' % lmin - else: - sql_w_row = 'WHERE w_row > %i' % lmin - return '%s %s FROM (SELECT w_tmp.*, ROWNO w_row FROM (SELECT %s FROM %s%s%s) w_tmp WHERE ROWNO=%i) %s %s %s;' % (sql_s, sql_f, sql_f, sql_t, sql_w, sql_o, lmax, sql_t, sql_w_row, sql_o) - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def create_sequence_and_triggers(self, query, table, **args): - # following lines should only be executed if table._sequence_name does not exist - self.execute('CREATE SEQUENCE %s;' % table._sequence_name) - self.execute("ALTER TABLE %s ALTER COLUMN %s SET DEFAULT NEXTVAL('%s');" \ - % (table._tablename, table._id.name, table._sequence_name)) - self.execute(query) - - REGEX_URI = re.compile('^(?P[^:@]+)(\:(?P[^@]*))?@(?P[^\:@]+)(\:(?P[0-9]+))?/(?P[^\?]+)(\?sslmode=(?P.+))?$') - - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "sapdb" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://',1)[1] - m = self.REGEX_URI.match(ruri) - if not m: - raise SyntaxError("Invalid URI string in DAL") - user = credential_decoder(m.group('user')) - if not user: - raise SyntaxError('User required') - password = credential_decoder(m.group('password')) - if not password: - password = '' - host = m.group('host') - if not host: - raise SyntaxError('Host name required') - db = m.group('db') - if not db: - raise SyntaxError('Database name required') - def connector(user=user, password=password, database=db, - host=host, driver_args=driver_args): - return self.driver.Connection(user, password, database, - host, **driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def lastrowid(self,table): - self.execute("select %s.NEXTVAL from dual" % table._sequence_name) - return long(self.cursor.fetchone()[0]) diff --git a/gluon/dal/adapters/sqlite.py b/gluon/dal/adapters/sqlite.py deleted file mode 100644 index 588b1027..00000000 --- a/gluon/dal/adapters/sqlite.py +++ /dev/null @@ -1,280 +0,0 @@ -# -*- coding: utf-8 -*- -import copy -import datetime -import locale -import platform -import re -import sys -import time - -from .._compat import PY2, pjoin -from .._globals import IDENTITY -from .base import BaseAdapter - - -class SQLiteAdapter(BaseAdapter): - drivers = ('sqlite2','sqlite3') - - can_select_for_update = None # support ourselves with BEGIN TRANSACTION - - def EXTRACT(self,field,what): - return "web2py_extract('%s',%s)" % (what, self.expand(field)) - - @staticmethod - def web2py_extract(lookup, s): - table = { - 'year': (0, 4), - 'month': (5, 7), - 'day': (8, 10), - 'hour': (11, 13), - 'minute': (14, 16), - 'second': (17, 19), - } - try: - if lookup != 'epoch': - (i, j) = table[lookup] - return int(s[i:j]) - else: - return time.mktime(datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S').timetuple()) - except: - return None - - @staticmethod - def web2py_regexp(expression, item): - return re.compile(expression).search(item) is not None - - def __init__(self, db, uri, pool_size=0, folder=None, db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "sqlite" - self.uri = uri - self.adapter_args = adapter_args - if do_connect: self.find_driver(adapter_args) - self.pool_size = 0 - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - path_encoding = sys.getfilesystemencoding() \ - or locale.getdefaultlocale()[1] or 'utf8' - if uri.startswith('sqlite:memory'): - self.dbpath = ':memory:' - else: - self.dbpath = uri.split('://',1)[1] - if self.dbpath[0] != '/': - if PY2: - self.dbpath = pjoin( - self.folder.decode(path_encoding).encode('utf8'), self.dbpath) - else: - self.dbpath = pjoin(self.folder, self.dbpath) - if not 'check_same_thread' in driver_args: - driver_args['check_same_thread'] = False - if not 'detect_types' in driver_args and do_connect: - driver_args['detect_types'] = self.driver.PARSE_DECLTYPES - def connector(dbpath=self.dbpath, driver_args=driver_args): - return self.driver.Connection(dbpath, **driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.connection.create_function('web2py_extract', 2, - SQLiteAdapter.web2py_extract) - self.connection.create_function("REGEXP", 2, - SQLiteAdapter.web2py_regexp) - - if self.adapter_args.get('foreign_keys',True): - self.execute('PRAGMA foreign_keys=ON;') - - def _truncate(self, table, mode=''): - tablename = table._tablename - return ['DELETE FROM %s;' % tablename, - "DELETE FROM sqlite_sequence WHERE name='%s';" % tablename] - - def lastrowid(self, table): - return self.cursor.lastrowid - - def REGEXP(self,first,second): - return '(%s REGEXP %s)' % (self.expand(first), - self.expand(second,'string')) - - def delete(self, tablename, query): - # SQLite requires its own delete to handle CASCADE - db = self.db - table = db[tablename] - deleted = [x[table._id.name] for x in db(query).select(table._id)] - - counter = super(SQLiteAdapter, self).delete(tablename, query) - - if counter: - for field in table._referenced_by: - if field.type == 'reference '+ tablename \ - and field.ondelete == 'CASCADE': - db(field.belongs(deleted)).delete() - - return counter - - def select(self, query, fields, attributes): - """ - Simulate `SELECT ... FOR UPDATE` with `BEGIN IMMEDIATE TRANSACTION`. - Note that the entire database, rather than one record, is locked - (it will be locked eventually anyway by the following UPDATE). - """ - if attributes.get('for_update', False) and not 'cache' in attributes: - self.execute('BEGIN IMMEDIATE TRANSACTION;') - return super(SQLiteAdapter, self).select(query, fields, attributes) - - -SPATIALLIBS = { - 'Windows':'libspatialite', - 'Linux':'libspatialite.so', - 'Darwin':'libspatialite.dylib' - } - -class SpatiaLiteAdapter(SQLiteAdapter): - drivers = ('sqlite3','sqlite2') - - types = copy.copy(BaseAdapter.types) - types.update(geometry='GEOMETRY') - - def __init__(self, db, uri, pool_size=0, folder=None, db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, srid=4326, after_connection=None): - self.db = db - self.dbengine = "spatialite" - self.uri = uri - if do_connect: self.find_driver(adapter_args) - self.pool_size = 0 - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - self.srid = srid - path_encoding = sys.getfilesystemencoding() \ - or locale.getdefaultlocale()[1] or 'utf8' - if uri.startswith('spatialite:memory'): - self.dbpath = ':memory:' - else: - self.dbpath = uri.split('://',1)[1] - if self.dbpath[0] != '/': - self.dbpath = pjoin( - self.folder.decode(path_encoding).encode('utf8'), self.dbpath) - if not 'check_same_thread' in driver_args: - driver_args['check_same_thread'] = False - if not 'detect_types' in driver_args and do_connect: - driver_args['detect_types'] = self.driver.PARSE_DECLTYPES - def connector(dbpath=self.dbpath, driver_args=driver_args): - return self.driver.Connection(dbpath, **driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - self.connection.enable_load_extension(True) - # for Windows, rename libspatialite-2.dll to libspatialite.dll - # Linux uses libspatialite.so - # Mac OS X uses libspatialite.dylib - libspatialite = SPATIALLIBS[platform.system()] - self.execute(r'SELECT load_extension("%s");' % libspatialite) - - self.connection.create_function('web2py_extract', 2, - SQLiteAdapter.web2py_extract) - self.connection.create_function("REGEXP", 2, - SQLiteAdapter.web2py_regexp) - - # GIS functions - - def ST_ASGEOJSON(self, first, second): - return 'AsGeoJSON(%s,%s,%s)' %(self.expand(first), - second['precision'], second['options']) - - def ST_ASTEXT(self, first): - return 'AsText(%s)' %(self.expand(first)) - - def ST_CONTAINS(self, first, second): - return 'Contains(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_DISTANCE(self, first, second): - return 'Distance(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_EQUALS(self, first, second): - return 'Equals(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_INTERSECTS(self, first, second): - return 'Intersects(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_OVERLAPS(self, first, second): - return 'Overlaps(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_SIMPLIFY(self, first, second): - return 'Simplify(%s,%s)' %(self.expand(first), - self.expand(second, 'double')) - - def ST_TOUCHES(self, first, second): - return 'Touches(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def ST_WITHIN(self, first, second): - return 'Within(%s,%s)' %(self.expand(first), - self.expand(second, first.type)) - - def represent(self, obj, fieldtype): - field_is_type = fieldtype.startswith - if field_is_type('geo'): - srid = 4326 # Spatialite default srid for geometry - geotype, parms = fieldtype[:-1].split('(') - parms = parms.split(',') - if len(parms) >= 2: - schema, srid = parms[:2] -# if field_is_type('geometry'): - value = "ST_GeomFromText('%s',%s)" %(obj, srid) -# elif field_is_type('geography'): -# value = "ST_GeogFromText('SRID=%s;%s')" %(srid, obj) -# else: -# raise SyntaxError, 'Invalid field type %s' %fieldtype - return value - return BaseAdapter.represent(self, obj, fieldtype) - - -class JDBCSQLiteAdapter(SQLiteAdapter): - drivers = ('zxJDBC_sqlite',) - - def __init__(self, db, uri, pool_size=0, folder=None, db_codec='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "sqlite" - self.uri = uri - if do_connect: self.find_driver(adapter_args) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - path_encoding = sys.getfilesystemencoding() \ - or locale.getdefaultlocale()[1] or 'utf8' - if uri.startswith('sqlite:memory'): - self.dbpath = ':memory:' - else: - self.dbpath = uri.split('://',1)[1] - if self.dbpath[0] != '/': - self.dbpath = pjoin( - self.folder.decode(path_encoding).encode('utf8'), self.dbpath) - def connector(dbpath=self.dbpath,driver_args=driver_args): - return self.driver.connect( - self.driver.getConnection('jdbc:sqlite:'+dbpath), - **driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def after_connection(self): - # FIXME http://www.zentus.com/sqlitejdbc/custom_functions.html for UDFs - self.connection.create_function('web2py_extract', 2, - SQLiteAdapter.web2py_extract) - - def execute(self, a): - return self.log_execute(a) diff --git a/gluon/dal/adapters/teradata.py b/gluon/dal/adapters/teradata.py deleted file mode 100644 index f8b8f9e7..00000000 --- a/gluon/dal/adapters/teradata.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- - -from .._globals import IDENTITY -from ..connection import ConnectionPool -from .base import BaseAdapter - - -class TeradataAdapter(BaseAdapter): - drivers = ('pyodbc',) - - types = { - 'boolean': 'CHAR(1)', - 'string': 'VARCHAR(%(length)s)', - 'text': 'VARCHAR(2000)', - 'json': 'VARCHAR(4000)', - 'password': 'VARCHAR(%(length)s)', - 'blob': 'BLOB', - 'upload': 'VARCHAR(%(length)s)', - 'integer': 'INT', - 'bigint': 'BIGINT', - 'float': 'REAL', - 'double': 'DOUBLE', - 'decimal': 'NUMERIC(%(precision)s,%(scale)s)', - 'date': 'DATE', - 'time': 'TIME', - 'datetime': 'TIMESTAMP', - # Modified Constraint syntax for Teradata. - # Teradata does not support ON DELETE. - 'id': 'INT GENERATED ALWAYS AS IDENTITY', # Teradata Specific - 'reference': 'INT', - 'list:integer': 'VARCHAR(4000)', - 'list:string': 'VARCHAR(4000)', - 'list:reference': 'VARCHAR(4000)', - 'big-id': 'BIGINT GENERATED ALWAYS AS IDENTITY', # Teradata Specific - 'big-reference': 'BIGINT', - 'reference FK': ' REFERENCES %(foreign_key)s', - 'reference TFK': ' FOREIGN KEY (%(field_name)s) REFERENCES %(foreign_table)s (%(foreign_key)s)', - } - - def __init__(self,db,uri,pool_size=0,folder=None,db_codec ='UTF-8', - credential_decoder=IDENTITY, driver_args={}, - adapter_args={}, do_connect=True, after_connection=None): - self.db = db - self.dbengine = "teradata" - self.uri = uri - if do_connect: self.find_driver(adapter_args,uri) - self.pool_size = pool_size - self.folder = folder - self.db_codec = db_codec - self._after_connection = after_connection - self.find_or_make_work_folder() - ruri = uri.split('://', 1)[1] - def connector(cnxn=ruri,driver_args=driver_args): - return self.driver.connect(cnxn,**driver_args) - self.connector = connector - if do_connect: self.reconnect() - - def close(self,action='commit',really=True): - # Teradata does not implicitly close off the cursor - # leading to SQL_ACTIVE_STATEMENTS limit errors - self.cursor.close() - ConnectionPool.close(self, action, really) - - def LEFT_JOIN(self): - return 'LEFT OUTER JOIN' - - # Similar to MSSQL, Teradata can't specify a range (for Pageby) - def select_limitby(self, sql_s, sql_f, sql_t, sql_w, sql_o, limitby): - if limitby: - (lmin, lmax) = limitby - sql_s += ' TOP %i' % lmax - return 'SELECT %s %s FROM %s%s%s;' % (sql_s, sql_f, sql_t, sql_w, sql_o) - - def _truncate(self, table, mode=''): - tablename = table._tablename - return ['DELETE FROM %s ALL;' % (tablename)] diff --git a/gluon/dal/base.py b/gluon/dal/base.py deleted file mode 100644 index 4c9eeb3d..00000000 --- a/gluon/dal/base.py +++ /dev/null @@ -1,1095 +0,0 @@ -# -*- coding: utf-8 -*- - -""" -| This file is part of the web2py Web Framework -| Copyrighted by Massimo Di Pierro -| License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) -| - -Thanks to - - - Niall Sweeny for MS SQL support - - Marcel Leuthi for Oracle support - - Denes - - Chris Clark - - clach05 - - Denes Lengyel - -and many others who have contributed to current and previous versions - -This file contains the DAL support for many relational databases, including: - - - SQLite & SpatiaLite - - MySQL - - Postgres - - Firebird - - Oracle - - MS SQL - - DB2 - - Interbase - - Ingres - - Informix (9+ and SE) - - SapDB (experimental) - - Cubrid (experimental) - - CouchDB (experimental) - - MongoDB (in progress) - - Google:nosql - - Google:sql - - Teradata - - IMAP (experimental) - -Example of usage:: - - >>> # from dal import DAL, Field - - ### create DAL connection (and create DB if it doesn't exist) - >>> db = DAL(('sqlite://storage.sqlite','mysql://a:b@localhost/x'), - ... folder=None) - - ### define a table 'person' (create/alter as necessary) - >>> person = db.define_table('person',Field('name','string')) - - ### insert a record - >>> id = person.insert(name='James') - - ### retrieve it by id - >>> james = person(id) - - ### retrieve it by name - >>> james = person(name='James') - - ### retrieve it by arbitrary query - >>> query = (person.name=='James') & (person.name.startswith('J')) - >>> james = db(query).select(person.ALL)[0] - - ### update one record - >>> james.update_record(name='Jim') - - - ### update multiple records by query - >>> db(person.name.like('J%')).update(name='James') - 1 - - ### delete records by query - >>> db(person.name.lower() == 'jim').delete() - 0 - - ### retrieve multiple records (rows) - >>> people = db(person).select(orderby=person.name, - ... groupby=person.name, limitby=(0,100)) - - ### further filter them - >>> james = people.find(lambda row: row.name == 'James').first() - >>> print james.id, james.name - 1 James - - ### check aggregates - >>> counter = person.id.count() - >>> print db(person).select(counter).first()(counter) - 1 - - ### delete one record - >>> james.delete_record() - 1 - - ### delete (drop) entire database table - >>> person.drop() - - -Supported DAL URI strings:: - - 'sqlite://test.db' - 'spatialite://test.db' - 'sqlite:memory' - 'spatialite:memory' - 'jdbc:sqlite://test.db' - 'mysql://root:none@localhost/test' - 'postgres://mdipierro:password@localhost/test' - 'postgres:psycopg2://mdipierro:password@localhost/test' - 'postgres:pg8000://mdipierro:password@localhost/test' - 'jdbc:postgres://mdipierro:none@localhost/test' - 'mssql://web2py:none@A64X2/web2py_test' - 'mssql2://web2py:none@A64X2/web2py_test' # alternate mappings - 'mssql3://web2py:none@A64X2/web2py_test' # better pagination (requires >= 2005) - 'mssql4://web2py:none@A64X2/web2py_test' # best pagination (requires >= 2012) - 'oracle://username:password@database' - 'firebird://user:password@server:3050/database' - 'db2:ibm_db_dbi://DSN=dsn;UID=user;PWD=pass' - 'db2:pyodbc://driver=DB2;hostname=host;database=database;uid=user;pwd=password;port=port' - 'firebird://username:password@hostname/database' - 'firebird_embedded://username:password@c://path' - 'informix://user:password@server:3050/database' - 'informixu://user:password@server:3050/database' # unicode informix - 'ingres://database' # or use an ODBC connection string, e.g. 'ingres://dsn=dsn_name' - 'google:datastore' # for google app engine datastore - 'google:datastore+ndb' # for google app engine datastore + ndb - 'google:sql' # for google app engine with sql (mysql compatible) - 'teradata://DSN=dsn;UID=user;PWD=pass; DATABASE=database' # experimental - 'imap://user:password@server:port' # experimental - 'mongodb://user:password@server:port/database' # experimental - -For more info:: - - help(DAL) - help(Field) - -""" - -import threading -import socket -import urllib -import time -import copy -import traceback -import glob - -from ._compat import pickle, hashlib_md5, pjoin, ogetattr, osetattr, copyreg -from ._globals import GLOBAL_LOCKER, THREAD_LOCAL, LOGGER, DEFAULT -from ._load import have_serializers, serializers, is_jdbc, OrderedDict -from .helpers.classes import SQLCallableList -from .helpers.methods import hide_password, smart_query, sqlhtml_validators -from .helpers.regex import REGEX_PYTHON_KEYWORDS, REGEX_DBNAME, REGEX_SEARCH_PATTERN, REGEX_SQUARE_BRACKETS -from .objects import Table, Field, Row, Set -from .adapters import ADAPTERS -from .adapters.base import BaseAdapter - - -TABLE_ARGS = set( - ('migrate','primarykey','fake_migrate','format','redefine', - 'singular','plural','trigger_name','sequence_name','fields', - 'common_filter','polymodel','table_class','on_define','rname')) - - -class DAL(object): - - """ - An instance of this class represents a database connection - - Args: - uri(str): contains information for connecting to a database. - Defaults to `'sqlite://dummy.db'` - - Note: - experimental: you can specify a dictionary as uri - parameter i.e. with:: - - db = DAL({"uri": "sqlite://storage.sqlite", - "tables": {...}, ...}) - - for an example of dict input you can check the output - of the scaffolding db model with - - db.as_dict() - - Note that for compatibility with Python older than - version 2.6.5 you should cast your dict input keys - to str due to a syntax limitation on kwarg names. - for proper DAL dictionary input you can use one of:: - - obj = serializers.cast_keys(dict, [encoding="utf-8"]) - #or else (for parsing json input) - obj = serializers.loads_json(data, unicode_keys=False) - - pool_size: How many open connections to make to the database object. - folder: where .table files will be created. Automatically set within - web2py. Use an explicit path when using DAL outside web2py - db_codec: string encoding of the database (default: 'UTF-8') - table_hash: database identifier with .tables. If your connection hash - change you can still using old .tables if they have db_hash - as prefix - check_reserved: list of adapters to check tablenames and column names - against sql/nosql reserved keywords. Defaults to `None` - - - 'common' List of sql keywords that are common to all database - types such as "SELECT, INSERT". (recommended) - - 'all' Checks against all known SQL keywords - - ''' Checks against the specific adapters list of - keywords - - '_nonreserved' Checks against the specific adapters - list of nonreserved keywords. (if available) - - migrate: sets default migrate behavior for all tables - fake_migrate: sets default fake_migrate behavior for all tables - migrate_enabled: If set to False disables ALL migrations - fake_migrate_all: If set to True fake migrates ALL tables - attempts: Number of times to attempt connecting - auto_import: If set to True, tries import automatically table - definitions from the databases folder (works only for simple models) - bigint_id: If set, turn on bigint instead of int for id and reference - fields - lazy_tables: delaya table definition until table access - after_connection: can a callable that will be executed after the - connection - - Example: - Use as:: - - db = DAL('sqlite://test.db') - - or:: - - db = DAL(**{"uri": ..., "tables": [...]...}) # experimental - - db.define_table('tablename', Field('fieldname1'), - Field('fieldname2')) - - - """ - Table = Table - - def __new__(cls, uri='sqlite://dummy.db', *args, **kwargs): - if not hasattr(THREAD_LOCAL,'db_instances'): - THREAD_LOCAL.db_instances = {} - if not hasattr(THREAD_LOCAL,'db_instances_zombie'): - THREAD_LOCAL.db_instances_zombie = {} - if uri == '': - db_uid = kwargs['db_uid'] # a zombie must have a db_uid! - if db_uid in THREAD_LOCAL.db_instances: - db_group = THREAD_LOCAL.db_instances[db_uid] - db = db_group[-1] - elif db_uid in THREAD_LOCAL.db_instances_zombie: - db = THREAD_LOCAL.db_instances_zombie[db_uid] - else: - db = super(DAL, cls).__new__(cls) - THREAD_LOCAL.db_instances_zombie[db_uid] = db - else: - db_uid = kwargs.get('db_uid',hashlib_md5(repr(uri)).hexdigest()) - if db_uid in THREAD_LOCAL.db_instances_zombie: - db = THREAD_LOCAL.db_instances_zombie[db_uid] - del THREAD_LOCAL.db_instances_zombie[db_uid] - else: - db = super(DAL, cls).__new__(cls) - db_group = THREAD_LOCAL.db_instances.get(db_uid,[]) - db_group.append(db) - THREAD_LOCAL.db_instances[db_uid] = db_group - db._db_uid = db_uid - return db - - @staticmethod - def set_folder(folder): - # ## this allows gluon to set a folder for this thread - # ## <<<<<<<<< Should go away as new DAL replaces old sql.py - BaseAdapter.set_folder(folder) - - @staticmethod - def get_instances(): - """ - Returns a dictionary with uri as key with timings and defined tables:: - - {'sqlite://storage.sqlite': { - 'dbstats': [(select auth_user.email from auth_user, 0.02009)], - 'dbtables': { - 'defined': ['auth_cas', 'auth_event', 'auth_group', - 'auth_membership', 'auth_permission', 'auth_user'], - 'lazy': '[]' - } - } - } - - """ - dbs = getattr(THREAD_LOCAL,'db_instances',{}).items() - infos = {} - for db_uid, db_group in dbs: - for db in db_group: - if not db._uri: - continue - k = hide_password(db._adapter.uri) - infos[k] = dict( - dbstats = [(row[0], row[1]) for row in db._timings], - dbtables = {'defined': sorted( - list(set(db.tables)-set(db._LAZY_TABLES.keys()))), - 'lazy': sorted(db._LAZY_TABLES.keys())}) - return infos - - @staticmethod - def distributed_transaction_begin(*instances): - if not instances: - return - thread_key = '%s.%s' % (socket.gethostname(), threading.currentThread()) - keys = ['%s.%i' % (thread_key, i) for (i,db) in instances] - instances = enumerate(instances) - for (i, db) in instances: - if not db._adapter.support_distributed_transaction(): - raise SyntaxError( - 'distributed transaction not suported by %s' % db._dbname) - for (i, db) in instances: - db._adapter.distributed_transaction_begin(keys[i]) - - @staticmethod - def distributed_transaction_commit(*instances): - if not instances: - return - instances = enumerate(instances) - thread_key = '%s.%s' % (socket.gethostname(), threading.currentThread()) - keys = ['%s.%i' % (thread_key, i) for (i,db) in instances] - for (i, db) in instances: - if not db._adapter.support_distributed_transaction(): - raise SyntaxError( - 'distributed transaction not suported by %s' % db._dbanme) - try: - for (i, db) in instances: - db._adapter.prepare(keys[i]) - except: - for (i, db) in instances: - db._adapter.rollback_prepared(keys[i]) - raise RuntimeError('failure to commit distributed transaction') - else: - for (i, db) in instances: - db._adapter.commit_prepared(keys[i]) - return - - def __init__(self, uri='sqlite://dummy.db', - pool_size=0, folder=None, - db_codec='UTF-8', check_reserved=None, - migrate=True, fake_migrate=False, - migrate_enabled=True, fake_migrate_all=False, - decode_credentials=False, driver_args=None, - adapter_args=None, attempts=5, auto_import=False, - bigint_id=False, debug=False, lazy_tables=False, - db_uid=None, do_connect=True, - after_connection=None, tables=None, ignore_field_case=True, - entity_quoting=False, table_hash=None): - - if uri == '' and db_uid is not None: return - if not decode_credentials: - credential_decoder = lambda cred: cred - else: - credential_decoder = lambda cred: urllib.unquote(cred) - self._folder = folder - if folder: - self.set_folder(folder) - self._uri = uri - self._pool_size = pool_size - self._db_codec = db_codec - self._lastsql = '' - self._timings = [] - self._pending_references = {} - self._request_tenant = 'request_tenant' - self._common_fields = [] - self._referee_name = '%(table)s' - self._bigint_id = bigint_id - self._debug = debug - self._migrated = [] - self._LAZY_TABLES = {} - self._lazy_tables = lazy_tables - self._tables = SQLCallableList() - self._driver_args = driver_args - self._adapter_args = adapter_args - self._check_reserved = check_reserved - self._decode_credentials = decode_credentials - self._attempts = attempts - self._do_connect = do_connect - self._ignore_field_case = ignore_field_case - - if not str(attempts).isdigit() or attempts < 0: - attempts = 5 - if uri: - uris = isinstance(uri,(list,tuple)) and uri or [uri] - error = '' - connected = False - for k in range(attempts): - for uri in uris: - try: - if is_jdbc and not uri.startswith('jdbc:'): - uri = 'jdbc:'+uri - self._dbname = REGEX_DBNAME.match(uri).group() - if not self._dbname in ADAPTERS: - raise SyntaxError("Error in URI '%s' or database not supported" % self._dbname) - # notice that driver args or {} else driver_args - # defaults to {} global, not correct - kwargs = dict(db=self,uri=uri, - pool_size=pool_size, - folder=folder, - db_codec=db_codec, - credential_decoder=credential_decoder, - driver_args=driver_args or {}, - adapter_args=adapter_args or {}, - do_connect=do_connect, - after_connection=after_connection, - entity_quoting=entity_quoting) - self._adapter = ADAPTERS[self._dbname](**kwargs) - types = ADAPTERS[self._dbname].types - # copy so multiple DAL() possible - self._adapter.types = copy.copy(types) - self._adapter.build_parsemap() - self._adapter.ignore_field_case = ignore_field_case - if bigint_id: - if 'big-id' in types and 'reference' in types: - self._adapter.types['id'] = types['big-id'] - self._adapter.types['reference'] = types['big-reference'] - connected = True - break - except SyntaxError: - raise - except Exception: - tb = traceback.format_exc() - LOGGER.debug('DEBUG: connect attempt %i, connection error:\n%s' % (k, tb)) - if connected: - break - else: - time.sleep(1) - if not connected: - raise RuntimeError("Failure to connect, tried %d times:\n%s" % (attempts, tb)) - else: - self._adapter = BaseAdapter(db=self,pool_size=0, - uri='None',folder=folder, - db_codec=db_codec, after_connection=after_connection, - entity_quoting=entity_quoting) - migrate = fake_migrate = False - adapter = self._adapter - self._uri_hash = table_hash or hashlib_md5(adapter.uri).hexdigest() - self.check_reserved = check_reserved - if self.check_reserved: - from reserved_sql_keywords import ADAPTERS as RSK - self.RSK = RSK - self._migrate = migrate - self._fake_migrate = fake_migrate - self._migrate_enabled = migrate_enabled - self._fake_migrate_all = fake_migrate_all - if auto_import or tables: - self.import_table_definitions(adapter.folder, - tables=tables) - - @property - def tables(self): - return self._tables - - def import_table_definitions(self, path, migrate=False, - fake_migrate=False, tables=None): - if tables: - for table in tables: - self.define_table(**table) - else: - pattern = pjoin(path,self._uri_hash+'_*.table') - for filename in glob.glob(pattern): - tfile = self._adapter.file_open(filename, 'r') - try: - sql_fields = pickle.load(tfile) - name = filename[len(pattern)-7:-6] - mf = [(value['sortable'], - Field(key, - type=value['type'], - length=value.get('length',None), - notnull=value.get('notnull',False), - unique=value.get('unique',False))) \ - for key, value in sql_fields.iteritems()] - mf.sort(lambda a,b: cmp(a[0],b[0])) - self.define_table(name,*[item[1] for item in mf], - **dict(migrate=migrate, - fake_migrate=fake_migrate)) - finally: - self._adapter.file_close(tfile) - - def check_reserved_keyword(self, name): - """ - Validates `name` against SQL keywords - Uses self.check_reserve which is a list of operators to use. - """ - for backend in self.check_reserved: - if name.upper() in self.RSK[backend]: - raise SyntaxError( - 'invalid table/column name "%s" is a "%s" reserved SQL/NOSQL keyword' % (name, backend.upper())) - - def parse_as_rest(self,patterns,args,vars,queries=None,nested_select=True): - """ - Example: - Use as:: - - db.define_table('person',Field('name'),Field('info')) - db.define_table('pet', - Field('ownedby',db.person), - Field('name'),Field('info') - ) - - @request.restful() - def index(): - def GET(*args,**vars): - patterns = [ - "/friends[person]", - "/{person.name}/:field", - "/{person.name}/pets[pet.ownedby]", - "/{person.name}/pets[pet.ownedby]/{pet.name}", - "/{person.name}/pets[pet.ownedby]/{pet.name}/:field", - ("/dogs[pet]", db.pet.info=='dog'), - ("/dogs[pet]/{pet.name.startswith}", db.pet.info=='dog'), - ] - parser = db.parse_as_rest(patterns,args,vars) - if parser.status == 200: - return dict(content=parser.response) - else: - raise HTTP(parser.status,parser.error) - - def POST(table_name,**vars): - if table_name == 'person': - return db.person.validate_and_insert(**vars) - elif table_name == 'pet': - return db.pet.validate_and_insert(**vars) - else: - raise HTTP(400) - return locals() - """ - - db = self - re1 = REGEX_SEARCH_PATTERN - re2 = REGEX_SQUARE_BRACKETS - - def auto_table(table,base='',depth=0): - patterns = [] - for field in db[table].fields: - if base: - tag = '%s/%s' % (base,field.replace('_','-')) - else: - tag = '/%s/%s' % (table.replace('_','-'),field.replace('_','-')) - f = db[table][field] - if not f.readable: continue - if f.type=='id' or 'slug' in field or f.type.startswith('reference'): - tag += '/{%s.%s}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - elif f.type.startswith('boolean'): - tag += '/{%s.%s}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - elif f.type in ('float','double','integer','bigint'): - tag += '/{%s.%s.ge}/{%s.%s.lt}' % (table,field,table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - elif f.type.startswith('list:'): - tag += '/{%s.%s.contains}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - elif f.type in ('date','datetime'): - tag+= '/{%s.%s.year}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - tag+='/{%s.%s.month}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - tag+='/{%s.%s.day}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - if f.type in ('datetime','time'): - tag+= '/{%s.%s.hour}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - tag+='/{%s.%s.minute}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - tag+='/{%s.%s.second}' % (table,field) - patterns.append(tag) - patterns.append(tag+'/:field') - if depth>0: - for f in db[table]._referenced_by: - tag+='/%s[%s.%s]' % (table,f.tablename,f.name) - patterns.append(tag) - patterns += auto_table(table,base=tag,depth=depth-1) - return patterns - - if patterns == 'auto': - patterns=[] - for table in db.tables: - if not table.startswith('auth_'): - patterns.append('/%s[%s]' % (table,table)) - patterns += auto_table(table,base='',depth=1) - else: - i = 0 - while i2: - pattern, basequery, exposedfields = pattern[0:3] - otable=table=None - if not isinstance(queries,dict): - dbset=db(queries) - if basequery is not None: - dbset = dbset(basequery) - i=0 - tags = pattern[1:].split('/') - if len(tags)!=len(args): - continue - for tag in tags: - if re1.match(tag): - # print 're1:'+tag - tokens = tag[1:-1].split('.') - table, field = tokens[0], tokens[1] - if not otable or table == otable: - if len(tokens)==2 or tokens[2]=='eq': - query = db[table][field]==args[i] - elif tokens[2]=='ne': - query = db[table][field]!=args[i] - elif tokens[2]=='lt': - query = db[table][field]args[i] - elif tokens[2]=='ge': - query = db[table][field]>=args[i] - elif tokens[2]=='le': - query = db[table][field]<=args[i] - elif tokens[2]=='year': - query = db[table][field].year()==args[i] - elif tokens[2]=='month': - query = db[table][field].month()==args[i] - elif tokens[2]=='day': - query = db[table][field].day()==args[i] - elif tokens[2]=='hour': - query = db[table][field].hour()==args[i] - elif tokens[2]=='minute': - query = db[table][field].minutes()==args[i] - elif tokens[2]=='second': - query = db[table][field].seconds()==args[i] - elif tokens[2]=='startswith': - query = db[table][field].startswith(args[i]) - elif tokens[2]=='contains': - query = db[table][field].contains(args[i]) - else: - raise RuntimeError("invalid pattern: %s" % pattern) - if len(tokens)==4 and tokens[3]=='not': - query = ~query - elif len(tokens)>=4: - raise RuntimeError("invalid pattern: %s" % pattern) - if not otable and isinstance(queries,dict): - dbset = db(queries[table]) - if basequery is not None: - dbset = dbset(basequery) - dbset=dbset(query) - else: - raise RuntimeError("missing relation in pattern: %s" % pattern) - elif re2.match(tag) and args[i]==tag[:tag.find('[')]: - ref = tag[tag.find('[')+1:-1] - if '.' in ref and otable: - table,field = ref.split('.') - selfld = '_id' - if db[table][field].type.startswith('reference '): - refs = [ x.name for x in db[otable] if x.type == db[table][field].type ] - else: - refs = [ x.name for x in db[table]._referenced_by if x.tablename==otable ] - if refs: - selfld = refs[0] - if nested_select: - try: - dbset=db(db[table][field].belongs(dbset._select(db[otable][selfld]))) - except ValueError: - return Row({'status':400,'pattern':pattern, - 'error':'invalid path','response':None}) - else: - items = [item.id for item in dbset.select(db[otable][selfld])] - dbset=db(db[table][field].belongs(items)) - else: - table = ref - if not otable and isinstance(queries,dict): - dbset = db(queries[table]) - dbset=dbset(db[table]) - elif tag==':field' and table: - # print 're3:'+tag - field = args[i] - if not field in db[table]: break - # hand-built patterns should respect .readable=False as well - if not db[table][field].readable: - return Row({'status':418,'pattern':pattern, - 'error':'I\'m a teapot','response':None}) - try: - distinct = vars.get('distinct', False) == 'True' - offset = long(vars.get('offset',None) or 0) - limits = (offset,long(vars.get('limit',None) or 1000)+offset) - except ValueError: - return Row({'status':400,'error':'invalid limits','response':None}) - items = dbset.select(db[table][field], distinct=distinct, limitby=limits) - if items: - return Row({'status':200,'response':items, - 'pattern':pattern}) - else: - return Row({'status':404,'pattern':pattern, - 'error':'no record found','response':None}) - elif tag != args[i]: - break - otable = table - i += 1 - if i == len(tags) and table: - if hasattr(db[table], '_id'): - ofields = vars.get('order', db[table]._id.name).split('|') - else: - ofields = vars.get('order', db[table]._primarykey[0]).split('|') - try: - orderby = [db[table][f] if not f.startswith('~') else ~db[table][f[1:]] for f in ofields] - except (KeyError, AttributeError): - return Row({'status':400,'error':'invalid orderby','response':None}) - if exposedfields: - fields = [field for field in db[table] if str(field).split('.')[-1] in exposedfields and field.readable] - else: - fields = [field for field in db[table] if field.readable] - count = dbset.count() - try: - offset = long(vars.get('offset',None) or 0) - limits = (offset,long(vars.get('limit',None) or 1000)+offset) - except ValueError: - return Row({'status':400,'error':'invalid limits','response':None}) - #if count > limits[1]-limits[0]: - # return Row({'status':400,'error':'too many records','response':None}) - try: - response = dbset.select(limitby=limits,orderby=orderby,*fields) - except ValueError: - return Row({'status':400,'pattern':pattern, - 'error':'invalid path','response':None}) - return Row({'status':200,'response':response, - 'pattern':pattern,'count':count}) - return Row({'status':400,'error':'no matching pattern','response':None}) - - def define_table( - self, - tablename, - *fields, - **args - ): - if not fields and 'fields' in args: - fields = args.get('fields',()) - if not isinstance(tablename, str): - if isinstance(tablename, unicode): - try: - tablename = str(tablename) - except UnicodeEncodeError: - raise SyntaxError("invalid unicode table name") - else: - raise SyntaxError("missing table name") - elif hasattr(self,tablename) or tablename in self.tables: - if not args.get('redefine',False): - raise SyntaxError('table already defined: %s' % tablename) - elif tablename.startswith('_') or hasattr(self,tablename) or \ - REGEX_PYTHON_KEYWORDS.match(tablename): - raise SyntaxError('invalid table name: %s' % tablename) - elif self.check_reserved: - self.check_reserved_keyword(tablename) - else: - invalid_args = set(args)-TABLE_ARGS - if invalid_args: - raise SyntaxError('invalid table "%s" attributes: %s' \ - % (tablename,invalid_args)) - if self._lazy_tables and not tablename in self._LAZY_TABLES: - self._LAZY_TABLES[tablename] = (tablename,fields,args) - table = None - else: - table = self.lazy_define_table(tablename,*fields,**args) - if not tablename in self.tables: - self.tables.append(tablename) - return table - - def lazy_define_table( - self, - tablename, - *fields, - **args - ): - args_get = args.get - common_fields = self._common_fields - if common_fields: - fields = list(fields) + list(common_fields) - - table_class = args_get('table_class',Table) - table = table_class(self, tablename, *fields, **args) - table._actual = True - self[tablename] = table - # must follow above line to handle self references - table._create_references() - for field in table: - if field.requires == DEFAULT: - field.requires = sqlhtml_validators(field) - - migrate = self._migrate_enabled and args_get('migrate',self._migrate) - if migrate and not self._uri in (None,'None') \ - or self._adapter.dbengine=='google:datastore': - fake_migrate = self._fake_migrate_all or \ - args_get('fake_migrate',self._fake_migrate) - polymodel = args_get('polymodel',None) - try: - GLOBAL_LOCKER.acquire() - self._lastsql = self._adapter.create_table( - table,migrate=migrate, - fake_migrate=fake_migrate, - polymodel=polymodel) - finally: - GLOBAL_LOCKER.release() - else: - table._dbt = None - on_define = args_get('on_define',None) - if on_define: on_define(table) - return table - - def as_dict(self, flat=False, sanitize=True): - db_uid = uri = None - if not sanitize: - uri, db_uid = (self._uri, self._db_uid) - db_as_dict = dict(tables=[], uri=uri, db_uid=db_uid, - **dict([(k, getattr(self, "_" + k, None)) - for k in 'pool_size','folder','db_codec', - 'check_reserved','migrate','fake_migrate', - 'migrate_enabled','fake_migrate_all', - 'decode_credentials','driver_args', - 'adapter_args', 'attempts', - 'bigint_id','debug','lazy_tables', - 'do_connect'])) - for table in self: - db_as_dict["tables"].append(table.as_dict(flat=flat, - sanitize=sanitize)) - return db_as_dict - - def as_xml(self, sanitize=True): - if not have_serializers: - raise ImportError("No xml serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return serializers.xml(d) - - def as_json(self, sanitize=True): - if not have_serializers: - raise ImportError("No json serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return serializers.json(d) - - def as_yaml(self, sanitize=True): - if not have_serializers: - raise ImportError("No YAML serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return serializers.yaml(d) - - def __contains__(self, tablename): - try: - return tablename in self.tables - except AttributeError: - # The instance has no .tables attribute yet - return False - - has_key = __contains__ - - def get(self,key,default=None): - return self.__dict__.get(key,default) - - def __iter__(self): - for tablename in self.tables: - yield self[tablename] - - def __getitem__(self, key): - return self.__getattr__(str(key)) - - def __getattr__(self, key): - if ogetattr(self,'_lazy_tables') and \ - key in ogetattr(self,'_LAZY_TABLES'): - tablename, fields, args = self._LAZY_TABLES.pop(key) - return self.lazy_define_table(tablename,*fields,**args) - return ogetattr(self, key) - - def __setitem__(self, key, value): - osetattr(self, str(key), value) - - def __setattr__(self, key, value): - if key[:1]!='_' and key in self: - raise SyntaxError( - 'Object %s exists and cannot be redefined' % key) - osetattr(self,key,value) - - __delitem__ = object.__delattr__ - - def __repr__(self): - if hasattr(self,'_uri'): - return '' % hide_password(self._adapter.uri) - else: - return '' % self._db_uid - - def smart_query(self,fields,text): - return Set(self, smart_query(fields,text)) - - def __call__(self, query=None, ignore_common_filters=None): - if isinstance(query,Table): - query = self._adapter.id_query(query) - elif isinstance(query,Field): - query = query!=None - elif isinstance(query, dict): - icf = query.get("ignore_common_filters") - if icf: ignore_common_filters = icf - return Set(self, query, ignore_common_filters=ignore_common_filters) - - def commit(self): - self._adapter.commit() - - def rollback(self): - self._adapter.rollback() - - def close(self): - self._adapter.close() - if self._db_uid in THREAD_LOCAL.db_instances: - db_group = THREAD_LOCAL.db_instances[self._db_uid] - db_group.remove(self) - if not db_group: - del THREAD_LOCAL.db_instances[self._db_uid] - - def executesql(self, query, placeholders=None, as_dict=False, - fields=None, colnames=None, as_ordered_dict=False): - """ - Executes an arbitrary query - - Args: - query (str): the query to submit to the backend - placeholders: is optional and will always be None. - If using raw SQL with placeholders, placeholders may be - a sequence of values to be substituted in - or, (if supported by the DB driver), a dictionary with keys - matching named placeholders in your SQL. - as_dict: will always be None when using DAL. - If using raw SQL can be set to True and the results cursor - returned by the DB driver will be converted to a sequence of - dictionaries keyed with the db field names. Results returned - with as_dict=True are the same as those returned when applying - .to_list() to a DAL query. If "as_ordered_dict"=True the - behaviour is the same as when "as_dict"=True with the keys - (field names) guaranteed to be in the same order as returned - by the select name executed on the database. - fields: list of DAL Fields that match the fields returned from the - DB. The Field objects should be part of one or more Table - objects defined on the DAL object. The "fields" list can include - one or more DAL Table objects in addition to or instead of - including Field objects, or it can be just a single table - (not in a list). In that case, the Field objects will be - extracted from the table(s). - - Note: - if either `fields` or `colnames` is provided, the results - will be converted to a DAL `Rows` object using the - `db._adapter.parse()` method - colnames: list of field names in tablename.fieldname format - - Note: - It is also possible to specify both "fields" and the associated - "colnames". In that case, "fields" can also include DAL Expression - objects in addition to Field objects. For Field objects in "fields", - the associated "colnames" must still be in tablename.fieldname - format. For Expression objects in "fields", the associated - "colnames" can be any arbitrary labels. - - DAL Table objects referred to by "fields" or "colnames" can be dummy - tables and do not have to represent any real tables in the database. - Also, note that the "fields" and "colnames" must be in the - same order as the fields in the results cursor returned from the DB. - - """ - adapter = self._adapter - if placeholders: - adapter.execute(query, placeholders) - else: - adapter.execute(query) - if as_dict or as_ordered_dict: - if not hasattr(adapter.cursor,'description'): - raise RuntimeError("database does not support executesql(...,as_dict=True)") - # Non-DAL legacy db query, converts cursor results to dict. - # sequence of 7-item sequences. each sequence tells about a column. - # first item is always the field name according to Python Database API specs - columns = adapter.cursor.description - # reduce the column info down to just the field names - fields = colnames or [f[0] for f in columns] - if len(fields) != len(set(fields)): - raise RuntimeError("Result set includes duplicate column names. Specify unique column names using the 'colnames' argument") - - # will hold our finished resultset in a list - data = adapter._fetchall() - # convert the list for each row into a dictionary so it's - # easier to work with. row['field_name'] rather than row[0] - if as_ordered_dict: - _dict = OrderedDict - else: - _dict = dict - return [_dict(zip(fields,row)) for row in data] - try: - data = adapter._fetchall() - except: - return None - if fields or colnames: - fields = [] if fields is None else fields - if not isinstance(fields, list): - fields = [fields] - extracted_fields = [] - for field in fields: - if isinstance(field, Table): - extracted_fields.extend([f for f in field]) - else: - extracted_fields.append(field) - if not colnames: - colnames = ['%s.%s' % (f.tablename, f.name) - for f in extracted_fields] - data = adapter.parse( - data, fields=extracted_fields, colnames=colnames) - return data - - def _remove_references_to(self, thistable): - for table in self: - table._referenced_by = [field for field in table._referenced_by - if not field.table==thistable] - - def export_to_csv_file(self, ofile, *args, **kwargs): - step = long(kwargs.get('max_fetch_rows,',500)) - write_colnames = kwargs['write_colnames'] = \ - kwargs.get("write_colnames", True) - for table in self.tables: - ofile.write('TABLE %s\r\n' % table) - query = self._adapter.id_query(self[table]) - nrows = self(query).count() - kwargs['write_colnames'] = write_colnames - for k in range(0,nrows,step): - self(query).select(limitby=(k,k+step)).export_to_csv_file( - ofile, *args, **kwargs) - kwargs['write_colnames'] = False - ofile.write('\r\n\r\n') - ofile.write('END') - - def import_from_csv_file(self, ifile, id_map=None, null='', - unique='uuid', map_tablenames=None, - ignore_missing_tables=False, - *args, **kwargs): - #if id_map is None: id_map={} - id_offset = {} # only used if id_map is None - map_tablenames = map_tablenames or {} - for line in ifile: - line = line.strip() - if not line: - continue - elif line == 'END': - return - elif not line.startswith('TABLE ') or \ - not line[6:] in self.tables: - raise SyntaxError('invalid file format') - else: - tablename = line[6:] - tablename = map_tablenames.get(tablename,tablename) - if tablename is not None and tablename in self.tables: - self[tablename].import_from_csv_file( - ifile, id_map, null, unique, id_offset, - *args, **kwargs) - elif tablename is None or ignore_missing_tables: - # skip all non-empty lines - for line in ifile: - if not line.strip(): - break - else: - raise RuntimeError("Unable to import table that does not exist.\nTry db.import_from_csv_file(..., map_tablenames={'table':'othertable'},ignore_missing_tables=True)") - - -def DAL_unpickler(db_uid): - return DAL('', db_uid=db_uid) - - -def DAL_pickler(db): - return DAL_unpickler, (db._db_uid,) - -copyreg.pickle(DAL, DAL_pickler, DAL_unpickler) diff --git a/gluon/dal/connection.py b/gluon/dal/connection.py deleted file mode 100644 index 67643f72..00000000 --- a/gluon/dal/connection.py +++ /dev/null @@ -1,116 +0,0 @@ -# -*- coding: utf-8 -*- -import os - -from ._compat import exists -from ._globals import GLOBAL_LOCKER, THREAD_LOCAL -from .helpers.classes import UseDatabaseStoredFile - -class ConnectionPool(object): - - POOLS = {} - check_active_connection = True - - @staticmethod - def set_folder(folder): - THREAD_LOCAL.folder = folder - - # ## this allows gluon to commit/rollback all dbs in this thread - - def close(self,action='commit',really=True): - if action: - if callable(action): - action(self) - else: - getattr(self, action)() - # ## if you want pools, recycle this connection - if self.pool_size: - GLOBAL_LOCKER.acquire() - pool = ConnectionPool.POOLS[self.uri] - if len(pool) < self.pool_size: - pool.append(self.connection) - really = False - GLOBAL_LOCKER.release() - if really: - self.close_connection() - self.connection = None - - @staticmethod - def close_all_instances(action): - """ to close cleanly databases in a multithreaded environment """ - dbs = getattr(THREAD_LOCAL,'db_instances',{}).items() - for db_uid, db_group in dbs: - for db in db_group: - if hasattr(db,'_adapter'): - db._adapter.close(action) - getattr(THREAD_LOCAL,'db_instances',{}).clear() - getattr(THREAD_LOCAL,'db_instances_zombie',{}).clear() - if callable(action): - action(None) - return - - def find_or_make_work_folder(self): - #this actually does not make the folder. it has to be there - self.folder = getattr(THREAD_LOCAL,'folder','') - - if (os.path.isabs(self.folder) and - isinstance(self, UseDatabaseStoredFile) and - self.folder.startswith(os.getcwd())): - self.folder = os.path.relpath(self.folder, os.getcwd()) - - # Creating the folder if it does not exist - if False and self.folder and not exists(self.folder): - os.mkdir(self.folder) - - def after_connection_hook(self): - """Hook for the after_connection parameter""" - if callable(self._after_connection): - self._after_connection(self) - self.after_connection() - - def after_connection(self): - #this it is supposed to be overloaded by adapters - pass - - def reconnect(self, f=None, cursor=True): - """ - Defines: `self.connection` and `self.cursor` - (if cursor is True) - if `self.pool_size>0` it will try pull the connection from the pool - if the connection is not active (closed by db server) it will loop - if not `self.pool_size` or no active connections in pool makes a new one - """ - if getattr(self,'connection', None) is not None: - return - if f is None: - f = self.connector - - # if not hasattr(self, "driver") or self.driver is None: - # LOGGER.debug("Skipping connection since there's no driver") - # return - - if not self.pool_size: - self.connection = f() - self.cursor = cursor and self.connection.cursor() - else: - uri = self.uri - POOLS = ConnectionPool.POOLS - while True: - GLOBAL_LOCKER.acquire() - if not uri in POOLS: - POOLS[uri] = [] - if POOLS[uri]: - self.connection = POOLS[uri].pop() - GLOBAL_LOCKER.release() - self.cursor = cursor and self.connection.cursor() - try: - if self.cursor and self.check_active_connection: - self.execute('SELECT 1;') - break - except: - pass - else: - GLOBAL_LOCKER.release() - self.connection = f() - self.cursor = cursor and self.connection.cursor() - break - self.after_connection_hook() diff --git a/gluon/dal/helpers/__init__.py b/gluon/dal/helpers/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/gluon/dal/helpers/classes.py b/gluon/dal/helpers/classes.py deleted file mode 100644 index 51afaa40..00000000 --- a/gluon/dal/helpers/classes.py +++ /dev/null @@ -1,298 +0,0 @@ -# -*- coding: utf-8 -*- -import copy -import marshal -import struct -import traceback - -from .._compat import exists, copyreg -from .._globals import LOGGER - - -class Reference(long): - - def __allocate(self): - if not self._record: - self._record = self._table[long(self)] - if not self._record: - raise RuntimeError( - "Using a recursive select but encountered a broken reference: %s %d"%(self._table, long(self))) - - def __getattr__(self, key): - if key == 'id': - return long(self) - if key in self._table: - self.__allocate() - if self._record: - return self._record.get(key,None) # to deal with case self.update_record() - else: - return None - - def get(self, key, default=None): - return self.__getattr__(key, default) - - def __setattr__(self, key, value): - if key.startswith('_'): - long.__setattr__(self, key, value) - return - self.__allocate() - self._record[key] = value - - def __getitem__(self, key): - if key == 'id': - return long(self) - self.__allocate() - return self._record.get(key, None) - - def __setitem__(self,key,value): - self.__allocate() - self._record[key] = value - -def Reference_unpickler(data): - return marshal.loads(data) - -def Reference_pickler(data): - try: - marshal_dump = marshal.dumps(long(data)) - except AttributeError: - marshal_dump = 'i%s' % struct.pack('0: - data, self.p = self.data[self.p:i], i - else: - data, self.p = self.data[self.p:], len(self.data) - return data - - def write(self,data): - self.data += data - - def close_connection(self): - if self.db is not None: - self.db.executesql( - "DELETE FROM web2py_filesystem WHERE path='%s'" % self.filename) - query = "INSERT INTO web2py_filesystem(path,content) VALUES ('%s','%s')"\ - % (self.filename, self.data.replace("'","''")) - self.db.executesql(query) - self.db.commit() - self.db = None - - def close(self): - self.close_connection() - - @staticmethod - def exists(db, filename): - if exists(filename): - return True - - DatabaseStoredFile.try_create_web2py_filesystem(db) - - query = "SELECT path FROM web2py_filesystem WHERE path='%s'" % filename - try: - if db.executesql(query): - return True - except Exception, e: - if not (db._adapter.isOperationalError(e) or - db._adapter.isProgrammingError(e)): - raise - # no web2py_filesystem found? - tb = traceback.format_exc() - LOGGER.error("Could not retrieve %s\n%s" % (filename, tb)) - return False - - -class UseDatabaseStoredFile: - - def file_exists(self, filename): - return DatabaseStoredFile.exists(self.db,filename) - - def file_open(self, filename, mode='rb', lock=True): - return DatabaseStoredFile(self.db,filename,mode) - - def file_close(self, fileobj): - fileobj.close_connection() - - def file_delete(self,filename): - query = "DELETE FROM web2py_filesystem WHERE path='%s'" % filename - self.db.executesql(query) - self.db.commit() - diff --git a/gluon/dal/helpers/methods.py b/gluon/dal/helpers/methods.py deleted file mode 100644 index f1ada034..00000000 --- a/gluon/dal/helpers/methods.py +++ /dev/null @@ -1,342 +0,0 @@ -# -*- coding: utf-8 -*- -import uuid -import re - -from .regex import REGEX_NOPASSWD, REGEX_UNPACK, REGEX_CONST_STRING, REGEX_W -from .classes import SQLCustomType -#from ..objects import Field, Table - - -PLURALIZE_RULES = [ - (re.compile('child$'), re.compile('child$'), 'children'), - (re.compile('oot$'), re.compile('oot$'), 'eet'), - (re.compile('ooth$'), re.compile('ooth$'), 'eeth'), - (re.compile('l[eo]af$'), re.compile('l([eo])af$'), 'l\\1aves'), - (re.compile('sis$'), re.compile('sis$'), 'ses'), - (re.compile('man$'), re.compile('man$'), 'men'), - (re.compile('ife$'), re.compile('ife$'), 'ives'), - (re.compile('eau$'), re.compile('eau$'), 'eaux'), - (re.compile('lf$'), re.compile('lf$'), 'lves'), - (re.compile('[sxz]$'), re.compile('$'), 'es'), - (re.compile('[^aeioudgkprt]h$'), re.compile('$'), 'es'), - (re.compile('(qu|[^aeiou])y$'), re.compile('y$'), 'ies'), - (re.compile('$'), re.compile('$'), 's'), - ] - -def pluralize(singular, rules=PLURALIZE_RULES): - for line in rules: - re_search, re_sub, replace = line - plural = re_search.search(singular) and re_sub.sub(replace, singular) - if plural: return plural - -def hide_password(uri): - if isinstance(uri,(list,tuple)): - return [hide_password(item) for item in uri] - return REGEX_NOPASSWD.sub('******',uri) - - -def cleanup(text): - """ - Validates that the given text is clean: only contains [0-9a-zA-Z_] - """ - #if not REGEX_ALPHANUMERIC.match(text): - # raise SyntaxError('invalid table or field name: %s' % text) - return text - - -def list_represent(x,r=None): - return ', '.join(str(y) for y in x or []) - - -def xorify(orderby): - if not orderby: - return None - orderby2 = orderby[0] - for item in orderby[1:]: - orderby2 = orderby2 | item - return orderby2 - - -def use_common_filters(query): - return (query and hasattr(query,'ignore_common_filters') and \ - not query.ignore_common_filters) - - -def bar_escape(item): - return str(item).replace('|', '||') - - -def bar_encode(items): - return '|%s|' % '|'.join(bar_escape(item) for item in items if str(item).strip()) - - -def bar_decode_integer(value): - if not hasattr(value,'split') and hasattr(value,'read'): - value = value.read() - return [long(x) for x in value.split('|') if x.strip()] - - -def bar_decode_string(value): - return [x.replace('||', '|') for x in - REGEX_UNPACK.split(value[1:-1]) if x.strip()] - - -def archive_record(qset, fs, archive_table, current_record): - tablenames = qset.db._adapter.tables(qset.query) - if len(tablenames) != 1: - raise RuntimeError("cannot update join") - for row in qset.select(): - fields = archive_table._filter_fields(row) - fields[current_record] = row.id - archive_table.insert(**fields) - return False - - -def smart_query(fields,text): - from ..objects import Field, Table - if not isinstance(fields,(list,tuple)): - fields = [fields] - new_fields = [] - for field in fields: - if isinstance(field,Field): - new_fields.append(field) - elif isinstance(field,Table): - for ofield in field: - new_fields.append(ofield) - else: - raise RuntimeError("fields must be a list of fields") - fields = new_fields - field_map = {} - for field in fields: - n = field.name.lower() - if not n in field_map: - field_map[n] = field - n = str(field).lower() - if not n in field_map: - field_map[n] = field - constants = {} - i = 0 - while True: - m = REGEX_CONST_STRING.search(text) - if not m: break - text = text[:m.start()]+('#%i' % i)+text[m.end():] - constants[str(i)] = m.group()[1:-1] - i+=1 - text = re.sub('\s+',' ',text).lower() - for a,b in [('&','and'), - ('|','or'), - ('~','not'), - ('==','='), - ('<','<'), - ('>','>'), - ('<=','<='), - ('>=','>='), - ('<>','!='), - ('=<','<='), - ('=>','>='), - ('=','='), - (' less or equal than ','<='), - (' greater or equal than ','>='), - (' equal or less than ','<='), - (' equal or greater than ','>='), - (' less or equal ','<='), - (' greater or equal ','>='), - (' equal or less ','<='), - (' equal or greater ','>='), - (' not equal to ','!='), - (' not equal ','!='), - (' equal to ','='), - (' equal ','='), - (' equals ','='), - (' less than ','<'), - (' greater than ','>'), - (' starts with ','startswith'), - (' ends with ','endswith'), - (' not in ' , 'notbelongs'), - (' in ' , 'belongs'), - (' is ','=')]: - if a[0]==' ': - text = text.replace(' is'+a,' %s ' % b) - text = text.replace(a,' %s ' % b) - text = re.sub('\s+',' ',text).lower() - text = re.sub('(?P[\<\>\!\=])\s+(?P[\<\>\!\=])','\g\g',text) - query = field = neg = op = logic = None - for item in text.split(): - if field is None: - if item == 'not': - neg = True - elif not neg and not logic and item in ('and','or'): - logic = item - elif item in field_map: - field = field_map[item] - else: - raise RuntimeError("Invalid syntax") - elif not field is None and op is None: - op = item - elif not op is None: - if item.startswith('#'): - if not item[1:] in constants: - raise RuntimeError("Invalid syntax") - value = constants[item[1:]] - else: - value = item - if field.type in ('text', 'string', 'json'): - if op == '=': op = 'like' - if op == '=': new_query = field==value - elif op == '<': new_query = field': new_query = field>value - elif op == '<=': new_query = field<=value - elif op == '>=': new_query = field>=value - elif op == '!=': new_query = field!=value - elif op == 'belongs': new_query = field.belongs(value.split(',')) - elif op == 'notbelongs': new_query = ~field.belongs(value.split(',')) - elif field.type in ('text', 'string', 'json'): - if op == 'contains': new_query = field.contains(value) - elif op == 'like': new_query = field.ilike(value) - elif op == 'startswith': new_query = field.startswith(value) - elif op == 'endswith': new_query = field.endswith(value) - else: raise RuntimeError("Invalid operation") - elif field._db._adapter.dbengine=='google:datastore' and \ - field.type in ('list:integer', 'list:string', 'list:reference'): - if op == 'contains': new_query = field.contains(value) - else: raise RuntimeError("Invalid operation") - else: raise RuntimeError("Invalid operation") - if neg: new_query = ~new_query - if query is None: - query = new_query - elif logic == 'and': - query &= new_query - elif logic == 'or': - query |= new_query - field = op = neg = logic = None - return query - - -def sqlhtml_validators(field): - """ - Field type validation, using web2py's validators mechanism. - - makes sure the content of a field is in line with the declared - fieldtype - """ - db = field.db - try: - from gluon import validators - except ImportError: - return [] - field_type, field_length = field.type, field.length - if isinstance(field_type, SQLCustomType): - if hasattr(field_type, 'validator'): - return field_type.validator - else: - field_type = field_type.type - elif not isinstance(field_type,str): - return [] - requires=[] - def ff(r,id): - row=r(id) - if not row: - return str(id) - elif hasattr(r, '_format') and isinstance(r._format,str): - return r._format % row - elif hasattr(r, '_format') and callable(r._format): - return r._format(row) - else: - return str(id) - if field_type in (('string', 'text', 'password')): - requires.append(validators.IS_LENGTH(field_length)) - elif field_type == 'json': - requires.append(validators.IS_EMPTY_OR(validators.IS_JSON())) - elif field_type == 'double' or field_type == 'float': - requires.append(validators.IS_FLOAT_IN_RANGE(-1e100, 1e100)) - elif field_type == 'integer': - requires.append(validators.IS_INT_IN_RANGE(-2**31, 2**31)) - elif field_type == 'bigint': - requires.append(validators.IS_INT_IN_RANGE(-2**63, 2**63)) - elif field_type.startswith('decimal'): - requires.append(validators.IS_DECIMAL_IN_RANGE(-10**10, 10**10)) - elif field_type == 'date': - requires.append(validators.IS_DATE()) - elif field_type == 'time': - requires.append(validators.IS_TIME()) - elif field_type == 'datetime': - requires.append(validators.IS_DATETIME()) - elif db and field_type.startswith('reference') and \ - field_type.find('.') < 0 and \ - field_type[10:] in db.tables: - referenced = db[field_type[10:]] - def repr_ref(id, row=None, r=referenced, f=ff): return f(r, id) - field.represent = field.represent or repr_ref - if hasattr(referenced, '_format') and referenced._format: - requires = validators.IS_IN_DB(db,referenced._id, - referenced._format) - if field.unique: - requires._and = validators.IS_NOT_IN_DB(db,field) - if field.tablename == field_type[10:]: - return validators.IS_EMPTY_OR(requires) - return requires - elif db and field_type.startswith('list:reference') and \ - field_type.find('.') < 0 and \ - field_type[15:] in db.tables: - referenced = db[field_type[15:]] - def list_ref_repr(ids, row=None, r=referenced, f=ff): - if not ids: - return None - from ..adapters.google import GoogleDatastoreAdapter - refs = None - db, id = r._db, r._id - if isinstance(db._adapter, GoogleDatastoreAdapter): - def count(values): return db(id.belongs(values)).select(id) - rx = range(0, len(ids), 30) - refs = reduce(lambda a,b:a&b, [count(ids[i:i+30]) for i in rx]) - else: - refs = db(id.belongs(ids)).select(id) - return (refs and ', '.join(f(r,x.id) for x in refs) or '') - field.represent = field.represent or list_ref_repr - if hasattr(referenced, '_format') and referenced._format: - requires = validators.IS_IN_DB(db,referenced._id, - referenced._format,multiple=True) - else: - requires = validators.IS_IN_DB(db,referenced._id, - multiple=True) - if field.unique: - requires._and = validators.IS_NOT_IN_DB(db,field) - if not field.notnull: - requires = validators.IS_EMPTY_OR(requires) - return requires - elif field_type.startswith('list:'): - def repr_list(values,row=None): return', '.join(str(v) for v in (values or [])) - field.represent = field.represent or repr_list - if field.unique: - requires.append(validators.IS_NOT_IN_DB(db, field)) - sff = ['in', 'do', 'da', 'ti', 'de', 'bo'] - if field.notnull and not field_type[:2] in sff: - requires.append(validators.IS_NOT_EMPTY()) - elif not field.notnull and field_type[:2] in sff and requires: - requires[0] = validators.IS_EMPTY_OR(requires[0]) - return requires - - -def varquote_aux(name,quotestr='%s'): - return name if REGEX_W.match(name) else quotestr % name - - -def uuid2int(uuidv): - return uuid.UUID(uuidv).int - - -def int2uuid(n): - return str(uuid.UUID(int=n)) - - -# Geodal utils -def geoPoint(x, y): - return "POINT (%f %f)" % (x, y) - - -def geoLine(*line): - return "LINESTRING (%s)" % ','.join("%f %f" % item for item in line) - - -def geoPolygon(*line): - return "POLYGON ((%s))" % ','.join("%f %f" % item for item in line) diff --git a/gluon/dal/helpers/regex.py b/gluon/dal/helpers/regex.py deleted file mode 100644 index 1139aaa4..00000000 --- a/gluon/dal/helpers/regex.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- - -import re - -REGEX_TYPE = re.compile('^([\w\_\:]+)') -REGEX_DBNAME = re.compile('^(\w+)(\:\w+)*') -REGEX_W = re.compile('^\w+$') -REGEX_TABLE_DOT_FIELD = re.compile('^(\w+)\.([^.]+)$') -REGEX_NO_GREEDY_ENTITY_NAME = r'(.+?)' -REGEX_UPLOAD_PATTERN = re.compile('(?P
[\w\-]+)\.(?P[\w\-]+)\.(?P[\w\-]+)(\.(?P\w+))?\.\w+$') -REGEX_CLEANUP_FN = re.compile('[\'"\s;]+') -REGEX_UNPACK = re.compile('(?\w{1,5})$') -REGEX_QUOTES = re.compile("'[^']*'") -REGEX_ALPHANUMERIC = re.compile('^[0-9a-zA-Z]\w*$') -REGEX_PASSWORD = re.compile('\://([^:@]*)\:') -REGEX_NOPASSWD = re.compile('\/\/[\w\.\-]+[\:\/](.+)(?=@)') # was '(?<=[\:\/])([^:@/]+)(?=@.+)' diff --git a/gluon/dal/objects.py b/gluon/dal/objects.py deleted file mode 100644 index 44730709..00000000 --- a/gluon/dal/objects.py +++ /dev/null @@ -1,2704 +0,0 @@ -# -*- coding: utf-8 -*- - -import base64 -import cgi -import copy -import csv -import datetime -import decimal -import os -import shutil -import sys -import types - -from ._compat import StringIO, ogetattr, osetattr, pjoin, exists, hashlib_md5 -from ._globals import DEFAULT, IDENTITY, AND, OR -from ._load import have_serializers, serializers, simplejson, DRIVERS, Key, web2py_uuid -from .helpers.regex import REGEX_TABLE_DOT_FIELD, REGEX_ALPHANUMERIC, \ - REGEX_PYTHON_KEYWORDS, REGEX_STORE_PATTERN, REGEX_UPLOAD_PATTERN, \ - REGEX_CLEANUP_FN -from .helpers.classes import Reference, MethodAdder, SQLCallableList, SQLALL -from .helpers.methods import list_represent, bar_decode_integer, \ - bar_decode_string, bar_encode, archive_record, cleanup, \ - use_common_filters, pluralize - -DEFAULTLENGTH = {'string':512, - 'password':512, - 'upload':512, - 'text':2**15, - 'blob':2**31} - - -class Row(object): - - """ - A dictionary that lets you do d['a'] as well as d.a - this is only used to store a `Row` - """ - - __init__ = lambda self,*args,**kwargs: self.__dict__.update(*args,**kwargs) - - def __getitem__(self, k): - if isinstance(k, Table): - try: - return ogetattr(self, k._tablename) - except (KeyError,AttributeError,TypeError): - pass - elif isinstance(k, Field): - try: - return ogetattr(self, k.name) - except (KeyError,AttributeError,TypeError): - pass - try: - return ogetattr(ogetattr(self, k.tablename), k.name) - except (KeyError,AttributeError,TypeError): - pass - - key=str(k) - _extra = ogetattr(self, '__dict__').get('_extra', None) - if _extra is not None: - v = _extra.get(key, DEFAULT) - if v != DEFAULT: - return v - try: - return ogetattr(self, key) - except (KeyError,AttributeError,TypeError): - pass - - m = REGEX_TABLE_DOT_FIELD.match(key) - if m: - try: - return ogetattr(self, m.group(1))[m.group(2)] - except (KeyError,AttributeError,TypeError): - key = m.group(2) - try: - return ogetattr(self, key) - except (KeyError,AttributeError,TypeError), ae: - try: - self[key] = ogetattr(self,'__get_lazy_reference__')(key) - return self[key] - except: - raise ae - - __setitem__ = lambda self, key, value: setattr(self, str(key), value) - - __delitem__ = object.__delattr__ - - __copy__ = lambda self: Row(self) - - __call__ = __getitem__ - - - def get(self, key, default=None): - try: - return self.__getitem__(key) - except(KeyError, AttributeError, TypeError): - return self.__dict__.get(key,default) - - has_key = __contains__ = lambda self, key: key in self.__dict__ - - __nonzero__ = lambda self: len(self.__dict__)>0 - - update = lambda self, *args, **kwargs: self.__dict__.update(*args, **kwargs) - - keys = lambda self: self.__dict__.keys() - - items = lambda self: self.__dict__.items() - - values = lambda self: self.__dict__.values() - - __iter__ = lambda self: self.__dict__.__iter__() - - iteritems = lambda self: self.__dict__.iteritems() - - __str__ = __repr__ = lambda self: '' % self.as_dict() - - __int__ = lambda self: object.__getattribute__(self,'id') - - __long__ = lambda self: long(object.__getattribute__(self,'id')) - - __getattr__ = __getitem__ - - # def __getattribute__(self, key): - # try: - # return object.__getattribute__(self, key) - # except AttributeError, ae: - # try: - # return self.__get_lazy_reference__(key) - # except: - # raise ae - - def __eq__(self,other): - try: - return self.as_dict() == other.as_dict() - except AttributeError: - return False - - def __ne__(self,other): - return not (self == other) - - def __copy__(self): - return Row(dict(self)) - - def as_dict(self, datetime_to_str=False, custom_types=None): - SERIALIZABLE_TYPES = [str, unicode, int, long, float, bool, list, dict] - if isinstance(custom_types,(list,tuple,set)): - SERIALIZABLE_TYPES += custom_types - elif custom_types: - SERIALIZABLE_TYPES.append(custom_types) - d = dict(self) - for k in copy.copy(d.keys()): - v=d[k] - if d[k] is None: - continue - elif isinstance(v,Row): - d[k]=v.as_dict() - elif isinstance(v,Reference): - d[k]=long(v) - elif isinstance(v,decimal.Decimal): - d[k]=float(v) - elif isinstance(v, (datetime.date, datetime.datetime, datetime.time)): - if datetime_to_str: - d[k] = v.isoformat().replace('T',' ')[:19] - elif not isinstance(v,tuple(SERIALIZABLE_TYPES)): - del d[k] - return d - - def as_xml(self, row_name="row", colnames=None, indent=' '): - def f(row,field,indent=' '): - if isinstance(row,Row): - spc = indent+' \n' - items = [f(row[x],x,indent+' ') for x in row] - return '%s<%s>\n%s\n%s' % ( - indent, - field, - spc.join(item for item in items if item), - indent, - field) - elif not callable(row): - if REGEX_ALPHANUMERIC.match(field): - return '%s<%s>%s' % (indent,field,row,field) - else: - return '%s%s' % \ - (indent,field,row) - else: - return None - return f(self, row_name, indent=indent) - - def as_json(self, mode="object", default=None, colnames=None, - serialize=True, **kwargs): - """ - serializes the row to a JSON object - kwargs are passed to .as_dict method - only "object" mode supported - - `serialize = False` used by Rows.as_json - - TODO: return array mode with query column order - - mode and colnames are not implemented - """ - - item = self.as_dict(**kwargs) - if serialize: - if have_serializers: - return serializers.json(item, - default=default or - serializers.custom_json) - elif simplejson: - return simplejson.dumps(item) - else: - raise RuntimeError("missing simplejson") - else: - return item - - -class Table(object): - - """ - Represents a database table - - Example:: - You can create a table as:: - db = DAL(...) - db.define_table('users', Field('name')) - - And then:: - - db.users.insert(name='me') # print db.users._insert(...) to see SQL - db.users.drop() - - """ - - def __init__( - self, - db, - tablename, - *fields, - **args): - """ - Initializes the table and performs checking on the provided fields. - - Each table will have automatically an 'id'. - - If a field is of type Table, the fields (excluding 'id') from that table - will be used instead. - - Raises: - SyntaxError: when a supplied field is of incorrect type. - """ - # import DAL here to avoid circular imports - from .base import DAL - self._actual = False # set to True by define_table() - self._tablename = tablename - if (not isinstance(tablename, str) or tablename[0] == '_' - or hasattr(DAL, tablename) or '.' in tablename - or REGEX_PYTHON_KEYWORDS.match(tablename) - ): - raise SyntaxError('Field: invalid table name: %s, ' - 'use rname for "funny" names' % tablename) - self._ot = None - self._rname = args.get('rname') - self._sequence_name = (args.get('sequence_name') or - db and db._adapter.sequence_name(self._rname - or tablename)) - self._trigger_name = (args.get('trigger_name') or - db and db._adapter.trigger_name(tablename)) - self._common_filter = args.get('common_filter') - self._format = args.get('format') - self._singular = args.get( - 'singular', tablename.replace('_', ' ').capitalize()) - self._plural = args.get( - 'plural', pluralize(self._singular.lower()).capitalize()) - # horrible but for backard compatibility of appamdin: - if 'primarykey' in args and args['primarykey'] is not None: - self._primarykey = args.get('primarykey') - - self._before_insert = [] - self._before_update = [Set.delete_uploaded_files] - self._before_delete = [Set.delete_uploaded_files] - self._after_insert = [] - self._after_update = [] - self._after_delete = [] - - self.add_method = MethodAdder(self) - - fieldnames, newfields=set(), [] - _primarykey = getattr(self, '_primarykey', None) - if _primarykey is not None: - if not isinstance(_primarykey, list): - raise SyntaxError( - "primarykey must be a list of fields from table '%s'" - % tablename) - if len(_primarykey) == 1: - self._id = [f for f in fields if isinstance(f, Field) - and f.name ==_primarykey[0]][0] - elif not [f for f in fields if (isinstance(f, Field) and - f.type == 'id') or (isinstance(f, dict) and - f.get("type", None) == "id")]: - field = Field('id', 'id') - newfields.append(field) - fieldnames.add('id') - self._id = field - virtual_fields = [] - - def include_new(field): - newfields.append(field) - fieldnames.add(field.name) - if field.type == 'id': - self._id = field - for field in fields: - if isinstance(field, (FieldMethod, FieldVirtual)): - virtual_fields.append(field) - elif isinstance(field, Field) and not field.name in fieldnames: - if field.db is not None: - field = copy.copy(field) - include_new(field) - elif isinstance(field, dict) and not field['fieldname'] in fieldnames: - include_new(Field(**field)) - elif isinstance(field, Table): - table = field - for field in table: - if not field.name in fieldnames and not field.type == 'id': - t2 = not table._actual and self._tablename - include_new(field.clone(point_self_references_to=t2)) - elif not isinstance(field, (Field, Table)): - raise SyntaxError( - 'define_table argument is not a Field or Table: %s' % field) - fields = newfields - self._db = db - tablename = tablename - self._fields = SQLCallableList() - self.virtualfields = [] - fields = list(fields) - - if db and db._adapter.uploads_in_blob is True: - uploadfields = [f.name for f in fields if f.type == 'blob'] - for field in fields: - fn = field.uploadfield - if isinstance(field, Field) and field.type == 'upload'\ - and fn is True and not field.uploadfs: - fn = field.uploadfield = '%s_blob' % field.name - if isinstance(fn, str) and not fn in uploadfields and not field.uploadfs: - fields.append(Field(fn, 'blob', default='', - writable=False, readable=False)) - - fieldnames_set = set() - reserved = dir(Table) + ['fields'] - if (db and db.check_reserved): - check_reserved = db.check_reserved_keyword - else: - def check_reserved(field_name): - if field_name in reserved: - raise SyntaxError("field name %s not allowed" % field_name) - for field in fields: - field_name = field.name - check_reserved(field_name) - if db and db._ignore_field_case: - fname_item = field_name.lower() - else: - fname_item = field_name - if fname_item in fieldnames_set: - raise SyntaxError("duplicate field %s in table %s" % - (field_name, tablename)) - else: - fieldnames_set.add(fname_item) - - self.fields.append(field_name) - self[field_name] = field - if field.type == 'id': - self['id'] = field - field.tablename = field._tablename = tablename - field.table = field._table = self - field.db = field._db = db - self.ALL = SQLALL(self) - - if _primarykey is not None: - for k in _primarykey: - if k not in self.fields: - raise SyntaxError( - "primarykey must be a list of fields from table '%s " % - tablename) - else: - self[k].notnull = True - for field in virtual_fields: - self[field.name] = field - - @property - def fields(self): - return self._fields - - def update(self, *args, **kwargs): - raise RuntimeError("Syntax Not Supported") - - def _enable_record_versioning(self, - archive_db=None, - archive_name='%(tablename)s_archive', - is_active='is_active', - current_record='current_record', - current_record_label=None): - db = self._db - archive_db = archive_db or db - archive_name = archive_name % dict(tablename=self._tablename) - if archive_name in archive_db.tables(): - return # do not try define the archive if already exists - fieldnames = self.fields() - same_db = archive_db is db - field_type = self if same_db else 'bigint' - clones = [] - for field in self: - nfk = same_db or not field.type.startswith('reference') - clones.append( - field.clone(unique=False, type=field.type if nfk else 'bigint') - ) - archive_db.define_table( - archive_name, - Field(current_record, field_type, label=current_record_label), - *clones, **dict(format=self._format)) - - self._before_update.append( - lambda qset, fs, db=archive_db, an=archive_name, cn=current_record: - archive_record(qset, fs, db[an], cn)) - if is_active and is_active in fieldnames: - self._before_delete.append( - lambda qset: qset.update(is_active=False)) - newquery = lambda query, t=self, name=self._tablename: \ - reduce(AND, [db[tn].is_active == True - for tn in db._adapter.tables(query) - if tn == name or getattr(db[tn],'_ot',None)==name]) - query = self._common_filter - if query: - newquery = query & newquery - self._common_filter = newquery - - def _validate(self, **vars): - errors = Row() - for key, value in vars.iteritems(): - value, error = self[key].validate(value) - if error: - errors[key] = error - return errors - - def _create_references(self): - db = self._db - pr = db._pending_references - self._referenced_by = [] - self._references = [] - for field in self: - #fieldname = field.name ##FIXME not used ? - field_type = field.type - if isinstance(field_type, str) and field_type[:10] == 'reference ': - ref = field_type[10:].strip() - if not ref: - SyntaxError('Table: reference to nothing: %s' % ref) - if '.' in ref: - rtablename, throw_it, rfieldname = ref.partition('.') - else: - rtablename, rfieldname = ref, None - if not rtablename in db: - pr[rtablename] = pr.get(rtablename, []) + [field] - continue - rtable = db[rtablename] - if rfieldname: - if not hasattr(rtable, '_primarykey'): - raise SyntaxError( - 'keyed tables can only reference other keyed tables (for now)') - if rfieldname not in rtable.fields: - raise SyntaxError( - "invalid field '%s' for referenced table '%s'" - " in table '%s'" % (rfieldname, rtablename, self._tablename) - ) - rfield = rtable[rfieldname] - else: - rfield = rtable._id - rtable._referenced_by.append(field) - field.referent = rfield - self._references.append(field) - else: - field.referent = None - if self._tablename in pr: - referees = pr.pop(self._tablename) - for referee in referees: - self._referenced_by.append(referee) - - def _filter_fields(self, record, id=False): - return dict([(k, v) for (k, v) in record.iteritems() if k - in self.fields and (self[k].type!='id' or id)]) - - def _build_query(self,key): - """ for keyed table only """ - query = None - for k,v in key.iteritems(): - if k in self._primarykey: - if query: - query = query & (self[k] == v) - else: - query = (self[k] == v) - else: - raise SyntaxError( - 'Field %s is not part of the primary key of %s' % - (k,self._tablename) - ) - return query - - def __getitem__(self, key): - if not key: - return None - elif isinstance(key, dict): - """ for keyed table """ - query = self._build_query(key) - return self._db(query).select(limitby=(0, 1), orderby_on_limitby=False).first() - elif str(key).isdigit() or 'google' in DRIVERS and isinstance(key, Key): - return self._db(self._id == key).select(limitby=(0, 1), orderby_on_limitby=False).first() - elif key: - return ogetattr(self, str(key)) - - def __call__(self, key=DEFAULT, **kwargs): - for_update = kwargs.get('_for_update', False) - if '_for_update' in kwargs: - del kwargs['_for_update'] - - orderby = kwargs.get('_orderby', None) - if '_orderby' in kwargs: - del kwargs['_orderby'] - - if not key is DEFAULT: - if isinstance(key, Query): - record = self._db(key).select( - limitby=(0,1),for_update=for_update, orderby=orderby, orderby_on_limitby=False).first() - elif not str(key).isdigit(): - record = None - else: - record = self._db(self._id == key).select( - limitby=(0,1),for_update=for_update, orderby=orderby, orderby_on_limitby=False).first() - if record: - for k,v in kwargs.iteritems(): - if record[k]!=v: return None - return record - elif kwargs: - query = reduce(lambda a,b:a&b,[self[k]==v for k,v in kwargs.iteritems()]) - return self._db(query).select(limitby=(0,1),for_update=for_update, orderby=orderby, orderby_on_limitby=False).first() - else: - return None - - def __setitem__(self, key, value): - if isinstance(key, dict) and isinstance(value, dict): - """ option for keyed table """ - if set(key.keys()) == set(self._primarykey): - value = self._filter_fields(value) - kv = {} - kv.update(value) - kv.update(key) - if not self.insert(**kv): - query = self._build_query(key) - self._db(query).update(**self._filter_fields(value)) - else: - raise SyntaxError( - 'key must have all fields from primary key: %s'% - (self._primarykey)) - elif str(key).isdigit(): - if key == 0: - self.insert(**self._filter_fields(value)) - elif self._db(self._id == key)\ - .update(**self._filter_fields(value)) is None: - raise SyntaxError('No such record: %s' % key) - else: - if isinstance(key, dict): - raise SyntaxError( - 'value must be a dictionary: %s' % value) - osetattr(self, str(key), value) - - __getattr__ = __getitem__ - - def __setattr__(self, key, value): - if key[:1]!='_' and key in self: - raise SyntaxError('Object exists and cannot be redefined: %s' % key) - osetattr(self,key,value) - - def __delitem__(self, key): - if isinstance(key, dict): - query = self._build_query(key) - if not self._db(query).delete(): - raise SyntaxError('No such record: %s' % key) - elif not str(key).isdigit() or \ - not self._db(self._id == key).delete(): - raise SyntaxError('No such record: %s' % key) - - def __contains__(self,key): - return hasattr(self, key) - - has_key = __contains__ - - def items(self): - return self.__dict__.items() - - def __iter__(self): - for fieldname in self.fields: - yield self[fieldname] - - def iteritems(self): - return self.__dict__.iteritems() - - def __repr__(self): - return '
' % (self._tablename, ','.join(self.fields())) - - def __str__(self): - if self._ot is not None: - ot = self._ot - if 'Oracle' in str(type(self._db._adapter)): - return '%s %s' % (ot, self._tablename) - return '%s AS %s' % (ot, self._tablename) - - return self._tablename - - @property - def sqlsafe(self): - rname = self._rname - if rname: return rname - return self._db._adapter.sqlsafe_table(self._tablename) - - @property - def sqlsafe_alias(self): - rname = self._rname - ot = self._ot - if rname and not ot: return rname - return self._db._adapter.sqlsafe_table(self._tablename, self._ot) - - - def _drop(self, mode=''): - return self._db._adapter._drop(self, mode) - - def drop(self, mode=''): - return self._db._adapter.drop(self,mode) - - def _listify(self,fields,update=False): - new_fields = {} # format: new_fields[name] = (field,value) - - # store all fields passed as input in new_fields - for name in fields: - if not name in self.fields: - if name != 'id': - raise SyntaxError( - 'Field %s does not belong to the table' % name) - else: - field = self[name] - value = fields[name] - if field.filter_in: - value = field.filter_in(value) - new_fields[name] = (field, value) - - # check all fields that should be in the table but are not passed - to_compute = [] - for ofield in self: - name = ofield.name - if not name in new_fields: - # if field is supposed to be computed, compute it! - if ofield.compute: # save those to compute for later - to_compute.append((name, ofield)) - # if field is required, check its default value - elif not update and not ofield.default is None: - value = ofield.default - fields[name] = value - new_fields[name] = (ofield, value) - # if this is an update, user the update field instead - elif update and not ofield.update is None: - value = ofield.update - fields[name] = value - new_fields[name] = (ofield, value) - # if the field is still not there but it should, error - elif not update and ofield.required: - raise RuntimeError( - 'Table: missing required field: %s' % name) - # now deal with fields that are supposed to be computed - if to_compute: - row = Row(fields) - for name, ofield in to_compute: - # try compute it - try: - row[name] = new_value = ofield.compute(row) - new_fields[name] = (ofield, new_value) - except (KeyError, AttributeError): - # error silently unless field is required! - if ofield.required: - raise SyntaxError('unable to compute field: %s' % name) - return new_fields.values() - - def _attempt_upload(self, fields): - for field in self: - if field.type == 'upload' and field.name in fields: - value = fields[field.name] - if not (value is None or isinstance(value, str)): - if hasattr(value, 'file') and hasattr(value, 'filename'): - new_name = field.store(value.file, filename=value.filename) - elif isinstance(value,dict): - if 'data' in value and 'filename' in value: - stream = StringIO.StringIO(value['data']) - new_name = field.store(stream, filename=value['filename']) - else: - new_name = None - elif hasattr(value, 'read') and hasattr(value, 'name'): - new_name = field.store(value, filename=value.name) - else: - raise RuntimeError("Unable to handle upload") - fields[field.name] = new_name - - def _defaults(self, fields): - "If there are no fields/values specified, return table defaults" - if not fields: - fields = {} - for field in self: - if field.type != "id": - fields[field.name] = field.default - return fields - - def _insert(self, **fields): - fields = self._defaults(fields) - return self._db._adapter._insert(self, self._listify(fields)) - - def insert(self, **fields): - fields = self._defaults(fields) - self._attempt_upload(fields) - if any(f(fields) for f in self._before_insert): return 0 - ret = self._db._adapter.insert(self, self._listify(fields)) - if ret and self._after_insert: - fields = Row(fields) - [f(fields,ret) for f in self._after_insert] - return ret - - def validate_and_insert(self, **fields): - response = Row() - response.errors = Row() - new_fields = copy.copy(fields) - for key,value in fields.iteritems(): - value,error = self[key].validate(value) - if error: - response.errors[key] = "%s" % error - else: - new_fields[key] = value - if not response.errors: - response.id = self.insert(**new_fields) - else: - response.id = None - return response - - def validate_and_update(self, _key=DEFAULT, **fields): - response = Row() - response.errors = Row() - new_fields = copy.copy(fields) - - for key, value in fields.iteritems(): - value, error = self[key].validate(value) - if error: - response.errors[key] = "%s" % error - else: - new_fields[key] = value - - if _key is DEFAULT: - record = self(**fields) - elif isinstance(_key, dict): - record = self(**_key) - else: - record = self(_key) - - if not response.errors and record: - if '_id' in self: - myset = self._db(self._id == record[self._id.name]) - else: - query = None - for key, value in _key.iteritems(): - if query is None: - query = getattr(self, key) == value - else: - query = query & (getattr(self, key) == value) - myset = self._db(query) - response.id = myset.update(**fields) - else: - response.id = None - return response - - def update_or_insert(self, _key=DEFAULT, **values): - if _key is DEFAULT: - record = self(**values) - elif isinstance(_key, dict): - record = self(**_key) - else: - record = self(_key) - if record: - record.update_record(**values) - newid = None - else: - newid = self.insert(**values) - return newid - - def validate_and_update_or_insert(self, _key=DEFAULT, **fields): - if _key is DEFAULT or _key == '': - primary_keys = {} - for key, value in fields.iteritems(): - if key in self._primarykey: - primary_keys[key] = value - if primary_keys != {}: - record = self(**primary_keys) - _key = primary_keys - else: - required_keys = {} - for key, value in fields.iteritems(): - if getattr(self, key).required: - required_keys[key] = value - record = self(**required_keys) - _key = required_keys - elif isinstance(_key, dict): - record = self(**_key) - else: - record = self(_key) - - if record: - response = self.validate_and_update(_key, **fields) - primary_keys = {} - for key in self._primarykey: - primary_keys[key] = getattr(record, key) - response.id = primary_keys - else: - response = self.validate_and_insert(**fields) - return response - - def bulk_insert(self, items): - """ - here items is a list of dictionaries - """ - items = [self._listify(item) for item in items] - if any(f(item) for item in items for f in self._before_insert):return 0 - ret = self._db._adapter.bulk_insert(self,items) - ret and [[f(item,ret[k]) for k,item in enumerate(items)] for f in self._after_insert] - return ret - - def _truncate(self, mode=None): - return self._db._adapter._truncate(self, mode) - - def truncate(self, mode=None): - return self._db._adapter.truncate(self, mode) - - def import_from_csv_file( - self, - csvfile, - id_map=None, - null='', - unique='uuid', - id_offset=None, # id_offset used only when id_map is None - *args, **kwargs - ): - """ - Import records from csv file. - Column headers must have same names as table fields. - Field 'id' is ignored. - If column names read 'table.file' the 'table.' prefix is ignored. - - - 'unique' argument is a field which must be unique (typically a - uuid field) - - 'restore' argument is default False; if set True will remove old values - in table first. - - 'id_map' if set to None will not map ids - - The import will keep the id numbers in the restored table. - This assumes that there is an field of type id that is integer and in - incrementing order. - Will keep the id numbers in restored table. - """ - - delimiter = kwargs.get('delimiter', ',') - quotechar = kwargs.get('quotechar', '"') - quoting = kwargs.get('quoting', csv.QUOTE_MINIMAL) - restore = kwargs.get('restore', False) - if restore: - self._db[self].truncate() - - reader = csv.reader(csvfile, delimiter=delimiter, - quotechar=quotechar, quoting=quoting) - colnames = None - if isinstance(id_map, dict): - if not self._tablename in id_map: - id_map[self._tablename] = {} - id_map_self = id_map[self._tablename] - - def fix(field, value, id_map, id_offset): - list_reference_s='list:reference' - if value == null: - value = None - elif field.type=='blob': - value = base64.b64decode(value) - elif field.type=='double' or field.type=='float': - if not value.strip(): - value = None - else: - value = float(value) - elif field.type in ('integer','bigint'): - if not value.strip(): - value = None - else: - value = long(value) - elif field.type.startswith('list:string'): - value = bar_decode_string(value) - elif field.type.startswith(list_reference_s): - ref_table = field.type[len(list_reference_s):].strip() - if id_map is not None: - value = [id_map[ref_table][long(v)] \ - for v in bar_decode_string(value)] - else: - value = [v for v in bar_decode_string(value)] - elif field.type.startswith('list:'): - value = bar_decode_integer(value) - elif id_map and field.type.startswith('reference'): - try: - value = id_map[field.type[9:].strip()][long(value)] - except KeyError: - pass - elif id_offset and field.type.startswith('reference'): - try: - value = id_offset[field.type[9:].strip()]+long(value) - except KeyError: - pass - return (field.name, value) - - def is_id(colname): - if colname in self: - return self[colname].type == 'id' - else: - return False - - first = True - unique_idx = None - for lineno, line in enumerate(reader): - if not line: - break - if not colnames: - # assume this is the first line of the input, contains colnames - colnames = [x.split('.',1)[-1] for x in line][:len(line)] - cols, cid = [], None - for i,colname in enumerate(colnames): - if is_id(colname): - cid = i - elif colname in self.fields: - cols.append((i,self[colname])) - if colname == unique: - unique_idx = i - else: - # every other line contains instead data - items = [] - for i, field in cols: - try: - items.append(fix(field, line[i], id_map, id_offset)) - except ValueError: - raise RuntimeError("Unable to parse line:%s field:%s value:'%s'" - % (lineno+1,field,line[i])) - - if not (id_map or cid is None or id_offset is None or unique_idx): - csv_id = long(line[cid]) - curr_id = self.insert(**dict(items)) - if first: - first = False - # First curr_id is bigger than csv_id, - # then we are not restoring but - # extending db table with csv db table - id_offset[self._tablename] = (curr_id-csv_id) \ - if curr_id>csv_id else 0 - # create new id until we get the same as old_id+offset - while curr_id/./uuid_key[:2] - # directory) - uploadfs=None # a pyfilesystem where to store upload - ) - - to be used as argument of `DAL.define_table` - - """ - - def __init__( - self, - fieldname, - type='string', - length=None, - default=DEFAULT, - required=False, - requires=DEFAULT, - ondelete='CASCADE', - notnull=False, - unique=False, - uploadfield=True, - widget=None, - label=None, - comment=None, - writable=True, - readable=True, - update=None, - authorize=None, - autodelete=False, - represent=None, - uploadfolder=None, - uploadseparate=False, - uploadfs=None, - compute=None, - custom_store=None, - custom_retrieve=None, - custom_retrieve_file_properties=None, - custom_delete=None, - filter_in=None, - filter_out=None, - custom_qualifier=None, - map_none=None, - rname=None - ): - self._db = self.db = None # both for backward compatibility - self.op = None - self.first = None - self.second = None - if isinstance(fieldname, unicode): - try: - fieldname = str(fieldname) - except UnicodeEncodeError: - raise SyntaxError('Field: invalid unicode field name') - self.name = fieldname = cleanup(fieldname) - if not isinstance(fieldname, str) or hasattr(Table, fieldname) or \ - fieldname[0] == '_' or '.' in fieldname or \ - REGEX_PYTHON_KEYWORDS.match(fieldname): - raise SyntaxError('Field: invalid field name: %s, ' - 'use rname for "funny" names' % fieldname) - - if not isinstance(type, (Table, Field)): - self.type = type - else: - self.type = 'reference %s' % type - - self.length = length if not length is None else DEFAULTLENGTH.get(self.type, 512) - self.default = default if default != DEFAULT else (update or None) - self.required = required # is this field required - self.ondelete = ondelete.upper() # this is for reference fields only - self.notnull = notnull - self.unique = unique - self.uploadfield = uploadfield - self.uploadfolder = uploadfolder - self.uploadseparate = uploadseparate - self.uploadfs = uploadfs - self.widget = widget - self.comment = comment - self.writable = writable - self.readable = readable - self.update = update - self.authorize = authorize - self.autodelete = autodelete - self.represent = (list_represent if represent is None and - type in ('list:integer', 'list:string') else represent) - self.compute = compute - self.isattachment = True - self.custom_store = custom_store - self.custom_retrieve = custom_retrieve - self.custom_retrieve_file_properties = custom_retrieve_file_properties - self.custom_delete = custom_delete - self.filter_in = filter_in - self.filter_out = filter_out - self.custom_qualifier = custom_qualifier - self.label = (label if label is not None else - fieldname.replace('_', ' ').title()) - self.requires = requires if requires is not None else [] - self.map_none = map_none - self._rname = rname - - def set_attributes(self, *args, **attributes): - self.__dict__.update(*args, **attributes) - - def clone(self, point_self_references_to=False, **args): - field = copy.copy(self) - if point_self_references_to and \ - field.type == 'reference %s'+field._tablename: - field.type = 'reference %s' % point_self_references_to - field.__dict__.update(args) - return field - - def store(self, file, filename=None, path=None): - if self.custom_store: - return self.custom_store(file, filename, path) - if isinstance(file, cgi.FieldStorage): - filename = filename or file.filename - file = file.file - elif not filename: - filename = file.name - filename = os.path.basename(filename.replace('/', os.sep).replace('\\', os.sep)) - m = REGEX_STORE_PATTERN.search(filename) - extension = m and m.group('e') or 'txt' - uuid_key = web2py_uuid().replace('-', '')[-16:] - encoded_filename = base64.b16encode(filename).lower() - newfilename = '%s.%s.%s.%s' % \ - (self._tablename, self.name, uuid_key, encoded_filename) - newfilename = newfilename[:(self.length - 1 - len(extension))] + '.' + extension - self_uploadfield = self.uploadfield - if isinstance(self_uploadfield, Field): - blob_uploadfield_name = self_uploadfield.uploadfield - keys = {self_uploadfield.name: newfilename, - blob_uploadfield_name: file.read()} - self_uploadfield.table.insert(**keys) - elif self_uploadfield is True: - if path: - pass - elif self.uploadfolder: - path = self.uploadfolder - elif self.db._adapter.folder: - path = pjoin(self.db._adapter.folder, '..', 'uploads') - else: - raise RuntimeError( - "you must specify a Field(...,uploadfolder=...)") - if self.uploadseparate: - if self.uploadfs: - raise RuntimeError("not supported") - path = pjoin(path, "%s.%s" % ( - self._tablename, self.name), uuid_key[:2] - ) - if not exists(path): - os.makedirs(path) - pathfilename = pjoin(path, newfilename) - if self.uploadfs: - dest_file = self.uploadfs.open(newfilename, 'wb') - else: - dest_file = open(pathfilename, 'wb') - try: - shutil.copyfileobj(file, dest_file) - except IOError: - raise IOError( - 'Unable to store file "%s" because invalid permissions, ' - 'readonly file system, or filename too long' % pathfilename) - dest_file.close() - return newfilename - - def retrieve(self, name, path=None, nameonly=False): - """ - If `nameonly==True` return (filename, fullfilename) instead of - (filename, stream) - """ - self_uploadfield = self.uploadfield - if self.custom_retrieve: - return self.custom_retrieve(name, path) - import gluon.http as http - if self.authorize or isinstance(self_uploadfield, str): - row = self.db(self == name).select().first() - if not row: - raise http.HTTP(404) - if self.authorize and not self.authorize(row): - raise http.HTTP(403) - file_properties = self.retrieve_file_properties(name, path) - filename = file_properties['filename'] - if isinstance(self_uploadfield, str): # ## if file is in DB - stream = StringIO.StringIO(row[self_uploadfield] or '') - elif isinstance(self_uploadfield, Field): - blob_uploadfield_name = self_uploadfield.uploadfield - query = self_uploadfield == name - data = self_uploadfield.table(query)[blob_uploadfield_name] - stream = StringIO.StringIO(data) - elif self.uploadfs: - # ## if file is on pyfilesystem - stream = self.uploadfs.open(name, 'rb') - else: - # ## if file is on regular filesystem - # this is intentially a sting with filename and not a stream - # this propagates and allows stream_file_or_304_or_206 to be called - fullname = pjoin(file_properties['path'], name) - if nameonly: - return (filename, fullname) - stream = open(fullname, 'rb') - return (filename, stream) - - def retrieve_file_properties(self, name, path=None): - m = REGEX_UPLOAD_PATTERN.match(name) - if not m or not self.isattachment: - raise TypeError('Can\'t retrieve %s file properties' % name) - self_uploadfield = self.uploadfield - if self.custom_retrieve_file_properties: - return self.custom_retrieve_file_properties(name, path) - if m.group('name'): - try: - filename = base64.b16decode(m.group('name'), True) - filename = REGEX_CLEANUP_FN.sub('_', filename) - except (TypeError, AttributeError): - filename = name - else: - filename = name - # ## if file is in DB - if isinstance(self_uploadfield, (str, Field)): - return dict(path=None, filename=filename) - # ## if file is on filesystem - if not path: - if self.uploadfolder: - path = self.uploadfolder - else: - path = pjoin(self.db._adapter.folder, '..', 'uploads') - if self.uploadseparate: - t = m.group('table') - f = m.group('field') - u = m.group('uuidkey') - path = pjoin(path, "%s.%s" % (t, f), u[:2]) - return dict(path=path, filename=filename) - - def formatter(self, value): - requires = self.requires - if value is None: - return self.map_none - if not requires: - return value - if not isinstance(requires, (list, tuple)): - requires = [requires] - elif isinstance(requires, tuple): - requires = list(requires) - else: - requires = copy.copy(requires) - requires.reverse() - for item in requires: - if hasattr(item, 'formatter'): - value = item.formatter(value) - return value - - def validate(self, value): - if not self.requires or self.requires == DEFAULT: - return ((value if value != self.map_none else None), None) - requires = self.requires - if not isinstance(requires, (list, tuple)): - requires = [requires] - for validator in requires: - (value, error) = validator(value) - if error: - return (value, error) - return ((value if value != self.map_none else None), None) - - def count(self, distinct=None): - return Expression(self.db, self.db._adapter.COUNT, self, distinct, 'integer') - - def as_dict(self, flat=False, sanitize=True): - attrs = ( - 'name', 'authorize', 'represent', 'ondelete', - 'custom_store', 'autodelete', 'custom_retrieve', - 'filter_out', 'uploadseparate', 'widget', 'uploadfs', - 'update', 'custom_delete', 'uploadfield', 'uploadfolder', - 'custom_qualifier', 'unique', 'writable', 'compute', - 'map_none', 'default', 'type', 'required', 'readable', - 'requires', 'comment', 'label', 'length', 'notnull', - 'custom_retrieve_file_properties', 'filter_in') - serializable = (int, long, basestring, float, tuple, - bool, type(None)) - - def flatten(obj): - if isinstance(obj, dict): - return dict((flatten(k), flatten(v)) for k, v in obj.items()) - elif isinstance(obj, (tuple, list, set)): - return [flatten(v) for v in obj] - elif isinstance(obj, serializable): - return obj - elif isinstance(obj, (datetime.datetime, - datetime.date, datetime.time)): - return str(obj) - else: - return None - - d = dict() - if not (sanitize and not (self.readable or self.writable)): - for attr in attrs: - if flat: - d.update({attr: flatten(getattr(self, attr))}) - else: - d.update({attr: getattr(self, attr)}) - d["fieldname"] = d.pop("name") - return d - - def as_xml(self, sanitize=True): - if have_serializers: - xml = serializers.xml - else: - raise ImportError("No xml serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return xml(d) - - def as_json(self, sanitize=True): - if have_serializers: - json = serializers.json - else: - raise ImportError("No json serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return json(d) - - def as_yaml(self, sanitize=True): - if have_serializers: - d = self.as_dict(flat=True, sanitize=sanitize) - return serializers.yaml(d) - else: - raise ImportError("No YAML serializers available") - - def __nonzero__(self): - return True - - def __str__(self): - try: - return '%s.%s' % (self.tablename, self.name) - except: - return '.%s' % self.name - - @property - def sqlsafe(self): - if self._table: - return self._table.sqlsafe + '.' + \ - (self._rname or self._db._adapter.sqlsafe_field(self.name)) - return '.%s' % self.name - - @property - def sqlsafe_name(self): - return self._rname or self._db._adapter.sqlsafe_field(self.name) - - -class Query(object): - - """ - Necessary to define a set. - It can be stored or can be passed to `DAL.__call__()` to obtain a `Set` - - Example: - Use as:: - - query = db.users.name=='Max' - set = db(query) - records = set.select() - - """ - - def __init__( - self, - db, - op, - first=None, - second=None, - ignore_common_filters=False, - **optional_args - ): - self.db = self._db = db - self.op = op - self.first = first - self.second = second - self.ignore_common_filters = ignore_common_filters - self.optional_args = optional_args - - def __repr__(self): - from .adapters.base import BaseAdapter - return '' % BaseAdapter.expand(self.db._adapter,self) - - def __str__(self): - return str(self.db._adapter.expand(self)) - - def __and__(self, other): - return Query(self.db,self.db._adapter.AND,self,other) - - __rand__ = __and__ - - def __or__(self, other): - return Query(self.db,self.db._adapter.OR,self,other) - - __ror__ = __or__ - - def __invert__(self): - if self.op==self.db._adapter.NOT: - return self.first - return Query(self.db,self.db._adapter.NOT,self) - - def __eq__(self, other): - return repr(self) == repr(other) - - def __ne__(self, other): - return not (self == other) - - def case(self,t=1,f=0): - return self.db._adapter.CASE(self,t,f) - - def as_dict(self, flat=False, sanitize=True): - """Experimental stuff - - This allows to return a plain dictionary with the basic - query representation. Can be used with json/xml services - for client-side db I/O - - Example: - Usage:: - - q = db.auth_user.id != 0 - q.as_dict(flat=True) - { - "op": "NE", - "first":{ - "tablename": "auth_user", - "fieldname": "id" - }, - "second":0 - } - """ - - SERIALIZABLE_TYPES = (tuple, dict, set, list, int, long, float, - basestring, type(None), bool) - - def loop(d): - newd = dict() - for k, v in d.items(): - if k in ("first", "second"): - if isinstance(v, self.__class__): - newd[k] = loop(v.__dict__) - elif isinstance(v, Field): - newd[k] = {"tablename": v._tablename, - "fieldname": v.name} - elif isinstance(v, Expression): - newd[k] = loop(v.__dict__) - elif isinstance(v, SERIALIZABLE_TYPES): - newd[k] = v - elif isinstance(v, (datetime.date, - datetime.time, - datetime.datetime)): - newd[k] = unicode(v) - elif k == "op": - if callable(v): - newd[k] = v.__name__ - elif isinstance(v, basestring): - newd[k] = v - else: pass # not callable or string - elif isinstance(v, SERIALIZABLE_TYPES): - if isinstance(v, dict): - newd[k] = loop(v) - else: newd[k] = v - return newd - - if flat: - return loop(self.__dict__) - else: return self.__dict__ - - def as_xml(self, sanitize=True): - if have_serializers: - xml = serializers.xml - else: - raise ImportError("No xml serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return xml(d) - - def as_json(self, sanitize=True): - if have_serializers: - json = serializers.json - else: - raise ImportError("No json serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return json(d) - - -class Set(object): - - """ - Represents a set of records in the database. - Records are identified by the `query=Query(...)` object. - Normally the Set is generated by `DAL.__call__(Query(...))` - - Given a set, for example:: - - myset = db(db.users.name=='Max') - - you can:: - - myset.update(db.users.name='Massimo') - myset.delete() # all elements in the set - myset.select(orderby=db.users.id, groupby=db.users.name, limitby=(0,10)) - - and take subsets: - - subset = myset(db.users.id<5) - - """ - - def __init__(self, db, query, ignore_common_filters = None): - self.db = db - self._db = db # for backward compatibility - self.dquery = None - - # if query is a dict, parse it - if isinstance(query, dict): - query = self.parse(query) - - if not ignore_common_filters is None and \ - use_common_filters(query) == ignore_common_filters: - query = copy.copy(query) - query.ignore_common_filters = ignore_common_filters - self.query = query - - def __repr__(self): - from .adapters.base import BaseAdapter - return '' % BaseAdapter.expand(self.db._adapter,self.query) - - def __call__(self, query, ignore_common_filters=False): - if query is None: - return self - elif isinstance(query,Table): - query = self.db._adapter.id_query(query) - elif isinstance(query,str): - query = Expression(self.db,query) - elif isinstance(query,Field): - query = query!=None - if self.query: - return Set(self.db, self.query & query, - ignore_common_filters=ignore_common_filters) - else: - return Set(self.db, query, - ignore_common_filters=ignore_common_filters) - - def _count(self,distinct=None): - return self.db._adapter._count(self.query,distinct) - - def _select(self, *fields, **attributes): - adapter = self.db._adapter - tablenames = adapter.tables(self.query, - attributes.get('join',None), - attributes.get('left',None), - attributes.get('orderby',None), - attributes.get('groupby',None)) - fields = adapter.expand_all(fields, tablenames) - return adapter._select(self.query,fields,attributes) - - def _delete(self): - db = self.db - tablename = db._adapter.get_table(self.query) - return db._adapter._delete(tablename,self.query) - - def _update(self, **update_fields): - db = self.db - tablename = db._adapter.get_table(self.query) - fields = db[tablename]._listify(update_fields,update=True) - return db._adapter._update(tablename,self.query,fields) - - def as_dict(self, flat=False, sanitize=True): - if flat: - uid = dbname = uri = None - codec = self.db._db_codec - if not sanitize: - uri, dbname, uid = (self.db._dbname, str(self.db), - self.db._db_uid) - d = {"query": self.query.as_dict(flat=flat)} - d["db"] = {"uid": uid, "codec": codec, - "name": dbname, "uri": uri} - return d - else: return self.__dict__ - - def as_xml(self, sanitize=True): - if have_serializers: - xml = serializers.xml - else: - raise ImportError("No xml serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return xml(d) - - def as_json(self, sanitize=True): - if have_serializers: - json = serializers.json - else: - raise ImportError("No json serializers available") - d = self.as_dict(flat=True, sanitize=sanitize) - return json(d) - - def parse(self, dquery): - "Experimental: Turn a dictionary into a Query object" - self.dquery = dquery - return self.build(self.dquery) - - def build(self, d): - "Experimental: see .parse()" - op, first, second = (d["op"], d["first"], - d.get("second", None)) - left = right = built = None - - if op in ("AND", "OR"): - if not (type(first), type(second)) == (dict, dict): - raise SyntaxError("Invalid AND/OR query") - if op == "AND": - built = self.build(first) & self.build(second) - else: built = self.build(first) | self.build(second) - - elif op == "NOT": - if first is None: - raise SyntaxError("Invalid NOT query") - built = ~self.build(first) - else: - # normal operation (GT, EQ, LT, ...) - for k, v in {"left": first, "right": second}.items(): - if isinstance(v, dict) and v.get("op"): - v = self.build(v) - if isinstance(v, dict) and ("tablename" in v): - v = self.db[v["tablename"]][v["fieldname"]] - if k == "left": left = v - else: right = v - - if hasattr(self.db._adapter, op): - opm = getattr(self.db._adapter, op) - - if op == "EQ": built = left == right - elif op == "NE": built = left != right - elif op == "GT": built = left > right - elif op == "GE": built = left >= right - elif op == "LT": built = left < right - elif op == "LE": built = left <= right - elif op in ("JOIN", "LEFT_JOIN", "RANDOM", "ALLOW_NULL"): - built = Expression(self.db, opm) - elif op in ("LOWER", "UPPER", "EPOCH", "PRIMARY_KEY", - "COALESCE_ZERO", "RAW", "INVERT"): - built = Expression(self.db, opm, left) - elif op in ("COUNT", "EXTRACT", "AGGREGATE", "SUBSTRING", - "REGEXP", "LIKE", "ILIKE", "STARTSWITH", - "ENDSWITH", "ADD", "SUB", "MUL", "DIV", - "MOD", "AS", "ON", "COMMA", "NOT_NULL", - "COALESCE", "CONTAINS", "BELONGS"): - built = Expression(self.db, opm, left, right) - # expression as string - elif not (left or right): built = Expression(self.db, op) - else: - raise SyntaxError("Operator not supported: %s" % op) - - return built - - def isempty(self): - return not self.select(limitby=(0,1), orderby_on_limitby=False) - - def count(self,distinct=None, cache=None): - db = self.db - if cache: - cache_model, time_expire = cache - sql = self._count(distinct=distinct) - key = db._uri + '/' + sql - if len(key)>200: key = hashlib_md5(key).hexdigest() - return cache_model( - key, - (lambda self=self,distinct=distinct: \ - db._adapter.count(self.query,distinct)), - time_expire) - return db._adapter.count(self.query,distinct) - - def select(self, *fields, **attributes): - adapter = self.db._adapter - tablenames = adapter.tables(self.query, - attributes.get('join',None), - attributes.get('left',None), - attributes.get('orderby',None), - attributes.get('groupby',None)) - fields = adapter.expand_all(fields, tablenames) - return adapter.select(self.query,fields,attributes) - - def nested_select(self,*fields,**attributes): - return Expression(self.db,self._select(*fields,**attributes)) - - def delete(self): - db = self.db - tablename = db._adapter.get_table(self.query) - table = db[tablename] - if any(f(self) for f in table._before_delete): return 0 - ret = db._adapter.delete(tablename,self.query) - ret and [f(self) for f in table._after_delete] - return ret - - def update(self, **update_fields): - db = self.db - tablename = db._adapter.get_table(self.query) - table = db[tablename] - table._attempt_upload(update_fields) - if any(f(self,update_fields) for f in table._before_update): - return 0 - fields = table._listify(update_fields,update=True) - if not fields: - raise SyntaxError("No fields to update") - ret = db._adapter.update("%s" % table._tablename,self.query,fields) - ret and [f(self,update_fields) for f in table._after_update] - return ret - - def update_naive(self, **update_fields): - """ - Same as update but does not call table._before_update and _after_update - """ - tablename = self.db._adapter.get_table(self.query) - table = self.db[tablename] - fields = table._listify(update_fields,update=True) - if not fields: raise SyntaxError("No fields to update") - - ret = self.db._adapter.update("%s" % table,self.query,fields) - return ret - - def validate_and_update(self, **update_fields): - tablename = self.db._adapter.get_table(self.query) - response = Row() - response.errors = Row() - new_fields = copy.copy(update_fields) - for key,value in update_fields.iteritems(): - value,error = self.db[tablename][key].validate(value) - if error: - response.errors[key] = '%s' % error - else: - new_fields[key] = value - table = self.db[tablename] - if response.errors: - response.updated = None - else: - if not any(f(self, new_fields) for f in table._before_update): - table._attempt_upload(new_fields) - fields = table._listify(new_fields,update=True) - if not fields: raise SyntaxError("No fields to update") - ret = self.db._adapter.update(tablename,self.query,fields) - ret and [f(self,new_fields) for f in table._after_update] - else: - ret = 0 - response.updated = ret - return response - - def delete_uploaded_files(self, upload_fields=None): - table = self.db[self.db._adapter.tables(self.query)[0]] - # ## mind uploadfield==True means file is not in DB - if upload_fields: - fields = upload_fields.keys() - else: - fields = table.fields - fields = [f for f in fields if table[f].type == 'upload' - and table[f].uploadfield == True - and table[f].autodelete] - if not fields: - return False - for record in self.select(*[table[f] for f in fields]): - for fieldname in fields: - field = table[fieldname] - oldname = record.get(fieldname, None) - if not oldname: - continue - if upload_fields and oldname == upload_fields[fieldname]: - continue - if field.custom_delete: - field.custom_delete(oldname) - else: - uploadfolder = field.uploadfolder - if not uploadfolder: - uploadfolder = pjoin( - self.db._adapter.folder, '..', 'uploads') - if field.uploadseparate: - items = oldname.split('.') - uploadfolder = pjoin( - uploadfolder, - "%s.%s" % (items[0], items[1]), - items[2][:2]) - oldpath = pjoin(uploadfolder, oldname) - if exists(oldpath): - os.unlink(oldpath) - return False - - -class LazyReferenceGetter(object): - def __init__(self, table, id): - self.db, self.tablename, self.id = table._db, table._tablename, id - def __call__(self, other_tablename): - if self.db._lazy_tables is False: - raise AttributeError() - table = self.db[self.tablename] - other_table = self.db[other_tablename] - for rfield in table._referenced_by: - if rfield.table == other_table: - return LazySet(rfield, self.id) - raise AttributeError() - - -class LazySet(object): - def __init__(self, field, id): - self.db, self.tablename, self.fieldname, self.id = \ - field.db, field._tablename, field.name, id - def _getset(self): - query = self.db[self.tablename][self.fieldname]==self.id - return Set(self.db,query) - def __repr__(self): - return repr(self._getset()) - def __call__(self, query, ignore_common_filters=False): - return self._getset()(query, ignore_common_filters) - def _count(self,distinct=None): - return self._getset()._count(distinct) - def _select(self, *fields, **attributes): - return self._getset()._select(*fields,**attributes) - def _delete(self): - return self._getset()._delete() - def _update(self, **update_fields): - return self._getset()._update(**update_fields) - def isempty(self): - return self._getset().isempty() - def count(self,distinct=None, cache=None): - return self._getset().count(distinct,cache) - def select(self, *fields, **attributes): - return self._getset().select(*fields,**attributes) - def nested_select(self,*fields,**attributes): - return self._getset().nested_select(*fields,**attributes) - def delete(self): - return self._getset().delete() - def update(self, **update_fields): - return self._getset().update(**update_fields) - def update_naive(self, **update_fields): - return self._getset().update_naive(**update_fields) - def validate_and_update(self, **update_fields): - return self._getset().validate_and_update(**update_fields) - def delete_uploaded_files(self, upload_fields=None): - return self._getset().delete_uploaded_files(upload_fields) - - -class VirtualCommand(object): - def __init__(self,method,row): - self.method=method - self.row=row - def __call__(self,*args,**kwargs): - return self.method(self.row,*args,**kwargs) - - -class Rows(object): - - """ - A wrapper for the return value of a select. It basically represents a table. - It has an iterator and each row is represented as a `Row` dictionary. - """ - - # ## TODO: this class still needs some work to care for ID/OID - - def __init__( - self, - db=None, - records=[], - colnames=[], - compact=True, - rawrows=None - ): - self.db = db - self.records = records - self.colnames = colnames - self.compact = compact - self.response = rawrows - - def __repr__(self): - return '' % len(self.records) - - def setvirtualfields(self,**keyed_virtualfields): - """ - For reference:: - - db.define_table('x',Field('number','integer')) - if db(db.x).isempty(): [db.x.insert(number=i) for i in range(10)] - - from gluon.dal import lazy_virtualfield - - class MyVirtualFields(object): - # normal virtual field (backward compatible, discouraged) - def normal_shift(self): return self.x.number+1 - # lazy virtual field (because of @staticmethod) - @lazy_virtualfield - def lazy_shift(instance,row,delta=4): return row.x.number+delta - db.x.virtualfields.append(MyVirtualFields()) - - for row in db(db.x).select(): - print row.number, row.normal_shift, row.lazy_shift(delta=7) - - """ - if not keyed_virtualfields: - return self - for row in self.records: - for (tablename,virtualfields) in keyed_virtualfields.iteritems(): - attributes = dir(virtualfields) - if not tablename in row: - box = row[tablename] = Row() - else: - box = row[tablename] - updated = False - for attribute in attributes: - if attribute[0] != '_': - method = getattr(virtualfields,attribute) - if hasattr(method,'__lazy__'): - box[attribute]=VirtualCommand(method,row) - elif type(method)==types.MethodType: - if not updated: - virtualfields.__dict__.update(row) - updated = True - box[attribute]=method() - return self - - def __and__(self,other): - if self.colnames!=other.colnames: - raise Exception('Cannot & incompatible Rows objects') - records = self.records+other.records - return Rows(self.db,records,self.colnames, - compact=self.compact or other.compact) - - def __or__(self,other): - if self.colnames!=other.colnames: - raise Exception('Cannot | incompatible Rows objects') - records = [record for record in other.records - if not record in self.records] - records = self.records + records - return Rows(self.db,records,self.colnames, - compact=self.compact or other.compact) - - def __nonzero__(self): - if len(self.records): - return 1 - return 0 - - def __len__(self): - return len(self.records) - - def __getslice__(self, a, b): - return Rows(self.db,self.records[a:b],self.colnames,compact=self.compact) - - def __getitem__(self, i): - row = self.records[i] - keys = row.keys() - if self.compact and len(keys) == 1 and keys[0] != '_extra': - return row[row.keys()[0]] - return row - - def __iter__(self): - """ - Iterator over records - """ - - for i in xrange(len(self)): - yield self[i] - - def __str__(self): - """ - Serializes the table into a csv file - """ - - s = StringIO.StringIO() - self.export_to_csv_file(s) - return s.getvalue() - - def column(self, column=None): - return [r[str(column) if column else self.colnames[0]] for r in self] - - def first(self): - if not self.records: - return None - return self[0] - - def last(self): - if not self.records: - return None - return self[-1] - - def find(self,f,limitby=None): - """ - Returns a new Rows object, a subset of the original object, - filtered by the function `f` - """ - if not self: - return Rows(self.db, [], self.colnames, compact=self.compact) - records = [] - if limitby: - a,b = limitby - else: - a,b = 0,len(self) - k = 0 - for i, row in enumerate(self): - if f(row): - if a<=k: records.append(self.records[i]) - k += 1 - if k==b: break - return Rows(self.db, records, self.colnames, compact=self.compact) - - def exclude(self, f): - """ - Removes elements from the calling Rows object, filtered by the function - `f`, and returns a new Rows object containing the removed elements - """ - if not self.records: - return Rows(self.db, [], self.colnames, compact=self.compact) - removed = [] - i=0 - while i len(fields)-1: - if one_result: - return row - else: - return [row] - - key = fields[num] - value = row[key] - - if value not in groups: - groups[value] = build_fields_struct(row, fields, num+1, {}) - else: - struct = build_fields_struct(row, fields, num+1, groups[ value ]) - - # still have more grouping to do - if type(struct) == type(dict()): - groups[value].update() - # no more grouping, first only is off - elif type(struct) == type(list()): - groups[value] += struct - # no more grouping, first only on - else: - groups[value] = struct - - return groups - - if len(fields) == 0: - return self - - # if select returned no results - if not self.records: - return {} - - grouped_row_group = dict() - - # build the struct - for row in self: - build_fields_struct(row, fields, 0, grouped_row_group) - - return grouped_row_group - - def render(self, i=None, fields=None): - """ - Takes an index and returns a copy of the indexed row with values - transformed via the "represent" attributes of the associated fields. - - Args: - i: index. If not specified, a generator is returned for iteration - over all the rows. - fields: a list of fields to transform (if None, all fields with - "represent" attributes will be transformed) - """ - - if i is None: - return (self.render(i, fields=fields) for i in range(len(self))) - import gluon.sqlhtml as sqlhtml - row = copy.deepcopy(self.records[i]) - keys = row.keys() - tables = [f.tablename for f in fields] if fields \ - else [k for k in keys if k != '_extra'] - for table in tables: - repr_fields = [f.name for f in fields if f.tablename == table] \ - if fields else [k for k in row[table].keys() - if (hasattr(self.db[table], k) and - isinstance(self.db[table][k], Field) - and self.db[table][k].represent)] - for field in repr_fields: - row[table][field] = sqlhtml.represent( - self.db[table][field], row[table][field], row[table]) - if self.compact and len(keys) == 1 and keys[0] != '_extra': - return row[keys[0]] - return row - - def as_list(self, - compact=True, - storage_to_dict=True, - datetime_to_str=False, - custom_types=None): - """ - Returns the data as a list or dictionary. - - Args: - storage_to_dict: when True returns a dict, otherwise a list - datetime_to_str: convert datetime fields as strings - """ - (oc, self.compact) = (self.compact, compact) - if storage_to_dict: - items = [item.as_dict(datetime_to_str, custom_types) for item in self] - else: - items = [item for item in self] - self.compact = oc - return items - - def as_dict(self, - key='id', - compact=True, - storage_to_dict=True, - datetime_to_str=False, - custom_types=None): - """ - Returns the data as a dictionary of dictionaries (storage_to_dict=True) - or records (False) - - Args: - key: the name of the field to be used as dict key, normally the id - compact: ? (default True) - storage_to_dict: when True returns a dict, otherwise a list(default True) - datetime_to_str: convert datetime fields as strings (default False) - """ - - # test for multiple rows - multi = False - f = self.first() - if f and isinstance(key, basestring): - multi = any([isinstance(v, f.__class__) for v in f.values()]) - if (not "." in key) and multi: - # No key provided, default to int indices - def new_key(): - i = 0 - while True: - yield i - i += 1 - key_generator = new_key() - key = lambda r: key_generator.next() - - rows = self.as_list(compact, storage_to_dict, datetime_to_str, custom_types) - if isinstance(key,str) and key.count('.')==1: - (table, field) = key.split('.') - return dict([(r[table][field],r) for r in rows]) - elif isinstance(key,str): - return dict([(r[key],r) for r in rows]) - else: - return dict([(key(r),r) for r in rows]) - - def as_trees(self, parent_name='parent_id', children_name='children', render=False): - """ - returns the data as list of trees. - - :param parent_name: the name of the field holding the reference to the - parent (default parent_id). - :param children_name: the name where the children of each row will be - stored as a list (default children). - :param render: whether we will render the fields using their represent - (default False) can be a list of fields to render or - True to render all. - """ - roots = [] - drows = {} - rows = list(self.render(fields=None if render is True else render)) if render else self - for row in rows: - drows[row.id] = row - row[children_name] = [] - for row in rows: - parent = row[parent_name] - if parent is None: - roots.append(row) - else: - drows[parent][children_name].append(row) - return roots - - def export_to_csv_file(self, ofile, null='', *args, **kwargs): - """ - Exports data to csv, the first line contains the column names - - Args: - ofile: where the csv must be exported to - null: how null values must be represented (default '') - delimiter: delimiter to separate values (default ',') - quotechar: character to use to quote string values (default '"') - quoting: quote system, use csv.QUOTE_*** (default csv.QUOTE_MINIMAL) - represent: use the fields .represent value (default False) - colnames: list of column names to use (default self.colnames) - - This will only work when exporting rows objects!!!! - DO NOT use this with db.export_to_csv() - """ - delimiter = kwargs.get('delimiter', ',') - quotechar = kwargs.get('quotechar', '"') - quoting = kwargs.get('quoting', csv.QUOTE_MINIMAL) - represent = kwargs.get('represent', False) - writer = csv.writer(ofile, delimiter=delimiter, - quotechar=quotechar, quoting=quoting) - - def unquote_colnames(colnames): - unq_colnames = [] - for col in colnames: - m = self.db._adapter.REGEX_TABLE_DOT_FIELD.match(col) - if not m: - unq_colnames.append(col) - else: - unq_colnames.append('.'.join(m.groups())) - return unq_colnames - - colnames = kwargs.get('colnames', self.colnames) - write_colnames = kwargs.get('write_colnames',True) - # a proper csv starting with the column names - if write_colnames: - writer.writerow(unquote_colnames(colnames)) - - def none_exception(value): - """ - Returns a cleaned up value that can be used for csv export: - - - unicode text is encoded as such - - None values are replaced with the given representation (default ) - """ - if value is None: - return null - elif isinstance(value, unicode): - return value.encode('utf8') - elif isinstance(value,Reference): - return long(value) - elif hasattr(value, 'isoformat'): - return value.isoformat()[:19].replace('T', ' ') - elif isinstance(value, (list,tuple)): # for type='list:..' - return bar_encode(value) - return value - - for record in self: - row = [] - for col in colnames: - m = self.db._adapter.REGEX_TABLE_DOT_FIELD.match(col) - if not m: - row.append(record._extra[col]) - else: - (t, f) = m.groups() - field = self.db[t][f] - if isinstance(record.get(t, None), (Row,dict)): - value = record[t][f] - else: - value = record[f] - if field.type=='blob' and not value is None: - value = base64.b64encode(value) - elif represent and field.represent: - value = field.represent(value,record) - row.append(none_exception(value)) - writer.writerow(row) - - def xml(self,strict=False,row_name='row',rows_name='rows'): - """ - Serializes the table using sqlhtml.SQLTABLE (if present) - """ - - if strict: - return '<%s>\n%s\n' % (rows_name, - '\n'.join(row.as_xml(row_name=row_name, - colnames=self.colnames) for - row in self), rows_name) - - import gluon.sqlhtml as sqlhtml - return sqlhtml.SQLTABLE(self).xml() - - def as_xml(self,row_name='row',rows_name='rows'): - return self.xml(strict=True, row_name=row_name, rows_name=rows_name) - - def as_json(self, mode='object', default=None): - """ - Serializes the rows to a JSON list or object with objects - mode='object' is not implemented (should return a nested - object structure) - """ - - items = [record.as_json(mode=mode, default=default, - serialize=False, - colnames=self.colnames) for - record in self] - - if have_serializers: - return serializers.json(items, - default=default or - serializers.custom_json) - elif simplejson: - return simplejson.dumps(items) - else: - raise RuntimeError("missing simplejson") - - # for consistent naming yet backwards compatible - as_csv = __str__ - json = as_json - diff --git a/gluon/main.py b/gluon/main.py index b126181c..c3f63969 100644 --- a/gluon/main.py +++ b/gluon/main.py @@ -93,7 +93,7 @@ from gluon.globals import Request, Response, Session from gluon.compileapp import build_environment, run_models_in, \ run_controller_in, run_view_in from gluon.contenttype import contenttype -from gluon.dal.base import BaseAdapter +from gluon.pydal.base import BaseAdapter from gluon.validators import CRYPT from gluon.html import URL, xmlescape from gluon.utils import is_valid_ip_address, getipaddrinfo @@ -365,8 +365,8 @@ def wsgibase(environ, responder): client = client, folder = abspath('applications', app) + os.sep, ajax = x_req_with == 'xmlhttprequest', - cid = env.http_web2py_component_element, - is_local = (env.remote_addr in local_hosts and + cid = env.http_web2py_component_element, + is_local = (env.remote_addr in local_hosts and client == env.remote_addr), is_shell = cmd_opts and cmd_opts.shell, is_sheduler = cmd_opts and cmd_opts.scheduler, diff --git a/gluon/scheduler.py b/gluon/scheduler.py index 254c442f..69daf61c 100644 --- a/gluon/scheduler.py +++ b/gluon/scheduler.py @@ -598,7 +598,7 @@ class Scheduler(MetaScheduler): def define_tables(self, db, migrate): """Defines Scheduler tables structure""" - from gluon.dal.base import DEFAULT + from gluon.pydal.base import DEFAULT logger.debug('defining tables (migrate=%s)', migrate) now = self.now db.define_table( @@ -1316,7 +1316,7 @@ class Scheduler(MetaScheduler): have all fields == None """ - from gluon.dal.objects import Query + from gluon.pydal.objects import Query sr, st = self.db.scheduler_run, self.db.scheduler_task if isinstance(ref, (int, long)): q = st.id == ref diff --git a/gluon/shell.py b/gluon/shell.py index 4863bfea..89c26e60 100644 --- a/gluon/shell.py +++ b/gluon/shell.py @@ -28,7 +28,7 @@ from gluon.restricted import RestrictedError from gluon.globals import Request, Response, Session from gluon.storage import Storage, List from gluon.admin import w2p_unpack -from gluon.dal.base import BaseAdapter +from gluon.pydal.base import BaseAdapter logger = logging.getLogger("web2py") @@ -129,7 +129,7 @@ def env( request.function) if global_settings.cmd_options: ip = global_settings.cmd_options.ip - port = global_settings.cmd_options.port + port = global_settings.cmd_options.port else: ip, port = '127.0.0.1', '8000' request.env.http_host = '%s:%s' % (ip,port) diff --git a/gluon/sql.py b/gluon/sql.py index e0d0b46e..4f282f71 100644 --- a/gluon/sql.py +++ b/gluon/sql.py @@ -13,9 +13,10 @@ Just for backward compatibility __all__ = ['DAL', 'Field', 'DRIVERS'] from dal import DAL, Field, SQLCustomType -from dal.adapters.base import BaseAdapter, DRIVERS -from dal.objects import Table, Query, Set, Expression, Row, Rows -from dal.helpers.classes import SQLALL +from gluon.pydal.base import BaseAdapter +from gluon.pydal.drivers import DRIVERS +from gluon.pydal.objects import Table, Query, Set, Expression, Row, Rows +from gluon.pydal.helpers.classes import SQLALL SQLDB = DAL GQLDB = DAL diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 6a8abffc..7e1f234b 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -26,12 +26,11 @@ from gluon.html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG from gluon.html import FORM, INPUT, LABEL, OPTION, SELECT, COL, COLGROUP from gluon.html import TABLE, THEAD, TBODY, TR, TD, TH, STYLE, SCRIPT from gluon.html import URL, FIELDSET, P, DEFAULT_PASSWORD_DISPLAY -from gluon.dal import DAL, Field -from gluon.dal.base import DEFAULT -from gluon.dal.objects import Table, Row, Expression -from gluon.dal.adapters.base import CALLABLETYPES -from gluon.dal.helpers.methods import smart_query, bar_encode, sqlhtml_validators -from gluon.dal.helpers.classes import Reference, SQLCustomType +from gluon.pydal.base import DEFAULT +from gluon.pydal.objects import Table, Row, Expression +from gluon.pydal.adapters.base import CALLABLETYPES +from gluon.pydal.helpers.methods import smart_query, bar_encode, sqlhtml_validators +from gluon.pydal.helpers.classes import Reference, SQLCustomType from gluon.storage import Storage from gluon.utils import md5_hash from gluon.validators import IS_EMPTY_OR, IS_NOT_EMPTY, IS_LIST_OF, IS_DATE diff --git a/gluon/tools.py b/gluon/tools.py index 481d18ed..e848f464 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -43,7 +43,7 @@ from gluon import * from gluon.contrib.autolinks import expand_one from gluon.contrib.markmin.markmin2html import \ replace_at_urls, replace_autolinks, replace_components -from gluon.dal.objects import Table, Row, Set, Query +from gluon.pydal.objects import Table, Row, Set, Query import gluon.serializers as serializers @@ -172,7 +172,7 @@ class Mail(object): chain of email certificate. It can be a string containing the certs to. (PEM format) x509_nocerts : if True then no attached certificate in mail - x509_crypt_certfiles: the certificates file or strings to encrypt + x509_crypt_certfiles: the certificates file or strings to encrypt the messages with can be a file name / string or a list of file names / strings (PEM format) @@ -340,7 +340,7 @@ class Mail(object): from_address: address to appear in the 'From:' header, this is not the envelope sender. If not specified the sender will be used - cipher_type : + cipher_type : gpg - need a python-pyme package and gpgme lib x509 - smime gpg_home : you can set a GNUPGHOME environment variable @@ -359,8 +359,8 @@ class Mail(object): chain of email certificate. It can be a string containing the certs to. (PEM format) x509_nocerts : if True then no attached certificate in mail - x509_crypt_certfiles: the certificates file or strings to encrypt - the messages with can be a file name / string or + x509_crypt_certfiles: the certificates file or strings to encrypt + the messages with can be a file name / string or a list of file names / strings (PEM format) Examples: Send plain text message to single address:: @@ -1346,7 +1346,7 @@ class Auth(object): reset_password_onvalidation = [], reset_password_onaccept = [], hmac_key = hmac_key, - formstyle = current.response.formstyle, + formstyle = current.response.formstyle, ) settings.lock_keys = True @@ -2340,7 +2340,7 @@ class Auth(object): items = snext.split('/') if '//' in snext and items[2] != request.env.http_host: snext = None - + if snext: session._auth_next = snext elif session._auth_next: @@ -2428,20 +2428,20 @@ class Auth(object): separator=settings.label_separator, extra_fields = extra_fields, ) - - + + captcha = settings.login_captcha or \ (settings.login_captcha != False and settings.captcha) if captcha: addrow(form, captcha.label, captcha, captcha.comment, settings.formstyle, 'captcha__row') accepted_form = False - + if form.accepts(request, session if self.csrf_prevention else None, formname='login', dbio=False, onvalidation=onvalidation, hideerror=settings.hideerror): - + accepted_form = True # check for username in db entered_username = form.vars[username] @@ -2459,7 +2459,7 @@ class Auth(object): elif temp_user.registration_key in ('disabled', 'blocked'): response.flash = self.messages.login_disabled return form - elif (not temp_user.registration_key is None + elif (not temp_user.registration_key is None and temp_user.registration_key.strip()): response.flash = \ self.messages.registration_verifying @@ -2507,11 +2507,11 @@ class Auth(object): redirect( self.url(args=request.args, vars=request.get_vars), client_side=settings.client_side) - + else: # use a central authentication server cas = settings.login_form cas_user = cas.get_user() - + if cas_user: cas_user[passfield] = None user = self.get_or_create_user( @@ -2527,7 +2527,7 @@ class Auth(object): # Extra login logic for two-factor authentication ################################################# - # If the 'user' variable has a value, this means that the first + # If the 'user' variable has a value, this means that the first # authentication step was successful (i.e. user provided correct # username and password at the first challenge). # Check if this user is signed up for two-factor authentication @@ -2540,7 +2540,7 @@ class Auth(object): if session.auth_two_factor_enabled: form = SQLFORM.factory( Field('authentication_code', - required=True, + required=True, comment='This code was emailed to you and is required for login.'), hidden=dict(_next=next), formstyle=settings.formstyle, @@ -2559,8 +2559,8 @@ class Auth(object): session.auth_two_factor_tries_left = 3 # Allow user to try up to 4 times # TODO: Add some error checking to handle cases where email cannot be sent self.settings.mailer.send( - to=user.email, - subject="Two-step Login Authentication Code", + to=user.email, + subject="Two-step Login Authentication Code", message="Your temporary login code is {0}".format(session.auth_two_factor)) if form.accepts(request, session if self.csrf_prevention else None, formname='login', dbio=False, @@ -2575,15 +2575,15 @@ class Auth(object): # normal. if user is None or user == session.auth_two_factor_user: user = session.auth_two_factor_user - # For security, because the username stored in the + # For security, because the username stored in the # session somehow does not match the just validated # user. Should not be possible without session stealing # which is hard with SSL. elif user != session.auth_two_factor_user: user = None # Either way, the user and code associated with this session should - # be removed. This handles cases where the session login may have - # expired but browser window is open, so the old session key and + # be removed. This handles cases where the session login may have + # expired but browser window is open, so the old session key and # session usernamem will still exist self._reset_two_factor_auth(session) else: @@ -2631,11 +2631,11 @@ class Auth(object): """ Logouts and redirects to login """ - + # Clear out 2-step authentication information if user logs # out. This information is also cleared on successful login. self._reset_two_factor_auth(current.session) - + if next is DEFAULT: next = self.get_vars_next() or self.settings.logout_next if onlogout is DEFAULT: @@ -2786,7 +2786,7 @@ class Auth(object): else: next = replace_id(next, form) redirect(next, client_side=self.settings.client_side) - + return form def is_logged_in(self): @@ -3086,7 +3086,7 @@ class Auth(object): if log is DEFAULT: log = self.messages['reset_password_log'] userfield = self.settings.login_userfield or 'username' \ - if 'username' in table_user.fields else 'email' + if 'username' in table_user.fields else 'email' if userfield=='email': table_user.email.requires = [ IS_EMAIL(error_message=self.messages.invalid_email), @@ -3192,11 +3192,11 @@ class Auth(object): log = self.messages['change_password_log'] passfield = self.settings.password_field requires = table_user[passfield].requires - if not isinstance(requires,(list, tuple)): + if not isinstance(requires,(list, tuple)): requires = [requires] requires = filter(lambda t:isinstance(t,CRYPT), requires) if requires: - requires[0].min_length = 0 + requires[0].min_length = 0 form = SQLFORM.factory( Field('old_password', 'password', requires=requires, label=self.messages.old_password), @@ -3748,7 +3748,7 @@ class Auth(object): archive_current=False, fields=None): """ - If you have a table (db.mytable) that needs full revision history you + If you have a table (db.mytable) that needs full revision history you can just do:: form=crud.update(db.mytable,myrecord,onaccept=auth.archive) @@ -5366,8 +5366,8 @@ class Expose(object): base = base or os.path.join(current.request.folder, 'static') basename = basename or current.request.function self.basename = basename - - if current.request.raw_args: + + if current.request.raw_args: self.args = [arg for arg in current.request.raw_args.split('/') if arg] else: self.args = [arg for arg in current.request.args if args] @@ -5691,8 +5691,8 @@ class Wiki(object): def automenu(self): """adds the menu if not present""" - if (not self.wiki_menu_items and - self.settings.controller and + if (not self.wiki_menu_items and + self.settings.controller and self.settings.function): self.wiki_menu_items = self.menu(self.settings.controller, self.settings.function) diff --git a/gluon/validators.py b/gluon/validators.py index f98c6c0c..cb3806f0 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -22,7 +22,7 @@ import decimal import unicodedata from cStringIO import StringIO from gluon.utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE -from gluon.dal.objects import FieldVirtual, FieldMethod +from gluon.pydal.objects import FieldVirtual, FieldMethod regex_isint = re.compile('^[+-]?\d+$') @@ -506,7 +506,7 @@ class IS_IN_DB(Validator): sort=False, _and=None, ): - from dal.objects import Table + from pydal.objects import Table if isinstance(field, Table): field = field._id @@ -603,7 +603,7 @@ class IS_IN_DB(Validator): if not [v for v in values if not v in self.theset]: return (values, None) else: - from dal.adapters import GoogleDatastoreAdapter + from gluon.pydal.adapters import GoogleDatastoreAdapter def count(values, s=self.dbset, f=field): return s(f.belongs(map(int, values))).count() @@ -648,7 +648,7 @@ class IS_NOT_IN_DB(Validator): ignore_common_filters=False, ): - from dal.objects import Table + from pydal.objects import Table if isinstance(field, Table): field = field._id diff --git a/gluon/widget.py b/gluon/widget.py index b55f9a98..32aff00e 100644 --- a/gluon/widget.py +++ b/gluon/widget.py @@ -1086,7 +1086,7 @@ def start(cron=True): print ProgramAuthor print ProgramVersion - from dal.adapters.base import DRIVERS + from gluon.pydal.drivers import DRIVERS if not options.nobanner: print 'Database drivers available: %s' % ', '.join(DRIVERS) From 5a605f59b9e58688e2af1adcfd5f66a439e06db4 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 21:16:35 +0100 Subject: [PATCH 02/63] Started adding new tests for dal implementation --- gluon/tests/test_dal.py | 1666 +-------------------------------- gluon/tests/test_dal_nosql.py | 1409 ---------------------------- 2 files changed, 16 insertions(+), 3059 deletions(-) delete mode 100644 gluon/tests/test_dal_nosql.py diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index ec279e0d..07ea25ae 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -4,1665 +4,31 @@ Unit tests for gluon.dal """ -import sys -import os -import glob - import unittest -import datetime -try: - import cStringIO as StringIO -except: - from io import StringIO - from fix_path import fix_sys_path fix_sys_path(__file__) -#for travis-ci -DEFAULT_URI = os.getenv('DB', 'sqlite:memory') -print 'Testing against %s engine (%s)' % (DEFAULT_URI.partition(':')[0], DEFAULT_URI) - -from dal import DAL, Field -from dal.objects import Table -from dal.helpers.classes import SQLALL -from gluon.cache import CacheInRam - -ALLOWED_DATATYPES = [ - 'string', - 'text', - 'integer', - 'boolean', - 'double', - 'blob', - 'date', - 'time', - 'datetime', - 'upload', - 'password', - 'json', - 'bigint' - ] - -IS_POSTGRESQL = 'postgres' in DEFAULT_URI +from gluon.dal import DAL - -def setUpModule(): - pass - -def tearDownModule(): - if os.path.isfile('sql.log'): - os.unlink('sql.log') - for a in glob.glob('*.table'): - os.unlink(a) - - -class TestFields(unittest.TestCase): - - def testFieldName(self): - - # Check that Fields cannot start with underscores - self.assertRaises(SyntaxError, Field, '_abc', 'string') - - # Check that Fields cannot contain punctuation other than underscores - self.assertRaises(SyntaxError, Field, 'a.bc', 'string') - - # Check that Fields cannot be a name of a method or property of Table - for x in ['drop', 'on', 'truncate']: - self.assertRaises(SyntaxError, Field, x, 'string') - - # Check that Fields allows underscores in the body of a field name. - self.assert_(Field('a_bc', 'string'), - "Field isn't allowing underscores in fieldnames. It should.") - - def testFieldTypes(self): - - # Check that string, and password default length is 512 - for typ in ['string', 'password']: - self.assert_(Field('abc', typ).length == 512, - "Default length for type '%s' is not 512 or 255" % typ) - - # Check that upload default length is 512 - self.assert_(Field('abc', 'upload').length == 512, - "Default length for type 'upload' is not 512") - - # Check that Tables passed in the type creates a reference - self.assert_(Field('abc', Table(None, 'temp')).type - == 'reference temp', - 'Passing an Table does not result in a reference type.') - - def testFieldLabels(self): - - # Check that a label is successfully built from the supplied fieldname - self.assert_(Field('abc', 'string').label == 'Abc', - 'Label built is incorrect') - self.assert_(Field('abc_def', 'string').label == 'Abc Def', - 'Label built is incorrect') - - def testFieldFormatters(self): # Formatter should be called Validator - - # Test the default formatters - for typ in ALLOWED_DATATYPES: - f = Field('abc', typ) - if typ not in ['date', 'time', 'datetime']: - isinstance(f.formatter('test'), str) - else: - isinstance(f.formatter(datetime.datetime.now()), str) - +class TestDALSubclass(unittest.TestCase): def testRun(self): - """Test all field types and their return values""" - db = DAL(DEFAULT_URI, check_reserved=['all']) - for ft in ['string', 'text', 'password', 'upload', 'blob']: - db.define_table('tt', Field('aa', ft, default='')) - self.assertEqual(db.tt.insert(aa='x'), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 'x') - db.tt.drop() - db.define_table('tt', Field('aa', 'integer', default=1)) - self.assertEqual(db.tt.insert(aa=3), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3) - db.tt.drop() - db.define_table('tt', Field('aa', 'double', default=1)) - self.assertEqual(db.tt.insert(aa=3.1), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1) - db.tt.drop() - db.define_table('tt', Field('aa', 'boolean', default=True)) - self.assertEqual(db.tt.insert(aa=True), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, True) - db.tt.drop() - db.define_table('tt', Field('aa', 'json', default={})) - # test different python objects for correct serialization in json - objs = [ - {'a' : 1, 'b' : 2}, - [1, 2, 3], - 'abc', - True, - False, - None, - 11, - 14.3, - long(11) - ] - for obj in objs: - rtn_id = db.tt.insert(aa=obj) - rtn = db(db.tt.id == rtn_id).select().first().aa - self.assertEqual(obj, rtn) - db.tt.drop() - db.define_table('tt', Field('aa', 'date', - default=datetime.date.today())) - t0 = datetime.date.today() - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - db.tt.drop() - db.define_table('tt', Field('aa', 'datetime', - default=datetime.datetime.today())) - t0 = datetime.datetime( - 1971, - 12, - 21, - 10, - 30, - 55, - 0, - ) - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - - ## Row APIs - row = db().select(db.tt.aa)[0] - self.assertEqual(db.tt[1].aa,t0) - self.assertEqual(db.tt['aa'],db.tt.aa) - self.assertEqual(db.tt(1).aa,t0) - self.assertTrue(db.tt(1,aa=None)==None) - self.assertFalse(db.tt(1,aa=t0)==None) - self.assertEqual(row.aa,t0) - self.assertEqual(row['aa'],t0) - self.assertEqual(row['tt.aa'],t0) - self.assertEqual(row('tt.aa'),t0) - - ## Lazy and Virtual fields - db.tt.b = Field.Virtual(lambda row: row.tt.aa) - db.tt.c = Field.Lazy(lambda row: row.tt.aa) - row = db().select(db.tt.aa)[0] - self.assertEqual(row.b,t0) - self.assertEqual(row.c(),t0) - - db.tt.drop() - db.define_table('tt', Field('aa', 'time', default='11:30')) - t0 = datetime.time(10, 30, 55) - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - db.tt.drop() + import gluon.serializers as mserializers + import gluon.validators as mvalidators + from gluon.utils import web2py_uuid + from gluon import sqlhtml + db = DAL(check_reserved=['all']) + self.assertEqual(db.serializers, mserializers) + self.assertEqual(db.validators, mvalidators) + self.assertEqual(db.uuid, web2py_uuid) + self.assertEqual(db.representers['rows_render'], sqlhtml.represent) + self.assertEqual(db.representers['rows_xml'], sqlhtml.SQLTABLE) -class TestTables(unittest.TestCase): - - def testTableNames(self): - - # Check that Tables cannot start with underscores - self.assertRaises(SyntaxError, Table, None, '_abc') - - # Check that Tables cannot contain punctuation other than underscores - self.assertRaises(SyntaxError, Table, None, 'a.bc') - - # Check that Tables cannot be a name of a method or property of DAL - for x in ['define_table', 'tables', 'as_dict']: - self.assertRaises(SyntaxError, Table, None, x) - - # Check that Table allows underscores in the body of a field name. - self.assert_(Table(None, 'a_bc'), - "Table isn't allowing underscores in tablename. It should.") - - -class TestAll(unittest.TestCase): - - def setUp(self): - self.pt = Table(None,'PseudoTable',Field('name'),Field('birthdate')) - - def testSQLALL(self): - ans = 'PseudoTable.id, PseudoTable.name, PseudoTable.birthdate' - self.assertEqual(str(SQLALL(self.pt)), ans) - - -class TestTable(unittest.TestCase): - - def testTableCreation(self): - - # Check for error when not passing type other than Field or Table - - self.assertRaises(SyntaxError, Table, None, 'test', None) - - persons = Table(None, 'persons', - Field('firstname','string'), - Field('lastname', 'string')) - - # Does it have the correct fields? - - self.assert_(set(persons.fields).issuperset(set(['firstname', - 'lastname']))) - - # ALL is set correctly - - self.assert_('persons.firstname, persons.lastname' - in str(persons.ALL)) - - def testTableAlias(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - persons = Table(db, 'persons', Field('firstname', - 'string'), Field('lastname', 'string')) - aliens = persons.with_alias('aliens') - - # Are the different table instances with the same fields - - self.assert_(persons is not aliens) - self.assert_(set(persons.fields) == set(aliens.fields)) - - def testTableInheritance(self): - persons = Table(None, 'persons', Field('firstname', - 'string'), Field('lastname', 'string')) - customers = Table(None, 'customers', - Field('items_purchased', 'integer'), - persons) - self.assert_(set(customers.fields).issuperset(set( - ['items_purchased', 'firstname', 'lastname']))) - - -class TestInsert(unittest.TestCase): - +""" TODO: +class TestDefaultValidators(unittest.TestCase): def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(db.tt.insert(aa='1'), 1) - self.assertEqual(db.tt.insert(aa='1'), 2) - self.assertEqual(db.tt.insert(aa='1'), 3) - self.assertEqual(db(db.tt.aa == '1').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - self.assertEqual(db(db.tt.aa == '1').update(aa='2'), 3) - self.assertEqual(db(db.tt.aa == '2').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), False) - self.assertEqual(db(db.tt.aa == '2').delete(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - db.tt.drop() - - -class TestSelect(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(db.tt.insert(aa='1'), 1) - self.assertEqual(db.tt.insert(aa='2'), 2) - self.assertEqual(db.tt.insert(aa='3'), 3) - self.assertEqual(db(db.tt.id > 0).count(), 3) - self.assertEqual(db(db.tt.id > 0).select(orderby=~db.tt.aa - | db.tt.id)[0].aa, '3') - self.assertEqual(len(db(db.tt.id > 0).select(limitby=(1, 2))), 1) - self.assertEqual(db(db.tt.id > 0).select(limitby=(1, 2))[0].aa, - '2') - self.assertEqual(len(db().select(db.tt.ALL)), 3) - self.assertEqual(db(db.tt.aa == None).count(), 0) - self.assertEqual(db(db.tt.aa != None).count(), 3) - self.assertEqual(db(db.tt.aa > '1').count(), 2) - self.assertEqual(db(db.tt.aa >= '1').count(), 3) - self.assertEqual(db(db.tt.aa == '1').count(), 1) - self.assertEqual(db(db.tt.aa != '1').count(), 2) - self.assertEqual(db(db.tt.aa < '3').count(), 2) - self.assertEqual(db(db.tt.aa <= '3').count(), 3) - self.assertEqual(db(db.tt.aa > '1')(db.tt.aa < '3').count(), 1) - self.assertEqual(db((db.tt.aa > '1') & (db.tt.aa < '3')).count(), 1) - self.assertEqual(db((db.tt.aa > '1') | (db.tt.aa < '3')).count(), 3) - self.assertEqual(db((db.tt.aa > '1') & ~(db.tt.aa > '2')).count(), 1) - self.assertEqual(db(~(db.tt.aa > '1') & (db.tt.aa > '2')).count(), 0) - db.tt.drop() - -class TestAddMethod(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - @db.tt.add_method.all - def select_all(table,orderby=None): - return table._db(table).select(orderby=orderby) - self.assertEqual(db.tt.insert(aa='1'), 1) - self.assertEqual(db.tt.insert(aa='2'), 2) - self.assertEqual(db.tt.insert(aa='3'), 3) - self.assertEqual(len(db.tt.all()), 3) - db.tt.drop() - - -class TestBelongs(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(db.tt.insert(aa='1'), 1) - self.assertEqual(db.tt.insert(aa='2'), 2) - self.assertEqual(db.tt.insert(aa='3'), 3) - self.assertEqual(db(db.tt.aa.belongs(('1', '3'))).count(), - 2) - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.id - > 2)._select(db.tt.aa))).count(), 1) - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.aa.belongs(('1', - '3')))._select(db.tt.aa))).count(), 2) - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.aa.belongs(db - (db.tt.aa.belongs(('1', '3')))._select(db.tt.aa)))._select( - db.tt.aa))).count(), - 2) - db.tt.drop() - - -class TestContains(unittest.TestCase): - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'list:string'), Field('bb','string')) - self.assertEqual(db.tt.insert(aa=['aaa','bbb'],bb='aaa'), 1) - self.assertEqual(db.tt.insert(aa=['bbb','ddd'],bb='abb'), 2) - self.assertEqual(db.tt.insert(aa=['eee','aaa'],bb='acc'), 3) - self.assertEqual(db(db.tt.aa.contains('aaa')).count(), 2) - self.assertEqual(db(db.tt.aa.contains('bbb')).count(), 2) - self.assertEqual(db(db.tt.aa.contains('aa')).count(), 0) - self.assertEqual(db(db.tt.bb.contains('a')).count(), 3) - self.assertEqual(db(db.tt.bb.contains('b')).count(), 1) - self.assertEqual(db(db.tt.bb.contains('d')).count(), 0) - self.assertEqual(db(db.tt.aa.contains(db.tt.bb)).count(), 1) - db.tt.drop() - - -class TestLike(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(db.tt.insert(aa='abc'), 1) - self.assertEqual(db(db.tt.aa.like('a%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%b%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%c')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%d%')).count(), 0) - #DAL maps like() (and contains(), startswith(), endswith()) - #to the LIKE operator, that in ANSI-SQL is case-sensitive - #There are backends supporting case-sensitivity by default - #and backends that needs additional care to turn - #case-sensitivity on. To discern among those, let's run - #this query comparing previously inserted 'abc' with 'ABC': - #if the result is 0, then the backend recognizes - #case-sensitivity, if 1 it isn't - is_case_insensitive = db(db.tt.aa.like('%ABC%')).count() - if is_case_insensitive: - self.assertEqual(db(db.tt.aa.like('A%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%B%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%C')).count(), 1) - self.assertEqual(db(db.tt.aa.like('A%', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.like('%B%', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.like('%C', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('A%')).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('%B%')).count(),1) - self.assertEqual(db(db.tt.aa.upper().like('%C')).count(), 1) - else: - self.assertEqual(db(db.tt.aa.like('A%')).count(), 0) - self.assertEqual(db(db.tt.aa.like('%B%')).count(), 0) - self.assertEqual(db(db.tt.aa.like('%C')).count(), 0) - self.assertEqual(db(db.tt.aa.like('A%', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.like('%B%', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.like('%C', case_sensitive=False)).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('A%')).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('%B%')).count(),1) - self.assertEqual(db(db.tt.aa.upper().like('%C')).count(), 1) - - # startswith endswith tests - self.assertEqual(db(db.tt.aa.startswith('a')).count(), 1) - self.assertEqual(db(db.tt.aa.endswith('c')).count(), 1) - self.assertEqual(db(db.tt.aa.startswith('c')).count(), 0) - self.assertEqual(db(db.tt.aa.endswith('a')).count(), 0) - - db.tt.drop() - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(db.tt.insert(aa=1111111111), 1) - self.assertEqual(db(db.tt.aa.like('1%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('2%')).count(), 0) - db.tt.drop() - - -class TestDatetime(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'datetime')) - self.assertEqual(db.tt.insert(aa=datetime.datetime(1971, 12, 21, - 11, 30)), 1) - self.assertEqual(db.tt.insert(aa=datetime.datetime(1971, 11, 21, - 10, 30)), 2) - self.assertEqual(db.tt.insert(aa=datetime.datetime(1970, 12, 21, - 9, 30)), 3) - self.assertEqual(db(db.tt.aa == datetime.datetime(1971, 12, - 21, 11, 30)).count(), 1) - self.assertEqual(db(db.tt.aa.year() == 1971).count(), 2) - self.assertEqual(db(db.tt.aa.month() == 12).count(), 2) - self.assertEqual(db(db.tt.aa.day() == 21).count(), 3) - self.assertEqual(db(db.tt.aa.hour() == 11).count(), 1) - self.assertEqual(db(db.tt.aa.minutes() == 30).count(), 3) - self.assertEqual(db(db.tt.aa.seconds() == 0).count(), 3) - self.assertEqual(db(db.tt.aa.epoch()<365*24*3600).count(),1) - db.tt.drop() - - -class TestExpressions(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(db.tt.insert(aa=1), 1) - self.assertEqual(db.tt.insert(aa=2), 2) - self.assertEqual(db.tt.insert(aa=3), 3) - self.assertEqual(db(db.tt.aa == 3).update(aa=db.tt.aa + 1), 1) - self.assertEqual(db(db.tt.aa == 4).count(), 1) - self.assertEqual(db(db.tt.aa == -2).count(), 0) - sum = (db.tt.aa + 1).sum() - self.assertEqual(db(db.tt.aa == 2).select(sum).first()[sum], 3) - self.assertEqual(db(db.tt.aa == -2).select(sum).first()[sum], None) - db.tt.drop() - - def testSubstring(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - t0 = db.define_table('t0', Field('name')) - input_name = "web2py" - t0.insert(name=input_name) - exp_slice = t0.name.lower()[4:6] - exp_slice_no_max = t0.name.lower()[4:] - exp_slice_neg_max = t0.name.lower()[2:-2] - exp_slice_neg_start = t0.name.lower()[-2:] - exp_item = t0.name.lower()[3] - out = db(t0).select(exp_slice, exp_item, exp_slice_no_max, exp_slice_neg_max, exp_slice_neg_start).first() - self.assertEqual(out[exp_slice], input_name[4:6]) - self.assertEqual(out[exp_item], input_name[3]) - self.assertEqual(out[exp_slice_no_max], input_name[4:]) - self.assertEqual(out[exp_slice_neg_max], input_name[2:-2]) - self.assertEqual(out[exp_slice_neg_start], input_name[-2:]) - t0.drop() - return - - -class TestJoin(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('t1', Field('aa')) - db.define_table('t2', Field('aa'), Field('b', db.t1)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - db.t2.drop() - db.t1.drop() - - db.define_table('person',Field('name')) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name'),Field('ownerperson','reference person')) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - db.dog.drop() - self.assertEqual(len(db.person._referenced_by),0) - db.person.drop() - -class TestMinMaxSumAvg(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(db.tt.insert(aa=1), 1) - self.assertEqual(db.tt.insert(aa=2), 2) - self.assertEqual(db.tt.insert(aa=3), 3) - s = db.tt.aa.min() - self.assertEqual(db(db.tt.id > 0).select(s)[0]._extra[s], 1) - self.assertEqual(db(db.tt.id > 0).select(s).first()[s], 1) - self.assertEqual(db().select(s).first()[s], 1) - s = db.tt.aa.max() - self.assertEqual(db().select(s).first()[s], 3) - s = db.tt.aa.sum() - self.assertEqual(db().select(s).first()[s], 6) - s = db.tt.aa.count() - self.assertEqual(db().select(s).first()[s], 3) - s = db.tt.aa.avg() - self.assertEqual(db().select(s).first()[s], 2) - db.tt.drop() - - -class TestCacheSelect(unittest.TestCase): - def testRun(self): - cache = CacheInRam() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.tt.insert(aa='1') - r0 = db().select(db.tt.ALL) - r1 = db().select(db.tt.ALL, cache=(cache, 1000)) - self.assertEqual(len(r0),len(r1)) - r2 = db().select(db.tt.ALL, cache=(cache, 1000)) - self.assertEqual(len(r0),len(r2)) - r3 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True) - self.assertEqual(len(r0),len(r3)) - r4 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True) - self.assertEqual(len(r0),len(r4)) - db.tt.drop() - - -class TestMigrations(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), Field('b'), - migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), Field('b', 'text'), - migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), migrate='.storage.table') - db.tt.drop() - db.commit() - db.close() - - def tearDown(self): - if os.path.exists('.storage.db'): - os.unlink('.storage.db') - if os.path.exists('.storage.table'): - os.unlink('.storage.table') - -class TestReference(unittest.TestCase): - - def testRun(self): - for b in [True, False]: - db = DAL(DEFAULT_URI, check_reserved=['all'], bigint_id=b) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - db.define_table('tt', Field('name'), Field('aa','reference tt')) - db.commit() - x = db.tt.insert(name='max') - assert x.id == 1 - assert x['id'] == 1 - x.aa = x - assert x.aa == 1 - x.update_record() - y = db.tt[1] - assert y.aa == 1 - assert y.aa.aa.aa.aa.aa.aa.name == 'max' - z=db.tt.insert(name='xxx', aa = y) - assert z.aa == y.id - db.tt.drop() - db.commit() - -class TestClientLevelOps(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.commit() - db.tt.insert(aa="test") - rows1 = db(db.tt.id>0).select() - rows2 = db(db.tt.id>0).select() - rows3 = rows1 & rows2 - assert len(rows3) == 2 - rows4 = rows1 | rows2 - assert len(rows4) == 1 - rows5 = rows1.find(lambda row: row.aa=="test") - assert len(rows5) == 1 - rows6 = rows2.exclude(lambda row: row.aa=="test") - assert len(rows6) == 1 - rows7 = rows5.sort(lambda row: row.aa) - assert len(rows7) == 1 - db.tt.drop() - db.commit() - - -class TestVirtualFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.commit() - db.tt.insert(aa="test") - class Compute: - def a_upper(row): return row.tt.aa.upper() - db.tt.virtualfields.append(Compute()) - assert db(db.tt.id>0).select().first().a_upper == 'TEST' - db.tt.drop() - db.commit() - -class TestComputedFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', - Field('aa'), - Field('bb',default='x'), - Field('cc',compute=lambda r: r.aa+r.bb)) - db.commit() - id = db.tt.insert(aa="z") - self.assertEqual(db.tt[id].cc,'zx') - db.tt.drop() - db.commit() - - # test checking that a compute field can refer to earlier-defined computed fields - db.define_table('tt', - Field('aa'), - Field('bb',default='x'), - Field('cc',compute=lambda r: r.aa+r.bb), - Field('dd',compute=lambda r: r.bb + r.cc)) - db.commit() - id = db.tt.insert(aa="z") - self.assertEqual(db.tt[id].dd,'xzx') - db.tt.drop() - db.commit() - - -class TestCommonFilters(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('t1', Field('aa')) - db.define_table('t2', Field('aa'), Field('b', db.t1)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - db.t1._common_filter = lambda q: db.t1.aa>1 - self.assertEqual(db(db.t1).count(),2) - self.assertEqual(db(db.t1).count(),2) - q = db.t2.b==db.t1.id - self.assertEqual(db(q).count(),2) - self.assertEqual(db(q).count(),2) - self.assertEqual(len(db(db.t1).select(left=db.t2.on(q))),3) - db.t2._common_filter = lambda q: db.t2.aa<6 - self.assertEqual(db(q).count(),1) - self.assertEqual(db(q).count(),1) - self.assertEqual(len(db(db.t1).select(left=db.t2.on(q))),2) - db.t2.drop() - db.t1.drop() - -class TestImportExportFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name')) - db.define_table('pet',Field('friend',db.person),Field('name')) - for n in range(2): - db(db.pet).delete() - db(db.person).delete() - for k in range(10): - id = db.person.insert(name=str(k)) - db.pet.insert(friend=id,name=str(k)) - db.commit() - stream = StringIO.StringIO() - db.export_to_csv_file(stream) - db(db.pet).delete() - db(db.person).delete() - stream = StringIO.StringIO(stream.getvalue()) - db.import_from_csv_file(stream) - assert db(db.person.id==db.pet.friend)(db.person.name==db.pet.name).count()==10 - db.pet.drop() - db.person.drop() - db.commit() - -class TestImportExportUuidFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name'),Field('uuid')) - db.define_table('pet',Field('friend',db.person),Field('name')) - for n in range(2): - db(db.pet).delete() - db(db.person).delete() - for k in range(10): - id = db.person.insert(name=str(k),uuid=str(k)) - db.pet.insert(friend=id,name=str(k)) - db.commit() - stream = StringIO.StringIO() - db.export_to_csv_file(stream) - stream = StringIO.StringIO(stream.getvalue()) - db.import_from_csv_file(stream) - assert db(db.person).count()==10 - assert db(db.person.id==db.pet.friend)(db.person.name==db.pet.name).count()==20 - db.pet.drop() - db.person.drop() - db.commit() - - -class TestDALDictImportExport(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name', default="Michael"),Field('uuid')) - db.define_table('pet',Field('friend',db.person),Field('name')) - dbdict = db.as_dict(flat=True, sanitize=False) - assert isinstance(dbdict, dict) - uri = dbdict["uri"] - assert isinstance(uri, basestring) and uri - assert len(dbdict["tables"]) == 2 - assert len(dbdict["tables"][0]["fields"]) == 3 - assert dbdict["tables"][0]["fields"][1]["type"] == db.person.name.type - assert dbdict["tables"][0]["fields"][1]["default"] == db.person.name.default - - db2 = DAL(**dbdict) - assert len(db.tables) == len(db2.tables) - assert hasattr(db2, "pet") and isinstance(db2.pet, Table) - assert hasattr(db2.pet, "friend") and isinstance(db2.pet.friend, Field) - db.pet.drop() - db.commit() - - db2.commit() - - have_serializers = True - try: - import serializers - dbjson = db.as_json(sanitize=False) - assert isinstance(dbjson, basestring) and len(dbjson) > 0 - - unicode_keys = True - if sys.version < "2.6.5": - unicode_keys = False - db3 = DAL(**serializers.loads_json(dbjson, - unicode_keys=unicode_keys)) - assert hasattr(db3, "person") and hasattr(db3.person, "uuid") and\ - db3.person.uuid.type == db.person.uuid.type - db3.person.drop() - db3.commit() - except ImportError: - pass - - mpfc = "Monty Python's Flying Circus" - dbdict4 = {"uri": DEFAULT_URI, - "tables":[{"tablename": "tvshow", - "fields": [{"fieldname": "name", - "default":mpfc}, - {"fieldname": "rating", - "type":"double"}]}, - {"tablename": "staff", - "fields": [{"fieldname": "name", - "default":"Michael"}, - {"fieldname": "food", - "default":"Spam"}, - {"fieldname": "tvshow", - "type": "reference tvshow"}]}]} - db4 = DAL(**dbdict4) - assert "staff" in db4.tables - assert "name" in db4.staff - assert db4.tvshow.rating.type == "double" - assert (db4.tvshow.insert(), db4.tvshow.insert(name="Loriot"), - db4.tvshow.insert(name="Il Mattatore")) == (1, 2, 3) - assert db4(db4.tvshow).select().first().id == 1 - assert db4(db4.tvshow).select().first().name == mpfc - - db4.staff.drop() - db4.tvshow.drop() - db4.commit() - - dbdict5 = {"uri": DEFAULT_URI} - db5 = DAL(**dbdict5) - assert db5.tables in ([], None) - assert not (str(db5) in ("", None)) - - dbdict6 = {"uri": DEFAULT_URI, - "tables":[{"tablename": "staff"}, - {"tablename": "tvshow", - "fields": [{"fieldname": "name"}, - {"fieldname": "rating", "type":"double"} - ] - }] - } - db6 = DAL(**dbdict6) - - assert len(db6["staff"].fields) == 1 - assert "name" in db6["tvshow"].fields - - assert db6.staff.insert() is not None - assert db6(db6.staff).select().first().id == 1 - - - db6.staff.drop() - db6.tvshow.drop() - db6.commit() - - -class TestValidateAndInsert(unittest.TestCase): - - def testRun(self): - import datetime - from gluon.validators import IS_INT_IN_RANGE - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('val_and_insert', - Field('aa'), - Field('bb', 'integer', - requires=IS_INT_IN_RANGE(1,5)) - ) - rtn = db.val_and_insert.validate_and_insert(aa='test1', bb=2) - self.assertEqual(rtn.id, 1) - #errors should be empty - self.assertEqual(len(rtn.errors.keys()), 0) - #this insert won't pass - rtn = db.val_and_insert.validate_and_insert(bb="a") - #the returned id should be None - self.assertEqual(rtn.id, None) - #an error message should be in rtn.errors.bb - self.assertNotEqual(rtn.errors.bb, None) - #cleanup table - db.val_and_insert.drop() - -class TestSelectAsDict(unittest.TestCase): - - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table( - 'a_table', - Field('b_field'), - Field('a_field'), - ) - db.a_table.insert(a_field="aa1", b_field="bb1") - rtn = db.executesql("SELECT id, b_field, a_field FROM a_table", as_dict=True) - self.assertEqual(rtn[0]['b_field'], 'bb1') - rtn = db.executesql("SELECT id, b_field, a_field FROM a_table", as_ordered_dict=True) - self.assertEqual(rtn[0]['b_field'], 'bb1') - self.assertEqual(rtn[0].keys(), ['id', 'b_field', 'a_field']) - db.a_table.drop() - - -class TestRNameTable(unittest.TestCase): - #tests for highly experimental rname attribute - - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'a very complicated tablename' - db.define_table( - 'easy_name', - Field('a_field'), - rname=rname - ) - rtn = db.easy_name.insert(a_field='a') - self.assertEqual(rtn.id, 1) - rtn = db(db.easy_name.a_field == 'a').select() - self.assertEqual(len(rtn), 1) - self.assertEqual(rtn[0].id, 1) - self.assertEqual(rtn[0].a_field, 'a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).delete() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 0) - db.easy_name.insert(a_field='a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.a_field == 'a').update(a_field='c') - rtn = db(db.easy_name.a_field == 'c').count() - self.assertEqual(rtn, 1) - rtn = db(db.easy_name.a_field != 'c').count() - self.assertEqual(rtn, 1) - avg = db.easy_name.id.avg() - rtn = db(db.easy_name.id > 0).select(avg) - self.assertEqual(rtn[0][avg], 3) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is the person table' - db.define_table( - 'person', - Field('name', default="Michael"), - Field('uuid'), - rname=rname - ) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is the pet table' - db.define_table( - 'pet', - Field('friend','reference person'), - Field('name'), - rname=rname - ) - michael = db.person.insert() #default insert - john = db.person.insert(name='John') - luke = db.person.insert(name='Luke') - - #michael owns Phippo - phippo = db.pet.insert(friend=michael, name="Phippo") - #john owns Dunstin and Gertie - dunstin = db.pet.insert(friend=john, name="Dunstin") - gertie = db.pet.insert(friend=john, name="Gertie") - - rtn = db(db.person.id == db.pet.friend).select(orderby=db.person.id|db.pet.id) - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[1].person.id, john) - self.assertEqual(rtn[1].person.name, 'John') - self.assertEqual(rtn[1].pet.name, 'Dunstin') - self.assertEqual(rtn[2].pet.name, 'Gertie') - #fetch owners, eventually with pet - #main point is retrieving Luke with no pets - rtn = db(db.person.id > 0).select( - orderby=db.person.id|db.pet.id, - left=db.pet.on(db.person.id == db.pet.friend) - ) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[3].person.name, 'Luke') - self.assertEqual(rtn[3].person.id, luke) - self.assertEqual(rtn[3].pet.name, None) - #lets test a subquery - subq = db(db.pet.name == "Gertie")._select(db.pet.friend) - rtn = db(db.person.id.belongs(subq)).select() - self.assertEqual(rtn[0].id, 2) - self.assertEqual(rtn[0]('person.name'), 'John') - #as dict - rtn = db(db.person.id > 0).select().as_dict() - self.assertEqual(rtn[1]['name'], 'Michael') - #as list - rtn = db(db.person.id > 0).select().as_list() - self.assertEqual(rtn[0]['name'], 'Michael') - #isempty - rtn = db(db.person.id > 0).isempty() - self.assertEqual(rtn, False) - #join argument - rtn = db(db.person).select(orderby=db.person.id|db.pet.id, - join=db.pet.on(db.person.id==db.pet.friend)) - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[1].person.id, john) - self.assertEqual(rtn[1].person.name, 'John') - self.assertEqual(rtn[1].pet.name, 'Dunstin') - self.assertEqual(rtn[2].pet.name, 'Gertie') - - #aliases - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'the cubs' - db.define_table('pet_farm', - Field('name'), - Field('father','reference pet_farm'), - Field('mother','reference pet_farm'), - rname=rname - ) - - minali = db.pet_farm.insert(name='Minali') - osbert = db.pet_farm.insert(name='Osbert') - #they had a cub - selina = db.pet_farm.insert(name='Selina', father=osbert, mother=minali) - - father = db.pet_farm.with_alias('father') - mother = db.pet_farm.with_alias('mother') - - #fetch pets with relatives - rtn = db().select( - db.pet_farm.name, father.name, mother.name, - left=[ - father.on(father.id == db.pet_farm.father), - mother.on(mother.id == db.pet_farm.mother) - ], - orderby=db.pet_farm.id - ) - - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].pet_farm.name, 'Minali') - self.assertEqual(rtn[0].father.name, None) - self.assertEqual(rtn[0].mother.name, None) - self.assertEqual(rtn[1].pet_farm.name, 'Osbert') - self.assertEqual(rtn[2].pet_farm.name, 'Selina') - self.assertEqual(rtn[2].father.name, 'Osbert') - self.assertEqual(rtn[2].mother.name, 'Minali') - - #clean up - db.pet_farm.drop() - db.pet.drop() - db.person.drop() - db.easy_name.drop() - - def testJoin(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is table t1' - rname2 = db._adapter.__class__.QUOTE_TEMPLATE % 'this is table t2' - db.define_table('t1', Field('aa'), rname=rname) - db.define_table('t2', Field('aa'), Field('b', db.t1), rname=rname2) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - db.t2.drop() - db.t1.drop() - - db.define_table('person',Field('name'), rname=rname) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name'),Field('ownerperson','reference person'), rname=rname2) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - db.dog.drop() - self.assertEqual(len(db.person._referenced_by),0) - db.person.drop() - - -class TestRNameFields(unittest.TestCase): - # tests for highly experimental rname attribute - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'a very complicated fieldname' - rname2 = db._adapter.__class__.QUOTE_TEMPLATE % 'rrating from 1 to 10' - db.define_table( - 'easy_name', - Field('a_field', rname=rname), - Field('rating', 'integer', rname=rname2, default=2) - ) - rtn = db.easy_name.insert(a_field='a') - self.assertEqual(rtn.id, 1) - rtn = db(db.easy_name.a_field == 'a').select() - self.assertEqual(len(rtn), 1) - self.assertEqual(rtn[0].id, 1) - self.assertEqual(rtn[0].a_field, 'a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).delete() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 0) - db.easy_name.insert(a_field='a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.a_field == 'a').update(a_field='c') - rtn = db(db.easy_name.a_field == 'c').count() - self.assertEqual(rtn, 1) - rtn = db(db.easy_name.a_field != 'c').count() - self.assertEqual(rtn, 1) - avg = db.easy_name.id.avg() - rtn = db(db.easy_name.id > 0).select(avg) - self.assertEqual(rtn[0][avg], 3) - - avg = db.easy_name.rating.avg() - rtn = db(db.easy_name.id > 0).select(avg) - self.assertEqual(rtn[0][avg], 2) - - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is the person name' - db.define_table( - 'person', - Field('name', default="Michael", rname=rname), - Field('uuid') - ) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is the pet name' - db.define_table( - 'pet', - Field('friend','reference person'), - Field('name', rname=rname) - ) - michael = db.person.insert() #default insert - john = db.person.insert(name='John') - luke = db.person.insert(name='Luke') - - #michael owns Phippo - phippo = db.pet.insert(friend=michael, name="Phippo") - #john owns Dunstin and Gertie - dunstin = db.pet.insert(friend=john, name="Dunstin") - gertie = db.pet.insert(friend=john, name="Gertie") - - rtn = db(db.person.id == db.pet.friend).select(orderby=db.person.id|db.pet.id) - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[1].person.id, john) - self.assertEqual(rtn[1].person.name, 'John') - self.assertEqual(rtn[1].pet.name, 'Dunstin') - self.assertEqual(rtn[2].pet.name, 'Gertie') - #fetch owners, eventually with pet - #main point is retrieving Luke with no pets - rtn = db(db.person.id > 0).select( - orderby=db.person.id|db.pet.id, - left=db.pet.on(db.person.id == db.pet.friend) - ) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[3].person.name, 'Luke') - self.assertEqual(rtn[3].person.id, luke) - self.assertEqual(rtn[3].pet.name, None) - #lets test a subquery - subq = db(db.pet.name == "Gertie")._select(db.pet.friend) - rtn = db(db.person.id.belongs(subq)).select() - self.assertEqual(rtn[0].id, 2) - self.assertEqual(rtn[0]('person.name'), 'John') - #as dict - rtn = db(db.person.id > 0).select().as_dict() - self.assertEqual(rtn[1]['name'], 'Michael') - #as list - rtn = db(db.person.id > 0).select().as_list() - self.assertEqual(rtn[0]['name'], 'Michael') - #isempty - rtn = db(db.person.id > 0).isempty() - self.assertEqual(rtn, False) - #join argument - rtn = db(db.person).select(orderby=db.person.id|db.pet.id, - join=db.pet.on(db.person.id==db.pet.friend)) - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].person.id, michael) - self.assertEqual(rtn[0].person.name, 'Michael') - self.assertEqual(rtn[0].pet.id, phippo) - self.assertEqual(rtn[0].pet.name, 'Phippo') - self.assertEqual(rtn[1].person.id, john) - self.assertEqual(rtn[1].person.name, 'John') - self.assertEqual(rtn[1].pet.name, 'Dunstin') - self.assertEqual(rtn[2].pet.name, 'Gertie') - - #aliases - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'the cub name' - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - db.define_table('pet_farm', - Field('name', rname=rname), - Field('father','reference pet_farm'), - Field('mother','reference pet_farm'), - ) - - minali = db.pet_farm.insert(name='Minali') - osbert = db.pet_farm.insert(name='Osbert') - #they had a cub - selina = db.pet_farm.insert(name='Selina', father=osbert, mother=minali) - - father = db.pet_farm.with_alias('father') - mother = db.pet_farm.with_alias('mother') - - #fetch pets with relatives - rtn = db().select( - db.pet_farm.name, father.name, mother.name, - left=[ - father.on(father.id == db.pet_farm.father), - mother.on(mother.id == db.pet_farm.mother) - ], - orderby=db.pet_farm.id - ) - - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].pet_farm.name, 'Minali') - self.assertEqual(rtn[0].father.name, None) - self.assertEqual(rtn[0].mother.name, None) - self.assertEqual(rtn[1].pet_farm.name, 'Osbert') - self.assertEqual(rtn[2].pet_farm.name, 'Selina') - self.assertEqual(rtn[2].father.name, 'Osbert') - self.assertEqual(rtn[2].mother.name, 'Minali') - - #clean up - db.pet_farm.drop() - db.pet.drop() - db.person.drop() - db.easy_name.drop() - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'a very complicated fieldname' - for ft in ['string', 'text', 'password', 'upload', 'blob']: - db.define_table('tt', Field('aa', ft, default='', rname=rname)) - self.assertEqual(db.tt.insert(aa='x'), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 'x') - db.tt.drop() - db.define_table('tt', Field('aa', 'integer', default=1, rname=rname)) - self.assertEqual(db.tt.insert(aa=3), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3) - db.tt.drop() - db.define_table('tt', Field('aa', 'double', default=1, rname=rname)) - self.assertEqual(db.tt.insert(aa=3.1), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1) - db.tt.drop() - db.define_table('tt', Field('aa', 'boolean', default=True, rname=rname)) - self.assertEqual(db.tt.insert(aa=True), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, True) - db.tt.drop() - db.define_table('tt', Field('aa', 'json', default={}, rname=rname)) - self.assertEqual(db.tt.insert(aa={}), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, {}) - db.tt.drop() - db.define_table('tt', Field('aa', 'date', - default=datetime.date.today(), rname=rname)) - t0 = datetime.date.today() - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - db.tt.drop() - db.define_table('tt', Field('aa', 'datetime', - default=datetime.datetime.today(), rname=rname)) - t0 = datetime.datetime( - 1971, - 12, - 21, - 10, - 30, - 55, - 0, - ) - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - - ## Row APIs - row = db().select(db.tt.aa)[0] - self.assertEqual(db.tt[1].aa,t0) - self.assertEqual(db.tt['aa'],db.tt.aa) - self.assertEqual(db.tt(1).aa,t0) - self.assertTrue(db.tt(1,aa=None)==None) - self.assertFalse(db.tt(1,aa=t0)==None) - self.assertEqual(row.aa,t0) - self.assertEqual(row['aa'],t0) - self.assertEqual(row['tt.aa'],t0) - self.assertEqual(row('tt.aa'),t0) - - ## Lazy and Virtual fields - db.tt.b = Field.Virtual(lambda row: row.tt.aa) - db.tt.c = Field.Lazy(lambda row: row.tt.aa) - row = db().select(db.tt.aa)[0] - self.assertEqual(row.b,t0) - self.assertEqual(row.c(),t0) - - db.tt.drop() - db.define_table('tt', Field('aa', 'time', default='11:30', rname=rname)) - t0 = datetime.time(10, 30, 55) - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - db.tt.drop() - - def testInsert(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'a very complicated fieldname' - db.define_table('tt', Field('aa', rname=rname)) - self.assertEqual(db.tt.insert(aa='1'), 1) - self.assertEqual(db.tt.insert(aa='1'), 2) - self.assertEqual(db.tt.insert(aa='1'), 3) - self.assertEqual(db(db.tt.aa == '1').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - self.assertEqual(db(db.tt.aa == '1').update(aa='2'), 3) - self.assertEqual(db(db.tt.aa == '2').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), False) - self.assertEqual(db(db.tt.aa == '2').delete(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - db.tt.drop() - - def testJoin(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.__class__.QUOTE_TEMPLATE % 'this is field aa' - rname2 = db._adapter.__class__.QUOTE_TEMPLATE % 'this is field b' - db.define_table('t1', Field('aa', rname=rname)) - db.define_table('t2', Field('aa', rname=rname), Field('b', db.t1, rname=rname2)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - db.t2.drop() - db.t1.drop() - - db.define_table('person',Field('name', rname=rname)) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name', rname=rname),Field('ownerperson','reference person', rname=rname2)) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - db.dog.drop() - self.assertEqual(len(db.person._referenced_by),0) - db.person.drop() - -class TestQuoting(unittest.TestCase): - - # tests for case sensitivity - def testCase(self): - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=False, entity_quoting=True) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - - t0 = db.define_table('t0', - Field('f', 'string')) - t1 = db.define_table('b', - Field('B', t0), - Field('words', 'text')) - - blather = 'blah blah and so' - t0[0] = {'f': 'content'} - t1[0] = {'B': int(t0[1]['id']), - 'words': blather} - - r = db(db.t0.id==db.b.B).select() - - self.assertEqual(r[0].b.words, blather) - - t1.drop() - t0.drop() - - # test field case - try: - t0 = db.define_table('table_is_a_test', - Field('a_a'), - Field('a_A')) - except Exception, e: - # some db does not support case sensitive field names mysql is one of them. - if DEFAULT_URI.startswith('mysql:') or DEFAULT_URI.startswith('sqlite:'): - db.rollback() - return - raise e - - t0[0] = dict(a_a = 'a_a', a_A='a_A') - - self.assertEqual(t0[1].a_a, 'a_a') - self.assertEqual(t0[1].a_A, 'a_A') - - t0.drop() - - def testPKFK(self): - - # test primary keys - - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=False) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - # test table without surrogate key. Length must is limited to - # 100 because of MySQL limitations: it cannot handle more than - # 767 bytes in unique keys. - - t0 = db.define_table('t0', Field('Code', length=100), primarykey=['Code']) - t2 = db.define_table('t2', Field('f'), Field('t0_Code', 'reference t0')) - t3 = db.define_table('t3', Field('f', length=100), Field('t0_Code', t0.Code), primarykey=['f']) - t4 = db.define_table('t4', Field('f', length=100), Field('t0', t0), primarykey=['f']) - - try: - t5 = db.define_table('t5', Field('f', length=100), Field('t0', 'reference no_table_wrong_reference'), primarykey=['f']) - except Exception, e: - self.assertTrue(isinstance(e, KeyError)) - - if DEFAULT_URI.startswith('mssql'): - #there's no drop cascade in mssql - t3.drop() - t4.drop() - t2.drop() - t0.drop() - else: - t0.drop('cascade') - t2.drop() - t3.drop() - t4.drop() - - -class TestTableAndFieldCase(unittest.TestCase): - """ - at the Python level we should not allow db.C and db.c because of .table conflicts on windows - but it should be possible to map two different names into distinct tables "c" and "C" at the Python level - By default Python models names should be mapped into lower case table names and assume case insensitivity. - """ - def testme(self): - return - - -class TestQuotesByDefault(unittest.TestCase): - """ - all default tables names should be quoted unless an explicit mapping has been given for a table. - """ - def testme(self): - return - - -class TestGis(unittest.TestCase): - - def testGeometry(self): - from gluon.dal import geoPoint, geoLine, geoPolygon - if not IS_POSTGRESQL: return - db = DAL(DEFAULT_URI, check_reserved=['all']) - t0 = db.define_table('t0', Field('point', 'geometry()')) - t1 = db.define_table('t1', Field('line', 'geometry(public, 4326, 2)')) - t2 = db.define_table('t2', Field('polygon', 'geometry(public, 4326, 2)')) - t0.insert(point=geoPoint(1,1)) - text = db(db.t0.id).select(db.t0.point.st_astext()).first()[db.t0.point.st_astext()] - self.assertEqual(text, "POINT(1 1)") - t1.insert(line=geoLine((1,1),(2,2))) - text = db(db.t1.id).select(db.t1.line.st_astext()).first()[db.t1.line.st_astext()] - self.assertEqual(text, "LINESTRING(1 1,2 2)") - t2.insert(polygon=geoPolygon((0,0),(2,0),(2,2),(0,2),(0,0))) - text = db(db.t2.id).select(db.t2.polygon.st_astext()).first()[db.t2.polygon.st_astext()] - self.assertEqual(text, "POLYGON((0 0,2 0,2 2,0 2,0 0))") - query = t0.point.st_intersects(geoLine((0,0),(2,2))) - output = db(query).select(db.t0.point).first()[db.t0.point] - self.assertEqual(output, "POINT(1 1)") - query = t2.polygon.st_contains(geoPoint(1,1)) - n = db(query).count() - self.assertEqual(n, 1) - x=t0.point.st_x() - y=t0.point.st_y() - point = db(t0.id).select(x, y).first() - self.assertEqual(point[x], 1) - self.assertEqual(point[y], 1) - t0.drop() - t1.drop() - t2.drop() - return - - def testGeometryCase(self): - from gluon.dal import geoPoint, geoLine, geoPolygon - if not IS_POSTGRESQL: return - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=False) - t0 = db.define_table('t0', Field('point', 'geometry()'), Field('Point', 'geometry()')) - t0.insert(point=geoPoint(1,1)) - t0.insert(Point=geoPoint(2,2)) - t0.drop() - - def testGisMigration(self): - if not IS_POSTGRESQL: return - for b in [True, False]: - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=b) - t0 = db.define_table('t0', Field('Point', 'geometry()')) - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=b) - t0 = db.define_table('t0', Field('New_point', 'geometry()')) - t0.drop() - db.commit() - db.close() - return - -class TestSQLCustomType(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - from dal.helpers.classes import SQLCustomType - native_double = "double" - native_string = "string" - if hasattr(db._adapter, 'types'): - native_double = db._adapter.types['double'] - native_string = db._adapter.types['string'] % {'length': 256} - basic_t = SQLCustomType(type = "double", native = native_double) - basic_t_str = SQLCustomType(type = "string", native = native_string) - t0=db.define_table('t0', Field("price", basic_t), Field("product", basic_t_str)) - r_id = t0.insert(price=None, product=None) - row = db(t0.id == r_id).select(t0.ALL).first() - self.assertEqual(row['price'], None) - self.assertEqual(row['product'], None) - r_id = t0.insert(price=1.2, product="car") - row=db(t0.id == r_id).select(t0.ALL).first() - self.assertEqual(row['price'], 1.2) - self.assertEqual(row['product'], 'car') - t0.drop() - import zlib - compressed = SQLCustomType( - type ='text', - native='text', - encoder =(lambda x: zlib.compress(x or '', 1)), - decoder = (lambda x: zlib.decompress(x)) - ) - t1=db.define_table('t0',Field('cdata', compressed)) - #r_id=t1.insert(cdata="car") - #row=db(t1.id == r_id).select(t1.ALL).first() - #self.assertEqual(row['cdata'], "'car'") - t1.drop() - -class TestLazy(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all'], lazy_tables=True) - t0 = db.define_table('t0', Field('name')) - self.assertTrue(('t0' in db._LAZY_TABLES.keys())) - db.t0.insert(name='1') - self.assertFalse(('t0' in db._LAZY_TABLES.keys())) - db.t0.drop() - return - -if __name__ == '__main__': - unittest.main() - tearDownModule() + pass +""" diff --git a/gluon/tests/test_dal_nosql.py b/gluon/tests/test_dal_nosql.py deleted file mode 100644 index 980ac3cc..00000000 --- a/gluon/tests/test_dal_nosql.py +++ /dev/null @@ -1,1409 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" - Unit tests for gluon.dal (NoSQL adapters) -""" - -import sys -import os -import glob - -if sys.version < "2.7": - import unittest2 as unittest -else: - import unittest - -import datetime -try: - import cStringIO as StringIO -except: - from io import StringIO - -from fix_path import fix_sys_path - -fix_sys_path(__file__) - -#for travis-ci -DEFAULT_URI = os.environ.get('DB', 'sqlite:memory') -print 'Testing against %s engine (%s)' % (DEFAULT_URI.partition(':')[0], DEFAULT_URI) - -IS_GAE = "datastore" in DEFAULT_URI -IS_MONGODB = "mongodb" in DEFAULT_URI -IS_IMAP = "imap" in DEFAULT_URI - -if IS_IMAP: - from dal.adapters import IMAPAdapter - from contrib import mockimaplib - IMAPAdapter.driver = mockimaplib - -from dal import DAL, Field -from dal.objects import Table -from dal.helpers.classes import SQLALL - -def drop(table, cascade=None): - # mongodb implements drop() - # although it seems it does not work properly - if (IS_GAE or IS_MONGODB or IS_IMAP): - # GAE drop/cleanup is not implemented - db = table._db - db(table).delete() - del db[table._tablename] - del db.tables[db.tables.index(table._tablename)] - db._remove_references_to(table) - else: - if cascade: - table.drop(cascade) - else: - table.drop() - - -# setup GAE dummy database -if IS_GAE: - from google.appengine.ext import testbed - gaetestbed = testbed.Testbed() - gaetestbed.activate() - gaetestbed.init_datastore_v3_stub() - - -ALLOWED_DATATYPES = [ - 'string', - 'text', - 'integer', - 'boolean', - 'double', - 'blob', - 'date', - 'time', - 'datetime', - 'upload', - 'password', - 'json', - ] - - -def setUpModule(): - pass - -def tearDownModule(): - if os.path.isfile('sql.log'): - os.unlink('sql.log') - for a in glob.glob('*.table'): - os.unlink(a) - -@unittest.skipIf(IS_GAE or IS_IMAP, 'TODO: Datastore throws "AssertionError: SyntaxError not raised"') -class TestFields(unittest.TestCase): - - def testFieldName(self): - - # Check that Fields cannot start with underscores - self.assertRaises(SyntaxError, Field, '_abc', 'string') - - # Check that Fields cannot contain punctuation other than underscores - self.assertRaises(SyntaxError, Field, 'a.bc', 'string') - - # Check that Fields cannot be a name of a method or property of Table - for x in ['drop', 'on', 'truncate']: - self.assertRaises(SyntaxError, Field, x, 'string') - - # Check that Fields allows underscores in the body of a field name. - self.assert_(Field('a_bc', 'string'), - "Field isn't allowing underscores in fieldnames. It should.") - - def testFieldTypes(self): - - # Check that string, and password default length is 512 - for typ in ['string', 'password']: - self.assert_(Field('abc', typ).length == 512, - "Default length for type '%s' is not 512 or 255" % typ) - - # Check that upload default length is 512 - self.assert_(Field('abc', 'upload').length == 512, - "Default length for type 'upload' is not 512") - - # Check that Tables passed in the type creates a reference - self.assert_(Field('abc', Table(None, 'temp')).type - == 'reference temp', - 'Passing an Table does not result in a reference type.') - - def testFieldLabels(self): - - # Check that a label is successfully built from the supplied fieldname - self.assert_(Field('abc', 'string').label == 'Abc', - 'Label built is incorrect') - self.assert_(Field('abc_def', 'string').label == 'Abc Def', - 'Label built is incorrect') - - def testFieldFormatters(self): # Formatter should be called Validator - - # Test the default formatters - for typ in ALLOWED_DATATYPES: - f = Field('abc', typ) - if typ not in ['date', 'time', 'datetime']: - isinstance(f.formatter('test'), str) - else: - isinstance(f.formatter(datetime.datetime.now()), str) - - @unittest.skipIf(IS_GAE or IS_MONGODB, 'TODO: Datastore does accept dict objects as json field input. MongoDB assertion error Binary("x", 0) != "x"') - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - for ft in ['string', 'text', 'password', 'upload', 'blob']: - db.define_table('tt', Field('aa', ft, default='')) - self.assertEqual(isinstance(db.tt.insert(aa='x'), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 'x') - drop(db.tt) - db.define_table('tt', Field('aa', 'integer', default=1)) - self.assertEqual(isinstance(db.tt.insert(aa=3), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3) - drop(db.tt) - db.define_table('tt', Field('aa', 'double', default=1)) - self.assertEqual(isinstance(db.tt.insert(aa=3.1), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1) - drop(db.tt) - db.define_table('tt', Field('aa', 'boolean', default=True)) - self.assertEqual(isinstance(db.tt.insert(aa=True), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, True) - drop(db.tt) - db.define_table('tt', Field('aa', 'json', default={})) - self.assertEqual(isinstance(db.tt.insert(aa={}), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, {}) - drop(db.tt) - db.define_table('tt', Field('aa', 'date', - default=datetime.date.today())) - t0 = datetime.date.today() - self.assertEqual(isinstance(db.tt.insert(aa=t0), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - drop(db.tt) - db.define_table('tt', Field('aa', 'datetime', - default=datetime.datetime.today())) - t0 = datetime.datetime( - 1971, - 12, - 21, - 10, - 30, - 55, - 0, - ) - self.assertEqual(isinstance(db.tt.insert(aa=t0), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - - ## Row APIs - row = db().select(db.tt.aa)[0] - self.assertEqual(db.tt[1].aa,t0) - self.assertEqual(db.tt['aa'],db.tt.aa) - self.assertEqual(db.tt(1).aa,t0) - self.assertTrue(db.tt(1,aa=None)==None) - self.assertFalse(db.tt(1,aa=t0)==None) - self.assertEqual(row.aa,t0) - self.assertEqual(row['aa'],t0) - self.assertEqual(row['tt.aa'],t0) - self.assertEqual(row('tt.aa'),t0) - - ## Lazy and Virtual fields - db.tt.b = Field.Virtual(lambda row: row.tt.aa) - db.tt.c = Field.Lazy(lambda row: row.tt.aa) - row = db().select(db.tt.aa)[0] - self.assertEqual(row.b,t0) - self.assertEqual(row.c(),t0) - - drop(db.tt) - db.define_table('tt', Field('aa', 'time', default='11:30')) - t0 = datetime.time(10, 30, 55) - self.assertEqual(isinstance(db.tt.insert(aa=t0), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - drop(db.tt) - - -@unittest.skipIf(IS_GAE or IS_IMAP, 'TODO: Datastore throws "AssertionError: SyntaxError not raised"') -class TestTables(unittest.TestCase): - - def testTableNames(self): - - # Check that Tables cannot start with underscores - self.assertRaises(SyntaxError, Table, None, '_abc') - - # Check that Tables cannot contain punctuation other than underscores - self.assertRaises(SyntaxError, Table, None, 'a.bc') - - # Check that Tables cannot be a name of a method or property of DAL - for x in ['define_table', 'tables', 'as_dict']: - self.assertRaises(SyntaxError, Table, None, x) - - # Check that Table allows underscores in the body of a field name. - self.assert_(Table(None, 'a_bc'), - "Table isn't allowing underscores in tablename. It should.") - -@unittest.skipIf(IS_IMAP, "Skip IMAP") -class TestAll(unittest.TestCase): - - def setUp(self): - self.pt = Table(None,'PseudoTable',Field('name'),Field('birthdate')) - - def testSQLALL(self): - ans = 'PseudoTable.id, PseudoTable.name, PseudoTable.birthdate' - self.assertEqual(str(SQLALL(self.pt)), ans) - -@unittest.skipIf(IS_IMAP, "Skip IMAP") -class TestTable(unittest.TestCase): - - def testTableCreation(self): - - # Check for error when not passing type other than Field or Table - - self.assertRaises(SyntaxError, Table, None, 'test', None) - - persons = Table(None, 'persons', - Field('firstname','string'), - Field('lastname', 'string')) - - # Does it have the correct fields? - - self.assert_(set(persons.fields).issuperset(set(['firstname', - 'lastname']))) - - # ALL is set correctly - - self.assert_('persons.firstname, persons.lastname' - in str(persons.ALL)) - - @unittest.skipIf(IS_GAE or IS_MONGODB, "No table alias for this backend") - def testTableAlias(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - persons = Table(db, 'persons', Field('firstname', - 'string'), Field('lastname', 'string')) - aliens = persons.with_alias('aliens') - - # Are the different table instances with the same fields - - self.assert_(persons is not aliens) - self.assert_(set(persons.fields) == set(aliens.fields)) - - def testTableInheritance(self): - persons = Table(None, 'persons', Field('firstname', - 'string'), Field('lastname', 'string')) - customers = Table(None, 'customers', - Field('items_purchased', 'integer'), - persons) - self.assert_(set(customers.fields).issuperset(set( - ['items_purchased', 'firstname', 'lastname']))) - - -class TestInsert(unittest.TestCase): - def testRun(self): - if IS_IMAP: - imap = DAL(DEFAULT_URI) - imap.define_tables() - self.assertEqual(imap.Draft.insert(to="nurse@example.com", - subject="Nurse!", - sender="gumby@example.com", - content="Nurse!\r\nNurse!"), 2) - self.assertEqual(imap.Draft[2].subject, "Nurse!") - self.assertEqual(imap.Draft[2].sender, "gumby@example.com") - self.assertEqual(isinstance(imap.Draft[2].uid, long), True) - self.assertEqual(imap.Draft[2].content[0]["text"], "Nurse!\r\nNurse!") - else: - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(db(db.tt.aa == '1').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - self.assertEqual(db(db.tt.aa == '1').update(aa='2'), 3) - self.assertEqual(db(db.tt.aa == '2').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), False) - self.assertEqual(db(db.tt.aa == '2').delete(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - drop(db.tt) - - -@unittest.skipIf(IS_GAE or IS_MONGODB or IS_IMAP, 'TODO: Datastore throws "SyntaxError: Not supported (query using or)". MongoDB assertionerror 5L != 3') -class TestSelect(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='2'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='3'), long), True) - self.assertEqual(db(db.tt.id > 0).count(), 3) - self.assertEqual(db(db.tt.id > 0).select(orderby=~db.tt.aa - | db.tt.id)[0].aa, '3') - self.assertEqual(len(db(db.tt.id > 0).select(limitby=(1, 2))), 1) - self.assertEqual(db(db.tt.id > 0).select(limitby=(1, 2))[0].aa, - '2') - self.assertEqual(len(db().select(db.tt.ALL)), 3) - self.assertEqual(db(db.tt.aa == None).count(), 0) - self.assertEqual(db(db.tt.aa != None).count(), 3) - self.assertEqual(db(db.tt.aa > '1').count(), 2) - self.assertEqual(db(db.tt.aa >= '1').count(), 3) - self.assertEqual(db(db.tt.aa == '1').count(), 1) - self.assertEqual(db(db.tt.aa != '1').count(), 2) - self.assertEqual(db(db.tt.aa < '3').count(), 2) - self.assertEqual(db(db.tt.aa <= '3').count(), 3) - self.assertEqual(db(db.tt.aa > '1')(db.tt.aa < '3').count(), 1) - self.assertEqual(db((db.tt.aa > '1') & (db.tt.aa < '3')).count(), 1) - self.assertEqual(db((db.tt.aa > '1') | (db.tt.aa < '3')).count(), 3) - self.assertEqual(db((db.tt.aa > '1') & ~(db.tt.aa > '2')).count(), 1) - self.assertEqual(db(~(db.tt.aa > '1') & (db.tt.aa > '2')).count(), 0) - drop(db.tt) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestAddMethod(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - @db.tt.add_method.all - def select_all(table,orderby=None): - return table._db(table).select(orderby=orderby) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='2'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='3'), long), True) - self.assertEqual(len(db.tt.all()), 3) - drop(db.tt) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestBelongs(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='2'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='3'), long), True) - self.assertEqual(db(db.tt.aa.belongs(('1', '3'))).count(), - 2) - if not (IS_GAE or IS_MONGODB): - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.id > 2)._select(db.tt.aa))).count(), 1) - - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.aa.belongs(('1', - '3')))._select(db.tt.aa))).count(), 2) - self.assertEqual(db(db.tt.aa.belongs(db(db.tt.aa.belongs(db - (db.tt.aa.belongs(('1', '3')))._select(db.tt.aa)))._select( - db.tt.aa))).count(), - 2) - else: - print "Datastore/Mongodb belongs does not accept queries (skipping)" - drop(db.tt) - - -@unittest.skipIf(IS_GAE or IS_IMAP, "Contains not supported on GAE Datastore. TODO: IMAP tests") -class TestContains(unittest.TestCase): - @unittest.skipIf(IS_MONGODB, "TODO: MongoDB Contains error") - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'list:string'), Field('bb','string')) - self.assertEqual(isinstance(db.tt.insert(aa=['aaa','bbb'],bb='aaa'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=['bbb','ddd'],bb='abb'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=['eee','aaa'],bb='acc'), long), True) - self.assertEqual(db(db.tt.aa.contains('aaa')).count(), 2) - self.assertEqual(db(db.tt.aa.contains('bbb')).count(), 2) - self.assertEqual(db(db.tt.aa.contains('aa')).count(), 0) - self.assertEqual(db(db.tt.bb.contains('a')).count(), 3) - self.assertEqual(db(db.tt.bb.contains('b')).count(), 1) - self.assertEqual(db(db.tt.bb.contains('d')).count(), 0) - self.assertEqual(db(db.tt.aa.contains(db.tt.bb)).count(), 1) - drop(db.tt) - - -@unittest.skipIf(IS_GAE or IS_MONGODB or IS_IMAP, "Like not supported on GAE Datastore. TODO: IMAP test") -class TestLike(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - self.assertEqual(isinstance(db.tt.insert(aa='abc'), long), True) - self.assertEqual(db(db.tt.aa.like('a%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%b%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%c')).count(), 1) - self.assertEqual(db(db.tt.aa.like('%d%')).count(), 0) - self.assertEqual(db(db.tt.aa.lower().like('A%')).count(), 1) - self.assertEqual(db(db.tt.aa.lower().like('%B%')).count(), - 1) - self.assertEqual(db(db.tt.aa.lower().like('%C')).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('A%')).count(), 1) - self.assertEqual(db(db.tt.aa.upper().like('%B%')).count(), - 1) - self.assertEqual(db(db.tt.aa.upper().like('%C')).count(), 1) - drop(db.tt) - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(isinstance(db.tt.insert(aa=1111111111), long), True) - self.assertEqual(db(db.tt.aa.like('1%')).count(), 1) - self.assertEqual(db(db.tt.aa.like('2%')).count(), 0) - drop(db.tt) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestDatetime(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'datetime')) - self.assertEqual(isinstance(db.tt.insert(aa=datetime.datetime(1971, 12, 21, - 11, 30)), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=datetime.datetime(1971, 11, 21, - 10, 30)), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=datetime.datetime(1970, 12, 21, - 9, 30)), long), True) - self.assertEqual(db(db.tt.aa == datetime.datetime(1971, 12, - 21, 11, 30)).count(), 1) - self.assertEqual(db(db.tt.aa >= datetime.datetime(1971, 1, 1)).count(), 2) - drop(db.tt) - -@unittest.skipIf(IS_GAE or IS_MONGODB or IS_IMAP, "Expressions are not supported") -class TestExpressions(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(isinstance(db.tt.insert(aa=1), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=2), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=3), long), True) - self.assertEqual(db(db.tt.aa == 3).update(aa=db.tt.aa + 1), 1) - self.assertEqual(db(db.tt.aa == 4).count(), 1) - self.assertEqual(db(db.tt.aa == -2).count(), 0) - sum = (db.tt.aa + 1).sum() - self.assertEqual(db(db.tt.aa == 2).select(sum).first()[sum], 3) - self.assertEqual(db(db.tt.aa == -2).select(sum).first()[sum], None) - drop(db.tt) - - -@unittest.skip("JOIN queries are not supported") -class TestJoin(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('t1', Field('aa')) - db.define_table('t2', Field('aa'), Field('b', db.t1)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - drop(db.t2) - drop(db.t1) - - db.define_table('person',Field('name')) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name'),Field('ownerperson','reference person')) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - drop(db.dog) - self.assertEqual(len(db.person._referenced_by),0) - drop(db.person) - -class TestMinMaxSumAvg(unittest.TestCase): - @unittest.skipIf(IS_GAE or IS_MONGODB or IS_IMAP, 'TODO: Datastore throws "AttributeError: Row object has no attribute _extra"') - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa', 'integer')) - self.assertEqual(isinstance(db.tt.insert(aa=1), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=2), long), True) - self.assertEqual(isinstance(db.tt.insert(aa=3), long), True) - s = db.tt.aa.min() - self.assertEqual(db(db.tt.id > 0).select(s)[0]._extra[s], 1) - self.assertEqual(db(db.tt.id > 0).select(s).first()[s], 1) - self.assertEqual(db().select(s).first()[s], 1) - s = db.tt.aa.max() - self.assertEqual(db().select(s).first()[s], 3) - s = db.tt.aa.sum() - self.assertEqual(db().select(s).first()[s], 6) - s = db.tt.aa.count() - self.assertEqual(db().select(s).first()[s], 3) - s = db.tt.aa.avg() - self.assertEqual(db().select(s).first()[s], 2) - drop(db.tt) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestCache(unittest.TestCase): - def testRun(self): - from cache import CacheInRam - cache = CacheInRam() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.tt.insert(aa='1') - r0 = db().select(db.tt.ALL) - r1 = db().select(db.tt.ALL, cache=(cache, 1000)) - self.assertEqual(len(r0),len(r1)) - r2 = db().select(db.tt.ALL, cache=(cache, 1000)) - self.assertEqual(len(r0),len(r2)) - r3 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True) - self.assertEqual(len(r0),len(r3)) - r4 = db().select(db.tt.ALL, cache=(cache, 1000), cacheable=True) - self.assertEqual(len(r0),len(r4)) - drop(db.tt) - -@unittest.skipIf(IS_IMAP, "Skip IMAP") -class TestMigrations(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), Field('b'), - migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), Field('b', 'text'), - migrate='.storage.table') - db.commit() - db.close() - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa'), migrate='.storage.table') - drop(db.tt) - db.commit() - db.close() - - def tearDown(self): - if os.path.exists('.storage.db'): - os.unlink('.storage.db') - if os.path.exists('.storage.table'): - os.unlink('.storage.table') - -class TestReference(unittest.TestCase): - @unittest.skipIf(IS_MONGODB or IS_IMAP, "TODO: MongoDB assertion error (long object has no attribute id)") - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - db.define_table('tt', Field('name'), Field('aa','reference tt')) - db.commit() - x = db.tt.insert(name='max') - assert isinstance(x.id, long) == True - assert isinstance(x['id'], long) == True - x.aa = x - assert isinstance(x.aa, long) == True - x.update_record() - y = db.tt[x.id] - assert y.aa == x.aa - assert y.aa.aa.aa.aa.aa.aa.name == 'max' - z=db.tt.insert(name='xxx', aa = y) - assert z.aa == y.id - drop(db.tt) - db.commit() - -@unittest.skipIf(IS_IMAP, "Skip IMAP") -class TestClientLevelOps(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.commit() - db.tt.insert(aa="test") - rows1 = db(db.tt.aa=='test').select() - rows2 = db(db.tt.aa=='test').select() - rows3 = rows1 & rows2 - assert len(rows3) == 2 - rows4 = rows1 | rows2 - assert len(rows4) == 1 - rows5 = rows1.find(lambda row: row.aa=="test") - assert len(rows5) == 1 - rows6 = rows2.exclude(lambda row: row.aa=="test") - assert len(rows6) == 1 - rows7 = rows5.sort(lambda row: row.aa) - assert len(rows7) == 1 - drop(db.tt) - db.commit() - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestVirtualFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', Field('aa')) - db.commit() - db.tt.insert(aa="test") - class Compute: - def a_upper(row): return row.tt.aa.upper() - db.tt.virtualfields.append(Compute()) - assert db(db.tt.id>0).select().first().a_upper == 'TEST' - drop(db.tt) - db.commit() - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestComputedFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('tt', - Field('aa'), - Field('bb',default='x'), - Field('cc',compute=lambda r: r.aa+r.bb)) - db.commit() - id = db.tt.insert(aa="z") - self.assertEqual(db.tt[id].cc,'zx') - drop(db.tt) - db.commit() - - # test checking that a compute field can refer to earlier-defined computed fields - db.define_table('tt', - Field('aa'), - Field('bb',default='x'), - Field('cc',compute=lambda r: r.aa+r.bb), - Field('dd',compute=lambda r: r.bb + r.cc)) - db.commit() - id = db.tt.insert(aa="z") - self.assertEqual(db.tt[id].dd,'xzx') - drop(db.tt) - db.commit() - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestCommonFilters(unittest.TestCase): - - @unittest.skipIf(IS_MONGODB, "TODO: MongoDB Assertion error") - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('t1', Field('aa')) - # db.define_table('t2', Field('aa'), Field('b', db.t1)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - # db.t2.insert(aa='4', b=i1) - # db.t2.insert(aa='5', b=i2) - # db.t2.insert(aa='6', b=i2) - db.t1._common_filter = lambda q: db.t1.aa>'1' - self.assertEqual(db(db.t1).count(),2) - # self.assertEqual(db(db.t1).count(),2) - # q = db.t2.b==db.t1.id - # q = db.t1.aa != None - # self.assertEqual(db(q).count(),2) - # self.assertEqual(db(q).count(),2) - # self.assertEqual(len(db(db.t1).select(left=db.t2.on(q))),3) - # db.t2._common_filter = lambda q: db.t2.aa<6 - # self.assertEqual(db(q).count(),1) - # self.assertEqual(db(q).count(),1) - # self.assertEqual(len(db(db.t1).select(left=db.t2.on(q))),2) - # drop(db.t2) - drop(db.t1) - -@unittest.skipIf(IS_IMAP, "Skip IMAP test") -class TestImportExportFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name')) - db.define_table('pet',Field('friend',db.person),Field('name')) - for n in range(2): - db(db.pet).delete() - db(db.person).delete() - for k in range(10): - id = db.person.insert(name=str(k)) - db.pet.insert(friend=id,name=str(k)) - db.commit() - stream = StringIO.StringIO() - db.export_to_csv_file(stream) - db(db.pet).delete() - db(db.person).delete() - stream = StringIO.StringIO(stream.getvalue()) - db.import_from_csv_file(stream) - assert db(db.person).count()==10 - assert db(db.pet.name).count()==10 - drop(db.pet) - drop(db.person) - db.commit() - -@unittest.skipIf(IS_IMAP, "Skip IMAP test") -class TestImportExportUuidFields(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name'),Field('uuid')) - db.define_table('pet',Field('friend',db.person),Field('name')) - for n in range(2): - db(db.pet).delete() - db(db.person).delete() - for k in range(10): - id = db.person.insert(name=str(k),uuid=str(k)) - db.pet.insert(friend=id,name=str(k)) - db.commit() - stream = StringIO.StringIO() - db.export_to_csv_file(stream) - db(db.person).delete() - db(db.pet).delete() - stream = StringIO.StringIO(stream.getvalue()) - db.import_from_csv_file(stream) - assert db(db.person).count()==10 - assert db(db.pet).count()==10 - drop(db.pet) - drop(db.person) - db.commit() - -@unittest.skipIf(IS_IMAP, "Skip IMAP test") -class TestDALDictImportExport(unittest.TestCase): - - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('person', Field('name', default="Michael"),Field('uuid')) - db.define_table('pet',Field('friend',db.person),Field('name')) - dbdict = db.as_dict(flat=True, sanitize=False) - assert isinstance(dbdict, dict) - uri = dbdict["uri"] - assert isinstance(uri, basestring) and uri - assert len(dbdict["tables"]) == 2 - assert len(dbdict["tables"][0]["fields"]) == 3 - assert dbdict["tables"][0]["fields"][1]["type"] == db.person.name.type - assert dbdict["tables"][0]["fields"][1]["default"] == db.person.name.default - - db2 = DAL(**dbdict) - assert len(db.tables) == len(db2.tables) - assert hasattr(db2, "pet") and isinstance(db2.pet, Table) - assert hasattr(db2.pet, "friend") and isinstance(db2.pet.friend, Field) - drop(db.pet) - db.commit() - - db2.commit() - - have_serializers = True - try: - import serializers - dbjson = db.as_json(sanitize=False) - assert isinstance(dbjson, basestring) and len(dbjson) > 0 - - unicode_keys = True - if sys.version < "2.6.5": - unicode_keys = False - db3 = DAL(**serializers.loads_json(dbjson, - unicode_keys=unicode_keys)) - assert hasattr(db3, "person") and hasattr(db3.person, "uuid") and\ - db3.person.uuid.type == db.person.uuid.type - drop(db3.person) - db3.commit() - except ImportError: - pass - - mpfc = "Monty Python's Flying Circus" - dbdict4 = {"uri": DEFAULT_URI, - "tables":[{"tablename": "tvshow", - "fields": [{"fieldname": "name", - "default":mpfc}, - {"fieldname": "rating", - "type":"double"}]}, - {"tablename": "staff", - "fields": [{"fieldname": "name", - "default":"Michael"}, - {"fieldname": "food", - "default":"Spam"}, - {"fieldname": "tvshow", - "type": "reference tvshow"}]}]} - db4 = DAL(**dbdict4) - assert "staff" in db4.tables - assert "name" in db4.staff - assert db4.tvshow.rating.type == "double" - assert (isinstance(db4.tvshow.insert(), long), isinstance(db4.tvshow.insert(name="Loriot"), long), - isinstance(db4.tvshow.insert(name="Il Mattatore"), long)) == (True, True, True) - assert isinstance(db4(db4.tvshow).select().first().id, long) == True - assert db4(db4.tvshow).select().first().name == mpfc - - drop(db4.staff) - drop(db4.tvshow) - db4.commit() - - dbdict5 = {"uri": DEFAULT_URI} - db5 = DAL(**dbdict5) - assert db5.tables in ([], None) - assert not (str(db5) in ("", None)) - - dbdict6 = {"uri": DEFAULT_URI, - "tables":[{"tablename": "staff"}, - {"tablename": "tvshow", - "fields": [{"fieldname": "name"}, - {"fieldname": "rating", "type":"double"} - ] - }] - } - db6 = DAL(**dbdict6) - - assert len(db6["staff"].fields) == 1 - assert "name" in db6["tvshow"].fields - - assert db6.staff.insert() is not None - assert isinstance(db6(db6.staff).select().first().id, long) == True - - - drop(db6.staff) - drop(db6.tvshow) - db6.commit() - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestValidateAndInsert(unittest.TestCase): - - def testRun(self): - import datetime - from gluon.validators import IS_INT_IN_RANGE - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table('val_and_insert', - Field('aa'), - Field('bb', 'integer', - requires=IS_INT_IN_RANGE(1,5)) - ) - rtn = db.val_and_insert.validate_and_insert(aa='test1', bb=2) - self.assertEqual(isinstance(rtn.id, long), True) - #errors should be empty - self.assertEqual(len(rtn.errors.keys()), 0) - #this insert won't pass - rtn = db.val_and_insert.validate_and_insert(bb="a") - #the returned id should be None - self.assertEqual(rtn.id, None) - #an error message should be in rtn.errors.bb - self.assertNotEqual(rtn.errors.bb, None) - #cleanup table - drop(db.val_and_insert) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestSelectAsDict(unittest.TestCase): - - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - db.define_table( - 'a_table', - Field('b_field'), - Field('a_field'), - ) - db.a_table.insert(a_field="aa1", b_field="bb1") - rtn = db(db.a_table).select(db.a_table.id, db.a_table.b_field, db.a_table.a_field).as_list() - self.assertEqual(rtn[0]['b_field'], 'bb1') - keys = rtn[0].keys() - self.assertEqual(len(keys), 3) - self.assertEqual(("id" in keys, "b_field" in keys, "a_field" in keys), (True, True, True)) - drop(db.a_table) - - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestRNameTable(unittest.TestCase): - #tests for highly experimental rname attribute - @unittest.skipIf(IS_MONGODB, "TODO: MongoDB assertion error (long object has no attribute id)") - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'a very complicated tablename' - db.define_table( - 'easy_name', - Field('a_field'), - rname=rname - ) - rtn = db.easy_name.insert(a_field='a') - self.assertEqual(isinstance(rtn.id, long), True) - rtn = db(db.easy_name.a_field == 'a').select() - self.assertEqual(len(rtn), 1) - self.assertEqual(isinstance(rtn[0].id, long), True) - self.assertEqual(rtn[0].a_field, 'a') - db.easy_name.insert(a_field='b') - self.assertEqual(db(db.easy_name).count(), 2) - rtn = db(db.easy_name.a_field == 'a').update(a_field='c') - self.assertEqual(rtn, 1) - - #clean up - drop(db.easy_name) - - @unittest.skip("JOIN queries are not supported") - def testJoin(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'this is table t1' - rname2 = db._adapter.QUOTE_TEMPLATE % 'this is table t2' - db.define_table('t1', Field('aa'), rname=rname) - db.define_table('t2', Field('aa'), Field('b', db.t1), rname=rname2) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - drop(db.t2) - drop(db.t1) - - db.define_table('person',Field('name'), rname=rname) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name'),Field('ownerperson','reference person'), rname=rname2) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - drop(db.dog) - self.assertEqual(len(db.person._referenced_by),0) - drop(db.person) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestRNameFields(unittest.TestCase): - # tests for highly experimental rname attribute - @unittest.skipIf(IS_GAE or IS_MONGODB, 'TODO: Datastore throws unsupported error for AGGREGATE. MongoDB assertion error (long object has no attribute id)') - def testSelect(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'a very complicated fieldname' - rname2 = db._adapter.QUOTE_TEMPLATE % 'rrating from 1 to 10' - db.define_table( - 'easy_name', - Field('a_field', rname=rname), - Field('rating', 'integer', rname=rname2, default=2) - ) - rtn = db.easy_name.insert(a_field='a') - self.assertEqual(isinstance(rtn.id, long), True) - rtn = db(db.easy_name.a_field == 'a').select() - self.assertEqual(len(rtn), 1) - self.assertEqual(isinstance(rtn[0].id, long), True) - self.assertEqual(rtn[0].a_field, 'a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).delete() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 0) - db.easy_name.insert(a_field='a') - db.easy_name.insert(a_field='b') - rtn = db(db.easy_name.id > 0).count() - self.assertEqual(rtn, 2) - rtn = db(db.easy_name.a_field == 'a').update(a_field='c') - rtn = db(db.easy_name.a_field == 'c').count() - self.assertEqual(rtn, 1) - rtn = db(db.easy_name.a_field != 'c').count() - self.assertEqual(rtn, 1) - avg = db.easy_name.id.avg() - rtn = db(db.easy_name.id > 0).select(avg) - self.assertEqual(rtn[0][avg], 3) - - avg = db.easy_name.rating.avg() - rtn = db(db.easy_name.id > 0).select(avg) - self.assertEqual(rtn[0][avg], 2) - - rname = db._adapter.QUOTE_TEMPLATE % 'this is the person name' - db.define_table( - 'person', - Field('name', default="Michael", rname=rname), - Field('uuid') - ) - michael = db.person.insert() #default insert - john = db.person.insert(name='John') - luke = db.person.insert(name='Luke') - - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].id, michael) - self.assertEqual(rtn[0].name, 'Michael') - self.assertEqual(rtn[1].id, john) - self.assertEqual(rtn[1].name, 'John') - #fetch owners, eventually with pet - #main point is retrieving Luke with no pets - rtn = db(db.person.id > 0).select() - self.assertEqual(rtn[0].id, michael) - self.assertEqual(rtn[0].name, 'Michael') - self.assertEqual(rtn[3].name, 'Luke') - self.assertEqual(rtn[3].id, luke) - #as dict - rtn = db(db.person.id > 0).select().as_dict() - self.assertEqual(rtn[1]['name'], 'Michael') - #as list - rtn = db(db.person.id > 0).select().as_list() - self.assertEqual(rtn[0]['name'], 'Michael') - #isempty - rtn = db(db.person.id > 0).isempty() - self.assertEqual(rtn, False) - - #aliases - rname = db._adapter.QUOTE_TEMPLATE % 'the cub name' - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - db.define_table('pet_farm', - Field('name', rname=rname), - Field('father','reference pet_farm'), - Field('mother','reference pet_farm'), - ) - - minali = db.pet_farm.insert(name='Minali') - osbert = db.pet_farm.insert(name='Osbert') - - #they had a cub - selina = db.pet_farm.insert(name='Selina', father=osbert, mother=minali) - - father = db.pet_farm.with_alias('father') - mother = db.pet_farm.with_alias('mother') - - #fetch pets with relatives - rtn = db().select( - db.pet_farm.name, father.name, mother.name, - left=[ - father.on(father.id == db.pet_farm.father), - mother.on(mother.id == db.pet_farm.mother) - ], - orderby=db.pet_farm.id - ) - - self.assertEqual(len(rtn), 3) - self.assertEqual(rtn[0].pet_farm.name, 'Minali') - self.assertEqual(rtn[0].father.name, None) - self.assertEqual(rtn[0].mother.name, None) - self.assertEqual(rtn[1].pet_farm.name, 'Osbert') - self.assertEqual(rtn[2].pet_farm.name, 'Selina') - self.assertEqual(rtn[2].father.name, 'Osbert') - self.assertEqual(rtn[2].mother.name, 'Minali') - - #clean up - drop(db.pet_farm) - drop(db.person) - drop(db.easy_name) - - @unittest.skipIf(IS_GAE or IS_MONGODB, 'TODO: Datastore does not accept dict objects as json field input. MongoDB assertionerror Binary("x", 0) != "x"') - def testRun(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'a very complicated fieldname' - for ft in ['string', 'text', 'password', 'upload', 'blob']: - db.define_table('tt', Field('aa', ft, default='', rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa='x'), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 'x') - drop(db.tt) - db.define_table('tt', Field('aa', 'integer', default=1, rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa=3), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3) - drop(db.tt) - db.define_table('tt', Field('aa', 'double', default=1, rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa=3.1), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, 3.1) - drop(db.tt) - db.define_table('tt', Field('aa', 'boolean', default=True, rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa=True), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, True) - drop(db.tt) - db.define_table('tt', Field('aa', 'json', default={}, rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa={}), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, {}) - drop(db.tt) - db.define_table('tt', Field('aa', 'date', - default=datetime.date.today(), rname=rname)) - t0 = datetime.date.today() - self.assertEqual(isinstance(db.tt.insert(aa=t0), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - drop(db.tt) - db.define_table('tt', Field('aa', 'datetime', - default=datetime.datetime.today(), rname=rname)) - t0 = datetime.datetime( - 1971, - 12, - 21, - 10, - 30, - 55, - 0, - ) - self.assertEqual(db.tt.insert(aa=t0), 1) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - - ## Row APIs - row = db().select(db.tt.aa)[0] - self.assertEqual(db.tt[1].aa,t0) - self.assertEqual(db.tt['aa'],db.tt.aa) - self.assertEqual(db.tt(1).aa,t0) - self.assertTrue(db.tt(1,aa=None)==None) - self.assertFalse(db.tt(1,aa=t0)==None) - self.assertEqual(row.aa,t0) - self.assertEqual(row['aa'],t0) - self.assertEqual(row['tt.aa'],t0) - self.assertEqual(row('tt.aa'),t0) - - ## Lazy and Virtual fields - db.tt.b = Field.Virtual(lambda row: row.tt.aa) - db.tt.c = Field.Lazy(lambda row: row.tt.aa) - row = db().select(db.tt.aa)[0] - self.assertEqual(row.b,t0) - self.assertEqual(row.c(),t0) - - drop(db.tt) - db.define_table('tt', Field('aa', 'time', default='11:30', rname=rname)) - t0 = datetime.time(10, 30, 55) - self.assertEqual(isinstance(db.tt.insert(aa=t0), long), True) - self.assertEqual(db().select(db.tt.aa)[0].aa, t0) - drop(db.tt) - - def testInsert(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'a very complicated fieldname' - db.define_table('tt', Field('aa', rname=rname)) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(isinstance(db.tt.insert(aa='1'), long), True) - self.assertEqual(db(db.tt.aa == '1').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - self.assertEqual(db(db.tt.aa == '1').update(aa='2'), 3) - self.assertEqual(db(db.tt.aa == '2').count(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), False) - self.assertEqual(db(db.tt.aa == '2').delete(), 3) - self.assertEqual(db(db.tt.aa == '2').isempty(), True) - drop(db.tt) - - @unittest.skip("JOIN queries are not supported") - def testJoin(self): - db = DAL(DEFAULT_URI, check_reserved=['all']) - rname = db._adapter.QUOTE_TEMPLATE % 'this is field aa' - rname2 = db._adapter.QUOTE_TEMPLATE % 'this is field b' - db.define_table('t1', Field('aa', rname=rname)) - db.define_table('t2', Field('aa', rname=rname), Field('b', db.t1, rname=rname2)) - i1 = db.t1.insert(aa='1') - i2 = db.t1.insert(aa='2') - i3 = db.t1.insert(aa='3') - db.t2.insert(aa='4', b=i1) - db.t2.insert(aa='5', b=i2) - db.t2.insert(aa='6', b=i2) - self.assertEqual(len(db(db.t1.id - == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)), 3) - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db(db.t1.id == db.t2.b).select(orderby=db.t1.aa - | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(len(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)), 4) - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t1.aa, '2') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[2].t2.aa, '6') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t1.aa, '3') - self.assertEqual(db().select(db.t1.ALL, db.t2.ALL, - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa | db.t2.aa)[3].t2.aa, None) - self.assertEqual(len(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, groupby=db.t1.aa)), - 3) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[0]._extra[db.t2.id.count()], - 1) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[1]._extra[db.t2.id.count()], - 2) - self.assertEqual(db().select(db.t1.aa, db.t2.id.count(), - left=db.t2.on(db.t1.id == db.t2.b), - orderby=db.t1.aa, - groupby=db.t1.aa)[2]._extra[db.t2.id.count()], - 0) - drop(db.t2) - drop(db.t1) - - db.define_table('person',Field('name', rname=rname)) - id = db.person.insert(name="max") - self.assertEqual(id.name,'max') - db.define_table('dog',Field('name', rname=rname),Field('ownerperson','reference person', rname=rname2)) - db.dog.insert(name='skipper',ownerperson=1) - row = db(db.person.id==db.dog.ownerperson).select().first() - self.assertEqual(row[db.person.name],'max') - self.assertEqual(row['person.name'],'max') - drop(db.dog) - self.assertEqual(len(db.person._referenced_by),0) - drop(db.person) - -@unittest.skipIf(IS_IMAP, "TODO: IMAP test") -class TestQuoting(unittest.TestCase): - - # tests for case sensitivity - def testCase(self): - return - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=False) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - - # test table case - t0 = db.define_table('B', - Field('f', 'string')) - try: - t1 = db.define_table('b', - Field('B', t0), - Field('words', 'text')) - except Exception, e: - # An error is expected when database does not support case - # sensitive entity names. - if DEFAULT_URI.startswith('sqlite:'): - self.assertTrue(isinstance(e, db._adapter.driver.OperationalError)) - return - raise e - - blather = 'blah blah and so' - t0[0] = {'f': 'content'} - t1[0] = {'B': int(t0[1]['id']), - 'words': blather} - - r = db(db.B.id==db.b.B).select() - - self.assertEqual(r[0].b.words, blather) - - drop(t1) - drop(t0) - - # test field case - try: - t0 = db.define_table('table is a test', - Field('a_a'), - Field('a_A')) - except Exception, e: - # some db does not support case sensitive field names mysql is one of them. - if DEFAULT_URI.startswith('mysql:'): - db.rollback() - return - raise e - - t0[0] = dict(a_a = 'a_a', a_A='a_A') - - self.assertEqual(t0[1].a_a, 'a_a') - self.assertEqual(t0[1].a_A, 'a_A') - - drop(t0) - - def testPKFK(self): - - # test primary keys - - db = DAL(DEFAULT_URI, check_reserved=['all'], ignore_field_case=False) - if DEFAULT_URI.startswith('mssql'): - #multiple cascade gotcha - for key in ['reference','reference FK']: - db._adapter.types[key]=db._adapter.types[key].replace( - '%(on_delete_action)s','NO ACTION') - # test table without surrogate key. Length must is limited to - # 100 because of MySQL limitations: it cannot handle more than - # 767 bytes in unique keys. - - t0 = db.define_table('t0', Field('Code', length=100), primarykey=['Code']) - t2 = db.define_table('t2', Field('f'), Field('t0_Code', 'reference t0')) - t3 = db.define_table('t3', Field('f', length=100), Field('t0_Code', t0.Code), primarykey=['f']) - t4 = db.define_table('t4', Field('f', length=100), Field('t0', t0), primarykey=['f']) - - try: - t5 = db.define_table('t5', Field('f', length=100), Field('t0', 'reference no_table_wrong_reference'), primarykey=['f']) - except Exception, e: - self.assertTrue(isinstance(e, KeyError)) - - if DEFAULT_URI.startswith('mssql'): - #there's no drop cascade in mssql - drop(t3) - drop(t4) - drop(t2) - drop(t0) - else: - drop(t0, 'cascade') - drop(t2) - drop(t3) - drop(t4) - - -class TestTableAndFieldCase(unittest.TestCase): - """ - at the Python level we should not allow db.C and db.c because of .table conflicts on windows - but it should be possible to map two different names into distinct tables "c" and "C" at the Python level - By default Python models names should be mapped into lower case table names and assume case insensitivity. - """ - def testme(self): - return - - -class TestQuotesByDefault(unittest.TestCase): - """ - all default tables names should be quoted unless an explicit mapping has been given for a table. - """ - def testme(self): - return - -if __name__ == '__main__': - unittest.main() - tearDownModule() - From f60c1dff93652b7e423e5aaa2aef800a23dcb4b1 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 22:24:04 +0100 Subject: [PATCH 03/63] Fixing imports for new pydal --- gluon/__init__.py | 2 +- gluon/compileapp.py | 2 +- gluon/contrib/__init__.py | 4 +++- gluon/dal.py | 4 ++-- gluon/main.py | 2 +- gluon/scheduler.py | 4 ++-- gluon/shell.py | 2 +- gluon/sql.py | 8 ++++---- gluon/sqlhtml.py | 10 +++++----- gluon/tools.py | 2 +- gluon/validators.py | 4 ++-- gluon/widget.py | 2 +- 12 files changed, 24 insertions(+), 22 deletions(-) diff --git a/gluon/__init__.py b/gluon/__init__.py index baf54a5b..6e5b8f5f 100644 --- a/gluon/__init__.py +++ b/gluon/__init__.py @@ -12,8 +12,8 @@ Web2Py framework modules __all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_LIST_OF_EMAILS', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_JSON', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'LOAD', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STRONG', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML', 'redirect', 'current', 'embed64'] -from globals import current from contrib import pydal +from globals import current from html import * from validators import * from http import redirect, HTTP diff --git a/gluon/compileapp.py b/gluon/compileapp.py index c8cd2356..a6394513 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -26,7 +26,7 @@ from gluon.fileutils import mktree, listdir, read_file, write_file from gluon.myregex import regex_expose, regex_longcomments from gluon.languages import translator from gluon.dal import DAL, Field -from gluon.pydal.base import BaseAdapter +from pydal.base import BaseAdapter from gluon.sqlhtml import SQLFORM, SQLTABLE from gluon.cache import Cache from gluon.globals import current, Response diff --git a/gluon/contrib/__init__.py b/gluon/contrib/__init__.py index 195e3d0a..6e86cf30 100644 --- a/gluon/contrib/__init__.py +++ b/gluon/contrib/__init__.py @@ -1,6 +1,8 @@ import os import sys -sys.path.append(os.path.join(os.path.abspath(__file__), "pydal")) +sys.path.append(os.path.join( + os.path.dirname(os.path.abspath(__file__)), "pydal")) import pydal +sys.modules['pydal'] = pydal diff --git a/gluon/dal.py b/gluon/dal.py index 4844be57..19896993 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -from gluon.pydal import DAL as pyDAL -from gluon.pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon +from pydal import DAL as pyDAL +from pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon from gluon import serializers as w2p_serializers diff --git a/gluon/main.py b/gluon/main.py index c3f63969..026ee971 100644 --- a/gluon/main.py +++ b/gluon/main.py @@ -93,7 +93,7 @@ from gluon.globals import Request, Response, Session from gluon.compileapp import build_environment, run_models_in, \ run_controller_in, run_view_in from gluon.contenttype import contenttype -from gluon.pydal.base import BaseAdapter +from pydal.base import BaseAdapter from gluon.validators import CRYPT from gluon.html import URL, xmlescape from gluon.utils import is_valid_ip_address, getipaddrinfo diff --git a/gluon/scheduler.py b/gluon/scheduler.py index 69daf61c..342eb878 100644 --- a/gluon/scheduler.py +++ b/gluon/scheduler.py @@ -598,7 +598,7 @@ class Scheduler(MetaScheduler): def define_tables(self, db, migrate): """Defines Scheduler tables structure""" - from gluon.pydal.base import DEFAULT + from pydal.base import DEFAULT logger.debug('defining tables (migrate=%s)', migrate) now = self.now db.define_table( @@ -1316,7 +1316,7 @@ class Scheduler(MetaScheduler): have all fields == None """ - from gluon.pydal.objects import Query + from pydal.objects import Query sr, st = self.db.scheduler_run, self.db.scheduler_task if isinstance(ref, (int, long)): q = st.id == ref diff --git a/gluon/shell.py b/gluon/shell.py index 89c26e60..7add0607 100644 --- a/gluon/shell.py +++ b/gluon/shell.py @@ -28,7 +28,7 @@ from gluon.restricted import RestrictedError from gluon.globals import Request, Response, Session from gluon.storage import Storage, List from gluon.admin import w2p_unpack -from gluon.pydal.base import BaseAdapter +from pydal.base import BaseAdapter logger = logging.getLogger("web2py") diff --git a/gluon/sql.py b/gluon/sql.py index 4f282f71..c70d7295 100644 --- a/gluon/sql.py +++ b/gluon/sql.py @@ -13,10 +13,10 @@ Just for backward compatibility __all__ = ['DAL', 'Field', 'DRIVERS'] from dal import DAL, Field, SQLCustomType -from gluon.pydal.base import BaseAdapter -from gluon.pydal.drivers import DRIVERS -from gluon.pydal.objects import Table, Query, Set, Expression, Row, Rows -from gluon.pydal.helpers.classes import SQLALL +from pydal.base import BaseAdapter +from pydal.drivers import DRIVERS +from pydal.objects import Table, Query, Set, Expression, Row, Rows +from pydal.helpers.classes import SQLALL SQLDB = DAL GQLDB = DAL diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 7e1f234b..132ea23b 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -26,11 +26,11 @@ from gluon.html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG from gluon.html import FORM, INPUT, LABEL, OPTION, SELECT, COL, COLGROUP from gluon.html import TABLE, THEAD, TBODY, TR, TD, TH, STYLE, SCRIPT from gluon.html import URL, FIELDSET, P, DEFAULT_PASSWORD_DISPLAY -from gluon.pydal.base import DEFAULT -from gluon.pydal.objects import Table, Row, Expression -from gluon.pydal.adapters.base import CALLABLETYPES -from gluon.pydal.helpers.methods import smart_query, bar_encode, sqlhtml_validators -from gluon.pydal.helpers.classes import Reference, SQLCustomType +from pydal.base import DEFAULT +from pydal.objects import Table, Row, Expression +from pydal.adapters.base import CALLABLETYPES +from pydal.helpers.methods import smart_query, bar_encode, sqlhtml_validators +from pydal.helpers.classes import Reference, SQLCustomType from gluon.storage import Storage from gluon.utils import md5_hash from gluon.validators import IS_EMPTY_OR, IS_NOT_EMPTY, IS_LIST_OF, IS_DATE diff --git a/gluon/tools.py b/gluon/tools.py index e848f464..66516efa 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -43,7 +43,7 @@ from gluon import * from gluon.contrib.autolinks import expand_one from gluon.contrib.markmin.markmin2html import \ replace_at_urls, replace_autolinks, replace_components -from gluon.pydal.objects import Table, Row, Set, Query +from pydal.objects import Table, Row, Set, Query import gluon.serializers as serializers diff --git a/gluon/validators.py b/gluon/validators.py index cb3806f0..f8150ecd 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -22,7 +22,7 @@ import decimal import unicodedata from cStringIO import StringIO from gluon.utils import simple_hash, web2py_uuid, DIGEST_ALG_BY_SIZE -from gluon.pydal.objects import FieldVirtual, FieldMethod +from pydal.objects import FieldVirtual, FieldMethod regex_isint = re.compile('^[+-]?\d+$') @@ -603,7 +603,7 @@ class IS_IN_DB(Validator): if not [v for v in values if not v in self.theset]: return (values, None) else: - from gluon.pydal.adapters import GoogleDatastoreAdapter + from pydal.adapters import GoogleDatastoreAdapter def count(values, s=self.dbset, f=field): return s(f.belongs(map(int, values))).count() diff --git a/gluon/widget.py b/gluon/widget.py index 32aff00e..7d1f3903 100644 --- a/gluon/widget.py +++ b/gluon/widget.py @@ -1086,7 +1086,7 @@ def start(cron=True): print ProgramAuthor print ProgramVersion - from gluon.pydal.drivers import DRIVERS + from pydal.drivers import DRIVERS if not options.nobanner: print 'Database drivers available: %s' % ', '.join(DRIVERS) From ad1fe873864b99a17060077de064db8e8014fd4a Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 22:37:34 +0100 Subject: [PATCH 04/63] Updated tests configuration, avoid circular imports with new dal --- .travis.yml | 47 ++++------------------------------------- gluon/globals.py | 2 +- gluon/tests/__init__.py | 13 ++---------- 3 files changed, 7 insertions(+), 55 deletions(-) diff --git a/.travis.yml b/.travis.yml index ac5914e4..27d587cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,59 +4,20 @@ python: - '2.6' - '2.7' - 'pypy' + install: - pip install -e . -env: - - DB=sqlite:memory - - DB=mysql://root:@localhost/test_w2p - - DB=postgres://postgres:@localhost/test_w2p - - DB=google:datastore -# - DB=google:datastore+ndb - - DB=mongodb://mongodb:mongodb@localhost/test_w2p - - DB=imap://imap:imap@localhost:993 + before_script: - - if [[ $TRAVIS_PYTHON_VERSION != '2.7' ]]; then pip install unittest2; fi - - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install coverage; fi; - - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then pip install python-coveralls; fi - - if [[ $DB == postgres* ]]; then pip install psycopg2; fi; - - if [[ $TRAVIS_PYTHON_VERSION == '2.5' ]]; then pip install pysqlite; fi - - if [[ $DB == mysql* ]]; then mysql -e 'create database test_w2p;'; fi - - if [[ $DB == postgres* ]]; then psql -c 'create database test_w2p;' -U postgres; fi - - if [[ $DB == postgres* ]]; then psql -c 'create extension postgis;' -U postgres -d test_w2p; fi - - - # Install last sdk for app engine (update only whenever a new release is available) - - if [[ $DB == google* ]]; then wget http://googleappengine.googlecode.com/files/google_appengine_1.8.9.zip -nv; fi - - if [[ $DB == google* ]]; then unzip -q google_appengine_1.8.9.zip; fi - - if [[ $DB == google* ]]; then mv -f ./google_appengine/google ./google; fi - - - if [[ $DB == mongodb* ]]; then pip install pymongo; fi - - if [[ $DB == mongodb* ]]; then mongo test_w2p --eval 'db.addUser("mongodb", "mongodb");'; fi - #Temporal solution to travis issue #155 - sudo chmod 777 /dev/shm - sudo rm -rf /dev/shm && sudo ln -s /run/shm /dev/shm -matrix: - exclude: - - python: 'pypy' - env: DB=postgres://postgres:@localhost/test_w2p - - python: 'pypy' - env: DB=mysql://root:@localhost/test_w2p - - python: 'pypy' - env: DB=google:datastore - - python: '2.6' - env: DB=google:datastore -# - python: '2.6' -# env: DB=google:datastore+ndb - script: export COVERAGE_PROCESS_START=gluon/tests/coverage.ini; ./web2py.py --run_system_tests --with_coverage + after_success: - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coverage combine; fi - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coveralls --config_file=gluon/tests/coverage.ini; fi - + notifications: email: true - -services: mongodb - diff --git a/gluon/globals.py b/gluon/globals.py index 7091f40c..f872864a 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -25,7 +25,6 @@ from gluon.serializers import json, custom_json import gluon.settings as settings from gluon.utils import web2py_uuid, secure_dumps, secure_loads from gluon.settings import global_settings -from gluon.dal import Field from gluon import recfile import hashlib import portalocker @@ -767,6 +766,7 @@ class Session(Storage): compression_level(int): 0-9, sets zlib compression on the data before the encryption """ + from gluon.dal import Field request = request or current.request response = response or current.response masterapp = masterapp or request.application diff --git a/gluon/tests/__init__.py b/gluon/tests/__init__.py index 9d66896e..e2998d1c 100644 --- a/gluon/tests/__init__.py +++ b/gluon/tests/__init__.py @@ -1,4 +1,4 @@ -import os, sys +import sys from test_http import * from test_cache import * @@ -16,16 +16,7 @@ from test_validators import * from test_utils import * from test_contribs import * from test_web import * - +from test_dal import * if sys.version[:3] == '2.7': from test_old_doctests import * - - -NOSQL = any([name in (os.getenv("DB") or "") - for name in ("datastore", "mongodb", "imap")]) - -if NOSQL: - from test_dal_nosql import * -else: - from test_dal import * \ No newline at end of file From 846d8f4e4b9e0d45b3afb46d0e253d501308339e Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 22:51:02 +0100 Subject: [PATCH 05/63] Get tests working back on py26, disabled web2py_uuid comparison check --- .travis.yml | 1 + gluon/tests/test_dal.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 27d587cb..74b2ebe5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,7 @@ before_script: script: export COVERAGE_PROCESS_START=gluon/tests/coverage.ini; ./web2py.py --run_system_tests --with_coverage after_success: + - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coverage combine; fi - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coveralls --config_file=gluon/tests/coverage.ini; fi diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 07ea25ae..568c0f3f 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -17,12 +17,10 @@ class TestDALSubclass(unittest.TestCase): def testRun(self): import gluon.serializers as mserializers import gluon.validators as mvalidators - from gluon.utils import web2py_uuid from gluon import sqlhtml db = DAL(check_reserved=['all']) self.assertEqual(db.serializers, mserializers) self.assertEqual(db.validators, mvalidators) - self.assertEqual(db.uuid, web2py_uuid) self.assertEqual(db.representers['rows_render'], sqlhtml.represent) self.assertEqual(db.representers['rows_xml'], sqlhtml.SQLTABLE) From b04b3ab529424b8a9bab248269718d228dca0cf2 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 22:58:16 +0100 Subject: [PATCH 06/63] Fixed wrong unittest2 pip install for py26 in travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 74b2ebe5..ad3d7563 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ install: - pip install -e . before_script: + - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi #Temporal solution to travis issue #155 - sudo chmod 777 /dev/shm - sudo rm -rf /dev/shm && sudo ln -s /run/shm /dev/shm @@ -16,7 +17,6 @@ before_script: script: export COVERAGE_PROCESS_START=gluon/tests/coverage.ini; ./web2py.py --run_system_tests --with_coverage after_success: - - if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coverage combine; fi - if [[ $TRAVIS_PYTHON_VERSION == '2.7' ]]; then coveralls --config_file=gluon/tests/coverage.ini; fi From 24f197935e9b4b11ce57bf3cb8b7ddfd0a3db8ba Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 23:09:35 +0100 Subject: [PATCH 07/63] Handling new pydal Exceptions in response.download --- gluon/globals.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gluon/globals.py b/gluon/globals.py index f872864a..419578f7 100644 --- a/gluon/globals.py +++ b/gluon/globals.py @@ -597,6 +597,7 @@ class Response(Storage): Downloads from http://..../download/filename """ + from pydal.exceptions import NotAuthorizedException, NotFoundException current.session.forget(current.response) @@ -613,6 +614,10 @@ class Response(Storage): raise HTTP(404) try: (filename, stream) = field.retrieve(name, nameonly=True) + except NotAuthorizedException: + raise HTTP(403) + except NotFoundException: + raise HTTP(404) except IOError: raise HTTP(404) headers = self.headers From c213071ae912e863b426c698d4eeb300560cb746 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Mon, 15 Dec 2014 23:20:53 +0100 Subject: [PATCH 08/63] Updated to latest pydal --- gluon/contrib/pydal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal index d8865533..d510d47b 160000 --- a/gluon/contrib/pydal +++ b/gluon/contrib/pydal @@ -1 +1 @@ -Subproject commit d886553357bc45c75402d619c09cdee54e378dc9 +Subproject commit d510d47b2fc436f23b4f6897a3493bb7255ac372 From 4bc3422ac6ad0df30e8f37c455842fd432087537 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 13:27:56 +0100 Subject: [PATCH 09/63] Updated pydal --- gluon/contrib/pydal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal index d510d47b..97a45307 160000 --- a/gluon/contrib/pydal +++ b/gluon/contrib/pydal @@ -1 +1 @@ -Subproject commit d510d47b2fc436f23b4f6897a3493bb7255ac372 +Subproject commit 97a45307a30ef7ca4fef4c8d3a4308d2874a3e4d From dd73678601b937d0c940b75d830f1ff5cb6503ff Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 14:44:59 +0100 Subject: [PATCH 10/63] Moved sqlhtml_validators method back into web2py --- gluon/contrib/pydal | 2 +- gluon/dal.py | 110 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal index 97a45307..83d97c64 160000 --- a/gluon/contrib/pydal +++ b/gluon/contrib/pydal @@ -1 +1 @@ -Subproject commit 97a45307a30ef7ca4fef4c8d3a4308d2874a3e4d +Subproject commit 83d97c6466ed563991ac4d0d2358a0aaf12c7f68 diff --git a/gluon/dal.py b/gluon/dal.py index 19896993..3c7c1cdf 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -5,14 +5,120 @@ from pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon from gluon import serializers as w2p_serializers -from gluon import validators as w2p_validators from gluon.utils import web2py_uuid from gluon import sqlhtml +def _default_validators(field): + """ + Field type validation, using web2py's validators mechanism. + + makes sure the content of a field is in line with the declared + fieldtype + """ + from gluon import validators + db = field.db + field_type, field_length = field.type, field.length + requires = [] + + def ff(r, id): + row = r(id) + if not row: + return str(id) + elif hasattr(r, '_format') and isinstance(r._format, str): + return r._format % row + elif hasattr(r, '_format') and callable(r._format): + return r._format(row) + else: + return str(id) + + if field_type in (('string', 'text', 'password')): + requires.append(validators.IS_LENGTH(field_length)) + elif field_type == 'json': + requires.append(validators.IS_EMPTY_OR(validators.IS_JSON())) + elif field_type == 'double' or field_type == 'float': + requires.append(validators.IS_FLOAT_IN_RANGE(-1e100, 1e100)) + elif field_type == 'integer': + requires.append(validators.IS_INT_IN_RANGE(-2**31, 2**31)) + elif field_type == 'bigint': + requires.append(validators.IS_INT_IN_RANGE(-2**63, 2**63)) + elif field_type.startswith('decimal'): + requires.append(validators.IS_DECIMAL_IN_RANGE(-10**10, 10**10)) + elif field_type == 'date': + requires.append(validators.IS_DATE()) + elif field_type == 'time': + requires.append(validators.IS_TIME()) + elif field_type == 'datetime': + requires.append(validators.IS_DATETIME()) + elif db and field_type.startswith('reference') and \ + field_type.find('.') < 0 and \ + field_type[10:] in db.tables: + referenced = db[field_type[10:]] + + def repr_ref(id, row=None, r=referenced, f=ff): + return f(r, id) + + field.represent = field.represent or repr_ref + if hasattr(referenced, '_format') and referenced._format: + requires = validators.IS_IN_DB(db, referenced._id, + referenced._format) + if field.unique: + requires._and = validators.IS_NOT_IN_DB(db, field) + if field.tablename == field_type[10:]: + return validators.IS_EMPTY_OR(requires) + return requires + elif db and field_type.startswith('list:reference') and \ + field_type.find('.') < 0 and \ + field_type[15:] in db.tables: + referenced = db[field_type[15:]] + + def list_ref_repr(ids, row=None, r=referenced, f=ff): + if not ids: + return None + from ..adapters.google import GoogleDatastoreAdapter + refs = None + db, id = r._db, r._id + if isinstance(db._adapter, GoogleDatastoreAdapter): + def count(values): + return db(id.belongs(values)).select(id) + rx = range(0, len(ids), 30) + refs = reduce(lambda a, b: a & b, [count(ids[i:i+30]) + for i in rx]) + else: + refs = db(id.belongs(ids)).select(id) + return (refs and ', '.join(f(r, x.id) for x in refs) or '') + + field.represent = field.represent or list_ref_repr + if hasattr(referenced, '_format') and referenced._format: + requires = validators.IS_IN_DB(db, referenced._id, + referenced._format, multiple=True) + else: + requires = validators.IS_IN_DB(db, referenced._id, + multiple=True) + if field.unique: + requires._and = validators.IS_NOT_IN_DB(db, field) + if not field.notnull: + requires = validators.IS_EMPTY_OR(requires) + return requires + elif field_type.startswith('list:'): + def repr_list(values, row=None): + return', '.join(str(v) for v in (values or [])) + + field.represent = field.represent or repr_list + + if field.unique: + requires.append(validators.IS_NOT_IN_DB(db, field)) + sff = ['in', 'do', 'da', 'ti', 'de', 'bo'] + if field.notnull and not field_type[:2] in sff: + requires.append(validators.IS_NOT_EMPTY()) + elif not field.notnull and field_type[:2] in sff and requires: + requires[0] = validators.IS_EMPTY_OR(requires[0]) + return requires + + class DAL(pyDAL): serializers = w2p_serializers - validators = w2p_validators + validators_method = _default_validators uuid = web2py_uuid representers = { 'rows_render': sqlhtml.represent, From e99eb431ba807f7c9dfaedfb6a6317082ca4d5d5 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 14:53:37 +0100 Subject: [PATCH 11/63] Fixed an import due to last commit --- gluon/sqlhtml.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 132ea23b..4c8b7046 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -29,8 +29,9 @@ from gluon.html import URL, FIELDSET, P, DEFAULT_PASSWORD_DISPLAY from pydal.base import DEFAULT from pydal.objects import Table, Row, Expression from pydal.adapters.base import CALLABLETYPES -from pydal.helpers.methods import smart_query, bar_encode, sqlhtml_validators +from pydal.helpers.methods import smart_query, bar_encode from pydal.helpers.classes import Reference, SQLCustomType +from gluon.dal import _default_validators from gluon.storage import Storage from gluon.utils import md5_hash from gluon.validators import IS_EMPTY_OR, IS_NOT_EMPTY, IS_LIST_OF, IS_DATE @@ -1126,7 +1127,7 @@ class SQLFORM(FORM): extra_field.table = table extra_field.tablename = table._tablename if extra_field.requires == DEFAULT: - extra_field.requires = sqlhtml_validators(extra_field) + extra_field.requires = _default_validators(extra_field) for fieldname in self.fields: if fieldname.find('.') >= 0: From 76fa952be8d38bfbd492cdf186057901905043c7 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 14:56:31 +0100 Subject: [PATCH 12/63] Avoid circular imports on latest sqlhtml_validators changes --- gluon/dal.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/gluon/dal.py b/gluon/dal.py index 3c7c1cdf..6c1827f7 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -4,11 +4,6 @@ from pydal import DAL as pyDAL from pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon -from gluon import serializers as w2p_serializers -from gluon.utils import web2py_uuid -from gluon import sqlhtml - - def _default_validators(field): """ Field type validation, using web2py's validators mechanism. @@ -115,6 +110,10 @@ def _default_validators(field): requires[0] = validators.IS_EMPTY_OR(requires[0]) return requires +from gluon import serializers as w2p_serializers +from gluon.utils import web2py_uuid +from gluon import sqlhtml + class DAL(pyDAL): serializers = w2p_serializers From c49e32bfd64af65a467747a80a6d5a5088a884e4 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 15:00:06 +0100 Subject: [PATCH 13/63] Updated DAL tests due to latest pyDAL validators change --- gluon/tests/test_dal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 568c0f3f..80b49ef9 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -16,11 +16,11 @@ from gluon.dal import DAL class TestDALSubclass(unittest.TestCase): def testRun(self): import gluon.serializers as mserializers - import gluon.validators as mvalidators from gluon import sqlhtml + from gluon.dal import _default_validators db = DAL(check_reserved=['all']) self.assertEqual(db.serializers, mserializers) - self.assertEqual(db.validators, mvalidators) + self.assertEqual(db.validators_method, _default_validators) self.assertEqual(db.representers['rows_render'], sqlhtml.represent) self.assertEqual(db.representers['rows_xml'], sqlhtml.SQLTABLE) From 8000bda0c9ed42672612154e2a51217660095f69 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 15:03:55 +0100 Subject: [PATCH 14/63] Removed _default_validators impossible comparison check from DAL tests --- gluon/tests/test_dal.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 80b49ef9..980a92cb 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -17,10 +17,8 @@ class TestDALSubclass(unittest.TestCase): def testRun(self): import gluon.serializers as mserializers from gluon import sqlhtml - from gluon.dal import _default_validators db = DAL(check_reserved=['all']) self.assertEqual(db.serializers, mserializers) - self.assertEqual(db.validators_method, _default_validators) self.assertEqual(db.representers['rows_render'], sqlhtml.represent) self.assertEqual(db.representers['rows_xml'], sqlhtml.SQLTABLE) From ea14c5b83bbd3bf2e9d1196a3fca3b3ed1c01748 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Wed, 24 Dec 2014 15:19:34 +0100 Subject: [PATCH 15/63] DAL.validators_method become a bound method, requires dal instance as first parameter --- gluon/dal.py | 3 +-- gluon/sqlhtml.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gluon/dal.py b/gluon/dal.py index 6c1827f7..fe73fa13 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -4,7 +4,7 @@ from pydal import DAL as pyDAL from pydal import Field, SQLCustomType, geoPoint, geoLine, geoPolygon -def _default_validators(field): +def _default_validators(db, field): """ Field type validation, using web2py's validators mechanism. @@ -12,7 +12,6 @@ def _default_validators(field): fieldtype """ from gluon import validators - db = field.db field_type, field_length = field.type, field.length requires = [] diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 4c8b7046..87b40adc 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -1127,7 +1127,8 @@ class SQLFORM(FORM): extra_field.table = table extra_field.tablename = table._tablename if extra_field.requires == DEFAULT: - extra_field.requires = _default_validators(extra_field) + extra_field.requires = _default_validators(table._db, + extra_field) for fieldname in self.fields: if fieldname.find('.') >= 0: From 2a287852addc7cf49742feeb1e38e8ddb09c0987 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Fri, 26 Dec 2014 14:05:27 +0100 Subject: [PATCH 16/63] Tracking pyDAL v0.12.25 --- gluon/contrib/pydal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal index 83d97c64..0bbbe1d6 160000 --- a/gluon/contrib/pydal +++ b/gluon/contrib/pydal @@ -1 +1 @@ -Subproject commit 83d97c6466ed563991ac4d0d2358a0aaf12c7f68 +Subproject commit 0bbbe1d6655e86b33a659bc12f8c7e785076a4f5 From 21cb35d4ca0cd19ce58e8903e81c21e479d21281 Mon Sep 17 00:00:00 2001 From: gi0baro Date: Sat, 27 Dec 2014 12:16:52 +0100 Subject: [PATCH 17/63] Updated directory structure for pydal integration --- .gitmodules | 4 ++-- gluon/__init__.py | 9 ++++++++- gluon/contrib/__init__.py | 8 -------- gluon/contrib/pydal | 1 - gluon/packages/dal | 1 + 5 files changed, 11 insertions(+), 12 deletions(-) delete mode 160000 gluon/contrib/pydal create mode 160000 gluon/packages/dal diff --git a/.gitmodules b/.gitmodules index 7e2aea49..71525aff 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "gluon/contrib/pydal"] - path = gluon/contrib/pydal +[submodule "gluon/packages/dal"] + path = gluon/packages/dal url = https://github.com/web2py/pydal.git diff --git a/gluon/__init__.py b/gluon/__init__.py index 6e5b8f5f..588c3e28 100644 --- a/gluon/__init__.py +++ b/gluon/__init__.py @@ -12,7 +12,14 @@ Web2Py framework modules __all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_LIST_OF_EMAILS', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_JSON', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'LOAD', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STRONG', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML', 'redirect', 'current', 'embed64'] -from contrib import pydal +#: add pydal to sys.modules +import os +import sys +sys.path.append(os.path.join( + os.path.dirname(os.path.abspath(__file__)), "packages", "dal")) +import pydal +sys.modules['pydal'] = pydal + from globals import current from html import * from validators import * diff --git a/gluon/contrib/__init__.py b/gluon/contrib/__init__.py index 6e86cf30..e69de29b 100644 --- a/gluon/contrib/__init__.py +++ b/gluon/contrib/__init__.py @@ -1,8 +0,0 @@ -import os -import sys - -sys.path.append(os.path.join( - os.path.dirname(os.path.abspath(__file__)), "pydal")) - -import pydal -sys.modules['pydal'] = pydal diff --git a/gluon/contrib/pydal b/gluon/contrib/pydal deleted file mode 160000 index 0bbbe1d6..00000000 --- a/gluon/contrib/pydal +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0bbbe1d6655e86b33a659bc12f8c7e785076a4f5 diff --git a/gluon/packages/dal b/gluon/packages/dal new file mode 160000 index 00000000..414a03e8 --- /dev/null +++ b/gluon/packages/dal @@ -0,0 +1 @@ +Subproject commit 414a03e8e2d133ba75b47e1c8011fb37cb2107e8 From d7bc489e71d006010269dadf41deae8ee351258b Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sun, 28 Dec 2014 17:19:17 +0100 Subject: [PATCH 18/63] fix issue 2011: capital letters in it.py --- applications/admin/languages/it.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/applications/admin/languages/it.py b/applications/admin/languages/it.py index 6b8040ef..87bb2efc 100644 --- a/applications/admin/languages/it.py +++ b/applications/admin/languages/it.py @@ -325,7 +325,7 @@ 'unable to uninstall "%s"': 'impossibile disinstallare "%s"', 'unable to upgrade because "%s"': 'impossibile aggiornare perché "%s"', 'uncheck all': 'smarca tutti', -'Uninstall': 'disinstalla', +'Uninstall': 'Disinstalla', 'update': 'aggiorna', 'update all languages': 'aggiorna tutti i linguaggi', 'Update:': 'Aggiorna:', @@ -348,7 +348,7 @@ 'Versioning': 'Versioning', 'View': 'Vista', 'view': 'vista', -'Views': 'viste', +'Views': 'Viste', 'views': 'viste', 'Web Framework': 'Web Framework', 'web2py is up to date': 'web2py è aggiornato', From e6de16b1118238fedd8eac9427ac7c11770ce121 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sat, 3 Jan 2015 11:58:08 +0100 Subject: [PATCH 19/63] fix issue 2003: double translation --- gluon/storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/storage.py b/gluon/storage.py index 2c94079c..e12ee971 100644 --- a/gluon/storage.py +++ b/gluon/storage.py @@ -195,7 +195,7 @@ class Messages(Settings): def __getattr__(self, key): value = self[key] if isinstance(value, str): - return str(self.T(value)) + return self.T(value) return value class FastStorage(dict): From 3e1a918707a41250fd9f90306dbc396e4f953b82 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sat, 3 Jan 2015 14:11:06 +0100 Subject: [PATCH 20/63] Fix expression evaluation with postgres --- gluon/dal/adapters/base.py | 5 +++-- gluon/dal/adapters/postgres.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/gluon/dal/adapters/base.py b/gluon/dal/adapters/base.py index aad58af9..050edb68 100644 --- a/gluon/dal/adapters/base.py +++ b/gluon/dal/adapters/base.py @@ -76,7 +76,8 @@ class BaseAdapter(ConnectionPool): dbpath = None folder = None connector = lambda *args, **kwargs: None # __init__ should override this - + TRUE_exp = '1' + FALSE_exp = '0' TRUE = 'T' FALSE = 'F' T_SEP = ' ' @@ -909,7 +910,7 @@ class BaseAdapter(ConnectionPool): return ','.join(self.represent(item,field_type) \ for item in expression) elif isinstance(expression, bool): - return '1' if expression else '0' + return self.db._adapter.TRUE_exp if expression else self.db._adapter.FALSE_exp else: return str(expression) diff --git a/gluon/dal/adapters/postgres.py b/gluon/dal/adapters/postgres.py index 7e81d6c8..7e8c32fd 100644 --- a/gluon/dal/adapters/postgres.py +++ b/gluon/dal/adapters/postgres.py @@ -104,7 +104,8 @@ class PostgreSQLAdapter(BaseAdapter): self.srid = srid self.find_or_make_work_folder() self._last_insert = None # for INSERT ... RETURNING ID - + self.TRUE_exp = 'TRUE' + self.FALSE_exp = 'FALSE' ruri = uri.split('://',1)[1] m = self.REGEX_URI.match(ruri) if not m: From 132dfbcb19554cec7fd468921e8910adf35dc41a Mon Sep 17 00:00:00 2001 From: kelson Date: Sat, 3 Jan 2015 14:59:13 -0500 Subject: [PATCH 21/63] added support for POST variable passing to LOAD function A new post_vars parameter allows controllers to pass POST varaibles through LOAD as appropriate. GET variables are currently passed by default (via vars). --- gluon/compileapp.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gluon/compileapp.py b/gluon/compileapp.py index e12bb200..9ada3cfb 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -127,7 +127,7 @@ class mybuiltin(object): def LOAD(c=None, f='index', args=None, vars=None, extension=None, target=None, ajax=False, ajax_trap=False, url=None, user_signature=False, timeout=None, times=1, - content='loading...', **attr): + content='loading...', post_vars=Storage(), **attr): """ LOADs a component into the action's document Args: @@ -202,7 +202,7 @@ def LOAD(c=None, f='index', args=None, vars=None, other_request.args = List(args) other_request.vars = vars other_request.get_vars = vars - other_request.post_vars = Storage() + other_request.post_vars = post_vars other_response = Response() other_request.env.path_info = '/' + \ '/'.join([request.application, c, f] + From 1815864a672db8aef047ede80bd63da40d8ba4c1 Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 10:38:53 +0100 Subject: [PATCH 22/63] Catalan translation Added translation to catalan language --- applications/welcome/languages/ca.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 applications/welcome/languages/ca.py diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py new file mode 100644 index 00000000..593ca2cf --- /dev/null +++ b/applications/welcome/languages/ca.py @@ -0,0 +1 @@ +ads From 16c0e1a4b8ab8363c67f0b2730c98bb7b76de4fe Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 10:39:47 +0100 Subject: [PATCH 23/63] Update ca.py --- applications/welcome/languages/ca.py | 434 ++++++++++++++++++++++++++- 1 file changed, 433 insertions(+), 1 deletion(-) diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py index 593ca2cf..7579cc37 100644 --- a/applications/welcome/languages/ca.py +++ b/applications/welcome/languages/ca.py @@ -1 +1,433 @@ -ads +# -*- coding: utf-8 -*- +{ +'!langcode!': 'es', +'!langname!': 'Español', +'"update" is an optional expression like "field1=\'newvalue\'". You cannot update or delete the results of a JOIN': '"actualice" es una expresión opcional como "campo1=\'nuevo_valor\'". No se puede actualizar o eliminar resultados de un JOIN', +'%(nrows)s records found': '%(nrows)s registros encontrados', +'%s %%{position}': '%s %%{posición}', +'%s %%{row} deleted': '%s %%{fila} %%{eliminada}', +'%s %%{row} updated': '%s %%{fila} %%{actualizada}', +'%s selected': '%s %%{seleccionado}', +'%Y-%m-%d': '%d/%m/%Y', +'%Y-%m-%d %H:%M:%S': '%d/%m/%Y %H:%M:%S', +'(something like "it-it")': '(algo como "eso-eso")', +'@markmin\x01An error occured, please [[reload %s]] the page': 'Ha ocurrido un error, por favor [[recargar %s]] la página', +'@markmin\x01Number of entries: **%s**': 'Número de entradas: **%s**', +'A new version of web2py is available': 'Hay una nueva versión de web2py disponible', +'A new version of web2py is available: %s': 'Hay una nueva versión de web2py disponible: %s', +'About': 'Acerca de', +'about': 'acerca de', +'About application': 'Acerca de la aplicación', +'Access Control': 'Control de Acceso', +'Add': 'Añadir', +'additional code for your application': 'código adicional para su aplicación', +'admin disabled because no admin password': 'admin deshabilitado por falta de contraseña', +'admin disabled because not supported on google app engine': 'admin deshabilitado, no es soportado en GAE', +'admin disabled because unable to access password file': 'admin deshabilitado, imposible acceder al archivo con la contraseña', +'Admin is disabled because insecure channel': 'Admin deshabilitado, el canal no es seguro', +'Admin is disabled because unsecure channel': 'Admin deshabilitado, el canal no es seguro', +'Administrative interface': 'Interfaz administrativa', +'Administrative Interface': 'Interfaz Administrativa', +'Administrator Password:': 'Contraseña del Administrador:', +'Ajax Recipes': 'Recetas AJAX', +'An error occured, please %s the page': 'Ha ocurrido un error, por favor %s la página', +'And': 'Y', +'and rename it (required):': 'y renómbrela (requerido):', +'and rename it:': ' y renómbrelo:', +'appadmin': 'appadmin', +'appadmin is disabled because insecure channel': 'admin deshabilitado, el canal no es seguro', +'application "%s" uninstalled': 'aplicación "%s" desinstalada', +'application compiled': 'aplicación compilada', +'application is compiled and cannot be designed': 'la aplicación está compilada y no puede ser modificada', +'Apply changes': 'Aplicar cambios', +'Appointment': 'Nombramiento', +'Are you sure you want to delete file "%s"?': '¿Está seguro que desea eliminar el archivo "%s"?', +'Are you sure you want to delete this object?': '¿Está seguro que desea borrar este objeto?', +'Are you sure you want to uninstall application "%s"': '¿Está seguro que desea desinstalar la aplicación "%s"', +'Are you sure you want to uninstall application "%s"?': '¿Está seguro que desea desinstalar la aplicación "%s"?', +'at': 'en', +'ATTENTION: Login requires a secure (HTTPS) connection or running on localhost.': 'ATENCION: Inicio de sesión requiere una conexión segura (HTTPS) o localhost.', +'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.': 'ATENCION: NO EJECUTE VARIAS PRUEBAS SIMULTANEAMENTE, NO SON THREAD SAFE.', +'ATTENTION: you cannot edit the running application!': 'ATENCION: no puede modificar la aplicación que está ejecutandose!', +'Authentication': 'Autenticación', +'Authentication failed at client DB!': '¡La autenticación ha fallado en la BDD cliente!', +'Authentication failed at main DB!': '¡La autenticación ha fallado en la BDD principal!', +'Available Databases and Tables': 'Bases de datos y tablas disponibles', +'Back': 'Atrás', +'Buy this book': 'Compra este libro', +'Cache': 'Caché', +'cache': 'caché', +'Cache Keys': 'Llaves de la Caché', +'cache, errors and sessions cleaned': 'caché, errores y sesiones eliminados', +'Cannot be empty': 'No puede estar vacío', +'Cannot compile: there are errors in your app. Debug it, correct errors and try again.': 'No se puede compilar: hay errores en su aplicación. Depure, corrija errores y vuelva a intentarlo.', +'cannot create file': 'no es posible crear archivo', +'cannot upload file "%(filename)s"': 'no es posible subir archivo "%(filename)s"', +'Change Password': 'Cambie la Contraseña', +'Change password': 'Cambie la contraseña', +'change password': 'cambie la contraseña', +'check all': 'marcar todos', +'Check to delete': 'Marque para eliminar', +'choose one': 'escoja uno', +'clean': 'limpiar', +'Clear': 'Limpiar', +'Clear CACHE?': '¿Limpiar CACHÉ?', +'Clear DISK': 'Limpiar DISCO', +'Clear RAM': 'Limpiar RAM', +'Click on the link %(link)s to reset your password': 'Pulse en el enlace %(link)s para reiniciar su contraseña', +'click to check for upgrades': 'haga clic para buscar actualizaciones', +'client': 'cliente', +'Client IP': 'IP del Cliente', +'Close': 'Cerrar', +'Community': 'Comunidad', +'compile': 'compilar', +'compiled application removed': 'aplicación compilada eliminada', +'Components and Plugins': 'Componentes y Plugins', +'contains': 'contiene', +'Controller': 'Controlador', +'Controllers': 'Controladores', +'controllers': 'controladores', +'Copyright': 'Copyright', +'create file with filename:': 'cree archivo con nombre:', +'Create new application': 'Cree una nueva aplicación', +'create new application:': 'nombre de la nueva aplicación:', +'Created By': 'Creado Por', +'Created On': 'Creado En', +'CSV (hidden cols)': 'CSV (columnas ocultas)', +'Current request': 'Solicitud en curso', +'Current response': 'Respuesta en curso', +'Current session': 'Sesión en curso', +'currently saved or': 'actualmente guardado o', +'customize me!': '¡Adáptame!', +'data uploaded': 'datos subidos', +'Database': 'Base de datos', +'Database %s select': 'selección en base de datos %s', +'database administration': 'administración de base de datos', +'Database Administration (appadmin)': 'Administración de Base de Datos (appadmin)', +'Date and Time': 'Fecha y Hora', +'DB': 'BDD', +'db': 'bdd', +'DB Model': 'Modelo BDD', +'defines tables': 'define tablas', +'Delete': 'Eliminar', +'delete': 'eliminar', +'delete all checked': 'eliminar marcados', +'Delete:': 'Eliminar:', +'Demo': 'Demostración', +'Deploy on Google App Engine': 'Despliegue en Google App Engine', +'Deployment Recipes': 'Recetas de despliegue', +'Description': 'Descripción', +'design': 'diseño', +'DESIGN': 'DISEÑO', +'Design for': 'Diseño por', +'detecting': 'detectando', +'DISK': 'DISCO', +'Disk Cache Keys': 'Llaves de Caché en Disco', +'Disk Cleared': 'Disco limpiado', +'Documentation': 'Documentación', +"Don't know what to do?": '¿No sabe que hacer?', +'done!': '¡hecho!', +'Download': 'Descargas', +'E-mail': 'Correo electrónico', +'edit': 'editar', +'EDIT': 'EDITAR', +'Edit': 'Editar', +'Edit application': 'Editar aplicación', +'edit controller': 'editar controlador', +'Edit current record': 'Edite el registro actual', +'Edit Profile': 'Editar Perfil', +'edit profile': 'editar perfil', +'Edit This App': 'Edite esta App', +'Editing file': 'Editando archivo', +'Editing file "%s"': 'Editando archivo "%s"', +'Email and SMS': 'Correo electrónico y SMS', +'Email sent': 'Correo electrónico enviado', +'End of impersonation': 'Fin de suplantación', +'enter a number between %(min)g and %(max)g': 'introduzca un número entre %(min)g y %(max)g', +'enter a value': 'introduzca un valor', +'enter an integer between %(min)g and %(max)g': 'introduzca un entero entre %(min)g y %(max)g', +'enter date and time as %(format)s': 'introduzca fecha y hora como %(format)s', +'Error logs for "%(app)s"': 'Bitácora de errores en "%(app)s"', +'errors': 'errores', +'Errors': 'Errores', +'Errors in form, please check it out.': 'Hay errores en el formulario, por favor comprúebelo.', +'export as csv file': 'exportar como archivo CSV', +'Export:': 'Exportar:', +'exposes': 'expone', +'extends': 'extiende', +'failed to reload module': 'la recarga del módulo ha fallado', +'FAQ': 'FAQ', +'file "%(filename)s" created': 'archivo "%(filename)s" creado', +'file "%(filename)s" deleted': 'archivo "%(filename)s" eliminado', +'file "%(filename)s" uploaded': 'archivo "%(filename)s" subido', +'file "%(filename)s" was not deleted': 'archivo "%(filename)s" no fué eliminado', +'file "%s" of %s restored': 'archivo "%s" de %s restaurado', +'file changed on disk': 'archivo modificado en el disco', +'file does not exist': 'archivo no existe', +'file saved on %(time)s': 'archivo guardado %(time)s', +'file saved on %s': 'archivo guardado %s', +'First name': 'Nombre', +'Forgot username?': '¿Olvidó el nombre de usuario?', +'Forms and Validators': 'Formularios y validadores', +'Free Applications': 'Aplicaciones Libres', +'Functions with no doctests will result in [passed] tests.': 'Funciones sin doctests equivalen a pruebas [aceptadas].', +'Group %(group_id)s created': 'Grupo %(group_id)s creado', +'Group ID': 'ID de Grupo', +'Group uniquely assigned to user %(id)s': 'Grupo asignado únicamente al usuario %(id)s', +'Groups': 'Grupos', +'Hello World': 'Hola Mundo', +'help': 'ayuda', +'Home': 'Inicio', +'How did you get here?': '¿Cómo llegaste aquí?', +'htmledit': 'htmledit', +'Impersonate': 'Suplantar', +'import': 'importar', +'Import/Export': 'Importar/Exportar', +'in': 'en', +'includes': 'incluye', +'Index': 'Índice', +'insert new': 'inserte nuevo', +'insert new %s': 'inserte nuevo %s', +'Installed applications': 'Aplicaciones instaladas', +'Insufficient privileges': 'Privilegios insuficientes', +'internal error': 'error interno', +'Internal State': 'Estado Interno', +'Introduction': 'Introducción', +'Invalid action': 'Acción inválida', +'Invalid email': 'Correo electrónico inválido', +'invalid expression': 'expresión inválida', +'Invalid login': 'Inicio de sesión inválido', +'invalid password': 'contraseña inválida', +'Invalid Query': 'Consulta inválida', +'invalid request': 'solicitud inválida', +'Invalid reset password': 'Reinicio de contraseña inválido', +'invalid ticket': 'tiquete inválido', +'Is Active': 'Está Activo', +'Key': 'Llave', +'language file "%(filename)s" created/updated': 'archivo de lenguaje "%(filename)s" creado/actualizado', +'Language files (static strings) updated': 'Archivos de lenguaje (cadenas estáticas) actualizados', +'languages': 'lenguajes', +'Languages': 'Lenguajes', +'languages updated': 'lenguajes actualizados', +'Last name': 'Apellido', +'Last saved on:': 'Guardado en:', +'Layout': 'Diseño de página', +'Layout Plugins': 'Plugins de diseño', +'Layouts': 'Diseños de páginas', +'License for': 'Licencia para', +'Live Chat': 'Chat en vivo', +'loading...': 'cargando...', +'Logged in': 'Sesión iniciada', +'Logged out': 'Sesión finalizada', +'Login': 'Inicio de sesión', +'login': 'inicio de sesión', +'Login disabled by administrator': 'Inicio de sesión deshabilitado por el administrador', +'Login to the Administrative Interface': 'Inicio de sesión para la Interfaz Administrativa', +'logout': 'fin de sesión', +'Logout': 'Fin de sesión', +'Lost Password': 'Contraseña perdida', +'Lost password?': '¿Olvidó la contraseña?', +'lost password?': '¿olvidó la contraseña?', +'Main Menu': 'Menú principal', +'Manage Cache': 'Gestionar la Caché', +'Menu Model': 'Modelo "menu"', +'merge': 'combinar', +'Models': 'Modelos', +'models': 'modelos', +'Modified By': 'Modificado Por', +'Modified On': 'Modificado En', +'Modules': 'Módulos', +'modules': 'módulos', +'must be YYYY-MM-DD HH:MM:SS!': '¡debe ser DD/MM/YYYY HH:MM:SS!', +'must be YYYY-MM-DD!': '¡debe ser DD/MM/YYYY!', +'My Sites': 'Mis Sitios', +'Name': 'Nombre', +'New': 'Nuevo', +'New %(entity)s': 'Nuevo %(entity)s', +'new application "%s" created': 'nueva aplicación "%s" creada', +'New password': 'Contraseña nueva', +'New Record': 'Registro nuevo', +'new record inserted': 'nuevo registro insertado', +'next 100 rows': '100 filas siguientes', +'NO': 'NO', +'No databases in this application': 'No hay bases de datos en esta aplicación', +'No records found': 'No se han encontrado registros', +'Not authorized': 'No autorizado', +'not in': 'no en', +'Object or table name': 'Nombre del objeto o tabla', +'Old password': 'Contraseña vieja', +'Online examples': 'Ejemplos en línea', +'Or': 'O', +'or import from csv file': 'o importar desde archivo CSV', +'or provide application url:': 'o provea URL de la aplicación:', +'Origin': 'Origen', +'Original/Translation': 'Original/Traducción', +'Other Plugins': 'Otros Plugins', +'Other Recipes': 'Otras Recetas', +'Overview': 'Resumen', +'pack all': 'empaquetar todo', +'pack compiled': 'empaquetar compilados', +'Password': 'Contraseña', +'Password changed': 'Contraseña cambiada', +"Password fields don't match": 'Los campos de contraseña no coinciden', +'Password reset': 'Reinicio de contraseña', +'Peeking at file': 'Visualizando archivo', +'Phone': 'Teléfono', +'please input your password again': 'por favor introduzca su contraseña otra vez', +'Plugins': 'Plugins', +'Powered by': 'Este sitio usa', +'Preface': 'Prefacio', +'previous 100 rows': '100 filas anteriores', +'Profile': 'Perfil', +'Profile updated': 'Perfil actualizado', +'Python': 'Python', +'Query Not Supported: %s': 'Consulta No Soportada: %s', +'Query:': 'Consulta:', +'Quick Examples': 'Ejemplos Rápidos', +'RAM': 'RAM', +'RAM Cache Keys': 'Llaves de la Caché en RAM', +'Ram Cleared': 'Ram Limpiada', +'Recipes': 'Recetas', +'Record': 'Registro', +'Record %(id)s created': 'Registro %(id)s creado', +'Record Created': 'Registro Creado', +'record does not exist': 'el registro no existe', +'Record ID': 'ID de Registro', +'Record id': 'Id de registro', +'register': 'regístrese', +'Register': 'Regístrese', +'Registration identifier': 'Identificador de Registro', +'Registration key': 'Llave de registro', +'Registration successful': 'Registro con éxito', +'reload': 'recargar', +'Remember me (for 30 days)': 'Recuérdame (durante 30 días)', +'remove compiled': 'eliminar compiladas', +'Request reset password': 'Solicitar reinicio de contraseña', +'Reset password': 'Reiniciar contraseña', +'Reset Password key': 'Restaurar Llave de la Contraseña', +'Resolve Conflict file': 'archivo Resolución de Conflicto', +'restore': 'restaurar', +'Retrieve username': 'Recuperar nombre de usuario', +'revert': 'revertir', +'Role': 'Rol', +'Rows in Table': 'Filas en la tabla', +'Rows selected': 'Filas seleccionadas', +'save': 'guardar', +'Saved file hash:': 'Hash del archivo guardado:', +'Search': 'Buscar', +'Semantic': 'Semántica', +'Services': 'Servicios', +'session expired': 'sesión expirada', +'shell': 'terminal', +'site': 'sitio', +'Size of cache:': 'Tamaño de la Caché:', +'some files could not be removed': 'algunos archivos no pudieron ser removidos', +'start': 'inicio', +'starts with': 'comienza por', +'state': 'estado', +'static': 'estáticos', +'Static files': 'Archivos estáticos', +'Statistics': 'Estadísticas', +'Stylesheet': 'Hoja de estilo', +'Submit': 'Enviar', +'submit': 'enviar', +'Success!': '¡Correcto!', +'Support': 'Soporte', +'Sure you want to delete this object?': '¿Está seguro que desea eliminar este objeto?', +'Table': 'tabla', +'Table name': 'Nombre de la tabla', +'test': 'probar', +'Testing application': 'Probando aplicación', +'The "query" is a condition like "db.table1.field1==\'value\'". Something like "db.table1.field1==db.table2.field2" results in a SQL JOIN.': 'La "consulta" es una condición como "db.tabla1.campo1==\'valor\'". Algo como "db.tabla1.campo1==db.tabla2.campo2" resulta en un JOIN SQL.', +'the application logic, each URL path is mapped in one exposed function in the controller': 'la lógica de la aplicación, cada ruta URL se mapea en una función expuesta en el controlador', +'The Core': 'El Núcleo', +'the data representation, define database tables and sets': 'la representación de datos, define tablas y conjuntos de base de datos', +'The output of the file is a dictionary that was rendered by the view %s': 'La salida de dicha función es un diccionario que es desplegado por la vista %s', +'the presentations layer, views are also known as templates': 'la capa de presentación, las vistas también son llamadas plantillas', +'The Views': 'Las Vistas', +'There are no controllers': 'No hay controladores', +'There are no models': 'No hay modelos', +'There are no modules': 'No hay módulos', +'There are no static files': 'No hay archivos estáticos', +'There are no translators, only default language is supported': 'No hay traductores, sólo el lenguaje por defecto es soportado', +'There are no views': 'No hay vistas', +'these files are served without processing, your images go here': 'estos archivos son servidos sin procesar, sus imágenes van aquí', +'This App': 'Esta Aplicación', +'This email already has an account': 'Este correo electrónico ya tiene una cuenta', +'This is a copy of the scaffolding application': 'Esta es una copia de la aplicación de andamiaje', +'This is the %(filename)s template': 'Esta es la plantilla %(filename)s', +'Ticket': 'Tiquete', +'Time in Cache (h:m:s)': 'Tiempo en Caché (h:m:s)', +'Timestamp': 'Marca de tiempo', +'to previous version.': 'a la versión previa.', +'To emulate a breakpoint programatically, write:': 'Emular un punto de ruptura programáticamente, escribir:', +'to use the debugger!': '¡usar el depurador!', +'toggle breakpoint': 'alternar punto de ruptura', +'Toggle comment': 'Alternar comentario', +'Toggle Fullscreen': 'Alternar pantalla completa', +'too short': 'demasiado corto', +'translation strings for the application': 'cadenas de caracteres de traducción para la aplicación', +'try': 'intente', +'try something like': 'intente algo como', +'TSV (Excel compatible)': 'TSV (compatible Excel)', +'TSV (Excel compatible, hidden cols)': 'TSV (compatible Excel, columnas ocultas)', +'Twitter': 'Twitter', +'Unable to check for upgrades': 'No es posible verificar la existencia de actualizaciones', +'unable to create application "%s"': 'no es posible crear la aplicación "%s"', +'unable to delete file "%(filename)s"': 'no es posible eliminar el archivo "%(filename)s"', +'Unable to download': 'No es posible la descarga', +'Unable to download app': 'No es posible descarga la aplicación', +'unable to parse csv file': 'no es posible analizar el archivo CSV', +'unable to uninstall "%s"': 'no es posible instalar "%s"', +'uncheck all': 'desmarcar todos', +'uninstall': 'desinstalar', +'unknown': 'desconocido', +'update': 'actualizar', +'update all languages': 'actualizar todos los lenguajes', +'Update:': 'Actualice:', +'upload application:': 'subir aplicación:', +'Upload existing application': 'Suba esta aplicación', +'upload file:': 'suba archivo:', +'Use (...)&(...) for AND, (...)|(...) for OR, and ~(...) for NOT to build more complex queries.': 'Use (...)&(...) para AND, (...)|(...) para OR, y ~(...) para NOT, para crear consultas más complejas.', +'User %(id)s is impersonating %(other_id)s': 'El usuario %(id)s está suplantando %(other_id)s', +'User %(id)s Logged-in': 'El usuario %(id)s inició la sesión', +'User %(id)s Logged-out': 'El usuario %(id)s finalizó la sesión', +'User %(id)s Password changed': 'Contraseña del usuario %(id)s cambiada', +'User %(id)s Password reset': 'Contraseña del usuario %(id)s reiniciada', +'User %(id)s Profile updated': 'Actualizado el perfil del usuario %(id)s', +'User %(id)s Registered': 'Usuario %(id)s Registrado', +'User %(id)s Username retrieved': 'Se ha recuperado el nombre de usuario del usuario %(id)s', +'User %(username)s Logged-in': 'El usuario %(username)s inició la sesión', +"User '%(username)s' Logged-in": "El usuario '%(username)s' inició la sesión", +"User '%(username)s' Logged-out": "El usuario '%(username)s' finalizó la sesión", +'User Id': 'Id de Usuario', +'User ID': 'ID de Usuario', +'User Logged-out': 'El usuario finalizó la sesión', +'Username': 'Nombre de usuario', +'Username retrieve': 'Recuperar nombre de usuario', +'value already in database or empty': 'el valor ya existe en la base de datos o está vacío', +'value not allowed': 'valor no permitido', +'value not in database': 'el valor no está en la base de datos', +'Verify Password': 'Verificar Contraseña', +'Version': 'Versión', +'versioning': 'versiones', +'Videos': 'Vídeos', +'View': 'Vista', +'view': 'vista', +'View %(entity)s': 'Ver %(entity)s', +'Views': 'Vistas', +'views': 'vistas', +'web2py is up to date': 'web2py está actualizado', +'web2py Recent Tweets': 'Tweets Recientes de web2py', +'Welcome': 'Bienvenido', +'Welcome %s': 'Bienvenido %s', +'Welcome to web2py': 'Bienvenido a web2py', +'Welcome to web2py!': '¡Bienvenido a web2py!', +'Which called the function %s located in the file %s': 'La cual llamó la función %s localizada en el archivo %s', +'Working...': 'Trabajando...', +'YES': 'SÍ', +'You are successfully running web2py': 'Usted está ejecutando web2py exitosamente', +'You can modify this application and adapt it to your needs': 'Usted puede modificar esta aplicación y adaptarla a sus necesidades', +'You visited the url %s': 'Usted visitó la url %s', +'Your username is: %(username)s': 'Su nombre de usuario es: %(username)s', +} From f396094daf3d4a061deae3a03c860d92aa820f3d Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 10:47:11 +0100 Subject: [PATCH 24/63] Update ca.py --- applications/welcome/languages/ca.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py index 7579cc37..c0e4ec8a 100644 --- a/applications/welcome/languages/ca.py +++ b/applications/welcome/languages/ca.py @@ -1,16 +1,16 @@ # -*- coding: utf-8 -*- { -'!langcode!': 'es', -'!langname!': 'Español', -'"update" is an optional expression like "field1=\'newvalue\'". You cannot update or delete the results of a JOIN': '"actualice" es una expresión opcional como "campo1=\'nuevo_valor\'". No se puede actualizar o eliminar resultados de un JOIN', -'%(nrows)s records found': '%(nrows)s registros encontrados', -'%s %%{position}': '%s %%{posición}', +'!langcode!': 'ca', +'!langname!': 'Català', +'"update" is an optional expression like "field1=\'newvalue\'". You cannot update or delete the results of a JOIN': '"actualizi" és una expressió opcional com "camp1=\'nou_valor\'". No es poden actualitzar o eliminar resultats de un JOIN', +'%(nrows)s records found': '%(nrows)s registres trobats', +'%s %%{position}': '%s %%{posició}', '%s %%{row} deleted': '%s %%{fila} %%{eliminada}', -'%s %%{row} updated': '%s %%{fila} %%{actualizada}', -'%s selected': '%s %%{seleccionado}', +'%s %%{row} updated': '%s %%{fila} %%{actualitzada}', +'%s selected': '%s %%{seleccionat}', '%Y-%m-%d': '%d/%m/%Y', '%Y-%m-%d %H:%M:%S': '%d/%m/%Y %H:%M:%S', -'(something like "it-it")': '(algo como "eso-eso")', +'(something like "it-it")': '(similar a "això-això")', '@markmin\x01An error occured, please [[reload %s]] the page': 'Ha ocurrido un error, por favor [[recargar %s]] la página', '@markmin\x01Number of entries: **%s**': 'Número de entradas: **%s**', 'A new version of web2py is available': 'Hay una nueva versión de web2py disponible', From 83b94b8207eb49c5e0e31ba7b205713f8c72abac Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 6 Jan 2015 11:17:38 -0600 Subject: [PATCH 25/63] 2.9.12-beta --- Makefile | 2 +- gluon/tools.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8bf38b2c..3d4cb666 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ update: echo "remember that pymysql was tweaked" src: ### Use semantic versioning - echo 'Version 2.10.0-beta+timestamp.'`date +%Y.%m.%d.%H.%M.%S` > VERSION + echo 'Version 2.9.12-beta+timestamp.'`date +%Y.%m.%d.%H.%M.%S` > VERSION ### rm -f all junk files make clean ### clean up baisc apps diff --git a/gluon/tools.py b/gluon/tools.py index 481d18ed..0b1dbd10 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -842,6 +842,7 @@ class Recaptcha(DIV): comment = '', ajax=False ): + request = request or current.request self.request_vars = request and request.vars or current.request.vars self.remote_addr = request.env.remote_addr self.public_key = public_key From eb8cc3fc7615de57f80c38a57e347250df394c61 Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 21:22:43 +0100 Subject: [PATCH 26/63] Update ca.py --- applications/welcome/languages/ca.py | 616 ++++++++++----------------- 1 file changed, 230 insertions(+), 386 deletions(-) diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py index c0e4ec8a..659ea48b 100644 --- a/applications/welcome/languages/ca.py +++ b/applications/welcome/languages/ca.py @@ -11,423 +11,267 @@ '%Y-%m-%d': '%d/%m/%Y', '%Y-%m-%d %H:%M:%S': '%d/%m/%Y %H:%M:%S', '(something like "it-it")': '(similar a "això-això")', -'@markmin\x01An error occured, please [[reload %s]] the page': 'Ha ocurrido un error, por favor [[recargar %s]] la página', -'@markmin\x01Number of entries: **%s**': 'Número de entradas: **%s**', -'A new version of web2py is available': 'Hay una nueva versión de web2py disponible', -'A new version of web2py is available: %s': 'Hay una nueva versión de web2py disponible: %s', -'About': 'Acerca de', -'about': 'acerca de', -'About application': 'Acerca de la aplicación', -'Access Control': 'Control de Acceso', -'Add': 'Añadir', -'additional code for your application': 'código adicional para su aplicación', -'admin disabled because no admin password': 'admin deshabilitado por falta de contraseña', -'admin disabled because not supported on google app engine': 'admin deshabilitado, no es soportado en GAE', -'admin disabled because unable to access password file': 'admin deshabilitado, imposible acceder al archivo con la contraseña', -'Admin is disabled because insecure channel': 'Admin deshabilitado, el canal no es seguro', -'Admin is disabled because unsecure channel': 'Admin deshabilitado, el canal no es seguro', -'Administrative interface': 'Interfaz administrativa', -'Administrative Interface': 'Interfaz Administrativa', -'Administrator Password:': 'Contraseña del Administrador:', -'Ajax Recipes': 'Recetas AJAX', -'An error occured, please %s the page': 'Ha ocurrido un error, por favor %s la página', -'And': 'Y', -'and rename it (required):': 'y renómbrela (requerido):', -'and rename it:': ' y renómbrelo:', +'@markmin\x01An error occured, please [[reload %s]] the page': 'Hi ha hagut un error, si us plau [[recarregui %s]] la pàgina', +'@markmin\x01Number of entries: **%s**': "Nombre d'entrades: **%s**", +'A new version of web2py is available': 'Hi ha una nova versió de wep2py disponible', +'A new version of web2py is available: %s': 'Hi ha una nova versió de wep2py disponible: %s', +'About': 'Sobre', +'about': 'sobre', +'About application': "Sobre l'aplicació", +'Access Control': "Control d'Accés", +'Add': 'Afegir', +'Add Record': 'Afegeix registre', +'additional code for your application': '`codi addicional per a la seva aplicació', +'admin disabled because no admin password': 'admin inhabilitat per falta de contrasenya', +'admin disabled because not supported on google app engine': 'admin inhabilitat, no és suportat en GAE', +'admin disabled because unable to access password file': 'admin inhabilitat, impossible accedir al fitxer con la contrasenya', +'Admin is disabled because insecure channel': 'Admin inhabilitat, el canal no és segur', +'Admin is disabled because unsecure channel': 'Admin inhabilitat, el canal no és segur', +'Administrative interface': 'Interfície administrativa', +'Administrative Interface': 'Interfície Administrativa', +'administrative interface': 'interfície administrativa', +'Administrator Password:': 'Contrasenya del Administrador:', +'Ajax Recipes': 'Receptes AJAX', +'An error occured, please %s the page': 'Hi ha hagut un error, per favor %s la pàgina', +'And': 'I', +'and rename it (required):': 'i renombri-la (requerit):', +'and rename it:': " i renombri'l:", 'appadmin': 'appadmin', -'appadmin is disabled because insecure channel': 'admin deshabilitado, el canal no es seguro', -'application "%s" uninstalled': 'aplicación "%s" desinstalada', -'application compiled': 'aplicación compilada', -'application is compiled and cannot be designed': 'la aplicación está compilada y no puede ser modificada', -'Apply changes': 'Aplicar cambios', -'Appointment': 'Nombramiento', -'Are you sure you want to delete file "%s"?': '¿Está seguro que desea eliminar el archivo "%s"?', -'Are you sure you want to delete this object?': '¿Está seguro que desea borrar este objeto?', -'Are you sure you want to uninstall application "%s"': '¿Está seguro que desea desinstalar la aplicación "%s"', -'Are you sure you want to uninstall application "%s"?': '¿Está seguro que desea desinstalar la aplicación "%s"?', -'at': 'en', -'ATTENTION: Login requires a secure (HTTPS) connection or running on localhost.': 'ATENCION: Inicio de sesión requiere una conexión segura (HTTPS) o localhost.', +'appadmin is disabled because insecure channel': 'admin inhabilitat, el canal no és segur', +'application "%s" uninstalled': 'aplicació "%s" desinstal·lada', +'application compiled': 'aplicació compilada', +'application is compiled and cannot be designed': 'la aplicació està compilada i no pot ser modificada', +'Apply changes': 'Aplicar canvis', +'Appointment': 'Nomenament', +'Are you sure you want to delete file "%s"?': 'Està segur que vol eliminar el arxiu "%s"?', +'Are you sure you want to delete this object?': 'Està segur que vol esborrar aquest objecte?', +'Are you sure you want to uninstall application "%s"': '¿Està segur que vol desinstalar la aplicació "%s"', +'Are you sure you want to uninstall application "%s"?': '¿Està segur que vol desinstalar la aplicació "%s"?', +'at': 'a', +'ATTENTION: Login requires a secure (HTTPS) connection or running on localhost.': 'ATENCIÓ: Inici de sessió requereix una connexió segura (HTTPS) o localhost.', 'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.': 'ATENCION: NO EJECUTE VARIAS PRUEBAS SIMULTANEAMENTE, NO SON THREAD SAFE.', -'ATTENTION: you cannot edit the running application!': 'ATENCION: no puede modificar la aplicación que está ejecutandose!', -'Authentication': 'Autenticación', -'Authentication failed at client DB!': '¡La autenticación ha fallado en la BDD cliente!', -'Authentication failed at main DB!': '¡La autenticación ha fallado en la BDD principal!', -'Available Databases and Tables': 'Bases de datos y tablas disponibles', -'Back': 'Atrás', -'Buy this book': 'Compra este libro', +'ATTENTION: you cannot edit the running application!': 'ATENCIO: no pot modificar la aplicació que està ejecutant-se!', +'Authentication': 'Autenticació', +'Authentication failed at client DB!': '¡La autenticació ha fallat en la BDD client!', +'Authentication failed at main DB!': '¡La autenticació ha fallat en la BDD principal!', +'Available Databases and Tables': 'Bases de dades i taules disponibles', +'Back': 'Endarrera', +'Buy this book': 'Compra aquest lllibre', 'Cache': 'Caché', 'cache': 'caché', -'Cache Keys': 'Llaves de la Caché', -'cache, errors and sessions cleaned': 'caché, errores y sesiones eliminados', -'Cannot be empty': 'No puede estar vacío', -'Cannot compile: there are errors in your app. Debug it, correct errors and try again.': 'No se puede compilar: hay errores en su aplicación. Depure, corrija errores y vuelva a intentarlo.', -'cannot create file': 'no es posible crear archivo', -'cannot upload file "%(filename)s"': 'no es posible subir archivo "%(filename)s"', -'Change Password': 'Cambie la Contraseña', -'Change password': 'Cambie la contraseña', -'change password': 'cambie la contraseña', -'check all': 'marcar todos', -'Check to delete': 'Marque para eliminar', -'choose one': 'escoja uno', -'clean': 'limpiar', -'Clear': 'Limpiar', -'Clear CACHE?': '¿Limpiar CACHÉ?', -'Clear DISK': 'Limpiar DISCO', -'Clear RAM': 'Limpiar RAM', -'Click on the link %(link)s to reset your password': 'Pulse en el enlace %(link)s para reiniciar su contraseña', -'click to check for upgrades': 'haga clic para buscar actualizaciones', +'Cache Cleared': 'Caché Netejada', +'Cache Keys': 'Claus de la Caché', +'cache, errors and sessions cleaned': 'caché, errors i sessions eliminats', +'Cannot be empty': 'No pot estar buit', +'Cannot compile: there are errors in your app. Debug it, correct errors and try again.': 'No se pot compilar: hi ha errors en la seva aplicació. Depuri, corregeixi errors i torni a intentar-ho.', +'cannot upload file "%(filename)s"': 'no és possible pujar fitxer "%(filename)s"', +'Change Password': 'Canviï la Contrasenya', +'Change password': 'Canviï la contrasenya', +'change password': 'canviï la contrasenya', +'Changelog': 'Changelog', +'check all': 'marcar tots', +'Check to delete': 'Marqui per a eliminar', +'choose one': 'escolliu un', +'clean': 'neteja', +'Clear': 'Netejar', +'Clear CACHE?': 'Netejar Memòrica Cau?', +'Clear DISK': 'Netejar DISC', +'Clear RAM': 'Netejar RAM', +'Click on the link %(link)s to reset your password': "Cliqui en l'enllaç %(link)s per a reiniciar la seva contrasenya", +'click to check for upgrades': 'feu clic per buscar actualitzacions', 'client': 'cliente', -'Client IP': 'IP del Cliente', -'Close': 'Cerrar', -'Community': 'Comunidad', +'Client IP': 'IP del Client', +'Close': 'Tancar', +'Comma-separated export including columns not shown; fields from other tables are exported as raw values for faster export': 'Comma-separated export including columns not shown; fields from other tables are exported as raw values for faster export', +'Comma-separated export of visible columns. Fields from other tables are exported as they appear on-screen but this may be slow for many rows': 'Comma-separated export of visible columns. Fields from other tables are exported as they appear on-screen but this may be slow for many rows', +'Community': 'Comunitat', 'compile': 'compilar', -'compiled application removed': 'aplicación compilada eliminada', -'Components and Plugins': 'Componentes y Plugins', -'contains': 'contiene', +'compiled application removed': 'aplicació compilada eliminada', +'Components and Plugins': 'Components i Plugins', +'contains': 'conté', 'Controller': 'Controlador', -'Controllers': 'Controladores', -'controllers': 'controladores', +'Controllers': 'Controladors', +'controllers': 'controladors', 'Copyright': 'Copyright', -'create file with filename:': 'cree archivo con nombre:', -'Create new application': 'Cree una nueva aplicación', -'create new application:': 'nombre de la nueva aplicación:', -'Created By': 'Creado Por', -'Created On': 'Creado En', -'CSV (hidden cols)': 'CSV (columnas ocultas)', -'Current request': 'Solicitud en curso', -'Current response': 'Respuesta en curso', -'Current session': 'Sesión en curso', -'currently saved or': 'actualmente guardado o', -'customize me!': '¡Adáptame!', -'data uploaded': 'datos subidos', -'Database': 'Base de datos', -'Database %s select': 'selección en base de datos %s', -'database administration': 'administración de base de datos', -'Database Administration (appadmin)': 'Administración de Base de Datos (appadmin)', -'Date and Time': 'Fecha y Hora', +'Correo electrónico invàlid': 'Correu electrònic invàlid', +'create file with filename:': 'crear el fitxer amb el nom:', +'Create new application': 'Crear una nova aplicació', +'create new application:': 'crear una nova aplicació:', +'Create New Page': 'Crear Pàgina Nova', +'Create Page from Slug': 'Create Page from Slug', +'Created By': 'Creat Per', +'Created On': 'Creat a', +'CSV': 'CSV', +'CSV (hidden cols)': 'CSV (columnas ocultes)', +'Current request': 'Sol·licitud en curs', +'Current response': 'Resposta en curs', +'Current session': 'Sessió en curs', +'currently saved or': 'actualment guardat o', +'customize me!': "¡Adapta'm!", +'data uploaded': 'dades pujades', +'Database': 'Base de dades', +'Database %s select': 'selecció a base de dades %s', +'database administration': 'administració de base de dades', +'Database Administration (appadmin)': 'Administració de Base de Dades (appadmin)', +'Date and Time': 'Data i Hora', 'DB': 'BDD', 'db': 'bdd', -'DB Model': 'Modelo BDD', -'defines tables': 'define tablas', +'DB Model': 'Model BDD', +'defines tables': 'defineix taules', 'Delete': 'Eliminar', 'delete': 'eliminar', -'delete all checked': 'eliminar marcados', +'delete all checked': 'eliminar marcats', 'Delete:': 'Eliminar:', -'Demo': 'Demostración', -'Deploy on Google App Engine': 'Despliegue en Google App Engine', -'Deployment Recipes': 'Recetas de despliegue', -'Description': 'Descripción', -'design': 'diseño', -'DESIGN': 'DISEÑO', -'Design for': 'Diseño por', -'detecting': 'detectando', -'DISK': 'DISCO', -'Disk Cache Keys': 'Llaves de Caché en Disco', -'Disk Cleared': 'Disco limpiado', -'Documentation': 'Documentación', -"Don't know what to do?": '¿No sabe que hacer?', -'done!': '¡hecho!', -'Download': 'Descargas', -'E-mail': 'Correo electrónico', +'Demo': 'Demostració', +'Deploy on Google App Engine': 'Desplegament a Google App Engine', +'Deployment Recipes': 'Receptes de desplegament', +'Description': 'Descripció', +'design': 'diseny', +'DESIGN': 'DISENY', +'Design for': 'Diseny per a', +'detecting': 'detectant', +'DISK': 'DISC', +'Disk Cache Keys': 'Claus de Caché en Disc', +'Disk Cleared': 'Disc netejat', +'Documentation': 'Documentació', +"Don't know what to do?": 'No sap què fer?', +'done!': '¡fet!', +'Download': 'Descàrregues', +'E-mail': 'Correu electrònic', 'edit': 'editar', 'EDIT': 'EDITAR', 'Edit': 'Editar', -'Edit application': 'Editar aplicación', +'Edit application': 'Editar aplicació', 'edit controller': 'editar controlador', -'Edit current record': 'Edite el registro actual', +'Edit current record': 'Editar el registre actual', +'Edit Menu': 'Editar Menu', +'Edit Page': 'Editar Pàgina', +'Edit Page Media': 'Edit Page Media', 'Edit Profile': 'Editar Perfil', 'edit profile': 'editar perfil', -'Edit This App': 'Edite esta App', -'Editing file': 'Editando archivo', -'Editing file "%s"': 'Editando archivo "%s"', -'Email and SMS': 'Correo electrónico y SMS', -'Email sent': 'Correo electrónico enviado', -'End of impersonation': 'Fin de suplantación', -'enter a number between %(min)g and %(max)g': 'introduzca un número entre %(min)g y %(max)g', -'enter a value': 'introduzca un valor', -'enter an integer between %(min)g and %(max)g': 'introduzca un entero entre %(min)g y %(max)g', -'enter date and time as %(format)s': 'introduzca fecha y hora como %(format)s', -'Error logs for "%(app)s"': 'Bitácora de errores en "%(app)s"', -'errors': 'errores', -'Errors': 'Errores', -'Errors in form, please check it out.': 'Hay errores en el formulario, por favor comprúebelo.', -'export as csv file': 'exportar como archivo CSV', +'Edit This App': 'Editi aquesta App', +'Editing file': 'Editant fitxer', +'Editing file "%s"': 'Editant fitxer "%s"', +'El fitxer ha de ser PDF': 'El fitxer ha de ser PDF', +'El fitxer ha de ser PDF o XML': 'El fitxer ha de ser PDF o XML', +'Email': 'Email', +'Email and SMS': 'Correu electrònic i SMS', +'Email sent': 'Correu electrònic enviat', +'End of impersonation': 'Fi de suplantació', +'enter a number between %(min)g and %(max)g': 'introdueixi un número entre %(min)g i %(max)g', +'Enter a valid email address': 'Entri una adreça email vàlida', +'enter a value': 'entri un valor', +'Enter a value': 'Entri un valor', +'Enter an integer between %(min)g and %(max)g': 'Entri un numero enter entre %(min)g i %(max)g', +'enter an integer between %(min)g and %(max)g': 'entri numero enter entre %(min)g i %(max)g', +'enter date and time as %(format)s': 'entri data i hora com %(format)s', +'Enter from %(min)g to %(max)g characters': 'Entri des de %(min)g a %(max)g caràcters', +'Enter valid filename': 'Entri nom de fitxer vàlid', +'Error logs for "%(app)s"': 'Bitàcora de errors a "%(app)s"', +'errors': 'errors', +'Errors': 'Errors', +'Errors in form, please check it out.': 'Hi ha errors en el formulari, per favor comprovi-ho.', +'export as csv file': 'exportar com fitxer CSV', 'Export:': 'Exportar:', -'exposes': 'expone', -'extends': 'extiende', -'failed to reload module': 'la recarga del módulo ha fallado', +'exposes': 'exposa', +'extends': 'extén', +'failed to reload module': 'la recàrrega del mòdul ha fallat', 'FAQ': 'FAQ', -'file "%(filename)s" created': 'archivo "%(filename)s" creado', -'file "%(filename)s" deleted': 'archivo "%(filename)s" eliminado', -'file "%(filename)s" uploaded': 'archivo "%(filename)s" subido', -'file "%(filename)s" was not deleted': 'archivo "%(filename)s" no fué eliminado', -'file "%s" of %s restored': 'archivo "%s" de %s restaurado', -'file changed on disk': 'archivo modificado en el disco', -'file does not exist': 'archivo no existe', -'file saved on %(time)s': 'archivo guardado %(time)s', -'file saved on %s': 'archivo guardado %s', -'First name': 'Nombre', -'Forgot username?': '¿Olvidó el nombre de usuario?', -'Forms and Validators': 'Formularios y validadores', -'Free Applications': 'Aplicaciones Libres', -'Functions with no doctests will result in [passed] tests.': 'Funciones sin doctests equivalen a pruebas [aceptadas].', -'Group %(group_id)s created': 'Grupo %(group_id)s creado', -'Group ID': 'ID de Grupo', -'Group uniquely assigned to user %(id)s': 'Grupo asignado únicamente al usuario %(id)s', -'Groups': 'Grupos', -'Hello World': 'Hola Mundo', -'help': 'ayuda', -'Home': 'Inicio', -'How did you get here?': '¿Cómo llegaste aquí?', +'file': 'fitxer', +'file "%(filename)s" created': 'fitxer "%(filename)s" creat', +'file "%(filename)s" deleted': 'fitxer "%(filename)s" eliminat', +'file "%(filename)s" uploaded': 'fitxer "%(filename)s" pujat', +'file "%(filename)s" was not deleted': 'fitxer "%(filename)s" no fou eliminat', +'file "%s" of %s restored': 'fitxer "%s" de %s restaurat', +'file ## download': 'file ', +'file changed on disk': 'fitxer modificat en el disco', +'file does not exist': 'fitxer no existeix', +'file saved on %(time)s': 'fitxer guardat a %(time)s', +'file saved on %s': 'fitxer guardat a %s', +'First name': 'Nom', +'Forgot username?': 'Ha oblidat el nom de usuari?', +'Forms and Validators': 'Formularis i validadors', +'Free Applications': 'Aplicacions Lliures', +'Functions with no doctests will result in [passed] tests.': 'Funcions sense doctests equivalen a pruebas [aceptades].', +'Group %(group_id)s created': 'Grupo %(group_id)s creat', +'Group ID': 'ID de Grup', +'Group uniquely assigned to user %(id)s': 'Grup assignat únicament al usuari %(id)s', +'Groups': 'Grups', +'Hello': 'Hola', +'Hello World': 'Hola Món', +'help': 'ajuda', +'Home': 'Inici', +'Hosted by': 'Hosted by', +'How did you get here?': 'Com has arribat aquí?', +'HTML': 'HTML', +'HTML export of visible columns': 'HTML export de columnes visibles', 'htmledit': 'htmledit', 'Impersonate': 'Suplantar', 'import': 'importar', 'Import/Export': 'Importar/Exportar', -'in': 'en', -'includes': 'incluye', -'Index': 'Índice', -'insert new': 'inserte nuevo', -'insert new %s': 'inserte nuevo %s', -'Installed applications': 'Aplicaciones instaladas', -'Insufficient privileges': 'Privilegios insuficientes', -'internal error': 'error interno', -'Internal State': 'Estado Interno', -'Introduction': 'Introducción', -'Invalid action': 'Acción inválida', -'Invalid email': 'Correo electrónico inválido', -'invalid expression': 'expresión inválida', -'Invalid login': 'Inicio de sesión inválido', -'invalid password': 'contraseña inválida', -'Invalid Query': 'Consulta inválida', -'invalid request': 'solicitud inválida', -'Invalid reset password': 'Reinicio de contraseña inválido', -'invalid ticket': 'tiquete inválido', -'Is Active': 'Está Activo', -'Key': 'Llave', -'language file "%(filename)s" created/updated': 'archivo de lenguaje "%(filename)s" creado/actualizado', -'Language files (static strings) updated': 'Archivos de lenguaje (cadenas estáticas) actualizados', -'languages': 'lenguajes', -'Languages': 'Lenguajes', -'languages updated': 'lenguajes actualizados', -'Last name': 'Apellido', -'Last saved on:': 'Guardado en:', -'Layout': 'Diseño de página', -'Layout Plugins': 'Plugins de diseño', -'Layouts': 'Diseños de páginas', -'License for': 'Licencia para', -'Live Chat': 'Chat en vivo', -'loading...': 'cargando...', -'Logged in': 'Sesión iniciada', -'Logged out': 'Sesión finalizada', -'Login': 'Inicio de sesión', -'login': 'inicio de sesión', -'Login disabled by administrator': 'Inicio de sesión deshabilitado por el administrador', -'Login to the Administrative Interface': 'Inicio de sesión para la Interfaz Administrativa', -'logout': 'fin de sesión', -'Logout': 'Fin de sesión', -'Lost Password': 'Contraseña perdida', -'Lost password?': '¿Olvidó la contraseña?', -'lost password?': '¿olvidó la contraseña?', +'in': 'a', +'includes': 'inclou', +'Index': 'Índex', +'insert new': 'inserti nou', +'insert new %s': 'inserti nou %s', +'Installed applications': 'Aplicacions instalades', +'Insufficient privileges': 'Privilegis insuficients', +'internal error': 'error intern', +'Internal State': 'Estat Intern', +'Introduction': 'Introducció', +'Invalid action': 'Acció invàlida', +'Invalid email': 'Correo electrónico invàlid', +'invalid expression': 'expressió invàlida', +'Invalid login': 'Inici de sessió invàlida', +'invalid password': 'contrasenya invàlida', +'Invalid Query': 'Consulta invàlida', +'invalid request': 'sol·licitud invàlida', +'Invalid reset password': 'Reinici de contrasenya invàlid', +'invalid ticket': 'tiquet invàlid', +'Is Active': 'Està Actiu', +'Key': 'Clau', +'language file "%(filename)s" created/updated': 'fitxer de llenguatge "%(filename)s" creat/actualitzat', +'Language files (static strings) updated': 'Fitxers de llenguatge (cadenes estàtiques) actualitzats', +'languages': 'llenguatges', +'Languages': 'Llenguatges', +'languages updated': 'llenguatges actualitzats', +'Last name': 'Cognom', +'Last saved on:': 'Guardat a:', +'Layout': 'Diseny de pàgina', +'Layout Plugins': 'Plugins de disseny', +'Layouts': 'Dissenys de pàgines', +'License for': 'Llicència per a', +'Live Chat': 'Xat en viu', +'loading...': 'carregant...', +'Log In': 'Log In', +'Log Out': 'Log Out', +'Logged in': 'Sessió iniciada', +'Logged out': 'Sessió finalitzada', +'Login': 'Inici de sessió', +'login': 'inici de sessió', +'Login disabled by administrator': 'Inici de sessió inhabilitat pel administrador', +'Login to the Administrative Interface': 'Inici de sessió per a la Interfície Administrativa', +'logout': 'fi de sessió', +'Logout': 'Fi de sessió', +'Lost Password': 'Contrasenya perdida', +'Lost password?': 'Ha oblidat la contrasenya?', +'lost password?': '¿ha oblidat la contrasenya?', 'Main Menu': 'Menú principal', +'Manage %(action)s': 'Manage %(action)s', +'Manage Access Control': 'Manage Access Control', 'Manage Cache': 'Gestionar la Caché', -'Menu Model': 'Modelo "menu"', +'Menu Model': 'Model "menu"', 'merge': 'combinar', -'Models': 'Modelos', -'models': 'modelos', -'Modified By': 'Modificado Por', -'Modified On': 'Modificado En', -'Modules': 'Módulos', -'modules': 'módulos', +'Models': 'Models', +'models': 'models', +'Modified By': 'Modificat Per', +'Modified On': 'Modificat A', +'Modules': 'Mòduls', +'modules': 'mòduls', 'must be YYYY-MM-DD HH:MM:SS!': '¡debe ser DD/MM/YYYY HH:MM:SS!', 'must be YYYY-MM-DD!': '¡debe ser DD/MM/YYYY!', -'My Sites': 'Mis Sitios', +'My Sites': 'Els Meus Llocs', 'Name': 'Nombre', 'New': 'Nuevo', 'New %(entity)s': 'Nuevo %(entity)s', -'new application "%s" created': 'nueva aplicación "%s" creada', -'New password': 'Contraseña nueva', -'New Record': 'Registro nuevo', -'new record inserted': 'nuevo registro insertado', -'next 100 rows': '100 filas siguientes', -'NO': 'NO', -'No databases in this application': 'No hay bases de datos en esta aplicación', -'No records found': 'No se han encontrado registros', -'Not authorized': 'No autorizado', -'not in': 'no en', -'Object or table name': 'Nombre del objeto o tabla', -'Old password': 'Contraseña vieja', -'Online examples': 'Ejemplos en línea', -'Or': 'O', -'or import from csv file': 'o importar desde archivo CSV', -'or provide application url:': 'o provea URL de la aplicación:', -'Origin': 'Origen', -'Original/Translation': 'Original/Traducción', -'Other Plugins': 'Otros Plugins', -'Other Recipes': 'Otras Recetas', -'Overview': 'Resumen', -'pack all': 'empaquetar todo', -'pack compiled': 'empaquetar compilados', -'Password': 'Contraseña', -'Password changed': 'Contraseña cambiada', -"Password fields don't match": 'Los campos de contraseña no coinciden', -'Password reset': 'Reinicio de contraseña', -'Peeking at file': 'Visualizando archivo', -'Phone': 'Teléfono', -'please input your password again': 'por favor introduzca su contraseña otra vez', -'Plugins': 'Plugins', -'Powered by': 'Este sitio usa', -'Preface': 'Prefacio', -'previous 100 rows': '100 filas anteriores', -'Profile': 'Perfil', -'Profile updated': 'Perfil actualizado', -'Python': 'Python', -'Query Not Supported: %s': 'Consulta No Soportada: %s', -'Query:': 'Consulta:', -'Quick Examples': 'Ejemplos Rápidos', -'RAM': 'RAM', -'RAM Cache Keys': 'Llaves de la Caché en RAM', -'Ram Cleared': 'Ram Limpiada', -'Recipes': 'Recetas', -'Record': 'Registro', -'Record %(id)s created': 'Registro %(id)s creado', -'Record Created': 'Registro Creado', -'record does not exist': 'el registro no existe', -'Record ID': 'ID de Registro', -'Record id': 'Id de registro', -'register': 'regístrese', -'Register': 'Regístrese', -'Registration identifier': 'Identificador de Registro', -'Registration key': 'Llave de registro', -'Registration successful': 'Registro con éxito', -'reload': 'recargar', -'Remember me (for 30 days)': 'Recuérdame (durante 30 días)', -'remove compiled': 'eliminar compiladas', -'Request reset password': 'Solicitar reinicio de contraseña', -'Reset password': 'Reiniciar contraseña', -'Reset Password key': 'Restaurar Llave de la Contraseña', -'Resolve Conflict file': 'archivo Resolución de Conflicto', -'restore': 'restaurar', -'Retrieve username': 'Recuperar nombre de usuario', -'revert': 'revertir', -'Role': 'Rol', -'Rows in Table': 'Filas en la tabla', -'Rows selected': 'Filas seleccionadas', -'save': 'guardar', -'Saved file hash:': 'Hash del archivo guardado:', -'Search': 'Buscar', -'Semantic': 'Semántica', -'Services': 'Servicios', -'session expired': 'sesión expirada', -'shell': 'terminal', -'site': 'sitio', -'Size of cache:': 'Tamaño de la Caché:', -'some files could not be removed': 'algunos archivos no pudieron ser removidos', -'start': 'inicio', -'starts with': 'comienza por', -'state': 'estado', -'static': 'estáticos', -'Static files': 'Archivos estáticos', -'Statistics': 'Estadísticas', -'Stylesheet': 'Hoja de estilo', -'Submit': 'Enviar', -'submit': 'enviar', -'Success!': '¡Correcto!', -'Support': 'Soporte', -'Sure you want to delete this object?': '¿Está seguro que desea eliminar este objeto?', -'Table': 'tabla', -'Table name': 'Nombre de la tabla', -'test': 'probar', -'Testing application': 'Probando aplicación', -'The "query" is a condition like "db.table1.field1==\'value\'". Something like "db.table1.field1==db.table2.field2" results in a SQL JOIN.': 'La "consulta" es una condición como "db.tabla1.campo1==\'valor\'". Algo como "db.tabla1.campo1==db.tabla2.campo2" resulta en un JOIN SQL.', -'the application logic, each URL path is mapped in one exposed function in the controller': 'la lógica de la aplicación, cada ruta URL se mapea en una función expuesta en el controlador', -'The Core': 'El Núcleo', -'the data representation, define database tables and sets': 'la representación de datos, define tablas y conjuntos de base de datos', -'The output of the file is a dictionary that was rendered by the view %s': 'La salida de dicha función es un diccionario que es desplegado por la vista %s', -'the presentations layer, views are also known as templates': 'la capa de presentación, las vistas también son llamadas plantillas', -'The Views': 'Las Vistas', -'There are no controllers': 'No hay controladores', -'There are no models': 'No hay modelos', -'There are no modules': 'No hay módulos', -'There are no static files': 'No hay archivos estáticos', -'There are no translators, only default language is supported': 'No hay traductores, sólo el lenguaje por defecto es soportado', -'There are no views': 'No hay vistas', -'these files are served without processing, your images go here': 'estos archivos son servidos sin procesar, sus imágenes van aquí', -'This App': 'Esta Aplicación', -'This email already has an account': 'Este correo electrónico ya tiene una cuenta', -'This is a copy of the scaffolding application': 'Esta es una copia de la aplicación de andamiaje', -'This is the %(filename)s template': 'Esta es la plantilla %(filename)s', -'Ticket': 'Tiquete', -'Time in Cache (h:m:s)': 'Tiempo en Caché (h:m:s)', -'Timestamp': 'Marca de tiempo', -'to previous version.': 'a la versión previa.', -'To emulate a breakpoint programatically, write:': 'Emular un punto de ruptura programáticamente, escribir:', -'to use the debugger!': '¡usar el depurador!', -'toggle breakpoint': 'alternar punto de ruptura', -'Toggle comment': 'Alternar comentario', -'Toggle Fullscreen': 'Alternar pantalla completa', -'too short': 'demasiado corto', -'translation strings for the application': 'cadenas de caracteres de traducción para la aplicación', -'try': 'intente', -'try something like': 'intente algo como', -'TSV (Excel compatible)': 'TSV (compatible Excel)', -'TSV (Excel compatible, hidden cols)': 'TSV (compatible Excel, columnas ocultas)', -'Twitter': 'Twitter', -'Unable to check for upgrades': 'No es posible verificar la existencia de actualizaciones', -'unable to create application "%s"': 'no es posible crear la aplicación "%s"', -'unable to delete file "%(filename)s"': 'no es posible eliminar el archivo "%(filename)s"', -'Unable to download': 'No es posible la descarga', -'Unable to download app': 'No es posible descarga la aplicación', -'unable to parse csv file': 'no es posible analizar el archivo CSV', -'unable to uninstall "%s"': 'no es posible instalar "%s"', -'uncheck all': 'desmarcar todos', -'uninstall': 'desinstalar', -'unknown': 'desconocido', -'update': 'actualizar', -'update all languages': 'actualizar todos los lenguajes', -'Update:': 'Actualice:', -'upload application:': 'subir aplicación:', -'Upload existing application': 'Suba esta aplicación', -'upload file:': 'suba archivo:', -'Use (...)&(...) for AND, (...)|(...) for OR, and ~(...) for NOT to build more complex queries.': 'Use (...)&(...) para AND, (...)|(...) para OR, y ~(...) para NOT, para crear consultas más complejas.', -'User %(id)s is impersonating %(other_id)s': 'El usuario %(id)s está suplantando %(other_id)s', -'User %(id)s Logged-in': 'El usuario %(id)s inició la sesión', -'User %(id)s Logged-out': 'El usuario %(id)s finalizó la sesión', -'User %(id)s Password changed': 'Contraseña del usuario %(id)s cambiada', -'User %(id)s Password reset': 'Contraseña del usuario %(id)s reiniciada', -'User %(id)s Profile updated': 'Actualizado el perfil del usuario %(id)s', -'User %(id)s Registered': 'Usuario %(id)s Registrado', -'User %(id)s Username retrieved': 'Se ha recuperado el nombre de usuario del usuario %(id)s', -'User %(username)s Logged-in': 'El usuario %(username)s inició la sesión', -"User '%(username)s' Logged-in": "El usuario '%(username)s' inició la sesión", -"User '%(username)s' Logged-out": "El usuario '%(username)s' finalizó la sesión", -'User Id': 'Id de Usuario', -'User ID': 'ID de Usuario', -'User Logged-out': 'El usuario finalizó la sesión', -'Username': 'Nombre de usuario', -'Username retrieve': 'Recuperar nombre de usuario', -'value already in database or empty': 'el valor ya existe en la base de datos o está vacío', -'value not allowed': 'valor no permitido', -'value not in database': 'el valor no está en la base de datos', -'Verify Password': 'Verificar Contraseña', -'Version': 'Versión', -'versioning': 'versiones', -'Videos': 'Vídeos', -'View': 'Vista', -'view': 'vista', -'View %(entity)s': 'Ver %(entity)s', -'Views': 'Vistas', -'views': 'vistas', -'web2py is up to date': 'web2py está actualizado', -'web2py Recent Tweets': 'Tweets Recientes de web2py', -'Welcome': 'Bienvenido', -'Welcome %s': 'Bienvenido %s', -'Welcome to web2py': 'Bienvenido a web2py', -'Welcome to web2py!': '¡Bienvenido a web2py!', -'Which called the function %s located in the file %s': 'La cual llamó la función %s localizada en el archivo %s', -'Working...': 'Trabajando...', -'YES': 'SÍ', -'You are successfully running web2py': 'Usted está ejecutando web2py exitosamente', -'You can modify this application and adapt it to your needs': 'Usted puede modificar esta aplicación y adaptarla a sus necesidades', -'You visited the url %s': 'Usted visitó la url %s', -'Your username is: %(username)s': 'Su nombre de usuario es: %(username)s', } From 8c1ca50205d19d4076587dff92a91dcdaab6c888 Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 21:29:57 +0100 Subject: [PATCH 27/63] Update ca.py --- applications/welcome/languages/ca.py | 218 ++++++++++++++++++++++++++- 1 file changed, 217 insertions(+), 1 deletion(-) diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py index 659ea48b..6f830149 100644 --- a/applications/welcome/languages/ca.py +++ b/applications/welcome/languages/ca.py @@ -1,3 +1,4 @@ + # -*- coding: utf-8 -*- { '!langcode!': 'ca', @@ -273,5 +274,220 @@ 'My Sites': 'Els Meus Llocs', 'Name': 'Nombre', 'New': 'Nuevo', -'New %(entity)s': 'Nuevo %(entity)s', +'New %(entity)s': 'Nou %(entity)s', +'new application "%s" created': 'nova aplicació "%s" creada', +'New password': 'Contrasenya nova', +'New Record': 'Registre nou', +'new record inserted': 'nou registre insertat', +'New Search': 'Cerca nova', +'next %s rows': 'següents %s files', +'next 100 rows': '100 files següents', +'NO': 'NO', +'No databases in this application': 'No hi ha bases de dades en esta aplicació', +'No records found': "No s'han trobat registres", +'Not authorized': 'No autoritzat', +'not in': 'no a', +'Object or table name': 'Nom del objecte o taula', +'Old password': 'Contrasenya anterior', +'Online examples': 'Ejemples en línia', +'Or': 'O', +'or import from csv file': 'o importar desde fitxer CSV', +'or provide application url:': 'o proveeix URL de la aplicació:', +'Origin': 'Origen', +'Original/Translation': 'Original/Traducció', +'Other Plugins': 'Altres Plugins', +'Other Recipes': 'Altres Receptes', +'Overview': 'Resum', +'pack all': 'empaquetar tot', +'pack compiled': 'empaquetar compilats', +'Password': 'Contrasenya', +'Password changed': 'Contrasenya cambiada', +"Password fields don't match": 'Els camps de contrasenya no coincideixen', +'Password reset': 'Reinici de contrasenya', +'Peeking at file': 'Visualitzant fitxer', +'Permission': 'Permís', +'Permissions': 'Permisos', +'Phone': 'Telèfon', +'please input your password again': 'si us plau, entri un altre cop la seva contrasenya', +'Plugins': 'Plugins', +'Powered by': 'Aquest lloc utilitza', +'Preface': 'Prefaci', +'Presentar Factures': 'Presentar Factures', +'Presentar factures': 'Presentar factures', +'previous %s rows': '%s files prèvies', +'previous 100 rows': '100 files anteriors', +'Profile': 'Perfil', +'Profile updated': 'Perfil actualitzat', +'pygraphviz library not found': 'pygraphviz library not found', +'Python': 'Python', +'Query Not Supported: %s': 'Consulta No Suportada: %s', +'Query:': 'Consulta:', +'Quick Examples': 'Exemple Ràpids', +'RAM': 'RAM', +'RAM Cache Keys': 'Claus de la Caché en RAM', +'Ram Cleared': 'Ram Netjeda', +'Recipes': 'Receptes', +'Record': 'Registre', +'Record %(id)s created': 'Registre %(id)s creat', +'Record Created': 'Registre Creat', +'record does not exist': 'el registre no existe', +'Record ID': 'ID de Registre', +'Record id': 'Id de registre', +'Ref APB': 'Ref APB', +'register': "registri's", +'Register': "Registri's", +'Registration identifier': 'Identificador de Registre', +'Registration key': 'Clau de registre', +'Registration successful': 'Registre amb èxit', +'reload': 'recarregar', +'Remember me (for 30 days)': "Recordi'm (durant 30 dies)", +'remove compiled': 'eliminar compilades', +'Request reset password': 'Sol·licitud de restabliment de contrasenya', +'Reset password': 'Reiniciar contrasenya', +'Reset Password key': 'Restaurar Clau de la Contrasenya', +'Resolve Conflict file': 'Resolgui el Conflicte de fitxer', +'restore': 'restaurar', +'Retrieve username': 'Recuperar nom de usuari', +'revert': 'revertir', +'Role': 'Rol', +'Roles': 'Rols', +'Rows in Table': 'Files a la taula', +'Rows selected': 'Files seleccionades', +'save': 'guardar', +'Save model as...': 'Save model as...', +'Saved file hash:': 'Hash del fitxer guardat:', +'Search': 'Buscar', +'Search Pages': 'Search Pages', +'Semantic': 'Semàntica', +'Services': 'Serveis', +'session expired': 'sessió expirada', +'shell': 'terminal', +'Sign Up': 'Sign Up', +'site': 'lloc', +'Size of cache:': 'Mida de la Caché:', +'Slug': 'Slug', +'some files could not be removed': 'algunos archivos no pudieron ser removidos', +'Spreadsheet-optimised export of tab-separated content including hidden columns. May be slow': 'Spreadsheet-optimised export of tab-separated content including hidden columns. May be slow', +'Spreadsheet-optimised export of tab-separated content, visible columns only. May be slow.': 'Spreadsheet-optimised export of tab-separated content, visible columns only. May be slow.', +'start': 'inici', +'Start building a new search': 'Start building a new search', +'starts with': 'comença per', +'state': 'estat', +'static': 'estàtics', +'Static files': 'Fitxers estàtics', +'Statistics': 'Estadístiques', +'Stylesheet': "Fulla d'estil", +'Submit': 'Enviar', +'submit': 'enviar', +'Success!': 'Correcte!', +'Support': 'Suport', +'Sure you want to delete this object?': '¿Està segur que vol eliminar aquest objecte?', +'Table': 'taula', +'Table name': 'Nom de la taula', +'test': 'provar', +'Testing application': 'Provant aplicació', +'The "query" is a condition like "db.table1.field1==\'value\'". Something like "db.table1.field1==db.table2.field2" results in a SQL JOIN.': 'La "consulta" és una condición com "db.tabla1.campo1==\'valor\'". Algo com "db.tabla1.campo1==db.tabla2.campo2" resulta en un JOIN SQL.', +'the application logic, each URL path is mapped in one exposed function in the controller': 'la lògica de la aplicació, cada ruta URL es mapeja en una funció exposada en el controlador', +'The Core': 'El Nucli', +'the data representation, define database tables and sets': 'la representació de dades, defineix taules i conjunts de base de dades', +'The output of the file is a dictionary that was rendered by the view %s': 'El resultat de aquesta funció és un diccionari que és desplegat per la vista %s', +'the presentations layer, views are also known as templates': 'la capa de presentació, les vistes també són anomenades plantilles', +'The Views': 'Les Vistes', +'There are no controllers': 'No hi ha controladors', +'There are no models': 'No hi ha models', +'There are no modules': 'No hi ha mòduls', +'There are no static files': 'No hi ha fitxers estàtics', +'There are no translators, only default language is supported': 'No hi ha traductors, només el llenguatge per defecte és suportat', +'There are no views': 'No hi ha vistes', +'these files are served without processing, your images go here': 'aquests fitxers són servits sense processar, les seves imatges van aquí', +'This App': 'Aquesta Aplicació', +'This email already has an account': 'Aquest correu electrònic ja té un compte', +'This is a copy of the scaffolding application': 'Aquesta és una còpia de la aplicació de bastiment', +'This is the %(filename)s template': 'Aquesta és la plantilla %(filename)s', +'Ticket': 'Tiquet', +'Time in Cache (h:m:s)': 'Temps en Caché (h:m:s)', +'Timestamp': 'Marca de temps', +'Title': 'Títol', +'to previous version.': 'a la versió prèvia.', +'To emulate a breakpoint programatically, write:': 'Emular un punto de ruptura programàticament, escribir:', +'to use the debugger!': 'usar el depurador!', +'toggle breakpoint': 'alternar punt de ruptura', +'Toggle comment': 'Alternar comentari', +'Toggle Fullscreen': 'Alternar pantalla completa', +'too short': 'massa curt', +'Traceback': 'Traceback', +'translation strings for the application': 'cadenes de caracters de traducció per a la aplicació', +'try': 'intenti', +'try something like': 'intenti algo com', +'TSV (Excel compatible)': 'TSV (compatible Excel)', +'TSV (Excel compatible, hidden cols)': 'TSV (compatible Excel, columnes ocultes)', +'TSV (Spreadsheets)': 'TSV (Fulls de càlcul)', +'TSV (Spreadsheets, hidden cols)': 'TSV (Fulls de càlcul, columnes amagades)', +'Twitter': 'Twitter', +'Unable to check for upgrades': 'No és possible verificar la existencia de actualitzacions', +'unable to create application "%s"': 'no és possible crear la aplicació "%s"', +'unable to delete file "%(filename)s"': 'no és possible eliminar el fitxer "%(filename)s"', +'Unable to download': 'No és possible la descàrrega', +'Unable to download app': 'No és possible descarregar la aplicació', +'unable to parse csv file': 'no és possible analitzar el fitxer CSV', +'unable to uninstall "%s"': 'no és possible instalar "%s"', +'uncheck all': 'desmarcar tots', +'uninstall': 'desinstalar', +'unknown': 'desconocido', +'update': 'actualitzar', +'update all languages': 'actualitzar tots els llenguatges', +'Update:': 'Actualizi:', +'upload application:': 'pujar aplicació:', +'Upload existing application': 'Puji aquesta aplicació', +'upload file:': 'puji fitxer:', +'Use (...)&(...) for AND, (...)|(...) for OR, and ~(...) for NOT to build more complex queries.': 'Use (...)&(...) para AND, (...)|(...) para OR, i ~(...) para NOT, para crear consultas més complexes.', +'User': 'Usuari', +'User %(id)s is impersonating %(other_id)s': 'El usuari %(id)s està suplantant %(other_id)s', +'User %(id)s Logged-in': 'El usuari %(id)s inicià la sessió', +'User %(id)s Logged-out': 'El usuari %(id)s finalitzà la sessió', +'User %(id)s Password changed': 'Contrasenya del usuari %(id)s canviada', +'User %(id)s Password reset': 'Contrasenya del usuari %(id)s reiniciada', +'User %(id)s Profile updated': 'Actualitzat el perfil del usuari %(id)s', +'User %(id)s Registered': 'Usuari %(id)s Registrat', +'User %(id)s Username retrieved': 'Se ha recuperat el nom de usuari del usuari %(id)s', +'User %(username)s Logged-in': 'El usuari %(username)s inicià la sessió', +"User '%(username)s' Logged-in": "El usuari '%(username)s' inicià la sessió", +"User '%(username)s' Logged-out": "El usuari '%(username)s' finalitzà la sessió", +'User Id': 'Id de Usuari', +'User ID': 'ID de Usuari', +'User Logged-out': 'El usuari finalitzà la sessió', +'Username': 'Nom de usuari', +'Username retrieve': 'Recuperar nom de usuari', +'Users': 'Usuaris', +'Value already in database or empty': 'El valor ya existeix en la base de dades o està buit', +'value already in database or empty': 'el valor ya existeix en la base de dades o està buit', +'value not allowed': 'valor no permès', +'Value not in database': 'El valor no està a la base de dades', +'value not in database': 'el valor no està a la base de dades', +'Verify Password': 'Verificar Contrasenya', +'Version': 'Versió', +'versioning': 'versions', +'Videos': 'Videos', +'View': 'Vista', +'view': 'vista', +'View %(entity)s': 'Veure %(entity)s', +'View Page': 'View Page', +'Views': 'Vistes', +'views': 'vistes', +'web2py is up to date': 'web2py està actualitzat', +'web2py Recent Tweets': 'Tweets Recents de web2py', +'Welcome': 'Benvingut', +'Welcome %s': 'Benvingut %s', +'Welcome to web2py': 'Benvingut a web2py', +'Welcome to web2py!': '¡Benvingut a web2py!', +'Which called the function %s located in the file %s': 'La qual va cridar la funció %s localitzada en el fitxer %s', +'Wiki Page': 'Wiki Page', +'Working...': 'Treballant ...', +'XML': 'XML', +'XML export of columns shown': 'XML export of columns shown', +'YES': 'SÍ', +'You are successfully running web2py': 'Vostè està executant web2py amb èxit', +'You can modify this application and adapt it to your needs': 'Vostè pot modificar aquesta aplicació i adaptar-la a les seves necessitats', +'You visited the url %s': 'Vostè va visitar la url %s', +'Your username is: %(username)s': 'El seu nom de usuari és: %(username)s', } From 3d4de72b9cebf79f2484ba0c8855ca314f07a80b Mon Sep 17 00:00:00 2001 From: enricapbes Date: Tue, 6 Jan 2015 21:30:25 +0100 Subject: [PATCH 28/63] Update ca.py --- applications/welcome/languages/ca.py | 1 - 1 file changed, 1 deletion(-) diff --git a/applications/welcome/languages/ca.py b/applications/welcome/languages/ca.py index 6f830149..e0f3109e 100644 --- a/applications/welcome/languages/ca.py +++ b/applications/welcome/languages/ca.py @@ -1,4 +1,3 @@ - # -*- coding: utf-8 -*- { '!langcode!': 'ca', From 15c3ac1cb90515e320c32bbc681b30345f065acf Mon Sep 17 00:00:00 2001 From: mdipierro Date: Tue, 6 Jan 2015 22:59:05 -0600 Subject: [PATCH 29/63] fixed Makefile for new dal --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3d4cb666..14ff4d92 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ src: ### build web2py_src.zip echo '' > NEWINSTALL mv web2py_src.zip web2py_src_old.zip | echo 'no old' - cd ..; zip -r web2py/web2py_src.zip web2py/web2py.py web2py/anyserver.py web2py/gluon/*.py web2py/gluon/contrib/* web2py/extras/* web2py/handlers/* web2py/examples/* web2py/README.markdown web2py/LICENSE web2py/CHANGELOG web2py/NEWINSTALL web2py/VERSION web2py/MANIFEST.in web2py/scripts/*.sh web2py/scripts/*.py web2py/applications/admin web2py/applications/examples/ web2py/applications/welcome web2py/applications/__init__.py web2py/site-packages/__init__.py web2py/gluon/tests/*.sh web2py/gluon/tests/*.py + cd ..; zip -r web2py/web2py_src.zip web2py/web2py.py web2py/anyserver.py web2py/gluon/*.py web2py/gluon/dal/* web2py/gluon/contrib/* web2py/extras/* web2py/handlers/* web2py/examples/* web2py/README.markdown web2py/LICENSE web2py/CHANGELOG web2py/NEWINSTALL web2py/VERSION web2py/MANIFEST.in web2py/scripts/*.sh web2py/scripts/*.py web2py/applications/admin web2py/applications/examples/ web2py/applications/welcome web2py/applications/__init__.py web2py/site-packages/__init__.py web2py/gluon/tests/*.sh web2py/gluon/tests/*.py mdp: make src From f76a780d505549d30ba2226ccbd1ad4eca9a2786 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Wed, 7 Jan 2015 10:39:17 -0600 Subject: [PATCH 30/63] fixed import of reserved_sql_keywords --- gluon/dal/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/dal/base.py b/gluon/dal/base.py index 4c9eeb3d..743bee83 100644 --- a/gluon/dal/base.py +++ b/gluon/dal/base.py @@ -440,7 +440,7 @@ class DAL(object): self._uri_hash = table_hash or hashlib_md5(adapter.uri).hexdigest() self.check_reserved = check_reserved if self.check_reserved: - from reserved_sql_keywords import ADAPTERS as RSK + from gluon.reserved_sql_keywords import ADAPTERS as RSK self.RSK = RSK self._migrate = migrate self._fake_migrate = fake_migrate From daf382c4fbc50995acaa7c3cdc34cedb9af48af0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20C=C3=A2mara?= Date: Wed, 7 Jan 2015 21:36:15 +0000 Subject: [PATCH 31/63] fixes issue 2025 - Encode filenames using base32 if you're running on windows to avoid invalid characters. --- gluon/cache.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/gluon/cache.py b/gluon/cache.py index bac5a37f..3a03a19c 100644 --- a/gluon/cache.py +++ b/gluon/cache.py @@ -246,11 +246,11 @@ class CacheOnDisk(CacheAbstract): """ Disk based cache - This is implemented as a shelve object and it is shared by multiple web2py - processes (and threads) as long as they share the same filesystem. + This is implemented as a key value store where each key corresponds to a + single file in disk which is replaced when the value changes. - Disk cache provides persistance when web2py is started/stopped but it slower - than `CacheInRam` + Disk cache provides persistance when web2py is started/stopped but it is + slower than `CacheInRam` Values stored in disk cache must be pickable. """ @@ -259,8 +259,11 @@ class CacheOnDisk(CacheAbstract): """ Implements a key based storage in disk. """ + def __init__(self, folder): self.folder = folder + self.key_filter_in = lambda key: key + self.key_filter_out = lambda key: key # Check the best way to do atomic file replacement. if sys.version_info >= (3, 3): self.replace = os.replace @@ -287,6 +290,25 @@ class CacheOnDisk(CacheAbstract): # POSIX rename() is always atomic self.replace = os.rename + # Make sure we use valid filenames. + if sys.platform == "win32": + import base64 + def key_filter_in_windows(key): + """ + Windows doesn't allow \ / : * ? "< > | in filenames. + To go around this encode the keys with base32. + """ + return base64.b32encode(key) + + def key_filter_out_windows(key): + """ + We need to decode the keys so regex based removal works. + """ + return base64.b32decode(key) + + self.key_filter_in = key_filter_in_windows + self.key_filter_out = key_filter_out_windows + def __setitem__(self, key, value): tmp_name, tmp_path = tempfile.mkstemp(dir=self.folder) @@ -295,6 +317,7 @@ class CacheOnDisk(CacheAbstract): pickle.dump((time.time(), value), tmp, pickle.HIGHEST_PROTOCOL) finally: tmp.close() + key = self.key_filter_in(key) fullfilename = os.path.join(self.folder, recfile.generate(key)) if not os.path.exists(os.path.dirname(fullfilename)): os.makedirs(os.path.dirname(fullfilename)) @@ -302,27 +325,33 @@ class CacheOnDisk(CacheAbstract): def __getitem__(self, key): + key = self.key_filter_in(key) if recfile.exists(key, path=self.folder): timestamp, value = pickle.load(recfile.open(key, 'rb', path=self.folder)) return value else: raise KeyError + def __contains__(self, key): + key = self.key_filter_in(key) return recfile.exists(key, path=self.folder) def __delitem__(self, key): + key = self.key_filter_in(key) recfile.remove(key, path=self.folder) def __iter__(self): for dirpath, dirnames, filenames in os.walk(self.folder): for filename in filenames: - yield filename + yield self.key_filter_out(filename) + def keys(self): - return [filename for dirpath, dirnames, filenames in os.walk(self.folder) for filename in filenames] + return list(self.__iter__()) + def get(self, key, default=None): try: @@ -335,6 +364,7 @@ class CacheOnDisk(CacheAbstract): for key in self: del self[key] + def __init__(self, request=None, folder=None): self.initialized = False self.request = request @@ -389,6 +419,7 @@ class CacheOnDisk(CacheAbstract): return value + def clear(self, regex=None): self.initialize() storage = self.storage From 6be1f624b9c576d852ed0fb3f254baf3135c83de Mon Sep 17 00:00:00 2001 From: Anssi Hannula Date: Sat, 10 Jan 2015 03:44:51 +0200 Subject: [PATCH 32/63] GoogleSQLAdapter: Fix NDB orderby without limitby broken in 2.9.6 Commit 8d648f6137cea5 ("restore beahvior in gae that if no limitby, returns iterator") made select_raw() store the query iterator in a wrong variable ("rows", which is unused, should be "items"), causing unsorted results to be returned. Fix the assignment. --- gluon/dal/adapters/google.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/dal/adapters/google.py b/gluon/dal/adapters/google.py index 86c3ef3c..00e75cc1 100644 --- a/gluon/dal/adapters/google.py +++ b/gluon/dal/adapters/google.py @@ -507,7 +507,7 @@ class GoogleDatastoreAdapter(NoSQLAdapter): db['_lastcursor'] = cursor else: # if a limit is not specified, always return an iterator - rows = query + items = query return (items, tablename, projection or db[tablename].fields) From c5c5b5708ec1d6af269e6c41e0203fa84fab852d Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sat, 10 Jan 2015 18:11:00 +0100 Subject: [PATCH 33/63] fix issue 2023: common_filter issue in _enable_record_versioning --- gluon/dal/objects.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gluon/dal/objects.py b/gluon/dal/objects.py index 44730709..9a44329e 100644 --- a/gluon/dal/objects.py +++ b/gluon/dal/objects.py @@ -428,8 +428,9 @@ class Table(object): if tn == name or getattr(db[tn],'_ot',None)==name]) query = self._common_filter if query: - newquery = query & newquery - self._common_filter = newquery + self._common_filter = lambda q: reduce(AND, [query(q), newquery(q)]) + else: + self._common_filter = newquery def _validate(self, **vars): errors = Row() From 1c281cc163194ded601dba63fbc721cfac976094 Mon Sep 17 00:00:00 2001 From: ilvalle Date: Sun, 11 Jan 2015 21:44:20 +0100 Subject: [PATCH 34/63] Initial tests for auth --- gluon/tests/__init__.py | 7 ++-- gluon/tests/test_tools.py | 83 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 gluon/tests/test_tools.py diff --git a/gluon/tests/__init__.py b/gluon/tests/__init__.py index 9d66896e..6f809695 100644 --- a/gluon/tests/__init__.py +++ b/gluon/tests/__init__.py @@ -1,5 +1,5 @@ -import os, sys - +import os +import sys from test_http import * from test_cache import * from test_contenttype import * @@ -16,6 +16,7 @@ from test_validators import * from test_utils import * from test_contribs import * from test_web import * +from test_tools import * if sys.version[:3] == '2.7': @@ -28,4 +29,4 @@ NOSQL = any([name in (os.getenv("DB") or "") if NOSQL: from test_dal_nosql import * else: - from test_dal import * \ No newline at end of file + from test_dal import * diff --git a/gluon/tests/test_tools.py b/gluon/tests/test_tools.py new file mode 100644 index 00000000..de148db9 --- /dev/null +++ b/gluon/tests/test_tools.py @@ -0,0 +1,83 @@ +#!/bin/python +# -*- coding: utf-8 -*- + +""" + Unit tests for gluon.tools +""" +import os +import sys +if sys.version < "2.7": + import unittest2 as unittest +else: + import unittest + +from fix_path import fix_sys_path + +fix_sys_path(__file__) + +DEFAULT_URI = os.getenv('DB', 'sqlite:memory') + +from gluon.dal import DAL, Field +from dal.objects import Table +from tools import Auth +from gluon.globals import Request, Response, Session +from storage import Storage +from languages import translator +from gluon.http import HTTP + +python_version = sys.version[:3] +IS_IMAP = "imap" in DEFAULT_URI + +@unittest.skipIf(IS_IMAP, "TODO: Imap raises 'Connection refused'") +class testAuth(unittest.TestCase): + + def testRun(self): + # setup + request = Request(env={}) + request.application = 'a' + request.controller = 'c' + request.function = 'f' + request.folder = 'applications/admin' + response = Response() + session = Session() + T = translator('', 'en') + session.connect(request, response) + from gluon.globals import current + current.request = request + current.response = response + current.session = session + current.T = T + db = DAL(DEFAULT_URI, check_reserved=['all']) + auth = Auth(db) + auth.define_tables(username=True, signature=False) + self.assertTrue('auth_user' in db) + self.assertTrue('auth_group' in db) + self.assertTrue('auth_membership' in db) + self.assertTrue('auth_permission' in db) + self.assertTrue('auth_event' in db) + db.define_table('t0', Field('tt'), auth.signature) + auth.enable_record_versioning(db) + self.assertTrue('t0_archive' in db) + for f in ['login', 'register', 'retrieve_password', + 'retrieve_username']: + html_form = getattr(auth, f)().xml() + self.assertTrue('name="_formkey"' in html_form) + + for f in ['logout', 'verify_email', 'reset_password', + 'change_password', 'profile', 'groups']: + self.assertRaisesRegexp(HTTP, "303*", getattr(auth, f)) + + self.assertRaisesRegexp(HTTP, "401*", auth.impersonate) + + try: + for t in ['t0_archive', 't0', 'auth_cas', 'auth_event', + 'auth_membership', 'auth_permission', 'auth_group', + 'auth_user']: + db[t].drop() + except SyntaxError as e: + # GAE doesn't support drop + pass + return + +if __name__ == '__main__': + unittest.main() From 2af5e02c5f760f0fa06587bacb5605973975bbb0 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 12 Jan 2015 20:06:05 -0600 Subject: [PATCH 35/63] fixed issue #2032, thanks Paolo --- gluon/dal/objects.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/gluon/dal/objects.py b/gluon/dal/objects.py index 44730709..2eff6ab8 100644 --- a/gluon/dal/objects.py +++ b/gluon/dal/objects.py @@ -427,9 +427,12 @@ class Table(object): for tn in db._adapter.tables(query) if tn == name or getattr(db[tn],'_ot',None)==name]) query = self._common_filter - if query: + if query: + self._common_filter = \ + lambda q: reduce(AND, [query(q), newquery(q)]) newquery = query & newquery - self._common_filter = newquery + else: + self._common_filter = newquery def _validate(self, **vars): errors = Row() From b872cced3343089c081d6ee426a597bd066b8c3d Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 12 Jan 2015 20:39:14 -0600 Subject: [PATCH 36/63] fixed issue 2001, thanks Anthony --- VERSION | 2 +- gluon/tools.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index ce54ce20..70772022 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.10.0-beta+timestamp.2014.10.16.15.58.50 +Version 2.9.12-beta+timestamp.2015.01.12.20.38.57 diff --git a/gluon/tools.py b/gluon/tools.py index 0b1dbd10..8db8d0ae 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -1258,7 +1258,8 @@ class Auth(object): def __init__(self, environment=None, db=None, mailer=True, hmac_key=None, controller='default', function='user', cas_provider=None, signature=True, secure=False, - csrf_prevention=True, propagate_extension=None): + csrf_prevention=True, propagate_extension=None, + url_index=None): ## next two lines for backward compatibility if not db and environment and isinstance(environment, DAL): @@ -1295,7 +1296,7 @@ class Auth(object): del session.auth # ## what happens after login? - url_index = URL(controller, 'index') + url_index = url_index or URL(controller, 'index') url_login = URL(controller, function, args='login', extension = propagate_extension) # ## what happens after registration? From 15bf3e2ededab97d3183fd315d3e4c5136c97470 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 12 Jan 2015 20:53:28 -0600 Subject: [PATCH 37/63] fixed issue 1991, thanks Mark --- VERSION | 2 +- gluon/html.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 70772022..1ff6dcfe 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.9.12-beta+timestamp.2015.01.12.20.38.57 +Version 2.9.12-beta+timestamp.2015.01.12.20.53.19 diff --git a/gluon/html.py b/gluon/html.py index ee491479..7b88a87f 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -850,7 +850,7 @@ class DIV(XmlComponent): """ components = [] for c in self.components: - if isinstance(c, allowed_parents): + if isinstance(c, (allowed_parents,CAT)): pass elif wrap_lambda: c = wrap_lambda(c) From 57c5fb64f6d056d67302a3377d814f14f2a29104 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 12 Jan 2015 21:00:49 -0600 Subject: [PATCH 38/63] fixed issue 1978, thanks mbelletti --- VERSION | 2 +- gluon/sqlhtml.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 1ff6dcfe..8cc74977 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.9.12-beta+timestamp.2015.01.12.20.53.19 +Version 2.9.12-beta+timestamp.2015.01.12.21.00.43 diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 2058f291..28c37615 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2348,7 +2348,9 @@ class SQLFORM(FORM): # expcolumns is all cols to be exported including virtual fields rows.colnames = expcolumns oExp = clazz(rows) - filename = '.'.join(('rows', oExp.file_ext)) + export_filename = \ + request.vars.get('_export_filename') or 'rows' + filename = '.'.join((export_filename, oExp.file_ext)) response.headers['Content-Type'] = oExp.content_type response.headers['Content-Disposition'] = \ 'attachment;filename=' + filename + ';' From 1c2358671db99839be4e7370ab87fffda960c6e6 Mon Sep 17 00:00:00 2001 From: Andrew Willimott Date: Wed, 14 Jan 2015 14:28:43 +1300 Subject: [PATCH 39/63] Initial simple change for adding geospatial support to Teradata adaptor. --- gluon/dal/adapters/teradata.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gluon/dal/adapters/teradata.py b/gluon/dal/adapters/teradata.py index f8b8f9e7..817af60b 100644 --- a/gluon/dal/adapters/teradata.py +++ b/gluon/dal/adapters/teradata.py @@ -31,6 +31,7 @@ class TeradataAdapter(BaseAdapter): 'list:integer': 'VARCHAR(4000)', 'list:string': 'VARCHAR(4000)', 'list:reference': 'VARCHAR(4000)', + 'geometry': 'ST_GEOMETRY', # http://www.info.teradata.com/HTMLPubs/DB_TTU_14_00/index.html#page/Database_Management/B035_1094_111A/ch14.055.160.html 'big-id': 'BIGINT GENERATED ALWAYS AS IDENTITY', # Teradata Specific 'big-reference': 'BIGINT', 'reference FK': ' REFERENCES %(foreign_key)s', From aaf1dd614acde834d5afa1f6d8651960a758ec45 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 14 Jan 2015 14:16:42 +0200 Subject: [PATCH 40/63] fix issuer comparison the issuer looks like gmail.login.persona.org and the expected value was login.persona.org --- gluon/contrib/login_methods/browserid_account.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/contrib/login_methods/browserid_account.py b/gluon/contrib/login_methods/browserid_account.py index 8214276f..5698f746 100644 --- a/gluon/contrib/login_methods/browserid_account.py +++ b/gluon/contrib/login_methods/browserid_account.py @@ -73,7 +73,7 @@ class BrowserID(object): auth_info_json = fetch(self.verify_url, data=verify_data) j = json.loads(auth_info_json) epoch_time = int(time.time() * 1000) # we need 13 digit epoch time - if j["status"] == "okay" and j["audience"] == audience and j['issuer'] == issuer and j['expires'] >= epoch_time: + if j["status"] == "okay" and j["audience"] == audience and j['issuer'].endswith(issuer) and j['expires'] >= epoch_time: return dict(email=j['email']) elif self.on_login_failure: #print "status: ", j["status"]=="okay", j["status"] From bda69b0e88362ca39a8104bdd81d2f73044af368 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Thu, 15 Jan 2015 09:47:57 -0600 Subject: [PATCH 41/63] mail timeout --- VERSION | 2 +- gluon/tools.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 8cc74977..dcf9788c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.9.12-beta+timestamp.2015.01.12.21.00.43 +Version 2.9.12-beta+timestamp.2015.01.15.09.47.52 diff --git a/gluon/tools.py b/gluon/tools.py index 8db8d0ae..d718f1f4 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -136,6 +136,7 @@ class Mail(object): mail.settings.server mail.settings.sender mail.settings.login + mail.settings.timeout = 60 # seconds (default) When server is 'logging', email is logged but not sent (debug mode) @@ -265,6 +266,7 @@ class Mail(object): settings.sender = sender settings.login = login settings.tls = tls + settings.timeout = 60 # seconds settings.hostname = None settings.ssl = False settings.cipher_type = None @@ -787,10 +789,11 @@ class Mail(object): subject=subject, body=text, **xcc) else: smtp_args = self.settings.server.split(':') + kwargs = dict(timeout = self.settings.timeout) if self.settings.ssl: - server = smtplib.SMTP_SSL(*smtp_args) + server = smtplib.SMTP_SSL(*smtp_args, **kwargs) else: - server = smtplib.SMTP(*smtp_args) + server = smtplib.SMTP(*smtp_args, **kwargs) if self.settings.tls and not self.settings.ssl: server.ehlo(self.settings.hostname) server.starttls() From c6cc06f6c033ae8a8d24bbe6a677d26414ba96e3 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Thu, 15 Jan 2015 09:58:13 -0600 Subject: [PATCH 42/63] fixed grid export with custom search, thanks Prasad Muley --- VERSION | 2 +- gluon/sqlhtml.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index dcf9788c..3374f8b2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.9.12-beta+timestamp.2015.01.15.09.47.52 +Version 2.9.12-beta+timestamp.2015.01.15.09.58.09 diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 28c37615..6e32aa22 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2326,7 +2326,7 @@ class SQLFORM(FORM): expcolumns.append(str(field)) if export_type in exportManager and exportManager[export_type]: - if keywords: + if keywords and not callable(searchable): try: #the query should be constructed using searchable #fields but not virtual fields @@ -2339,6 +2339,19 @@ class SQLFORM(FORM): except Exception, e: response.flash = T('Internal Error') rows = [] + elif callable(searchable): + #use custom_query using searchable + try: + #the query should be constructed using searchable + #fields but not virtual fields + sfields = reduce(lambda a, b: a + b, + [[f for f in t if f.readable and not isinstance(f, Field.Virtual)] for t in tables]) + dbset = dbset(searchable(sfields, keywords)) + rows = dbset.select(left=left, orderby=orderby, + cacheable=True, *selectable_columns) + except Exception, e: + response.flash = T('Internal Error') + rows = [] else: rows = dbset.select(left=left, orderby=orderby, cacheable=True, *selectable_columns) From 5958704509ca8cd37def98822a46ecd741b99068 Mon Sep 17 00:00:00 2001 From: Prasad Muley Date: Thu, 15 Jan 2015 15:37:09 +0530 Subject: [PATCH 43/63] Fix: Exporting from a SQLFORM.grid with customized search queries issues #2006 --- gluon/sqlhtml.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 28c37615..df9e8f0f 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2332,8 +2332,12 @@ class SQLFORM(FORM): #fields but not virtual fields sfields = reduce(lambda a, b: a + b, [[f for f in t if f.readable and not isinstance(f, Field.Virtual)] for t in tables]) - dbset = dbset(SQLFORM.build_query( - sfields, keywords)) + #use custom_query using searchable + if callable(searchable): + dbset = dbset(searchable(sfields, keywords)) + else: + dbset = dbset(SQLFORM.build_query( + sfields, keywords)) rows = dbset.select(left=left, orderby=orderby, cacheable=True, *selectable_columns) except Exception, e: From 35840bc572c58724756e58ab3ad48dd8a10ceae0 Mon Sep 17 00:00:00 2001 From: Prasad Muley Date: Fri, 16 Jan 2015 21:01:33 +0530 Subject: [PATCH 44/63] Fix: Exporting from a SQLFORM.grid with customized search queries Massimo manually applied older diff file. He also merged my newer diff file. So Optimized code --- gluon/sqlhtml.py | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index e3171010..df9e8f0f 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -2326,7 +2326,7 @@ class SQLFORM(FORM): expcolumns.append(str(field)) if export_type in exportManager and exportManager[export_type]: - if keywords and not callable(searchable): + if keywords: try: #the query should be constructed using searchable #fields but not virtual fields @@ -2343,19 +2343,6 @@ class SQLFORM(FORM): except Exception, e: response.flash = T('Internal Error') rows = [] - elif callable(searchable): - #use custom_query using searchable - try: - #the query should be constructed using searchable - #fields but not virtual fields - sfields = reduce(lambda a, b: a + b, - [[f for f in t if f.readable and not isinstance(f, Field.Virtual)] for t in tables]) - dbset = dbset(searchable(sfields, keywords)) - rows = dbset.select(left=left, orderby=orderby, - cacheable=True, *selectable_columns) - except Exception, e: - response.flash = T('Internal Error') - rows = [] else: rows = dbset.select(left=left, orderby=orderby, cacheable=True, *selectable_columns) From 5bc5d0496ea83f9e8dd9822151872bd039ddd17e Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sat, 17 Jan 2015 00:07:10 -0600 Subject: [PATCH 45/63] R-2.9.12 --- CHANGELOG | 18 + Makefile | 2 +- VERSION | 2 +- applications/admin/controllers/default.py | 51 +- applications/admin/languages/cs.py | 960 ++++++------ applications/admin/models/0.py | 2 - applications/admin/models/menu.py | 1 - .../admin/models/plugin_statebutton.py | 5 +- extras/build_web2py/setup_app.py | 4 +- extras/build_web2py/setup_exe.py | 64 +- gluon/__init__.py | 2 +- gluon/cache.py | 1386 ++++++++--------- gluon/compileapp.py | 4 +- gluon/contrib/hypermedia.py | 21 +- gluon/contrib/memdb.py | 12 +- gluon/contrib/mockimaplib.py | 7 +- gluon/contrib/pbkdf2_ctypes.py | 8 +- gluon/contrib/pypyodbc.py | 816 +++++----- gluon/contrib/stripe.py | 44 +- gluon/contrib/webclient.py | 1 - gluon/contrib/websocket_messaging.py | 4 +- gluon/custom_import.py | 2 +- gluon/dal/_load.py | 4 +- gluon/dal/objects.py | 7 +- gluon/fileutils.py | 8 +- gluon/html.py | 2 +- gluon/main.py | 4 +- gluon/recfile.py | 126 +- gluon/shell.py | 2 +- gluon/template.py | 2 +- gluon/tests/fix_path.py | 2 +- gluon/tests/test_contribs.py | 2 +- gluon/tests/test_dal_nosql.py | 3 +- gluon/tests/test_router.py | 44 +- gluon/tests/test_routes.py | 4 +- gluon/tools.py | 63 +- gluon/utils.py | 1 - handlers/web2py_on_gevent.py | 17 +- scripts/cpdb.py | 20 +- scripts/extract_oracle_models.py | 2 +- scripts/import_static.py | 10 +- scripts/service/linux.py | 65 +- scripts/service/service.py | 56 +- scripts/sessions2trash.py | 4 +- 44 files changed, 1934 insertions(+), 1930 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9997466d..f6329e6b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,21 @@ +## 2.9.12 + +- Tornado HTTPS support, thanks Diego +- Modular DAL, thanks Giovanni +- Added coverage support, thanks Niphlod +- More tests, thanks Niphlod and Paolo Valleri +- Added support for show_if in readonly sqlform, thanks Paolo +- Improved scheduler, thanks Niphlod +- Email timeout support +- Made web2py's custom_import work with circular imports, thanks Jack Kuan +- Added Portuguese, Catalan, and Burmese translations +- Allow map_hyphen to work for application names, thanks Tim Nyborg +- New module appconfig.py, thanks Niphlod +- Added geospatial support to Teradata adaptor, thanks Andrew Willimott +- Many bug fixes + + + ## 2.9.6 - 2.9.10 - fixed support of GAE + SQL diff --git a/Makefile b/Makefile index 14ff4d92..42f7375b 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ update: echo "remember that pymysql was tweaked" src: ### Use semantic versioning - echo 'Version 2.9.12-beta+timestamp.'`date +%Y.%m.%d.%H.%M.%S` > VERSION + echo 'Version 2.9.12-stable+timestamp.'`date +%Y.%m.%d.%H.%M.%S` > VERSION ### rm -f all junk files make clean ### clean up baisc apps diff --git a/VERSION b/VERSION index 3374f8b2..3af04ccf 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.9.12-beta+timestamp.2015.01.15.09.58.09 +Version 2.9.12-stable+timestamp.2015.01.17.00.07.04 diff --git a/applications/admin/controllers/default.py b/applications/admin/controllers/default.py index 547d6c94..4623c517 100644 --- a/applications/admin/controllers/default.py +++ b/applications/admin/controllers/default.py @@ -589,7 +589,7 @@ def edit(): if 'settings' in request.vars: if request.post_vars: #save new preferences post_vars = request.post_vars.items() - # Since unchecked checkbox are not serialized, we must set them as false by hand to store the correct preference in the settings + # Since unchecked checkbox are not serialized, we must set them as false by hand to store the correct preference in the settings post_vars+= [(opt, 'false') for opt in preferences if opt not in request.post_vars ] if config.save(post_vars): response.headers["web2py-component-flash"] = T('Preferences saved correctly') @@ -775,12 +775,12 @@ def edit(): view_link=view_link, editviewlinks=editviewlinks, id=IS_SLUG()(filename)[0], - force= True if (request.vars.restore or + force= True if (request.vars.restore or request.vars.revert) else False) plain_html = response.render('default/edit_js.html', file_details) file_details['plain_html'] = plain_html if is_mobile: - return response.render('default.mobile/edit.html', + return response.render('default.mobile/edit.html', file_details, editor_settings=preferences) else: return response.json(file_details) @@ -1278,7 +1278,7 @@ def create_file(): path = abspath(request.vars.location) else: if request.vars.dir: - request.vars.location += request.vars.dir + '/' + request.vars.location += request.vars.dir + '/' app = get_app(name=request.vars.location.split('/')[0]) path = apath(request.vars.location, r=request) filename = re.sub('[^\w./-]+', '_', request.vars.filename) @@ -1387,7 +1387,7 @@ def create_file(): from gluon import *\n""")[1:] elif (path[-8:] == '/static/') or (path[-9:] == '/private/'): - if (request.vars.plugin and + if (request.vars.plugin and not filename.startswith('plugin_%s/' % request.vars.plugin)): filename = 'plugin_%s/%s' % (request.vars.plugin, filename) text = '' @@ -1434,37 +1434,37 @@ def create_file(): """ % URL('edit', args=[app,request.vars.dir,filename]) return '' else: - redirect(request.vars.sender + anchor) + redirect(request.vars.sender + anchor) def listfiles(app, dir, regexp='.*\.py$'): - files = sorted( + files = sorted( listdir(apath('%(app)s/%(dir)s/' % {'app':app, 'dir':dir}, r=request), regexp)) - files = [x.replace('\\', '/') for x in files if not x.endswith('.bak')] - return files - + files = [x.replace('\\', '/') for x in files if not x.endswith('.bak')] + return files + def editfile(path,file,vars={}, app = None): - args=(path,file) if 'app' in vars else (app,path,file) - url = URL('edit', args=args, vars=vars) - return A(file, _class='editor_filelink', _href=url, _style='word-wrap: nowrap;') - + args=(path,file) if 'app' in vars else (app,path,file) + url = URL('edit', args=args, vars=vars) + return A(file, _class='editor_filelink', _href=url, _style='word-wrap: nowrap;') + def files_menu(): - app = request.vars.app or 'welcome' - dirs=[{'name':'models', 'reg':'.*\.py$'}, + app = request.vars.app or 'welcome' + dirs=[{'name':'models', 'reg':'.*\.py$'}, {'name':'controllers', 'reg':'.*\.py$'}, {'name':'views', 'reg':'[\w/\-]+(\.\w+)+$'}, {'name':'modules', 'reg':'.*\.py$'}, {'name':'static', 'reg': '[^\.#].*'}, {'name':'private', 'reg':'.*\.py$'}] - result_files = [] - for dir in dirs: - result_files.append(TAG[''](LI(dir['name'], _class="nav-header component", _onclick="collapse('" + dir['name'] + "_files');"), - LI(UL(*[LI(editfile(dir['name'], f, dict(id=dir['name'] + f.replace('.','__')), app), _style="overflow:hidden", _id=dir['name']+"__"+f.replace('.','__')) - for f in listfiles(app, dir['name'], regexp=dir['reg'])], - _class="nav nav-list small-font"), - _id=dir['name'] + '_files', _style="display: none;"))) - return dict(result_files = result_files) - + result_files = [] + for dir in dirs: + result_files.append(TAG[''](LI(dir['name'], _class="nav-header component", _onclick="collapse('" + dir['name'] + "_files');"), + LI(UL(*[LI(editfile(dir['name'], f, dict(id=dir['name'] + f.replace('.','__')), app), _style="overflow:hidden", _id=dir['name']+"__"+f.replace('.','__')) + for f in listfiles(app, dir['name'], regexp=dir['reg'])], + _class="nav nav-list small-font"), + _id=dir['name'] + '_files', _style="display: none;"))) + return dict(result_files = result_files) + def upload_file(): """ File uploading handler """ if request.vars and not request.vars.token == session.token: @@ -1941,4 +1941,3 @@ def install_plugin(): T('unable to install plugin "%s"', filename) redirect(URL(f="plugins", args=[app,])) return dict(form=form, app=app, plugin=plugin, source=source) - diff --git a/applications/admin/languages/cs.py b/applications/admin/languages/cs.py index 9c8c7b63..1b9481ef 100644 --- a/applications/admin/languages/cs.py +++ b/applications/admin/languages/cs.py @@ -1,480 +1,480 @@ -# -*- coding: utf-8 -*- -{ -'!langcode!': 'cs-cz', -'!langname!': 'čeština', -'"update" is an optional expression like "field1=\'newvalue\'". You cannot update or delete the results of a JOIN': 'Kolonka "Upravit" je nepovinný výraz, například "pole1=\'nováhodnota\'". Výsledky databázového JOINu nemůžete mazat ani upravovat.', -'"User Exception" debug mode. An error ticket could be issued!': '"User Exception" debug mode. An error ticket could be issued!', -'%%{Row} in Table': '%%{řádek} v tabulce', -'%%{Row} selected': 'označených %%{řádek}', -'%s %%{row} deleted': '%s smazaných %%{záznam}', -'%s %%{row} updated': '%s upravených %%{záznam}', -'%s selected': '%s označených', -'%Y-%m-%d': '%d.%m.%Y', -'%Y-%m-%d %H:%M:%S': '%d.%m.%Y %H:%M:%S', -'(requires internet access)': '(vyžaduje připojení k internetu)', -'(requires internet access, experimental)': '(requires internet access, experimental)', -'(something like "it-it")': '(například "cs-cs")', -'@markmin\x01(file **gluon/contrib/plural_rules/%s.py** is not found)': '(soubor **gluon/contrib/plural_rules/%s.py** nenalezen)', -'@markmin\x01Searching: **%s** %%{file}': 'Hledání: **%s** %%{soubor}', -'About': 'O programu', -'About application': 'O aplikaci', -'Access Control': 'Řízení přístupu', -'Add breakpoint': 'Přidat bod přerušení', -'Additional code for your application': 'Další kód pro Vaši aplikaci', -'Admin design page': 'Admin design page', -'Admin language': 'jazyk rozhraní', -'Administrative interface': 'pro administrátorské rozhraní klikněte sem', -'Administrative Interface': 'Administrátorské rozhraní', -'administrative interface': 'rozhraní pro správu', -'Administrator Password:': 'Administrátorské heslo:', -'Ajax Recipes': 'Recepty s ajaxem', -'An error occured, please %s the page': 'An error occured, please %s the page', -'and rename it:': 'a přejmenovat na:', -'appadmin': 'appadmin', -'appadmin is disabled because insecure channel': 'appadmin je zakázaná bez zabezpečeného spojení', -'Application': 'Application', -'application "%s" uninstalled': 'application "%s" odinstalována', -'application compiled': 'aplikace zkompilována', -'Application name:': 'Název aplikace:', -'are not used': 'nepoužita', -'are not used yet': 'ještě nepoužita', -'Are you sure you want to delete this object?': 'Opravdu chcete odstranit tento objekt?', -'Are you sure you want to uninstall application "%s"?': 'Opravdu chcete odinstalovat aplikaci "%s"?', -'arguments': 'arguments', -'at char %s': 'at char %s', -'at line %s': 'at line %s', -'ATTENTION:': 'ATTENTION:', -'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.': 'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.', -'Available Databases and Tables': 'Dostupné databáze a tabulky', -'back': 'zpět', -'Back to wizard': 'Back to wizard', -'Basics': 'Basics', -'Begin': 'Začít', -'breakpoint': 'bod přerušení', -'Breakpoints': 'Body přerušení', -'breakpoints': 'body přerušení', -'Buy this book': 'Koupit web2py knihu', -'Cache': 'Cache', -'cache': 'cache', -'Cache Keys': 'Klíče cache', -'cache, errors and sessions cleaned': 'cache, chyby a relace byly pročištěny', -'can be a git repo': 'může to být git repo', -'Cancel': 'Storno', -'Cannot be empty': 'Nemůže být prázdné', -'Change Admin Password': 'Změnit heslo pro správu', -'Change admin password': 'Změnit heslo pro správu aplikací', -'Change password': 'Změna hesla', -'check all': 'vše označit', -'Check for upgrades': 'Zkusit aktualizovat', -'Check to delete': 'Označit ke smazání', -'Check to delete:': 'Označit ke smazání:', -'Checking for upgrades...': 'Zjišťuji, zda jsou k dispozici aktualizace...', -'Clean': 'Pročistit', -'Clear CACHE?': 'Vymazat CACHE?', -'Clear DISK': 'Vymazat DISK', -'Clear RAM': 'Vymazat RAM', -'Click row to expand traceback': 'Pro rozbalení stopy, klikněte na řádek', -'Click row to view a ticket': 'Pro zobrazení chyby (ticketu), klikněte na řádku...', -'Client IP': 'IP adresa klienta', -'code': 'code', -'Code listing': 'Code listing', -'collapse/expand all': 'vše sbalit/rozbalit', -'Community': 'Komunita', -'Compile': 'Zkompilovat', -'compiled application removed': 'zkompilovaná aplikace smazána', -'Components and Plugins': 'Komponenty a zásuvné moduly', -'Condition': 'Podmínka', -'continue': 'continue', -'Controller': 'Kontrolér (Controller)', -'Controllers': 'Kontroléry', -'controllers': 'kontroléry', -'Copyright': 'Copyright', -'Count': 'Počet', -'Create': 'Vytvořit', -'create file with filename:': 'vytvořit soubor s názvem:', -'created by': 'vytvořil', -'Created By': 'Vytvořeno - kým', -'Created On': 'Vytvořeno - kdy', -'crontab': 'crontab', -'Current request': 'Aktuální požadavek', -'Current response': 'Aktuální odpověď', -'Current session': 'Aktuální relace', -'currently running': 'právě běží', -'currently saved or': 'uloženo nebo', -'customize me!': 'upravte mě!', -'data uploaded': 'data nahrána', -'Database': 'Rozhraní databáze', -'Database %s select': 'databáze %s výběr', -'Database administration': 'Database administration', -'database administration': 'správa databáze', -'Date and Time': 'Datum a čas', -'day': 'den', -'db': 'db', -'DB Model': 'Databázový model', -'Debug': 'Ladění', -'defines tables': 'defines tables', -'Delete': 'Smazat', -'delete': 'smazat', -'delete all checked': 'smazat vše označené', -'delete plugin': 'delete plugin', -'Delete this file (you will be asked to confirm deletion)': 'Smazat tento soubor (budete požádán o potvrzení mazání)', -'Delete:': 'Smazat:', -'deleted after first hit': 'smazat po prvním dosažení', -'Demo': 'Demo', -'Deploy': 'Nahrát', -'Deploy on Google App Engine': 'Nahrát na Google App Engine', -'Deploy to OpenShift': 'Nahrát na OpenShift', -'Deployment Recipes': 'Postupy pro deployment', -'Description': 'Popis', -'design': 'návrh', -'Detailed traceback description': 'Podrobný výpis prostředí', -'details': 'podrobnosti', -'direction: ltr': 'směr: ltr', -'Disable': 'Zablokovat', -'DISK': 'DISK', -'Disk Cache Keys': 'Klíče diskové cache', -'Disk Cleared': 'Disk smazán', -'docs': 'dokumentace', -'Documentation': 'Dokumentace', -"Don't know what to do?": 'Nevíte kudy kam?', -'done!': 'hotovo!', -'Download': 'Stáhnout', -'download layouts': 'stáhnout moduly rozvržení stránky', -'download plugins': 'stáhnout zásuvné moduly', -'E-mail': 'E-mail', -'Edit': 'Upravit', -'edit all': 'edit all', -'Edit application': 'Správa aplikace', -'edit controller': 'edit controller', -'Edit current record': 'Upravit aktuální záznam', -'Edit Profile': 'Upravit profil', -'edit views:': 'upravit pohled:', -'Editing file "%s"': 'Úprava souboru "%s"', -'Editing Language file': 'Úprava jazykového souboru', -'Editing Plural Forms File': 'Editing Plural Forms File', -'Email and SMS': 'Email a SMS', -'Enable': 'Odblokovat', -'enter a number between %(min)g and %(max)g': 'zadejte číslo mezi %(min)g a %(max)g', -'enter an integer between %(min)g and %(max)g': 'zadejte celé číslo mezi %(min)g a %(max)g', -'Error': 'Chyba', -'Error logs for "%(app)s"': 'Seznam výskytu chyb pro aplikaci "%(app)s"', -'Error snapshot': 'Snapshot chyby', -'Error ticket': 'Ticket chyby', -'Errors': 'Chyby', -'Exception %(extype)s: %(exvalue)s': 'Exception %(extype)s: %(exvalue)s', -'Exception %s': 'Exception %s', -'Exception instance attributes': 'Prvky instance výjimky', -'Expand Abbreviation': 'Expand Abbreviation', -'export as csv file': 'exportovat do .csv souboru', -'exposes': 'vystavuje', -'exposes:': 'vystavuje funkce:', -'extends': 'rozšiřuje', -'failed to compile file because:': 'soubor se nepodařilo zkompilovat, protože:', -'FAQ': 'Často kladené dotazy', -'File': 'Soubor', -'file': 'soubor', -'file "%(filename)s" created': 'file "%(filename)s" created', -'file saved on %(time)s': 'soubor uložen %(time)s', -'file saved on %s': 'soubor uložen %s', -'Filename': 'Název souboru', -'filter': 'filtr', -'Find Next': 'Najít další', -'Find Previous': 'Najít předchozí', -'First name': 'Křestní jméno', -'Forgot username?': 'Zapomněl jste svoje přihlašovací jméno?', -'forgot username?': 'zapomněl jste svoje přihlašovací jméno?', -'Forms and Validators': 'Formuláře a validátory', -'Frames': 'Frames', -'Free Applications': 'Aplikace zdarma', -'Functions with no doctests will result in [passed] tests.': 'Functions with no doctests will result in [passed] tests.', -'Generate': 'Vytvořit', -'Get from URL:': 'Stáhnout z internetu:', -'Git Pull': 'Git Pull', -'Git Push': 'Git Push', -'Globals##debug': 'Globální proměnné', -'go!': 'OK!', -'Goto': 'Goto', -'graph model': 'graph model', -'Group %(group_id)s created': 'Skupina %(group_id)s vytvořena', -'Group ID': 'ID skupiny', -'Groups': 'Skupiny', -'Hello World': 'Ahoj světe', -'Help': 'Nápověda', -'Hide/Show Translated strings': 'Skrýt/Zobrazit přeložené texty', -'Hits': 'Kolikrát dosaženo', -'Home': 'Domovská stránka', -'honored only if the expression evaluates to true': 'brát v potaz jen když se tato podmínka vyhodnotí kladně', -'How did you get here?': 'Jak jste se sem vlastně dostal?', -'If start the upgrade, be patient, it may take a while to download': 'If start the upgrade, be patient, it may take a while to download', -'If the report above contains a ticket number it indicates a failure in executing the controller, before any attempt to execute the doctests. This is usually due to an indentation error or an error outside function code.\nA green title indicates that all tests (if defined) passed. In this case test results are not shown.': 'If the report above contains a ticket number it indicates a failure in executing the controller, before any attempt to execute the doctests. This is usually due to an indentation error or an error outside function code.\nA green title indicates that all tests (if defined) passed. In this case test results are not shown.', -'import': 'import', -'Import/Export': 'Import/Export', -'includes': 'zahrnuje', -'Index': 'Index', -'insert new': 'vložit nový záznam ', -'insert new %s': 'vložit nový záznam %s', -'inspect attributes': 'inspect attributes', -'Install': 'Instalovat', -'Installed applications': 'Nainstalované aplikace', -'Interaction at %s line %s': 'Interakce v %s, na řádce %s', -'Interactive console': 'Interaktivní příkazová řádka', -'Internal State': 'Vnitřní stav', -'Introduction': 'Úvod', -'Invalid email': 'Neplatný email', -'Invalid password': 'Nesprávné heslo', -'invalid password.': 'neplatné heslo', -'Invalid Query': 'Neplatný dotaz', -'invalid request': 'Neplatný požadavek', -'Is Active': 'Je aktivní', -'It is %s %%{day} today.': 'Dnes je to %s %%{den}.', -'Key': 'Klíč', -'Key bindings': 'Vazby klíčů', -'Key bindings for ZenCoding Plugin': 'Key bindings for ZenCoding Plugin', -'languages': 'jazyky', -'Languages': 'Jazyky', -'Last name': 'Příjmení', -'Last saved on:': 'Naposledy uloženo:', -'Layout': 'Rozvržení stránky (layout)', -'Layout Plugins': 'Moduly rozvržení stránky (Layout Plugins)', -'Layouts': 'Rozvržení stránek', -'License for': 'Licence pro', -'Line number': 'Číslo řádku', -'LineNo': 'Č.řádku', -'Live Chat': 'Online pokec', -'loading...': 'nahrávám...', -'locals': 'locals', -'Locals##debug': 'Lokální proměnné', -'Logged in': 'Přihlášení proběhlo úspěšně', -'Logged out': 'Odhlášení proběhlo úspěšně', -'Login': 'Přihlásit se', -'login': 'přihlásit se', -'Login to the Administrative Interface': 'Přihlásit se do Správce aplikací', -'logout': 'odhlásit se', -'Logout': 'Odhlásit se', -'Lost Password': 'Zapomněl jste heslo', -'Lost password?': 'Zapomněl jste heslo?', -'lost password?': 'zapomněl jste heslo?', -'Manage': 'Manage', -'Manage Cache': 'Manage Cache', -'Menu Model': 'Model rozbalovací nabídky', -'Models': 'Modely', -'models': 'modely', -'Modified By': 'Změněno - kým', -'Modified On': 'Změněno - kdy', -'Modules': 'Moduly', -'modules': 'moduly', -'My Sites': 'Správa aplikací', -'Name': 'Jméno', -'new application "%s" created': 'nová aplikace "%s" vytvořena', -'New Application Wizard': 'Nový průvodce aplikací', -'New application wizard': 'Nový průvodce aplikací', -'New password': 'Nové heslo', -'New Record': 'Nový záznam', -'new record inserted': 'nový záznam byl založen', -'New simple application': 'Vytvořit primitivní aplikaci', -'next': 'next', -'next 100 rows': 'dalších 100 řádků', -'No databases in this application': 'V této aplikaci nejsou žádné databáze', -'No Interaction yet': 'Ještě žádná interakce nenastala', -'No ticket_storage.txt found under /private folder': 'Soubor ticket_storage.txt v adresáři /private nenalezen', -'Object or table name': 'Objekt či tabulka', -'Old password': 'Původní heslo', -'online designer': 'online návrhář', -'Online examples': 'Příklady online', -'Open new app in new window': 'Open new app in new window', -'or alternatively': 'or alternatively', -'Or Get from URL:': 'Or Get from URL:', -'or import from csv file': 'nebo importovat z .csv souboru', -'Origin': 'Původ', -'Original/Translation': 'Originál/Překlad', -'Other Plugins': 'Ostatní moduly', -'Other Recipes': 'Ostatní zásuvné moduly', -'Overview': 'Přehled', -'Overwrite installed app': 'Přepsat instalovanou aplikaci', -'Pack all': 'Zabalit', -'Pack compiled': 'Zabalit zkompilované', -'pack plugin': 'pack plugin', -'password': 'heslo', -'Password': 'Heslo', -"Password fields don't match": 'Hesla se neshodují', -'Peeking at file': 'Peeking at file', -'Please': 'Prosím', -'Plugin "%s" in application': 'Plugin "%s" in application', -'plugins': 'zásuvné moduly', -'Plugins': 'Zásuvné moduly', -'Plural Form #%s': 'Plural Form #%s', -'Plural-Forms:': 'Množná čísla:', -'Powered by': 'Poháněno', -'Preface': 'Předmluva', -'previous 100 rows': 'předchozích 100 řádků', -'Private files': 'Soukromé soubory', -'private files': 'soukromé soubory', -'profile': 'profil', -'Project Progress': 'Vývoj projektu', -'Python': 'Python', -'Query:': 'Dotaz:', -'Quick Examples': 'Krátké příklady', -'RAM': 'RAM', -'RAM Cache Keys': 'Klíče RAM Cache', -'Ram Cleared': 'RAM smazána', -'Readme': 'Nápověda', -'Recipes': 'Postupy jak na to', -'Record': 'Záznam', -'record does not exist': 'záznam neexistuje', -'Record ID': 'ID záznamu', -'Record id': 'id záznamu', -'refresh': 'obnovte', -'register': 'registrovat', -'Register': 'Zaregistrovat se', -'Registration identifier': 'Registrační identifikátor', -'Registration key': 'Registrační klíč', -'reload': 'reload', -'Reload routes': 'Znovu nahrát cesty', -'Remember me (for 30 days)': 'Zapamatovat na 30 dní', -'Remove compiled': 'Odstranit zkompilované', -'Removed Breakpoint on %s at line %s': 'Bod přerušení smazán - soubor %s na řádce %s', -'Replace': 'Zaměnit', -'Replace All': 'Zaměnit vše', -'request': 'request', -'Reset Password key': 'Reset registračního klíče', -'response': 'response', -'restart': 'restart', -'restore': 'obnovit', -'Retrieve username': 'Získat přihlašovací jméno', -'return': 'return', -'revert': 'vrátit se k původnímu', -'Role': 'Role', -'Rows in Table': 'Záznamy v tabulce', -'Rows selected': 'Záznamů zobrazeno', -'rules are not defined': 'pravidla nejsou definována', -"Run tests in this file (to run all files, you may also use the button labelled 'test')": "Spustí testy v tomto souboru (ke spuštění všech testů, použijte tlačítko 'test')", -'Running on %s': 'Běží na %s', -'Save': 'Uložit', -'Save file:': 'Save file:', -'Save via Ajax': 'Uložit pomocí Ajaxu', -'Saved file hash:': 'hash uloženého souboru:', -'Semantic': 'Modul semantic', -'Services': 'Služby', -'session': 'session', -'session expired': 'session expired', -'Set Breakpoint on %s at line %s: %s': 'Bod přerušení nastaven v souboru %s na řádce %s: %s', -'shell': 'příkazová řádka', -'Singular Form': 'Singular Form', -'Site': 'Správa aplikací', -'Size of cache:': 'Velikost cache:', -'skip to generate': 'skip to generate', -'Sorry, could not find mercurial installed': 'Bohužel mercurial není nainstalován.', -'Start a new app': 'Vytvořit novou aplikaci', -'Start searching': 'Začít hledání', -'Start wizard': 'Spustit průvodce', -'state': 'stav', -'Static': 'Static', -'static': 'statické soubory', -'Static files': 'Statické soubory', -'Statistics': 'Statistika', -'Step': 'Step', -'step': 'step', -'stop': 'stop', -'Stylesheet': 'CSS styly', -'submit': 'odeslat', -'Submit': 'Odeslat', -'successful': 'úspěšně', -'Support': 'Podpora', -'Sure you want to delete this object?': 'Opravdu chcete smazat tento objekt?', -'Table': 'tabulka', -'Table name': 'Název tabulky', -'Temporary': 'Dočasný', -'test': 'test', -'Testing application': 'Testing application', -'The "query" is a condition like "db.table1.field1==\'value\'". Something like "db.table1.field1==db.table2.field2" results in a SQL JOIN.': '"Dotaz" je podmínka, například "db.tabulka1.pole1==\'hodnota\'". Podmínka "db.tabulka1.pole1==db.tabulka2.pole2" pak vytvoří SQL JOIN.', -'The application logic, each URL path is mapped in one exposed function in the controller': 'Logika aplikace: každá URL je mapována na funkci vystavovanou kontrolérem.', -'The Core': 'Jádro (The Core)', -'The data representation, define database tables and sets': 'Reprezentace dat: definovat tabulky databáze a záznamy', -'The output of the file is a dictionary that was rendered by the view %s': 'Výstup ze souboru je slovník, který se zobrazil v pohledu %s.', -'The presentations layer, views are also known as templates': 'Prezentační vrstva: pohledy či templaty (šablony)', -'The Views': 'Pohledy (The Views)', -'There are no controllers': 'There are no controllers', -'There are no modules': 'There are no modules', -'There are no plugins': 'Žádné moduly nejsou instalovány.', -'There are no private files': 'Žádné soukromé soubory neexistují.', -'There are no static files': 'There are no static files', -'There are no translators, only default language is supported': 'There are no translators, only default language is supported', -'There are no views': 'There are no views', -'These files are not served, they are only available from within your app': 'Tyto soubory jsou klientům nepřístupné. K dispozici jsou pouze v rámci aplikace.', -'These files are served without processing, your images go here': 'Tyto soubory jsou servírovány bez přídavné logiky, sem patří např. obrázky.', -'This App': 'Tato aplikace', -'This is a copy of the scaffolding application': 'Toto je kopie aplikace skelet.', -'This is an experimental feature and it needs more testing. If you decide to upgrade you do it at your own risk': 'This is an experimental feature and it needs more testing. If you decide to upgrade you do it at your own risk', -'This is the %(filename)s template': 'This is the %(filename)s template', -'this page to see if a breakpoint was hit and debug interaction is required.': 'tuto stránku, abyste uviděli, zda se dosáhlo bodu přerušení.', -'Ticket': 'Ticket', -'Ticket ID': 'Ticket ID', -'Time in Cache (h:m:s)': 'Čas v Cache (h:m:s)', -'Timestamp': 'Časové razítko', -'to previous version.': 'k předchozí verzi.', -'To create a plugin, name a file/folder plugin_[name]': 'Zásuvný modul vytvoříte tak, že pojmenujete soubor/adresář plugin_[jméno modulu]', -'To emulate a breakpoint programatically, write:': 'K nastavení bodu přerušení v kódu programu, napište:', -'to use the debugger!': ', abyste mohli ladící program používat!', -'toggle breakpoint': 'vyp./zap. bod přerušení', -'Toggle Fullscreen': 'Na celou obrazovku a zpět', -'too short': 'Příliš krátké', -'Traceback': 'Traceback', -'Translation strings for the application': 'Překlad textů pro aplikaci', -'try something like': 'try something like', -'Try the mobile interface': 'Zkuste rozhraní pro mobilní zařízení', -'try view': 'try view', -'Twitter': 'Twitter', -'Type python statement in here and hit Return (Enter) to execute it.': 'Type python statement in here and hit Return (Enter) to execute it.', -'Type some Python code in here and hit Return (Enter) to execute it.': 'Type some Python code in here and hit Return (Enter) to execute it.', -'Unable to check for upgrades': 'Unable to check for upgrades', -'unable to parse csv file': 'csv soubor nedá sa zpracovat', -'uncheck all': 'vše odznačit', -'Uninstall': 'Odinstalovat', -'update': 'aktualizovat', -'update all languages': 'aktualizovat všechny jazyky', -'Update:': 'Upravit:', -'Upgrade': 'Upgrade', -'upgrade now': 'upgrade now', -'upgrade now to %s': 'upgrade now to %s', -'upload': 'nahrát', -'Upload': 'Upload', -'Upload a package:': 'Nahrát balík:', -'Upload and install packed application': 'Nahrát a instalovat zabalenou aplikaci', -'upload file:': 'nahrát soubor:', -'upload plugin file:': 'nahrát soubor modulu:', -'Use (...)&(...) for AND, (...)|(...) for OR, and ~(...) for NOT to build more complex queries.': 'Použijte (...)&(...) pro AND, (...)|(...) pro OR a ~(...) pro NOT pro sestavení složitějších dotazů.', -'User %(id)s Logged-in': 'Uživatel %(id)s přihlášen', -'User %(id)s Logged-out': 'Uživatel %(id)s odhlášen', -'User %(id)s Password changed': 'Uživatel %(id)s změnil heslo', -'User %(id)s Profile updated': 'Uživatel %(id)s upravil profil', -'User %(id)s Registered': 'Uživatel %(id)s se zaregistroval', -'User %(id)s Username retrieved': 'Uživatel %(id)s si nachal zaslat přihlašovací jméno', -'User ID': 'ID uživatele', -'Username': 'Přihlašovací jméno', -'variables': 'variables', -'Verify Password': 'Zopakujte heslo', -'Version': 'Verze', -'Version %s.%s.%s (%s) %s': 'Verze %s.%s.%s (%s) %s', -'Versioning': 'Verzování', -'Videos': 'Videa', -'View': 'Pohled (View)', -'Views': 'Pohledy', -'views': 'pohledy', -'Web Framework': 'Web Framework', -'web2py is up to date': 'Máte aktuální verzi web2py.', -'web2py online debugger': 'Ladící online web2py program', -'web2py Recent Tweets': 'Štěbetání na Twitteru o web2py', -'web2py upgrade': 'web2py upgrade', -'web2py upgraded; please restart it': 'web2py upgraded; please restart it', -'Welcome': 'Vítejte', -'Welcome to web2py': 'Vitejte ve web2py', -'Welcome to web2py!': 'Vítejte ve web2py!', -'Which called the function %s located in the file %s': 'která zavolala funkci %s v souboru (kontroléru) %s.', -'You are successfully running web2py': 'Úspěšně jste spustili web2py.', -'You can also set and remove breakpoint in the edit window, using the Toggle Breakpoint button': 'Nastavovat a mazat body přerušení je též možno v rámci editování zdrojového souboru přes tlačítko Vyp./Zap. bod přerušení', -'You can modify this application and adapt it to your needs': 'Tuto aplikaci si můžete upravit a přizpůsobit ji svým potřebám.', -'You need to set up and reach a': 'Je třeba nejprve nastavit a dojít až na', -'You visited the url %s': 'Navštívili jste stránku %s,', -'Your application will be blocked until you click an action button (next, step, continue, etc.)': 'Aplikace bude blokována než se klikne na jedno z tlačítek (další, krok, pokračovat, atd.)', -'You can inspect variables using the console bellow': 'Níže pomocí příkazové řádky si můžete prohlédnout proměnné', -} +# -*- coding: utf-8 -*- +{ +'!langcode!': 'cs-cz', +'!langname!': 'čeština', +'"update" is an optional expression like "field1=\'newvalue\'". You cannot update or delete the results of a JOIN': 'Kolonka "Upravit" je nepovinný výraz, například "pole1=\'nováhodnota\'". Výsledky databázového JOINu nemůžete mazat ani upravovat.', +'"User Exception" debug mode. An error ticket could be issued!': '"User Exception" debug mode. An error ticket could be issued!', +'%%{Row} in Table': '%%{řádek} v tabulce', +'%%{Row} selected': 'označených %%{řádek}', +'%s %%{row} deleted': '%s smazaných %%{záznam}', +'%s %%{row} updated': '%s upravených %%{záznam}', +'%s selected': '%s označených', +'%Y-%m-%d': '%d.%m.%Y', +'%Y-%m-%d %H:%M:%S': '%d.%m.%Y %H:%M:%S', +'(requires internet access)': '(vyžaduje připojení k internetu)', +'(requires internet access, experimental)': '(requires internet access, experimental)', +'(something like "it-it")': '(například "cs-cs")', +'@markmin\x01(file **gluon/contrib/plural_rules/%s.py** is not found)': '(soubor **gluon/contrib/plural_rules/%s.py** nenalezen)', +'@markmin\x01Searching: **%s** %%{file}': 'Hledání: **%s** %%{soubor}', +'About': 'O programu', +'About application': 'O aplikaci', +'Access Control': 'Řízení přístupu', +'Add breakpoint': 'Přidat bod přerušení', +'Additional code for your application': 'Další kód pro Vaši aplikaci', +'Admin design page': 'Admin design page', +'Admin language': 'jazyk rozhraní', +'Administrative interface': 'pro administrátorské rozhraní klikněte sem', +'Administrative Interface': 'Administrátorské rozhraní', +'administrative interface': 'rozhraní pro správu', +'Administrator Password:': 'Administrátorské heslo:', +'Ajax Recipes': 'Recepty s ajaxem', +'An error occured, please %s the page': 'An error occured, please %s the page', +'and rename it:': 'a přejmenovat na:', +'appadmin': 'appadmin', +'appadmin is disabled because insecure channel': 'appadmin je zakázaná bez zabezpečeného spojení', +'Application': 'Application', +'application "%s" uninstalled': 'application "%s" odinstalována', +'application compiled': 'aplikace zkompilována', +'Application name:': 'Název aplikace:', +'are not used': 'nepoužita', +'are not used yet': 'ještě nepoužita', +'Are you sure you want to delete this object?': 'Opravdu chcete odstranit tento objekt?', +'Are you sure you want to uninstall application "%s"?': 'Opravdu chcete odinstalovat aplikaci "%s"?', +'arguments': 'arguments', +'at char %s': 'at char %s', +'at line %s': 'at line %s', +'ATTENTION:': 'ATTENTION:', +'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.': 'ATTENTION: TESTING IS NOT THREAD SAFE SO DO NOT PERFORM MULTIPLE TESTS CONCURRENTLY.', +'Available Databases and Tables': 'Dostupné databáze a tabulky', +'back': 'zpět', +'Back to wizard': 'Back to wizard', +'Basics': 'Basics', +'Begin': 'Začít', +'breakpoint': 'bod přerušení', +'Breakpoints': 'Body přerušení', +'breakpoints': 'body přerušení', +'Buy this book': 'Koupit web2py knihu', +'Cache': 'Cache', +'cache': 'cache', +'Cache Keys': 'Klíče cache', +'cache, errors and sessions cleaned': 'cache, chyby a relace byly pročištěny', +'can be a git repo': 'může to být git repo', +'Cancel': 'Storno', +'Cannot be empty': 'Nemůže být prázdné', +'Change Admin Password': 'Změnit heslo pro správu', +'Change admin password': 'Změnit heslo pro správu aplikací', +'Change password': 'Změna hesla', +'check all': 'vše označit', +'Check for upgrades': 'Zkusit aktualizovat', +'Check to delete': 'Označit ke smazání', +'Check to delete:': 'Označit ke smazání:', +'Checking for upgrades...': 'Zjišťuji, zda jsou k dispozici aktualizace...', +'Clean': 'Pročistit', +'Clear CACHE?': 'Vymazat CACHE?', +'Clear DISK': 'Vymazat DISK', +'Clear RAM': 'Vymazat RAM', +'Click row to expand traceback': 'Pro rozbalení stopy, klikněte na řádek', +'Click row to view a ticket': 'Pro zobrazení chyby (ticketu), klikněte na řádku...', +'Client IP': 'IP adresa klienta', +'code': 'code', +'Code listing': 'Code listing', +'collapse/expand all': 'vše sbalit/rozbalit', +'Community': 'Komunita', +'Compile': 'Zkompilovat', +'compiled application removed': 'zkompilovaná aplikace smazána', +'Components and Plugins': 'Komponenty a zásuvné moduly', +'Condition': 'Podmínka', +'continue': 'continue', +'Controller': 'Kontrolér (Controller)', +'Controllers': 'Kontroléry', +'controllers': 'kontroléry', +'Copyright': 'Copyright', +'Count': 'Počet', +'Create': 'Vytvořit', +'create file with filename:': 'vytvořit soubor s názvem:', +'created by': 'vytvořil', +'Created By': 'Vytvořeno - kým', +'Created On': 'Vytvořeno - kdy', +'crontab': 'crontab', +'Current request': 'Aktuální požadavek', +'Current response': 'Aktuální odpověď', +'Current session': 'Aktuální relace', +'currently running': 'právě běží', +'currently saved or': 'uloženo nebo', +'customize me!': 'upravte mě!', +'data uploaded': 'data nahrána', +'Database': 'Rozhraní databáze', +'Database %s select': 'databáze %s výběr', +'Database administration': 'Database administration', +'database administration': 'správa databáze', +'Date and Time': 'Datum a čas', +'day': 'den', +'db': 'db', +'DB Model': 'Databázový model', +'Debug': 'Ladění', +'defines tables': 'defines tables', +'Delete': 'Smazat', +'delete': 'smazat', +'delete all checked': 'smazat vše označené', +'delete plugin': 'delete plugin', +'Delete this file (you will be asked to confirm deletion)': 'Smazat tento soubor (budete požádán o potvrzení mazání)', +'Delete:': 'Smazat:', +'deleted after first hit': 'smazat po prvním dosažení', +'Demo': 'Demo', +'Deploy': 'Nahrát', +'Deploy on Google App Engine': 'Nahrát na Google App Engine', +'Deploy to OpenShift': 'Nahrát na OpenShift', +'Deployment Recipes': 'Postupy pro deployment', +'Description': 'Popis', +'design': 'návrh', +'Detailed traceback description': 'Podrobný výpis prostředí', +'details': 'podrobnosti', +'direction: ltr': 'směr: ltr', +'Disable': 'Zablokovat', +'DISK': 'DISK', +'Disk Cache Keys': 'Klíče diskové cache', +'Disk Cleared': 'Disk smazán', +'docs': 'dokumentace', +'Documentation': 'Dokumentace', +"Don't know what to do?": 'Nevíte kudy kam?', +'done!': 'hotovo!', +'Download': 'Stáhnout', +'download layouts': 'stáhnout moduly rozvržení stránky', +'download plugins': 'stáhnout zásuvné moduly', +'E-mail': 'E-mail', +'Edit': 'Upravit', +'edit all': 'edit all', +'Edit application': 'Správa aplikace', +'edit controller': 'edit controller', +'Edit current record': 'Upravit aktuální záznam', +'Edit Profile': 'Upravit profil', +'edit views:': 'upravit pohled:', +'Editing file "%s"': 'Úprava souboru "%s"', +'Editing Language file': 'Úprava jazykového souboru', +'Editing Plural Forms File': 'Editing Plural Forms File', +'Email and SMS': 'Email a SMS', +'Enable': 'Odblokovat', +'enter a number between %(min)g and %(max)g': 'zadejte číslo mezi %(min)g a %(max)g', +'enter an integer between %(min)g and %(max)g': 'zadejte celé číslo mezi %(min)g a %(max)g', +'Error': 'Chyba', +'Error logs for "%(app)s"': 'Seznam výskytu chyb pro aplikaci "%(app)s"', +'Error snapshot': 'Snapshot chyby', +'Error ticket': 'Ticket chyby', +'Errors': 'Chyby', +'Exception %(extype)s: %(exvalue)s': 'Exception %(extype)s: %(exvalue)s', +'Exception %s': 'Exception %s', +'Exception instance attributes': 'Prvky instance výjimky', +'Expand Abbreviation': 'Expand Abbreviation', +'export as csv file': 'exportovat do .csv souboru', +'exposes': 'vystavuje', +'exposes:': 'vystavuje funkce:', +'extends': 'rozšiřuje', +'failed to compile file because:': 'soubor se nepodařilo zkompilovat, protože:', +'FAQ': 'Často kladené dotazy', +'File': 'Soubor', +'file': 'soubor', +'file "%(filename)s" created': 'file "%(filename)s" created', +'file saved on %(time)s': 'soubor uložen %(time)s', +'file saved on %s': 'soubor uložen %s', +'Filename': 'Název souboru', +'filter': 'filtr', +'Find Next': 'Najít další', +'Find Previous': 'Najít předchozí', +'First name': 'Křestní jméno', +'Forgot username?': 'Zapomněl jste svoje přihlašovací jméno?', +'forgot username?': 'zapomněl jste svoje přihlašovací jméno?', +'Forms and Validators': 'Formuláře a validátory', +'Frames': 'Frames', +'Free Applications': 'Aplikace zdarma', +'Functions with no doctests will result in [passed] tests.': 'Functions with no doctests will result in [passed] tests.', +'Generate': 'Vytvořit', +'Get from URL:': 'Stáhnout z internetu:', +'Git Pull': 'Git Pull', +'Git Push': 'Git Push', +'Globals##debug': 'Globální proměnné', +'go!': 'OK!', +'Goto': 'Goto', +'graph model': 'graph model', +'Group %(group_id)s created': 'Skupina %(group_id)s vytvořena', +'Group ID': 'ID skupiny', +'Groups': 'Skupiny', +'Hello World': 'Ahoj světe', +'Help': 'Nápověda', +'Hide/Show Translated strings': 'Skrýt/Zobrazit přeložené texty', +'Hits': 'Kolikrát dosaženo', +'Home': 'Domovská stránka', +'honored only if the expression evaluates to true': 'brát v potaz jen když se tato podmínka vyhodnotí kladně', +'How did you get here?': 'Jak jste se sem vlastně dostal?', +'If start the upgrade, be patient, it may take a while to download': 'If start the upgrade, be patient, it may take a while to download', +'If the report above contains a ticket number it indicates a failure in executing the controller, before any attempt to execute the doctests. This is usually due to an indentation error or an error outside function code.\nA green title indicates that all tests (if defined) passed. In this case test results are not shown.': 'If the report above contains a ticket number it indicates a failure in executing the controller, before any attempt to execute the doctests. This is usually due to an indentation error or an error outside function code.\nA green title indicates that all tests (if defined) passed. In this case test results are not shown.', +'import': 'import', +'Import/Export': 'Import/Export', +'includes': 'zahrnuje', +'Index': 'Index', +'insert new': 'vložit nový záznam ', +'insert new %s': 'vložit nový záznam %s', +'inspect attributes': 'inspect attributes', +'Install': 'Instalovat', +'Installed applications': 'Nainstalované aplikace', +'Interaction at %s line %s': 'Interakce v %s, na řádce %s', +'Interactive console': 'Interaktivní příkazová řádka', +'Internal State': 'Vnitřní stav', +'Introduction': 'Úvod', +'Invalid email': 'Neplatný email', +'Invalid password': 'Nesprávné heslo', +'invalid password.': 'neplatné heslo', +'Invalid Query': 'Neplatný dotaz', +'invalid request': 'Neplatný požadavek', +'Is Active': 'Je aktivní', +'It is %s %%{day} today.': 'Dnes je to %s %%{den}.', +'Key': 'Klíč', +'Key bindings': 'Vazby klíčů', +'Key bindings for ZenCoding Plugin': 'Key bindings for ZenCoding Plugin', +'languages': 'jazyky', +'Languages': 'Jazyky', +'Last name': 'Příjmení', +'Last saved on:': 'Naposledy uloženo:', +'Layout': 'Rozvržení stránky (layout)', +'Layout Plugins': 'Moduly rozvržení stránky (Layout Plugins)', +'Layouts': 'Rozvržení stránek', +'License for': 'Licence pro', +'Line number': 'Číslo řádku', +'LineNo': 'Č.řádku', +'Live Chat': 'Online pokec', +'loading...': 'nahrávám...', +'locals': 'locals', +'Locals##debug': 'Lokální proměnné', +'Logged in': 'Přihlášení proběhlo úspěšně', +'Logged out': 'Odhlášení proběhlo úspěšně', +'Login': 'Přihlásit se', +'login': 'přihlásit se', +'Login to the Administrative Interface': 'Přihlásit se do Správce aplikací', +'logout': 'odhlásit se', +'Logout': 'Odhlásit se', +'Lost Password': 'Zapomněl jste heslo', +'Lost password?': 'Zapomněl jste heslo?', +'lost password?': 'zapomněl jste heslo?', +'Manage': 'Manage', +'Manage Cache': 'Manage Cache', +'Menu Model': 'Model rozbalovací nabídky', +'Models': 'Modely', +'models': 'modely', +'Modified By': 'Změněno - kým', +'Modified On': 'Změněno - kdy', +'Modules': 'Moduly', +'modules': 'moduly', +'My Sites': 'Správa aplikací', +'Name': 'Jméno', +'new application "%s" created': 'nová aplikace "%s" vytvořena', +'New Application Wizard': 'Nový průvodce aplikací', +'New application wizard': 'Nový průvodce aplikací', +'New password': 'Nové heslo', +'New Record': 'Nový záznam', +'new record inserted': 'nový záznam byl založen', +'New simple application': 'Vytvořit primitivní aplikaci', +'next': 'next', +'next 100 rows': 'dalších 100 řádků', +'No databases in this application': 'V této aplikaci nejsou žádné databáze', +'No Interaction yet': 'Ještě žádná interakce nenastala', +'No ticket_storage.txt found under /private folder': 'Soubor ticket_storage.txt v adresáři /private nenalezen', +'Object or table name': 'Objekt či tabulka', +'Old password': 'Původní heslo', +'online designer': 'online návrhář', +'Online examples': 'Příklady online', +'Open new app in new window': 'Open new app in new window', +'or alternatively': 'or alternatively', +'Or Get from URL:': 'Or Get from URL:', +'or import from csv file': 'nebo importovat z .csv souboru', +'Origin': 'Původ', +'Original/Translation': 'Originál/Překlad', +'Other Plugins': 'Ostatní moduly', +'Other Recipes': 'Ostatní zásuvné moduly', +'Overview': 'Přehled', +'Overwrite installed app': 'Přepsat instalovanou aplikaci', +'Pack all': 'Zabalit', +'Pack compiled': 'Zabalit zkompilované', +'pack plugin': 'pack plugin', +'password': 'heslo', +'Password': 'Heslo', +"Password fields don't match": 'Hesla se neshodují', +'Peeking at file': 'Peeking at file', +'Please': 'Prosím', +'Plugin "%s" in application': 'Plugin "%s" in application', +'plugins': 'zásuvné moduly', +'Plugins': 'Zásuvné moduly', +'Plural Form #%s': 'Plural Form #%s', +'Plural-Forms:': 'Množná čísla:', +'Powered by': 'Poháněno', +'Preface': 'Předmluva', +'previous 100 rows': 'předchozích 100 řádků', +'Private files': 'Soukromé soubory', +'private files': 'soukromé soubory', +'profile': 'profil', +'Project Progress': 'Vývoj projektu', +'Python': 'Python', +'Query:': 'Dotaz:', +'Quick Examples': 'Krátké příklady', +'RAM': 'RAM', +'RAM Cache Keys': 'Klíče RAM Cache', +'Ram Cleared': 'RAM smazána', +'Readme': 'Nápověda', +'Recipes': 'Postupy jak na to', +'Record': 'Záznam', +'record does not exist': 'záznam neexistuje', +'Record ID': 'ID záznamu', +'Record id': 'id záznamu', +'refresh': 'obnovte', +'register': 'registrovat', +'Register': 'Zaregistrovat se', +'Registration identifier': 'Registrační identifikátor', +'Registration key': 'Registrační klíč', +'reload': 'reload', +'Reload routes': 'Znovu nahrát cesty', +'Remember me (for 30 days)': 'Zapamatovat na 30 dní', +'Remove compiled': 'Odstranit zkompilované', +'Removed Breakpoint on %s at line %s': 'Bod přerušení smazán - soubor %s na řádce %s', +'Replace': 'Zaměnit', +'Replace All': 'Zaměnit vše', +'request': 'request', +'Reset Password key': 'Reset registračního klíče', +'response': 'response', +'restart': 'restart', +'restore': 'obnovit', +'Retrieve username': 'Získat přihlašovací jméno', +'return': 'return', +'revert': 'vrátit se k původnímu', +'Role': 'Role', +'Rows in Table': 'Záznamy v tabulce', +'Rows selected': 'Záznamů zobrazeno', +'rules are not defined': 'pravidla nejsou definována', +"Run tests in this file (to run all files, you may also use the button labelled 'test')": "Spustí testy v tomto souboru (ke spuštění všech testů, použijte tlačítko 'test')", +'Running on %s': 'Běží na %s', +'Save': 'Uložit', +'Save file:': 'Save file:', +'Save via Ajax': 'Uložit pomocí Ajaxu', +'Saved file hash:': 'hash uloženého souboru:', +'Semantic': 'Modul semantic', +'Services': 'Služby', +'session': 'session', +'session expired': 'session expired', +'Set Breakpoint on %s at line %s: %s': 'Bod přerušení nastaven v souboru %s na řádce %s: %s', +'shell': 'příkazová řádka', +'Singular Form': 'Singular Form', +'Site': 'Správa aplikací', +'Size of cache:': 'Velikost cache:', +'skip to generate': 'skip to generate', +'Sorry, could not find mercurial installed': 'Bohužel mercurial není nainstalován.', +'Start a new app': 'Vytvořit novou aplikaci', +'Start searching': 'Začít hledání', +'Start wizard': 'Spustit průvodce', +'state': 'stav', +'Static': 'Static', +'static': 'statické soubory', +'Static files': 'Statické soubory', +'Statistics': 'Statistika', +'Step': 'Step', +'step': 'step', +'stop': 'stop', +'Stylesheet': 'CSS styly', +'submit': 'odeslat', +'Submit': 'Odeslat', +'successful': 'úspěšně', +'Support': 'Podpora', +'Sure you want to delete this object?': 'Opravdu chcete smazat tento objekt?', +'Table': 'tabulka', +'Table name': 'Název tabulky', +'Temporary': 'Dočasný', +'test': 'test', +'Testing application': 'Testing application', +'The "query" is a condition like "db.table1.field1==\'value\'". Something like "db.table1.field1==db.table2.field2" results in a SQL JOIN.': '"Dotaz" je podmínka, například "db.tabulka1.pole1==\'hodnota\'". Podmínka "db.tabulka1.pole1==db.tabulka2.pole2" pak vytvoří SQL JOIN.', +'The application logic, each URL path is mapped in one exposed function in the controller': 'Logika aplikace: každá URL je mapována na funkci vystavovanou kontrolérem.', +'The Core': 'Jádro (The Core)', +'The data representation, define database tables and sets': 'Reprezentace dat: definovat tabulky databáze a záznamy', +'The output of the file is a dictionary that was rendered by the view %s': 'Výstup ze souboru je slovník, který se zobrazil v pohledu %s.', +'The presentations layer, views are also known as templates': 'Prezentační vrstva: pohledy či templaty (šablony)', +'The Views': 'Pohledy (The Views)', +'There are no controllers': 'There are no controllers', +'There are no modules': 'There are no modules', +'There are no plugins': 'Žádné moduly nejsou instalovány.', +'There are no private files': 'Žádné soukromé soubory neexistují.', +'There are no static files': 'There are no static files', +'There are no translators, only default language is supported': 'There are no translators, only default language is supported', +'There are no views': 'There are no views', +'These files are not served, they are only available from within your app': 'Tyto soubory jsou klientům nepřístupné. K dispozici jsou pouze v rámci aplikace.', +'These files are served without processing, your images go here': 'Tyto soubory jsou servírovány bez přídavné logiky, sem patří např. obrázky.', +'This App': 'Tato aplikace', +'This is a copy of the scaffolding application': 'Toto je kopie aplikace skelet.', +'This is an experimental feature and it needs more testing. If you decide to upgrade you do it at your own risk': 'This is an experimental feature and it needs more testing. If you decide to upgrade you do it at your own risk', +'This is the %(filename)s template': 'This is the %(filename)s template', +'this page to see if a breakpoint was hit and debug interaction is required.': 'tuto stránku, abyste uviděli, zda se dosáhlo bodu přerušení.', +'Ticket': 'Ticket', +'Ticket ID': 'Ticket ID', +'Time in Cache (h:m:s)': 'Čas v Cache (h:m:s)', +'Timestamp': 'Časové razítko', +'to previous version.': 'k předchozí verzi.', +'To create a plugin, name a file/folder plugin_[name]': 'Zásuvný modul vytvoříte tak, že pojmenujete soubor/adresář plugin_[jméno modulu]', +'To emulate a breakpoint programatically, write:': 'K nastavení bodu přerušení v kódu programu, napište:', +'to use the debugger!': ', abyste mohli ladící program používat!', +'toggle breakpoint': 'vyp./zap. bod přerušení', +'Toggle Fullscreen': 'Na celou obrazovku a zpět', +'too short': 'Příliš krátké', +'Traceback': 'Traceback', +'Translation strings for the application': 'Překlad textů pro aplikaci', +'try something like': 'try something like', +'Try the mobile interface': 'Zkuste rozhraní pro mobilní zařízení', +'try view': 'try view', +'Twitter': 'Twitter', +'Type python statement in here and hit Return (Enter) to execute it.': 'Type python statement in here and hit Return (Enter) to execute it.', +'Type some Python code in here and hit Return (Enter) to execute it.': 'Type some Python code in here and hit Return (Enter) to execute it.', +'Unable to check for upgrades': 'Unable to check for upgrades', +'unable to parse csv file': 'csv soubor nedá sa zpracovat', +'uncheck all': 'vše odznačit', +'Uninstall': 'Odinstalovat', +'update': 'aktualizovat', +'update all languages': 'aktualizovat všechny jazyky', +'Update:': 'Upravit:', +'Upgrade': 'Upgrade', +'upgrade now': 'upgrade now', +'upgrade now to %s': 'upgrade now to %s', +'upload': 'nahrát', +'Upload': 'Upload', +'Upload a package:': 'Nahrát balík:', +'Upload and install packed application': 'Nahrát a instalovat zabalenou aplikaci', +'upload file:': 'nahrát soubor:', +'upload plugin file:': 'nahrát soubor modulu:', +'Use (...)&(...) for AND, (...)|(...) for OR, and ~(...) for NOT to build more complex queries.': 'Použijte (...)&(...) pro AND, (...)|(...) pro OR a ~(...) pro NOT pro sestavení složitějších dotazů.', +'User %(id)s Logged-in': 'Uživatel %(id)s přihlášen', +'User %(id)s Logged-out': 'Uživatel %(id)s odhlášen', +'User %(id)s Password changed': 'Uživatel %(id)s změnil heslo', +'User %(id)s Profile updated': 'Uživatel %(id)s upravil profil', +'User %(id)s Registered': 'Uživatel %(id)s se zaregistroval', +'User %(id)s Username retrieved': 'Uživatel %(id)s si nachal zaslat přihlašovací jméno', +'User ID': 'ID uživatele', +'Username': 'Přihlašovací jméno', +'variables': 'variables', +'Verify Password': 'Zopakujte heslo', +'Version': 'Verze', +'Version %s.%s.%s (%s) %s': 'Verze %s.%s.%s (%s) %s', +'Versioning': 'Verzování', +'Videos': 'Videa', +'View': 'Pohled (View)', +'Views': 'Pohledy', +'views': 'pohledy', +'Web Framework': 'Web Framework', +'web2py is up to date': 'Máte aktuální verzi web2py.', +'web2py online debugger': 'Ladící online web2py program', +'web2py Recent Tweets': 'Štěbetání na Twitteru o web2py', +'web2py upgrade': 'web2py upgrade', +'web2py upgraded; please restart it': 'web2py upgraded; please restart it', +'Welcome': 'Vítejte', +'Welcome to web2py': 'Vitejte ve web2py', +'Welcome to web2py!': 'Vítejte ve web2py!', +'Which called the function %s located in the file %s': 'která zavolala funkci %s v souboru (kontroléru) %s.', +'You are successfully running web2py': 'Úspěšně jste spustili web2py.', +'You can also set and remove breakpoint in the edit window, using the Toggle Breakpoint button': 'Nastavovat a mazat body přerušení je též možno v rámci editování zdrojového souboru přes tlačítko Vyp./Zap. bod přerušení', +'You can modify this application and adapt it to your needs': 'Tuto aplikaci si můžete upravit a přizpůsobit ji svým potřebám.', +'You need to set up and reach a': 'Je třeba nejprve nastavit a dojít až na', +'You visited the url %s': 'Navštívili jste stránku %s,', +'Your application will be blocked until you click an action button (next, step, continue, etc.)': 'Aplikace bude blokována než se klikne na jedno z tlačítek (další, krok, pokračovat, atd.)', +'You can inspect variables using the console bellow': 'Níže pomocí příkazové řádky si můžete prohlédnout proměnné', +} diff --git a/applications/admin/models/0.py b/applications/admin/models/0.py index 9f0a1d5a..adf95f7f 100644 --- a/applications/admin/models/0.py +++ b/applications/admin/models/0.py @@ -47,5 +47,3 @@ if 'adminLanguage' in request.cookies and not (request.cookies['adminLanguage'] #set static_version from gluon.settings import global_settings response.static_version = global_settings.web2py_version.split('-')[0] - - diff --git a/applications/admin/models/menu.py b/applications/admin/models/menu.py index 5534bcc9..f29f904c 100644 --- a/applications/admin/models/menu.py +++ b/applications/admin/models/menu.py @@ -34,4 +34,3 @@ else: URL(_a, 'default', f='logout'))) response.menu.append((T('Debug'), False, URL(_a, 'debug', 'interact'))) - diff --git a/applications/admin/models/plugin_statebutton.py b/applications/admin/models/plugin_statebutton.py index a857c5b0..21d0377e 100644 --- a/applications/admin/models/plugin_statebutton.py +++ b/applications/admin/models/plugin_statebutton.py @@ -7,11 +7,10 @@ def stateWidget(field, value, data={'on-label':'Enabled', 'off-label':'Disabled' except: fieldName = field - div = DIV(INPUT( _type='checkbox', _name='%s' % fieldName, _checked= 'checked' if value == 'true' else None, _value='true'), - _class='make-bootstrap-switch', + div = DIV(INPUT( _type='checkbox', _name='%s' % fieldName, _checked= 'checked' if value == 'true' else None, _value='true'), + _class='make-bootstrap-switch', data=data) script = SCRIPT(""" jQuery(".make-bootstrap-switch input[name='%s']").parent().bootstrapSwitch(); """ % fieldName) return DIV(div, script) - diff --git a/extras/build_web2py/setup_app.py b/extras/build_web2py/setup_app.py index aa68bbff..92985418 100755 --- a/extras/build_web2py/setup_app.py +++ b/extras/build_web2py/setup_app.py @@ -25,9 +25,9 @@ import sys import re import zipfile -#read web2py version from VERSION file +#read web2py version from VERSION file web2py_version_line = readlines_file('VERSION')[0] -#use regular expression to get just the version number +#use regular expression to get just the version number v_re = re.compile('[0-9]+\.[0-9]+\.[0-9]+') web2py_version = v_re.search(web2py_version_line).group(0) diff --git a/extras/build_web2py/setup_exe.py b/extras/build_web2py/setup_exe.py index 19b2b2fa..ae0d00ee 100755 --- a/extras/build_web2py/setup_exe.py +++ b/extras/build_web2py/setup_exe.py @@ -1,8 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - + #Adapted from http://bazaar.launchpad.net/~flavour/sahana-eden/trunk/view/head:/static/scripts/tools/standalone_exe.py - + USAGE = """ Usage: Copy this and setup_exe.conf to web2py root folder @@ -13,7 +13,7 @@ Usage: Install bbfreeze: https://pypi.python.org/pypi/bbfreeze/ run python setup_exe.py bbfreeze """ - + from distutils.core import setup from gluon.import_all import base_modules, contributed_modules from gluon.fileutils import readlines_file @@ -24,7 +24,7 @@ import shutil import sys import re import zipfile - + if len(sys.argv) != 2 or not os.path.isfile('web2py.py'): print USAGE sys.exit(1) @@ -32,11 +32,11 @@ BUILD_MODE = sys.argv[1] if not BUILD_MODE in ('py2exe', 'bbfreeze'): print USAGE sys.exit(1) - + def unzip(source_filename, dest_dir): with zipfile.ZipFile(source_filename) as zf: zf.extractall(dest_dir) - + #borrowed from http://bytes.com/topic/python/answers/851018-how-zip-directory-python-using-zipfile def recursive_zip(zipf, directory, folder=""): for item in os.listdir(directory): @@ -45,14 +45,14 @@ def recursive_zip(zipf, directory, folder=""): elif os.path.isdir(os.path.join(directory, item)): recursive_zip( zipf, os.path.join(directory, item), folder + os.sep + item) - - + + #read web2py version from VERSION file web2py_version_line = readlines_file('VERSION')[0] #use regular expression to get just the version number v_re = re.compile('[0-9]+\.[0-9]+\.[0-9]+') web2py_version = v_re.search(web2py_version_line).group(0) - + #pull in preferences from config file import ConfigParser Config = ConfigParser.ConfigParser() @@ -68,12 +68,12 @@ include_gevent = Config.getboolean("Setup", "include_gevent") # Python base version python_version = sys.version_info[:3] - - - + + + if BUILD_MODE == 'py2exe': import py2exe - + setup( console=[{'script':'web2py.py', 'icon_resources': [(0, 'extras/icons/web2py.ico')] @@ -88,8 +88,8 @@ if BUILD_MODE == 'py2exe': author="Massimo DiPierro", license="LGPL v3", data_files=[ - 'ABOUT', - 'LICENSE', + 'ABOUT', + 'LICENSE', 'VERSION' ], options={'py2exe': { @@ -108,7 +108,7 @@ if BUILD_MODE == 'py2exe': zipl.close() shutil.rmtree(library_temp_dir) print "web2py binary successfully built" - + elif BUILD_MODE == 'bbfreeze': modules = base_modules + contributed_modules from bbfreeze import Freezer @@ -131,26 +131,26 @@ elif BUILD_MODE == 'bbfreeze': for req in ['ABOUT', 'LICENSE', 'VERSION']: shutil.copy(req, os.path.join('dist', req)) print "web2py binary successfully built" - + try: os.unlink('storage.sqlite') except: pass - -#This need to happen after bbfreeze is run because Freezer() deletes distdir before starting! + +#This need to happen after bbfreeze is run because Freezer() deletes distdir before starting! if python_version > (2,5): # Python26 compatibility: http://www.py2exe.org/index.cgi/Tutorial#Step52 try: shutil.copytree('C:\Bin\Microsoft.VC90.CRT', 'dist/Microsoft.VC90.CRT/') except: print "You MUST copy Microsoft.VC90.CRT folder into the archive" - + def copy_folders(source, destination): """Copy files & folders from source to destination (within dist/)""" if os.path.exists(os.path.join('dist', destination)): shutil.rmtree(os.path.join('dist', destination)) shutil.copytree(os.path.join(source), os.path.join('dist', destination)) - + #should we remove Windows OS dlls user is unlikely to be able to distribute if remove_msft_dlls: print "Deleted Microsoft files not licensed for open source distribution" @@ -166,7 +166,7 @@ if remove_msft_dlls: os.unlink(os.path.join('dist', f)) except: print "unable to delete dist/" + f - + #Should we include applications? if copy_apps: copy_folders('applications', 'applications') @@ -177,12 +177,12 @@ else: copy_folders('applications/welcome', 'applications/welcome') copy_folders('applications/examples', 'applications/examples') print "Only web2py's admin, examples & welcome applications have been added" - + copy_folders('extras', 'extras') copy_folders('examples', 'examples') copy_folders('handlers', 'handlers') - - + + #should we copy project's site-packages into dist/site-packages if copy_site_packages: #copy site-packages @@ -190,7 +190,7 @@ if copy_site_packages: else: #no worries, web2py will create the (empty) folder first run print "Skipping site-packages" - + #should we copy project's scripts into dist/scripts if copy_scripts: #copy scripts @@ -198,7 +198,7 @@ if copy_scripts: else: #no worries, web2py will create the (empty) folder first run print "Skipping scripts" - + #should we create a zip file of the build? if make_zip: #create a web2py folder & copy dist's files into it @@ -209,13 +209,13 @@ if make_zip: # just temp so the web2py directory is included in our zip file path = 'zip_temp' # leave the first folder as None, as path is root. - recursive_zip(zipf, path) + recursive_zip(zipf, path) zipf.close() shutil.rmtree('zip_temp') print "Your Windows binary version of web2py can be found in " + \ zip_filename + ".zip" print "You may extract the archive anywhere and then run web2py/web2py.exe" - + #should py2exe build files be removed? if remove_build_files: if BUILD_MODE == 'py2exe': @@ -223,10 +223,10 @@ if remove_build_files: shutil.rmtree('deposit') shutil.rmtree('dist') print "build files removed" - + #final info if not make_zip and not remove_build_files: print "Your Windows binary & associated files can also be found in /dist" - + print "Finished!" -print "Enjoy web2py " + web2py_version_line \ No newline at end of file +print "Enjoy web2py " + web2py_version_line diff --git a/gluon/__init__.py b/gluon/__init__.py index 457b061d..d489d923 100644 --- a/gluon/__init__.py +++ b/gluon/__init__.py @@ -11,7 +11,7 @@ Web2Py framework modules """ __all__ = ['A', 'B', 'BEAUTIFY', 'BODY', 'BR', 'CAT', 'CENTER', 'CLEANUP', 'CODE', 'CRYPT', 'DAL', 'DIV', 'EM', 'EMBED', 'FIELDSET', 'FORM', 'Field', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'HEAD', 'HR', 'HTML', 'HTTP', 'I', 'IFRAME', 'IMG', 'INPUT', 'IS_ALPHANUMERIC', 'IS_DATE', 'IS_DATETIME', 'IS_DATETIME_IN_RANGE', 'IS_DATE_IN_RANGE', 'IS_DECIMAL_IN_RANGE', 'IS_EMAIL', 'IS_LIST_OF_EMAILS', 'IS_EMPTY_OR', 'IS_EQUAL_TO', 'IS_EXPR', 'IS_FLOAT_IN_RANGE', 'IS_IMAGE', 'IS_JSON', 'IS_INT_IN_RANGE', 'IS_IN_DB', 'IS_IN_SET', 'IS_IPV4', 'IS_LENGTH', 'IS_LIST_OF', 'IS_LOWER', 'IS_MATCH', 'IS_NOT_EMPTY', 'IS_NOT_IN_DB', 'IS_NULL_OR', 'IS_SLUG', 'IS_STRONG', 'IS_TIME', 'IS_UPLOAD_FILENAME', 'IS_UPPER', 'IS_URL', 'LABEL', 'LEGEND', 'LI', 'LINK', 'LOAD', 'MARKMIN', 'MENU', 'META', 'OBJECT', 'OL', 'ON', 'OPTGROUP', 'OPTION', 'P', 'PRE', 'SCRIPT', 'SELECT', 'SPAN', 'SQLFORM', 'SQLTABLE', 'STRONG', 'STYLE', 'TABLE', 'TAG', 'TBODY', 'TD', 'TEXTAREA', 'TFOOT', 'TH', 'THEAD', 'TITLE', 'TR', 'TT', 'UL', 'URL', 'XHTML', 'XML', 'redirect', 'current', 'embed64'] - + from globals import current from html import * from validators import * diff --git a/gluon/cache.py b/gluon/cache.py index 3a03a19c..671abe7e 100644 --- a/gluon/cache.py +++ b/gluon/cache.py @@ -1,693 +1,693 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -""" -| This file is part of the web2py Web Framework -| Copyrighted by Massimo Di Pierro -| License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) - -Basic caching classes and methods ---------------------------------- - -- Cache - The generic caching object interfacing with the others -- CacheInRam - providing caching in ram -- CacheOnDisk - provides caches on disk - -Memcache is also available via a different module (see gluon.contrib.memcache) - -When web2py is running on Google App Engine, -caching will be provided by the GAE memcache -(see gluon.contrib.gae_memcache) -""" -import time -import thread -import os -import sys -import logging -import re -import hashlib -import datetime -import tempfile -from gluon import recfile -try: - from gluon import settings - have_settings = True -except ImportError: - have_settings = False - -try: - import cPickle as pickle -except: - import pickle - -logger = logging.getLogger("web2py.cache") - -__all__ = ['Cache', 'lazy_cache'] - - -DEFAULT_TIME_EXPIRE = 300 - - - -class CacheAbstract(object): - """ - Abstract class for cache implementations. - Main function just provides referenced api documentation. - - Use CacheInRam or CacheOnDisk instead which are derived from this class. - - Note: - Michele says: there are signatures inside gdbm files that are used - directly by the python gdbm adapter that often are lagging behind in the - detection code in python part. - On every occasion that a gdbm store is probed by the python adapter, - the probe fails, because gdbm file version is newer. - Using gdbm directly from C would work, because there is backward - compatibility, but not from python! - The .shelve file is discarded and a new one created (with new - signature) and it works until it is probed again... - The possible consequences are memory leaks and broken sessions. - """ - - cache_stats_name = 'web2py_cache_statistics' - - def __init__(self, request=None): - """Initializes the object - - Args: - request: the global request object - """ - raise NotImplementedError - - def __call__(self, key, f, - time_expire=DEFAULT_TIME_EXPIRE): - """ - Tries to retrieve the value corresponding to `key` from the cache if the - object exists and if it did not expire, else it calls the function `f` - and stores the output in the cache corresponding to `key`. It always - returns the function that is returned. - - Args: - key(str): the key of the object to be stored or retrieved - f(function): the function whose output is to be cached. - - If `f` is `None` the cache is cleared. - time_expire(int): expiration of the cache in seconds. - - It's used to compare the current time with the time - when the requested object was last saved in cache. It does not - affect future requests. Setting `time_expire` to 0 or negative - value forces the cache to refresh. - """ - raise NotImplementedError - - def clear(self, regex=None): - """ - Clears the cache of all keys that match the provided regular expression. - If no regular expression is provided, it clears all entries in cache. - - Args: - regex: if provided, only keys matching the regex will be cleared, - otherwise all keys are cleared. - """ - - raise NotImplementedError - - def increment(self, key, value=1): - """ - Increments the cached value for the given key by the amount in value - - Args: - key(str): key for the cached object to be incremeneted - value(int): amount of the increment (defaults to 1, can be negative) - """ - raise NotImplementedError - - def _clear(self, storage, regex): - """ - Auxiliary function called by `clear` to search and clear cache entries - """ - r = re.compile(regex) - for key in storage.keys(): - if r.match(str(key)): - del storage[key] - return - - -class CacheInRam(CacheAbstract): - """ - Ram based caching - - This is implemented as global (per process, shared by all threads) - dictionary. - A mutex-lock mechanism avoid conflicts. - """ - - locker = thread.allocate_lock() - meta_storage = {} - - def __init__(self, request=None): - self.initialized = False - self.request = request - self.storage = {} - - def initialize(self): - if self.initialized: - return - else: - self.initialized = True - self.locker.acquire() - request = self.request - if request: - app = request.application - else: - app = '' - if not app in self.meta_storage: - self.storage = self.meta_storage[app] = { - CacheAbstract.cache_stats_name: {'hit_total': 0, 'misses': 0}} - else: - self.storage = self.meta_storage[app] - self.locker.release() - - def clear(self, regex=None): - self.initialize() - self.locker.acquire() - storage = self.storage - if regex is None: - storage.clear() - else: - self._clear(storage, regex) - - if not CacheAbstract.cache_stats_name in storage.keys(): - storage[CacheAbstract.cache_stats_name] = { - 'hit_total': 0, 'misses': 0} - - self.locker.release() - - def __call__(self, key, f, - time_expire=DEFAULT_TIME_EXPIRE, - destroyer=None): - """ - Attention! cache.ram does not copy the cached object. - It just stores a reference to it. Turns out the deepcopying the object - has some problems: - - - would break backward compatibility - - would be limiting because people may want to cache live objects - - would work unless we deepcopy no storage and retrival which would make - things slow. - - Anyway. You can deepcopy explicitly in the function generating the value - to be cached. - """ - self.initialize() - - dt = time_expire - now = time.time() - - self.locker.acquire() - item = self.storage.get(key, None) - if item and f is None: - del self.storage[key] - if destroyer: - destroyer(item[1]) - self.storage[CacheAbstract.cache_stats_name]['hit_total'] += 1 - self.locker.release() - - if f is None: - return None - if item and (dt is None or item[0] > now - dt): - return item[1] - elif item and (item[0] < now - dt) and destroyer: - destroyer(item[1]) - value = f() - - self.locker.acquire() - self.storage[key] = (now, value) - self.storage[CacheAbstract.cache_stats_name]['misses'] += 1 - self.locker.release() - return value - - def increment(self, key, value=1): - self.initialize() - self.locker.acquire() - try: - if key in self.storage: - value = self.storage[key][1] + value - self.storage[key] = (time.time(), value) - except BaseException, e: - self.locker.release() - raise e - self.locker.release() - return value - - -class CacheOnDisk(CacheAbstract): - """ - Disk based cache - - This is implemented as a key value store where each key corresponds to a - single file in disk which is replaced when the value changes. - - Disk cache provides persistance when web2py is started/stopped but it is - slower than `CacheInRam` - - Values stored in disk cache must be pickable. - """ - - class PersistentStorage(object): - """ - Implements a key based storage in disk. - """ - - def __init__(self, folder): - self.folder = folder - self.key_filter_in = lambda key: key - self.key_filter_out = lambda key: key - # Check the best way to do atomic file replacement. - if sys.version_info >= (3, 3): - self.replace = os.replace - elif sys.platform == "win32": - import ctypes - from ctypes import wintypes - ReplaceFile = ctypes.windll.kernel32.ReplaceFileW - ReplaceFile.restype = wintypes.BOOL - ReplaceFile.argtypes = [ - wintypes.LPWSTR, - wintypes.LPWSTR, - wintypes.LPWSTR, - wintypes.DWORD, - wintypes.LPVOID, - wintypes.LPVOID, - ] - - def replace_windows(src, dst): - if not ReplaceFile(dst, src, None, 0, 0, 0): - os.rename(src, dst) - - self.replace = replace_windows - else: - # POSIX rename() is always atomic - self.replace = os.rename - - # Make sure we use valid filenames. - if sys.platform == "win32": - import base64 - def key_filter_in_windows(key): - """ - Windows doesn't allow \ / : * ? "< > | in filenames. - To go around this encode the keys with base32. - """ - return base64.b32encode(key) - - def key_filter_out_windows(key): - """ - We need to decode the keys so regex based removal works. - """ - return base64.b32decode(key) - - self.key_filter_in = key_filter_in_windows - self.key_filter_out = key_filter_out_windows - - - def __setitem__(self, key, value): - tmp_name, tmp_path = tempfile.mkstemp(dir=self.folder) - tmp = os.fdopen(tmp_name, 'wb') - try: - pickle.dump((time.time(), value), tmp, pickle.HIGHEST_PROTOCOL) - finally: - tmp.close() - key = self.key_filter_in(key) - fullfilename = os.path.join(self.folder, recfile.generate(key)) - if not os.path.exists(os.path.dirname(fullfilename)): - os.makedirs(os.path.dirname(fullfilename)) - self.replace(tmp_path, fullfilename) - - - def __getitem__(self, key): - key = self.key_filter_in(key) - if recfile.exists(key, path=self.folder): - timestamp, value = pickle.load(recfile.open(key, 'rb', path=self.folder)) - return value - else: - raise KeyError - - - def __contains__(self, key): - key = self.key_filter_in(key) - return recfile.exists(key, path=self.folder) - - - def __delitem__(self, key): - key = self.key_filter_in(key) - recfile.remove(key, path=self.folder) - - - def __iter__(self): - for dirpath, dirnames, filenames in os.walk(self.folder): - for filename in filenames: - yield self.key_filter_out(filename) - - - def keys(self): - return list(self.__iter__()) - - - def get(self, key, default=None): - try: - return self[key] - except KeyError: - return default - - - def clear(self): - for key in self: - del self[key] - - - def __init__(self, request=None, folder=None): - self.initialized = False - self.request = request - self.folder = folder - self.storage = None - - - def initialize(self): - if self.initialized: - return - else: - self.initialized = True - - folder = self.folder - request = self.request - - # Lets test if the cache folder exists, if not - # we are going to create it - folder = os.path.join(folder or request.folder, 'cache') - - if not os.path.exists(folder): - os.mkdir(folder) - - self.storage = CacheOnDisk.PersistentStorage(folder) - - if not CacheAbstract.cache_stats_name in self.storage: - self.storage[CacheAbstract.cache_stats_name] = {'hit_total': 0, 'misses': 0} - - - def __call__(self, key, f, - time_expire=DEFAULT_TIME_EXPIRE): - self.initialize() - - dt = time_expire - item = self.storage.get(key) - self.storage[CacheAbstract.cache_stats_name]['hit_total'] += 1 - - if item and f is None: - del self.storage[key] - - if f is None: - return None - - now = time.time() - - if item and ((dt is None) or (item[0] > now - dt)): - value = item[1] - else: - value = f() - self.storage[key] = (now, value) - self.storage[CacheAbstract.cache_stats_name]['misses'] += 1 - - return value - - - def clear(self, regex=None): - self.initialize() - storage = self.storage - if regex is None: - storage.clear() - else: - self._clear(storage, regex) - - if not CacheAbstract.cache_stats_name in storage: - storage[CacheAbstract.cache_stats_name] = { - 'hit_total': 0, 'misses': 0} - - - def increment(self, key, value=1): - self.initialize() - storage = self.storage - try: - if key in storage: - value = storage[key][1] + value - storage[key] = (time.time(), value) - except: - pass - return value - - - -class CacheAction(object): - def __init__(self, func, key, time_expire, cache, cache_model): - self.__name__ = func.__name__ - self.__doc__ = func.__doc__ - self.func = func - self.key = key - self.time_expire = time_expire - self.cache = cache - self.cache_model = cache_model - - def __call__(self, *a, **b): - if not self.key: - key2 = self.__name__ + ':' + repr(a) + ':' + repr(b) - else: - key2 = self.key.replace('%(name)s', self.__name__)\ - .replace('%(args)s', str(a)).replace('%(vars)s', str(b)) - cache_model = self.cache_model - if not cache_model or isinstance(cache_model, str): - cache_model = getattr(self.cache, cache_model or 'ram') - return cache_model(key2, - lambda a=a, b=b: self.func(*a, **b), - self.time_expire) - - -class Cache(object): - """ - Sets up generic caching, creating an instance of both CacheInRam and - CacheOnDisk. - In case of GAE will make use of gluon.contrib.gae_memcache. - - - self.ram is an instance of CacheInRam - - self.disk is an instance of CacheOnDisk - """ - - autokey = ':%(name)s:%(args)s:%(vars)s' - - def __init__(self, request): - """ - Args: - request: the global request object - """ - # GAE will have a special caching - if have_settings and settings.global_settings.web2py_runtime_gae: - from gluon.contrib.gae_memcache import MemcacheClient - self.ram = self.disk = MemcacheClient(request) - else: - # Otherwise use ram (and try also disk) - self.ram = CacheInRam(request) - try: - self.disk = CacheOnDisk(request) - except IOError: - logger.warning('no cache.disk (IOError)') - except AttributeError: - # normally not expected anymore, as GAE has already - # been accounted for - logger.warning('no cache.disk (AttributeError)') - - def action(self, time_expire=DEFAULT_TIME_EXPIRE, cache_model=None, - prefix=None, session=False, vars=True, lang=True, - user_agent=False, public=True, valid_statuses=None, - quick=None): - """Better fit for caching an action - - Warning: - Experimental! - - Currently only HTTP 1.1 compliant - reference : http://code.google.com/p/doctype-mirror/wiki/ArticleHttpCaching - - Args: - time_expire(int): same as @cache - cache_model(str): same as @cache - prefix(str): add a prefix to the calculated key - session(bool): adds response.session_id to the key - vars(bool): adds request.env.query_string - lang(bool): adds T.accepted_language - user_agent(bool or dict): if True, adds is_mobile and is_tablet to the key. - Pass a dict to use all the needed values (uses str(.items())) - (e.g. user_agent=request.user_agent()). Used only if session is - not True - public(bool): if False forces the Cache-Control to be 'private' - valid_statuses: by default only status codes starting with 1,2,3 will be cached. - pass an explicit list of statuses on which turn the cache on - quick: Session,Vars,Lang,User-agent,Public: - fast overrides with initials, e.g. 'SVLP' or 'VLP', or 'VLP' - """ - from gluon import current - from gluon.http import HTTP - def wrap(func): - def wrapped_f(): - if current.request.env.request_method != 'GET': - return func() - if time_expire: - cache_control = 'max-age=%(time_expire)s, s-maxage=%(time_expire)s' % dict(time_expire=time_expire) - if quick: - session_ = True if 'S' in quick else False - vars_ = True if 'V' in quick else False - lang_ = True if 'L' in quick else False - user_agent_ = True if 'U' in quick else False - public_ = True if 'P' in quick else False - else: - session_, vars_, lang_, user_agent_, public_ = session, vars, lang, user_agent, public - if not session_ and public_: - cache_control += ', public' - expires = (current.request.utcnow + datetime.timedelta(seconds=time_expire)).strftime('%a, %d %b %Y %H:%M:%S GMT') - else: - cache_control += ', private' - expires = 'Fri, 01 Jan 1990 00:00:00 GMT' - if cache_model: - #figure out the correct cache key - cache_key = [current.request.env.path_info, current.response.view] - if session_: - cache_key.append(current.response.session_id) - elif user_agent_: - if user_agent_ is True: - cache_key.append("%(is_mobile)s_%(is_tablet)s" % current.request.user_agent()) - else: - cache_key.append(str(user_agent_.items())) - if vars_: - cache_key.append(current.request.env.query_string) - if lang_: - cache_key.append(current.T.accepted_language) - cache_key = hashlib.md5('__'.join(cache_key)).hexdigest() - if prefix: - cache_key = prefix + cache_key - try: - #action returns something - rtn = cache_model(cache_key, lambda : func(), time_expire=time_expire) - http, status = None, current.response.status - except HTTP, e: - #action raises HTTP (can still be valid) - rtn = cache_model(cache_key, lambda : e.body, time_expire=time_expire) - http, status = HTTP(e.status, rtn, **e.headers), e.status - else: - #action raised a generic exception - http = None - else: - #no server-cache side involved - try: - #action returns something - rtn = func() - http, status = None, current.response.status - except HTTP, e: - #action raises HTTP (can still be valid) - status = e.status - http = HTTP(e.status, e.body, **e.headers) - else: - #action raised a generic exception - http = None - send_headers = False - if http and isinstance(valid_statuses, list): - if status in valid_statuses: - send_headers = True - elif valid_statuses is None: - if str(status)[0] in '123': - send_headers = True - if send_headers: - headers = { - 'Pragma' : None, - 'Expires' : expires, - 'Cache-Control' : cache_control - } - current.response.headers.update(headers) - if cache_model and not send_headers: - #we cached already the value, but the status is not valid - #so we need to delete the cached value - cache_model(cache_key, None) - if http: - if send_headers: - http.headers.update(current.response.headers) - raise http - return rtn - wrapped_f.__name__ = func.__name__ - wrapped_f.__doc__ = func.__doc__ - return wrapped_f - return wrap - - def __call__(self, - key=None, - time_expire=DEFAULT_TIME_EXPIRE, - cache_model=None): - """ - Decorator function that can be used to cache any function/method. - - Args: - key(str) : the key of the object to be store or retrieved - time_expire(int) : expiration of the cache in seconds - `time_expire` is used to compare the current time with the time - when the requested object was last saved in cache. - It does not affect future requests. - Setting `time_expire` to 0 or negative value forces the cache to - refresh. - cache_model(str): can be "ram", "disk" or other (like "memcache"). - Defaults to "ram" - - When the function `f` is called, web2py tries to retrieve - the value corresponding to `key` from the cache if the - object exists and if it did not expire, else it calles the function `f` - and stores the output in the cache corresponding to `key`. In the case - the output of the function is returned. - - Example: :: - - @cache('key', 5000, cache.ram) - def f(): - return time.ctime() - - Note: - If the function `f` is an action, we suggest using - @cache.action instead - """ - - def tmp(func, cache=self, cache_model=cache_model): - return CacheAction(func, key, time_expire, self, cache_model) - return tmp - - @staticmethod - def with_prefix(cache_model, prefix): - """ - allow replacing cache.ram with cache.with_prefix(cache.ram,'prefix') - it will add prefix to all the cache keys used. - """ - return lambda key, f, time_expire=DEFAULT_TIME_EXPIRE, prefix=prefix:\ - cache_model(prefix + key, f, time_expire) - - -def lazy_cache(key=None, time_expire=None, cache_model='ram'): - """ - Can be used to cache any function including ones in modules, - as long as the cached function is only called within a web2py request - - If a key is not provided, one is generated from the function name - `time_expire` defaults to None (no cache expiration) - - If cache_model is "ram" then the model is current.cache.ram, etc. - """ - def decorator(f, key=key, time_expire=time_expire, cache_model=cache_model): - key = key or repr(f) - - def g(*c, **d): - from gluon import current - return current.cache(key, time_expire, cache_model)(f)(*c, **d) - g.__name__ = f.__name__ - return g - return decorator +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +| This file is part of the web2py Web Framework +| Copyrighted by Massimo Di Pierro +| License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) + +Basic caching classes and methods +--------------------------------- + +- Cache - The generic caching object interfacing with the others +- CacheInRam - providing caching in ram +- CacheOnDisk - provides caches on disk + +Memcache is also available via a different module (see gluon.contrib.memcache) + +When web2py is running on Google App Engine, +caching will be provided by the GAE memcache +(see gluon.contrib.gae_memcache) +""" +import time +import thread +import os +import sys +import logging +import re +import hashlib +import datetime +import tempfile +from gluon import recfile +try: + from gluon import settings + have_settings = True +except ImportError: + have_settings = False + +try: + import cPickle as pickle +except: + import pickle + +logger = logging.getLogger("web2py.cache") + +__all__ = ['Cache', 'lazy_cache'] + + +DEFAULT_TIME_EXPIRE = 300 + + + +class CacheAbstract(object): + """ + Abstract class for cache implementations. + Main function just provides referenced api documentation. + + Use CacheInRam or CacheOnDisk instead which are derived from this class. + + Note: + Michele says: there are signatures inside gdbm files that are used + directly by the python gdbm adapter that often are lagging behind in the + detection code in python part. + On every occasion that a gdbm store is probed by the python adapter, + the probe fails, because gdbm file version is newer. + Using gdbm directly from C would work, because there is backward + compatibility, but not from python! + The .shelve file is discarded and a new one created (with new + signature) and it works until it is probed again... + The possible consequences are memory leaks and broken sessions. + """ + + cache_stats_name = 'web2py_cache_statistics' + + def __init__(self, request=None): + """Initializes the object + + Args: + request: the global request object + """ + raise NotImplementedError + + def __call__(self, key, f, + time_expire=DEFAULT_TIME_EXPIRE): + """ + Tries to retrieve the value corresponding to `key` from the cache if the + object exists and if it did not expire, else it calls the function `f` + and stores the output in the cache corresponding to `key`. It always + returns the function that is returned. + + Args: + key(str): the key of the object to be stored or retrieved + f(function): the function whose output is to be cached. + + If `f` is `None` the cache is cleared. + time_expire(int): expiration of the cache in seconds. + + It's used to compare the current time with the time + when the requested object was last saved in cache. It does not + affect future requests. Setting `time_expire` to 0 or negative + value forces the cache to refresh. + """ + raise NotImplementedError + + def clear(self, regex=None): + """ + Clears the cache of all keys that match the provided regular expression. + If no regular expression is provided, it clears all entries in cache. + + Args: + regex: if provided, only keys matching the regex will be cleared, + otherwise all keys are cleared. + """ + + raise NotImplementedError + + def increment(self, key, value=1): + """ + Increments the cached value for the given key by the amount in value + + Args: + key(str): key for the cached object to be incremeneted + value(int): amount of the increment (defaults to 1, can be negative) + """ + raise NotImplementedError + + def _clear(self, storage, regex): + """ + Auxiliary function called by `clear` to search and clear cache entries + """ + r = re.compile(regex) + for key in storage.keys(): + if r.match(str(key)): + del storage[key] + return + + +class CacheInRam(CacheAbstract): + """ + Ram based caching + + This is implemented as global (per process, shared by all threads) + dictionary. + A mutex-lock mechanism avoid conflicts. + """ + + locker = thread.allocate_lock() + meta_storage = {} + + def __init__(self, request=None): + self.initialized = False + self.request = request + self.storage = {} + + def initialize(self): + if self.initialized: + return + else: + self.initialized = True + self.locker.acquire() + request = self.request + if request: + app = request.application + else: + app = '' + if not app in self.meta_storage: + self.storage = self.meta_storage[app] = { + CacheAbstract.cache_stats_name: {'hit_total': 0, 'misses': 0}} + else: + self.storage = self.meta_storage[app] + self.locker.release() + + def clear(self, regex=None): + self.initialize() + self.locker.acquire() + storage = self.storage + if regex is None: + storage.clear() + else: + self._clear(storage, regex) + + if not CacheAbstract.cache_stats_name in storage.keys(): + storage[CacheAbstract.cache_stats_name] = { + 'hit_total': 0, 'misses': 0} + + self.locker.release() + + def __call__(self, key, f, + time_expire=DEFAULT_TIME_EXPIRE, + destroyer=None): + """ + Attention! cache.ram does not copy the cached object. + It just stores a reference to it. Turns out the deepcopying the object + has some problems: + + - would break backward compatibility + - would be limiting because people may want to cache live objects + - would work unless we deepcopy no storage and retrival which would make + things slow. + + Anyway. You can deepcopy explicitly in the function generating the value + to be cached. + """ + self.initialize() + + dt = time_expire + now = time.time() + + self.locker.acquire() + item = self.storage.get(key, None) + if item and f is None: + del self.storage[key] + if destroyer: + destroyer(item[1]) + self.storage[CacheAbstract.cache_stats_name]['hit_total'] += 1 + self.locker.release() + + if f is None: + return None + if item and (dt is None or item[0] > now - dt): + return item[1] + elif item and (item[0] < now - dt) and destroyer: + destroyer(item[1]) + value = f() + + self.locker.acquire() + self.storage[key] = (now, value) + self.storage[CacheAbstract.cache_stats_name]['misses'] += 1 + self.locker.release() + return value + + def increment(self, key, value=1): + self.initialize() + self.locker.acquire() + try: + if key in self.storage: + value = self.storage[key][1] + value + self.storage[key] = (time.time(), value) + except BaseException, e: + self.locker.release() + raise e + self.locker.release() + return value + + +class CacheOnDisk(CacheAbstract): + """ + Disk based cache + + This is implemented as a key value store where each key corresponds to a + single file in disk which is replaced when the value changes. + + Disk cache provides persistance when web2py is started/stopped but it is + slower than `CacheInRam` + + Values stored in disk cache must be pickable. + """ + + class PersistentStorage(object): + """ + Implements a key based storage in disk. + """ + + def __init__(self, folder): + self.folder = folder + self.key_filter_in = lambda key: key + self.key_filter_out = lambda key: key + # Check the best way to do atomic file replacement. + if sys.version_info >= (3, 3): + self.replace = os.replace + elif sys.platform == "win32": + import ctypes + from ctypes import wintypes + ReplaceFile = ctypes.windll.kernel32.ReplaceFileW + ReplaceFile.restype = wintypes.BOOL + ReplaceFile.argtypes = [ + wintypes.LPWSTR, + wintypes.LPWSTR, + wintypes.LPWSTR, + wintypes.DWORD, + wintypes.LPVOID, + wintypes.LPVOID, + ] + + def replace_windows(src, dst): + if not ReplaceFile(dst, src, None, 0, 0, 0): + os.rename(src, dst) + + self.replace = replace_windows + else: + # POSIX rename() is always atomic + self.replace = os.rename + + # Make sure we use valid filenames. + if sys.platform == "win32": + import base64 + def key_filter_in_windows(key): + """ + Windows doesn't allow \ / : * ? "< > | in filenames. + To go around this encode the keys with base32. + """ + return base64.b32encode(key) + + def key_filter_out_windows(key): + """ + We need to decode the keys so regex based removal works. + """ + return base64.b32decode(key) + + self.key_filter_in = key_filter_in_windows + self.key_filter_out = key_filter_out_windows + + + def __setitem__(self, key, value): + tmp_name, tmp_path = tempfile.mkstemp(dir=self.folder) + tmp = os.fdopen(tmp_name, 'wb') + try: + pickle.dump((time.time(), value), tmp, pickle.HIGHEST_PROTOCOL) + finally: + tmp.close() + key = self.key_filter_in(key) + fullfilename = os.path.join(self.folder, recfile.generate(key)) + if not os.path.exists(os.path.dirname(fullfilename)): + os.makedirs(os.path.dirname(fullfilename)) + self.replace(tmp_path, fullfilename) + + + def __getitem__(self, key): + key = self.key_filter_in(key) + if recfile.exists(key, path=self.folder): + timestamp, value = pickle.load(recfile.open(key, 'rb', path=self.folder)) + return value + else: + raise KeyError + + + def __contains__(self, key): + key = self.key_filter_in(key) + return recfile.exists(key, path=self.folder) + + + def __delitem__(self, key): + key = self.key_filter_in(key) + recfile.remove(key, path=self.folder) + + + def __iter__(self): + for dirpath, dirnames, filenames in os.walk(self.folder): + for filename in filenames: + yield self.key_filter_out(filename) + + + def keys(self): + return list(self.__iter__()) + + + def get(self, key, default=None): + try: + return self[key] + except KeyError: + return default + + + def clear(self): + for key in self: + del self[key] + + + def __init__(self, request=None, folder=None): + self.initialized = False + self.request = request + self.folder = folder + self.storage = None + + + def initialize(self): + if self.initialized: + return + else: + self.initialized = True + + folder = self.folder + request = self.request + + # Lets test if the cache folder exists, if not + # we are going to create it + folder = os.path.join(folder or request.folder, 'cache') + + if not os.path.exists(folder): + os.mkdir(folder) + + self.storage = CacheOnDisk.PersistentStorage(folder) + + if not CacheAbstract.cache_stats_name in self.storage: + self.storage[CacheAbstract.cache_stats_name] = {'hit_total': 0, 'misses': 0} + + + def __call__(self, key, f, + time_expire=DEFAULT_TIME_EXPIRE): + self.initialize() + + dt = time_expire + item = self.storage.get(key) + self.storage[CacheAbstract.cache_stats_name]['hit_total'] += 1 + + if item and f is None: + del self.storage[key] + + if f is None: + return None + + now = time.time() + + if item and ((dt is None) or (item[0] > now - dt)): + value = item[1] + else: + value = f() + self.storage[key] = (now, value) + self.storage[CacheAbstract.cache_stats_name]['misses'] += 1 + + return value + + + def clear(self, regex=None): + self.initialize() + storage = self.storage + if regex is None: + storage.clear() + else: + self._clear(storage, regex) + + if not CacheAbstract.cache_stats_name in storage: + storage[CacheAbstract.cache_stats_name] = { + 'hit_total': 0, 'misses': 0} + + + def increment(self, key, value=1): + self.initialize() + storage = self.storage + try: + if key in storage: + value = storage[key][1] + value + storage[key] = (time.time(), value) + except: + pass + return value + + + +class CacheAction(object): + def __init__(self, func, key, time_expire, cache, cache_model): + self.__name__ = func.__name__ + self.__doc__ = func.__doc__ + self.func = func + self.key = key + self.time_expire = time_expire + self.cache = cache + self.cache_model = cache_model + + def __call__(self, *a, **b): + if not self.key: + key2 = self.__name__ + ':' + repr(a) + ':' + repr(b) + else: + key2 = self.key.replace('%(name)s', self.__name__)\ + .replace('%(args)s', str(a)).replace('%(vars)s', str(b)) + cache_model = self.cache_model + if not cache_model or isinstance(cache_model, str): + cache_model = getattr(self.cache, cache_model or 'ram') + return cache_model(key2, + lambda a=a, b=b: self.func(*a, **b), + self.time_expire) + + +class Cache(object): + """ + Sets up generic caching, creating an instance of both CacheInRam and + CacheOnDisk. + In case of GAE will make use of gluon.contrib.gae_memcache. + + - self.ram is an instance of CacheInRam + - self.disk is an instance of CacheOnDisk + """ + + autokey = ':%(name)s:%(args)s:%(vars)s' + + def __init__(self, request): + """ + Args: + request: the global request object + """ + # GAE will have a special caching + if have_settings and settings.global_settings.web2py_runtime_gae: + from gluon.contrib.gae_memcache import MemcacheClient + self.ram = self.disk = MemcacheClient(request) + else: + # Otherwise use ram (and try also disk) + self.ram = CacheInRam(request) + try: + self.disk = CacheOnDisk(request) + except IOError: + logger.warning('no cache.disk (IOError)') + except AttributeError: + # normally not expected anymore, as GAE has already + # been accounted for + logger.warning('no cache.disk (AttributeError)') + + def action(self, time_expire=DEFAULT_TIME_EXPIRE, cache_model=None, + prefix=None, session=False, vars=True, lang=True, + user_agent=False, public=True, valid_statuses=None, + quick=None): + """Better fit for caching an action + + Warning: + Experimental! + + Currently only HTTP 1.1 compliant + reference : http://code.google.com/p/doctype-mirror/wiki/ArticleHttpCaching + + Args: + time_expire(int): same as @cache + cache_model(str): same as @cache + prefix(str): add a prefix to the calculated key + session(bool): adds response.session_id to the key + vars(bool): adds request.env.query_string + lang(bool): adds T.accepted_language + user_agent(bool or dict): if True, adds is_mobile and is_tablet to the key. + Pass a dict to use all the needed values (uses str(.items())) + (e.g. user_agent=request.user_agent()). Used only if session is + not True + public(bool): if False forces the Cache-Control to be 'private' + valid_statuses: by default only status codes starting with 1,2,3 will be cached. + pass an explicit list of statuses on which turn the cache on + quick: Session,Vars,Lang,User-agent,Public: + fast overrides with initials, e.g. 'SVLP' or 'VLP', or 'VLP' + """ + from gluon import current + from gluon.http import HTTP + def wrap(func): + def wrapped_f(): + if current.request.env.request_method != 'GET': + return func() + if time_expire: + cache_control = 'max-age=%(time_expire)s, s-maxage=%(time_expire)s' % dict(time_expire=time_expire) + if quick: + session_ = True if 'S' in quick else False + vars_ = True if 'V' in quick else False + lang_ = True if 'L' in quick else False + user_agent_ = True if 'U' in quick else False + public_ = True if 'P' in quick else False + else: + session_, vars_, lang_, user_agent_, public_ = session, vars, lang, user_agent, public + if not session_ and public_: + cache_control += ', public' + expires = (current.request.utcnow + datetime.timedelta(seconds=time_expire)).strftime('%a, %d %b %Y %H:%M:%S GMT') + else: + cache_control += ', private' + expires = 'Fri, 01 Jan 1990 00:00:00 GMT' + if cache_model: + #figure out the correct cache key + cache_key = [current.request.env.path_info, current.response.view] + if session_: + cache_key.append(current.response.session_id) + elif user_agent_: + if user_agent_ is True: + cache_key.append("%(is_mobile)s_%(is_tablet)s" % current.request.user_agent()) + else: + cache_key.append(str(user_agent_.items())) + if vars_: + cache_key.append(current.request.env.query_string) + if lang_: + cache_key.append(current.T.accepted_language) + cache_key = hashlib.md5('__'.join(cache_key)).hexdigest() + if prefix: + cache_key = prefix + cache_key + try: + #action returns something + rtn = cache_model(cache_key, lambda : func(), time_expire=time_expire) + http, status = None, current.response.status + except HTTP, e: + #action raises HTTP (can still be valid) + rtn = cache_model(cache_key, lambda : e.body, time_expire=time_expire) + http, status = HTTP(e.status, rtn, **e.headers), e.status + else: + #action raised a generic exception + http = None + else: + #no server-cache side involved + try: + #action returns something + rtn = func() + http, status = None, current.response.status + except HTTP, e: + #action raises HTTP (can still be valid) + status = e.status + http = HTTP(e.status, e.body, **e.headers) + else: + #action raised a generic exception + http = None + send_headers = False + if http and isinstance(valid_statuses, list): + if status in valid_statuses: + send_headers = True + elif valid_statuses is None: + if str(status)[0] in '123': + send_headers = True + if send_headers: + headers = { + 'Pragma' : None, + 'Expires' : expires, + 'Cache-Control' : cache_control + } + current.response.headers.update(headers) + if cache_model and not send_headers: + #we cached already the value, but the status is not valid + #so we need to delete the cached value + cache_model(cache_key, None) + if http: + if send_headers: + http.headers.update(current.response.headers) + raise http + return rtn + wrapped_f.__name__ = func.__name__ + wrapped_f.__doc__ = func.__doc__ + return wrapped_f + return wrap + + def __call__(self, + key=None, + time_expire=DEFAULT_TIME_EXPIRE, + cache_model=None): + """ + Decorator function that can be used to cache any function/method. + + Args: + key(str) : the key of the object to be store or retrieved + time_expire(int) : expiration of the cache in seconds + `time_expire` is used to compare the current time with the time + when the requested object was last saved in cache. + It does not affect future requests. + Setting `time_expire` to 0 or negative value forces the cache to + refresh. + cache_model(str): can be "ram", "disk" or other (like "memcache"). + Defaults to "ram" + + When the function `f` is called, web2py tries to retrieve + the value corresponding to `key` from the cache if the + object exists and if it did not expire, else it calles the function `f` + and stores the output in the cache corresponding to `key`. In the case + the output of the function is returned. + + Example: :: + + @cache('key', 5000, cache.ram) + def f(): + return time.ctime() + + Note: + If the function `f` is an action, we suggest using + @cache.action instead + """ + + def tmp(func, cache=self, cache_model=cache_model): + return CacheAction(func, key, time_expire, self, cache_model) + return tmp + + @staticmethod + def with_prefix(cache_model, prefix): + """ + allow replacing cache.ram with cache.with_prefix(cache.ram,'prefix') + it will add prefix to all the cache keys used. + """ + return lambda key, f, time_expire=DEFAULT_TIME_EXPIRE, prefix=prefix:\ + cache_model(prefix + key, f, time_expire) + + +def lazy_cache(key=None, time_expire=None, cache_model='ram'): + """ + Can be used to cache any function including ones in modules, + as long as the cached function is only called within a web2py request + + If a key is not provided, one is generated from the function name + `time_expire` defaults to None (no cache expiration) + + If cache_model is "ram" then the model is current.cache.ram, etc. + """ + def decorator(f, key=key, time_expire=time_expire, cache_model=cache_model): + key = key or repr(f) + + def g(*c, **d): + from gluon import current + return current.cache(key, time_expire, cache_model)(f)(*c, **d) + g.__name__ = f.__name__ + return g + return decorator diff --git a/gluon/compileapp.py b/gluon/compileapp.py index 9ada3cfb..d04fd0d6 100644 --- a/gluon/compileapp.py +++ b/gluon/compileapp.py @@ -407,7 +407,7 @@ def build_environment(request, response, session, store_current=True): # Enable standard conditional models (i.e., /*.py, /[controller]/*.py, and # /[controller]/[function]/*.py) response.models_to_run = [ - r'^\w+\.py$', + r'^\w+\.py$', r'^%s/\w+\.py$' % request.controller, r'^%s/%s/\w+\.py$' % (request.controller, request.function) ] @@ -514,7 +514,7 @@ def compile_controllers(folder): for function in exposed: command = data + "\nresponse._vars=response._caller(%s)\n" % \ function - filename = pjoin(folder, 'compiled', + filename = pjoin(folder, 'compiled', 'controllers.%s.%s.py' % (fname[:-3],function)) write_file(filename, command) save_pyc(filename) diff --git a/gluon/contrib/hypermedia.py b/gluon/contrib/hypermedia.py index 50494516..86926e6f 100644 --- a/gluon/contrib/hypermedia.py +++ b/gluon/contrib/hypermedia.py @@ -47,7 +47,7 @@ class Collection(object): if self.compact: for fieldname in (self.table_policy.get('fields',table.fields)): field = table[fieldname] - if not ((field.type=='text' and text==False) or + if not ((field.type=='text' and text==False) or field.type=='blob' or field.type.startswith('reference ') or field.type.startswith('list:reference ')) and field.name in row: @@ -56,7 +56,7 @@ class Collection(object): for fieldname in (self.table_policy.get('fields',table.fields)): field = table[fieldname] if not ((field.type=='text' and text==False) or - field.type=='blob' or + field.type=='blob' or field.type.startswith('reference ') or field.type.startswith('list:reference ')) and field.name in row: data.append({'name':field.name,'value':row[field.name], @@ -128,10 +128,10 @@ class Collection(object): for key,value in vars.items(): if key=='_offset': limitby[0] = int(value) # MAY FAIL - elif key == '_limit': + elif key == '_limit': limitby[1] = int(value)+1 # MAY FAIL elif key=='_orderby': - orderby = value + orderby = value elif key in fieldnames: queries.append(table[key] == value) elif key.endswith('.eq') and key[:-3] in fieldnames: # for completeness (useless) @@ -156,14 +156,14 @@ class Collection(object): if filter_query: queries.append(filter_query) query = reduce(lambda a,b:a&b,queries[1:]) if len(queries)>1 else queries[0] - orderby = [table[f] if f[0]!='~' else ~table[f[1:]] for f in orderby.split(',')] + orderby = [table[f] if f[0]!='~' else ~table[f[1:]] for f in orderby.split(',')] return (query, limitby, orderby) def table2queries(self,table, href): """ generates a set of collection.queries examples for the table """ data = [] for fieldname in (self.table_policy.get('fields', table.fields)): - data.append({'name':fieldname,'value':''}) + data.append({'name':fieldname,'value':''}) if self.extensions: data.append({'name':fieldname+'.ne','value':''}) # NEW !!! data.append({'name':fieldname+'.lt','value':''}) @@ -192,7 +192,7 @@ class Collection(object): if not tablename: r['href'] = URL(scheme=True), # https://github.com/collection-json/extensions/blob/master/model.md - r['links'] = [{'rel' : t, 'href' : URL(args=t,scheme=True), 'model':t} + r['links'] = [{'rel' : t, 'href' : URL(args=t,scheme=True), 'model':t} for t in tablenames] response.headers['Content-Type'] = 'application/vnd.collection+json' return response.json({'collection':r}) @@ -207,7 +207,7 @@ class Collection(object): # process GET if request.env.request_method=='GET': table = db[tablename] - r['href'] = URL(args=tablename) + r['href'] = URL(args=tablename) r['items'] = items = [] try: (query, limitby, orderby) = self.request2query(table,request.get_vars) @@ -258,7 +258,7 @@ class Collection(object): return response.json({'collection':r}) # process DELETE elif request.env.request_method=='DELETE': - table = db[tablename] + table = db[tablename] if not request.get_vars: return self.error(400, "BAD REQUEST", "Nothing to delete") else: @@ -312,7 +312,7 @@ class Collection(object): request, response = self.request, self.response r = OrderedDict({ "version" : self.VERSION, - "href" : URL(args=request.args,vars=request.vars), + "href" : URL(args=request.args,vars=request.vars), "error" : { "title" : title, "code" : code, @@ -340,4 +340,3 @@ example_policies = { 'DELETE':{'query':None}, }, } - diff --git a/gluon/contrib/memdb.py b/gluon/contrib/memdb.py index 6ad9b159..e4b56ef7 100644 --- a/gluon/contrib/memdb.py +++ b/gluon/contrib/memdb.py @@ -254,12 +254,12 @@ class Table(DALStorage): self._db(self.id > 0).delete() - def insert(self, **fields): - # Checks 3 times that the id is new. 3 times is enough! - for i in range(3): - id = self._create_id() - if self.get(id) is None and self.update(id, **fields): - return long(id) + def insert(self, **fields): + # Checks 3 times that the id is new. 3 times is enough! + for i in range(3): + id = self._create_id() + if self.get(id) is None and self.update(id, **fields): + return long(id) else: raise RuntimeError("Too many ID conflicts") diff --git a/gluon/contrib/mockimaplib.py b/gluon/contrib/mockimaplib.py index 87f8b479..89975900 100644 --- a/gluon/contrib/mockimaplib.py +++ b/gluon/contrib/mockimaplib.py @@ -141,7 +141,7 @@ class Connection(object): parts = "complete" return ("OK", (("%s " % message_id, message[parts]), message["flags"])) - + def _get_messages(self, query): if query.strip().isdigit(): return [self.spam[self._mailbox][int(query.strip()) - 1],] @@ -151,7 +151,7 @@ class Connection(object): for item in self.spam[self._mailbox]: if item["uid"] == query[1:-1].replace("UID", "").strip(): return [item,] - messages = [] + messages = [] try: for m in self.results[self._mailbox][query]: try: @@ -169,7 +169,7 @@ class Connection(object): return messages except KeyError: raise ValueError("The client issued an unexpected query: %s" % query) - + def setup(self, spam={}, results={}): """adds custom message and query databases or sets the values to the module defaults. @@ -252,4 +252,3 @@ class IMAP4(object): return Connection() IMAP4_SSL = IMAP4 - diff --git a/gluon/contrib/pbkdf2_ctypes.py b/gluon/contrib/pbkdf2_ctypes.py index 685ab5d1..6aa62343 100644 --- a/gluon/contrib/pbkdf2_ctypes.py +++ b/gluon/contrib/pbkdf2_ctypes.py @@ -11,7 +11,7 @@ Note: This module is intended as a plugin replacement of pbkdf2.py by Armin Ronacher. - Git repository: + Git repository: $ git clone https://github.com/michele-comitini/pbkdf2_ctypes.git :copyright: Copyright (c) 2013: Michele Comitini @@ -86,7 +86,7 @@ def _openssl_hashlib_to_crypto_map_get(hashfunc): crypto_hashfunc.restype = ctypes.c_void_p return crypto_hashfunc() - + def _openssl_pbkdf2(data, salt, iterations, digest, keylen): """OpenSSL compatibile wrapper """ @@ -99,7 +99,7 @@ def _openssl_pbkdf2(data, salt, iterations, digest, keylen): c_iter = ctypes.c_int(iterations) c_keylen = ctypes.c_int(keylen) c_buff = ctypes.create_string_buffer(keylen) - + # PKCS5_PBKDF2_HMAC(const char *pass, int passlen, # const unsigned char *salt, int saltlen, int iter, # const EVP_MD *digest, @@ -109,7 +109,7 @@ def _openssl_pbkdf2(data, salt, iterations, digest, keylen): ctypes.c_char_p, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_char_p] - + crypto.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int err = crypto.PKCS5_PBKDF2_HMAC(c_pass, c_passlen, c_salt, c_saltlen, diff --git a/gluon/contrib/pypyodbc.py b/gluon/contrib/pypyodbc.py index 7d359ccb..bb2a9ceb 100644 --- a/gluon/contrib/pypyodbc.py +++ b/gluon/contrib/pypyodbc.py @@ -7,18 +7,18 @@ # Copyright (c) 2014 Henry Zhou and PyPyODBC contributors # Copyright (c) 2004 Michele Petrazzo -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated # documentation files (the "Software"), to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # -# The above copyright notice and this permission notice shall be included in all copies or substantial portions +# The above copyright notice and this permission notice shall be included in all copies or substantial portions # of the Software. # -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO -# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF -# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. pooling = True @@ -52,7 +52,7 @@ else: use_unicode = False if py_ver < '2.6': bytearray = str - + if not hasattr(ctypes, 'c_ssize_t'): if ctypes.sizeof(ctypes.c_uint) == ctypes.sizeof(ctypes.c_void_p): @@ -69,7 +69,7 @@ SQLWCHAR_SIZE = ctypes.sizeof(ctypes.c_wchar) #determin the size of Py_UNICODE #sys.maxunicode > 65536 and 'UCS4' or 'UCS2' -UNICODE_SIZE = sys.maxunicode > 65536 and 4 or 2 +UNICODE_SIZE = sys.maxunicode > 65536 and 4 or 2 # Define ODBC constants. They are widly used in ODBC documents and programs @@ -402,15 +402,15 @@ class OperationalError(DatabaseError): self.value = (error_code, error_desc) self.args = (error_code, error_desc) - - - -############################################################################ + + + +############################################################################ # # Find the ODBC library on the platform and connect to it using ctypes # ############################################################################ -# Get the References of the platform's ODBC functions via ctypes +# Get the References of the platform's ODBC functions via ctypes odbc_decoding = 'utf_16' odbc_encoding = 'utf_16_le' @@ -421,7 +421,7 @@ if sys.platform in ('win32','cli'): # On Windows, the size of SQLWCHAR is hardcoded to 2-bytes. SQLWCHAR_SIZE = ctypes.sizeof(ctypes.c_ushort) else: - # Set load the library on linux + # Set load the library on linux try: # First try direct loading libodbc.so ODBC_API = ctypes.cdll.LoadLibrary('libodbc.so') @@ -446,7 +446,7 @@ else: except: # If still fail loading, abort. raise OdbcLibraryError('Error while loading ' + library) - + # only iODBC uses utf-32 / UCS4 encoding data, others normally use utf-16 / UCS2 # So we set those for handling. if 'libiodbc.dylib' in library: @@ -506,17 +506,17 @@ if sys.platform not in ('win32','cli'): raise OdbcLibraryError('Using narrow Python build with ODBC library ' 'expecting wide unicode is not supported.') - - - - - - - - - - - + + + + + + + + + + + ############################################################ # Database value to Python data type mappings @@ -551,11 +551,11 @@ SQL_C_DOUBLE = SQL_DOUBLE = 8 SQL_C_TYPE_DATE = SQL_TYPE_DATE = 91 SQL_C_TYPE_TIME = SQL_TYPE_TIME = 92 SQL_C_BINARY = SQL_BINARY = -2 -SQL_C_SBIGINT = SQL_BIGINT + SQL_SIGNED_OFFSET +SQL_C_SBIGINT = SQL_BIGINT + SQL_SIGNED_OFFSET SQL_C_TINYINT = SQL_TINYINT = -6 SQL_C_BIT = SQL_BIT = -7 SQL_C_WCHAR = SQL_WCHAR = -8 -SQL_C_GUID = SQL_GUID = -11 +SQL_C_GUID = SQL_GUID = -11 SQL_C_TYPE_TIMESTAMP = SQL_TYPE_TIMESTAMP = 93 SQL_C_DEFAULT = 99 @@ -581,19 +581,19 @@ def dt_cvt(x): def Decimal_cvt(x): if py_v3: - x = x.decode('ascii') + x = x.decode('ascii') return Decimal(x) - + bytearray_cvt = bytearray if sys.platform == 'cli': bytearray_cvt = lambda x: bytearray(buffer(x)) - + # Below Datatype mappings referenced the document at # http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sdk_12.5.1.aseodbc/html/aseodbc/CACFDIGH.htm SQL_data_type_dict = { \ #SQL Data TYPE 0.Python Data Type 1.Default Output Converter 2.Buffer Type 3.Buffer Allocator 4.Default Size 5.Variable Length -SQL_TYPE_NULL : (None, lambda x: None, SQL_C_CHAR, create_buffer, 2 , False ), +SQL_TYPE_NULL : (None, lambda x: None, SQL_C_CHAR, create_buffer, 2 , False ), SQL_CHAR : (str, lambda x: x, SQL_C_CHAR, create_buffer, 2048 , False ), SQL_NUMERIC : (Decimal, Decimal_cvt, SQL_C_CHAR, create_buffer, 150 , False ), SQL_DECIMAL : (Decimal, Decimal_cvt, SQL_C_CHAR, create_buffer, 150 , False ), @@ -620,8 +620,8 @@ SQL_GUID : (str, str, SQL_C_CH SQL_WLONGVARCHAR : (unicode, lambda x: x, SQL_C_WCHAR, create_buffer_u, 20500 , True ), SQL_TYPE_DATE : (datetime.date, dt_cvt, SQL_C_CHAR, create_buffer, 30 , False ), SQL_TYPE_TIME : (datetime.time, tm_cvt, SQL_C_CHAR, create_buffer, 20 , False ), -SQL_TYPE_TIMESTAMP : (datetime.datetime, dttm_cvt, SQL_C_CHAR, create_buffer, 30 , False ), -SQL_SS_VARIANT : (str, lambda x: x, SQL_C_CHAR, create_buffer, 2048 , True ), +SQL_TYPE_TIMESTAMP : (datetime.datetime, dttm_cvt, SQL_C_CHAR, create_buffer, 30 , False ), +SQL_SS_VARIANT : (str, lambda x: x, SQL_C_CHAR, create_buffer, 2048 , True ), SQL_SS_XML : (unicode, lambda x: x, SQL_C_WCHAR, create_buffer_u, 20500 , True ), SQL_SS_UDT : (bytearray, bytearray_cvt, SQL_C_BINARY, create_buffer, 5120 , True ), } @@ -744,7 +744,7 @@ if sys.platform not in ('cli'): ODBC_API.SQLDrivers.argtypes = [ ctypes.c_void_p, ctypes.c_ushort, - ctypes.c_char_p, ctypes.c_short, ctypes.POINTER(ctypes.c_short), + ctypes.c_char_p, ctypes.c_short, ctypes.POINTER(ctypes.c_short), ctypes.c_char_p, ctypes.c_short, ctypes.POINTER(ctypes.c_short), ] @@ -920,7 +920,7 @@ BLANK_BYTE = str_8b() def ctrl_err(ht, h, val_ret, ansi): """Classify type of ODBC error from (type of handle, handle, return value) , and raise with a list""" - + if ansi: state = create_buffer(22) Message = create_buffer(1024*4) @@ -938,7 +938,7 @@ def ctrl_err(ht, h, val_ret, ansi): Buffer_len = c_short() err_list = [] number_errors = 1 - + while 1: ret = ODBC_func(ht, h, number_errors, state, \ ADDR(NativeError), Message, 1024, ADDR(Buffer_len)) @@ -985,19 +985,19 @@ def check_success(ODBC_obj, ret): ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi) else: ctrl_err(SQL_HANDLE_ENV, ODBC_obj, ret, False) - - + + def AllocateEnv(): if pooling: ret = ODBC_API.SQLSetEnvAttr(SQL_NULL_HANDLE, SQL_ATTR_CONNECTION_POOLING, SQL_CP_ONE_PER_HENV, SQL_IS_UINTEGER) check_success(SQL_NULL_HANDLE, ret) - ''' + ''' Allocate an ODBC environment by initializing the handle shared_env_h ODBC enviroment needed to be created, so connections can be created under it connections pooling can be shared under one environment ''' - global shared_env_h + global shared_env_h shared_env_h = ctypes.c_void_p() ret = ODBC_API.SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, ADDR(shared_env_h)) check_success(shared_env_h, ret) @@ -1005,7 +1005,7 @@ def AllocateEnv(): # Set the ODBC environment's compatibil leve to ODBC 3.0 ret = ODBC_API.SQLSetEnvAttr(shared_env_h, SQL_ATTR_ODBC_VERSION, SQL_OV_ODBC3, 0) check_success(shared_env_h, ret) - + """ Here, we have a few callables that determine how a result row is returned. @@ -1022,20 +1022,20 @@ def TupleRow(cursor): """ class Row(tuple): cursor_description = cursor.description - + def get(self, field): if not hasattr(self, 'field_dict'): self.field_dict = {} for i,item in enumerate(self): self.field_dict[self.cursor_description[i][0]] = item return self.field_dict.get(field) - + def __getitem__(self, field): if isinstance(field, (unicode,str)): return self.get(field) else: return tuple.__getitem__(self,field) - + return Row @@ -1088,19 +1088,19 @@ def MutableNamedTupleRow(cursor): return Row # When Null is used in a binary parameter, database usually would not -# accept the None for a binary field, so the work around is to use a +# accept the None for a binary field, so the work around is to use a # Specical None that the pypyodbc moudle would know this NULL is for # a binary field. class BinaryNullType(): pass BinaryNull = BinaryNullType() -# The get_type function is used to determine if parameters need to be re-binded +# The get_type function is used to determine if parameters need to be re-binded # against the changed parameter types # 'b' for bool, 'U' for long unicode string, 'u' for short unicode string # 'S' for long 8 bit string, 's' for short 8 bit string, 'l' for big integer, 'i' for normal integer # 'f' for float, 'D' for Decimal, 't' for datetime.time, 'd' for datetime.datetime, 'dt' for datetime.datetime # 'bi' for binary -def get_type(v): +def get_type(v): if isinstance(v, bool): return ('b',) @@ -1130,7 +1130,7 @@ def get_type(v): t = v.as_tuple() #1.23 -> (1,2,3),-2 , 1.23*E7 -> (1,2,3),5 return ('D',(len(t[1]),0 - t[2])) # number of digits, and number of decimal digits - + elif isinstance (v, datetime.datetime): return ('dt',) elif isinstance (v, datetime.date): @@ -1139,7 +1139,7 @@ def get_type(v): return ('t',) elif isinstance (v, (bytearray, buffer)): return ('bi',(len(v)//1000+1)*1000) - + return type(v) @@ -1169,16 +1169,16 @@ class Cursor: ret = ODBC_API.SQLAllocHandle(SQL_HANDLE_STMT, self.connection.dbc_h, ADDR(self.stmt_h)) check_success(self, ret) self._PARAM_SQL_TYPE_LIST = [] - self.closed = False + self.closed = False + - def prepare(self, query_string): """prepare a query""" - + #self._free_results(FREE_STATEMENT) if not self.connection: self.close() - + if type(query_string) == unicode: c_query_string = wchar_pointer(UCS_buf(query_string)) ret = ODBC_API.SQLPrepareW(self.stmt_h, c_query_string, len(query_string)) @@ -1187,10 +1187,10 @@ class Cursor: ret = ODBC_API.SQLPrepare(self.stmt_h, c_query_string, len(query_string)) if ret != SQL_SUCCESS: check_success(self, ret) - - - self._PARAM_SQL_TYPE_LIST = [] - + + + self._PARAM_SQL_TYPE_LIST = [] + if self.connection.support_SQLDescribeParam: # SQLServer's SQLDescribeParam only supports DML SQL, so avoid the SELECT statement if True:# 'SELECT' not in query_string.upper(): @@ -1199,7 +1199,7 @@ class Cursor: ret = ODBC_API.SQLNumParams(self.stmt_h, ADDR(NumParams)) if ret != SQL_SUCCESS: check_success(self, ret) - + for col_num in range(NumParams.value): ParameterNumber = ctypes.c_ushort(col_num + 1) DataType = c_short() @@ -1219,7 +1219,7 @@ class Cursor: check_success(self, ret) except DatabaseError: if sys.exc_info()[1].value[0] == '07009': - self._PARAM_SQL_TYPE_LIST = [] + self._PARAM_SQL_TYPE_LIST = [] break else: raise sys.exc_info()[1] @@ -1227,7 +1227,7 @@ class Cursor: raise sys.exc_info()[1] self._PARAM_SQL_TYPE_LIST.append((DataType.value,DecimalDigits.value)) - + self.statement = query_string @@ -1237,39 +1237,39 @@ class Cursor: if not self.connection: self.close() #self._free_results(NO_FREE_STATEMENT) - + # Get the number of query parameters judged by database. NumParams = c_short() ret = ODBC_API.SQLNumParams(self.stmt_h, ADDR(NumParams)) if ret != SQL_SUCCESS: check_success(self, ret) - + if len(param_types) != NumParams.value: # In case number of parameters provided do not same as number required error_desc = "The SQL contains %d parameter markers, but %d parameters were supplied" \ %(NumParams.value,len(param_types)) raise ProgrammingError('HY000',error_desc) - - + + # Every parameter needs to be binded to a buffer ParamBufferList = [] # Temporary holder since we can only call SQLDescribeParam before # calling SQLBindParam. temp_holder = [] for col_num in range(NumParams.value): - dec_num = 0 + dec_num = 0 buf_size = 512 - + if param_types[col_num][0] == 'u': sql_c_type = SQL_C_WCHAR - sql_type = SQL_WVARCHAR - buf_size = 255 - ParameterBuffer = create_buffer_u(buf_size) - + sql_type = SQL_WVARCHAR + buf_size = 255 + ParameterBuffer = create_buffer_u(buf_size) + elif param_types[col_num][0] == 's': sql_c_type = SQL_C_CHAR sql_type = SQL_VARCHAR - buf_size = 255 + buf_size = 255 ParameterBuffer = create_buffer(buf_size) @@ -1284,26 +1284,26 @@ class Cursor: sql_type = SQL_LONGVARCHAR buf_size = param_types[col_num][1]#len(self._inputsizers)>col_num and self._inputsizers[col_num] or 20500 ParameterBuffer = create_buffer(buf_size) - + # bool subclasses int, thus has to go first elif param_types[col_num][0] == 'b': sql_c_type = SQL_C_CHAR sql_type = SQL_BIT buf_size = SQL_data_type_dict[sql_type][4] ParameterBuffer = create_buffer(buf_size) - + elif param_types[col_num][0] == 'i': - sql_c_type = SQL_C_CHAR - sql_type = SQL_INTEGER - buf_size = SQL_data_type_dict[sql_type][4] - ParameterBuffer = create_buffer(buf_size) - - elif param_types[col_num][0] == 'l': - sql_c_type = SQL_C_CHAR - sql_type = SQL_BIGINT - buf_size = SQL_data_type_dict[sql_type][4] + sql_c_type = SQL_C_CHAR + sql_type = SQL_INTEGER + buf_size = SQL_data_type_dict[sql_type][4] ParameterBuffer = create_buffer(buf_size) - + + elif param_types[col_num][0] == 'l': + sql_c_type = SQL_C_CHAR + sql_type = SQL_BIGINT + buf_size = SQL_data_type_dict[sql_type][4] + ParameterBuffer = create_buffer(buf_size) + elif param_types[col_num][0] == 'D': #Decimal sql_c_type = SQL_C_CHAR @@ -1311,56 +1311,56 @@ class Cursor: digit_num, dec_num = param_types[col_num][1] if dec_num > 0: # has decimal - buf_size = digit_num + buf_size = digit_num dec_num = dec_num else: # no decimal - buf_size = digit_num - dec_num + buf_size = digit_num - dec_num dec_num = 0 ParameterBuffer = create_buffer(buf_size + 4)# add extra length for sign and dot - + elif param_types[col_num][0] == 'f': sql_c_type = SQL_C_CHAR - sql_type = SQL_DOUBLE + sql_type = SQL_DOUBLE buf_size = SQL_data_type_dict[sql_type][4] ParameterBuffer = create_buffer(buf_size) - - + + # datetime subclasses date, thus has to go first elif param_types[col_num][0] == 'dt': sql_c_type = SQL_C_CHAR sql_type = SQL_TYPE_TIMESTAMP - buf_size = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][0] + buf_size = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][0] ParameterBuffer = create_buffer(buf_size) dec_num = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][1] - - + + elif param_types[col_num][0] == 'd': sql_c_type = SQL_C_CHAR if SQL_TYPE_DATE in self.connection.type_size_dic: #if DEBUG:print('conx.type_size_dic.has_key(SQL_TYPE_DATE)') sql_type = SQL_TYPE_DATE buf_size = self.connection.type_size_dic[SQL_TYPE_DATE][0] - + ParameterBuffer = create_buffer(buf_size) dec_num = self.connection.type_size_dic[SQL_TYPE_DATE][1] - + else: # SQL Sever <2008 doesn't have a DATE type. - sql_type = SQL_TYPE_TIMESTAMP - buf_size = 10 + sql_type = SQL_TYPE_TIMESTAMP + buf_size = 10 ParameterBuffer = create_buffer(buf_size) - - + + elif param_types[col_num][0] == 't': sql_c_type = SQL_C_CHAR if SQL_TYPE_TIME in self.connection.type_size_dic: sql_type = SQL_TYPE_TIME - buf_size = self.connection.type_size_dic[SQL_TYPE_TIME][0] + buf_size = self.connection.type_size_dic[SQL_TYPE_TIME][0] ParameterBuffer = create_buffer(buf_size) - dec_num = self.connection.type_size_dic[SQL_TYPE_TIME][1] + dec_num = self.connection.type_size_dic[SQL_TYPE_TIME][1] elif SQL_SS_TIME2 in self.connection.type_size_dic: # TIME type added in SQL Server 2008 sql_type = SQL_SS_TIME2 @@ -1370,16 +1370,16 @@ class Cursor: else: # SQL Sever <2008 doesn't have a TIME type. sql_type = SQL_TYPE_TIMESTAMP - buf_size = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][0] + buf_size = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][0] ParameterBuffer = create_buffer(buf_size) dec_num = 3 - + elif param_types[col_num][0] == 'BN': sql_c_type = SQL_C_BINARY - sql_type = SQL_VARBINARY - buf_size = 1 - ParameterBuffer = create_buffer(buf_size) - + sql_type = SQL_VARBINARY + buf_size = 1 + ParameterBuffer = create_buffer(buf_size) + elif param_types[col_num][0] == 'N': if len(self._PARAM_SQL_TYPE_LIST) > 0: sql_c_type = SQL_C_DEFAULT @@ -1389,64 +1389,64 @@ class Cursor: else: sql_c_type = SQL_C_CHAR sql_type = SQL_CHAR - buf_size = 1 - ParameterBuffer = create_buffer(buf_size) + buf_size = 1 + ParameterBuffer = create_buffer(buf_size) elif param_types[col_num][0] == 'bi': sql_c_type = SQL_C_BINARY - sql_type = SQL_LONGVARBINARY - buf_size = param_types[col_num][1]#len(self._inputsizers)>col_num and self._inputsizers[col_num] or 20500 + sql_type = SQL_LONGVARBINARY + buf_size = param_types[col_num][1]#len(self._inputsizers)>col_num and self._inputsizers[col_num] or 20500 ParameterBuffer = create_buffer(buf_size) - - + + else: sql_c_type = SQL_C_CHAR sql_type = SQL_LONGVARCHAR - buf_size = len(self._inputsizers)>col_num and self._inputsizers[col_num] or 20500 + buf_size = len(self._inputsizers)>col_num and self._inputsizers[col_num] or 20500 ParameterBuffer = create_buffer(buf_size) - + temp_holder.append((sql_c_type, sql_type, buf_size, dec_num, ParameterBuffer)) for col_num, (sql_c_type, sql_type, buf_size, dec_num, ParameterBuffer) in enumerate(temp_holder): BufferLen = c_ssize_t(buf_size) LenOrIndBuf = c_ssize_t() - - + + InputOutputType = SQL_PARAM_INPUT if len(pram_io_list) > col_num: InputOutputType = pram_io_list[col_num] ret = SQLBindParameter(self.stmt_h, col_num + 1, InputOutputType, sql_c_type, sql_type, buf_size,\ dec_num, ADDR(ParameterBuffer), BufferLen,ADDR(LenOrIndBuf)) - if ret != SQL_SUCCESS: + if ret != SQL_SUCCESS: check_success(self, ret) # Append the value buffer and the length buffer to the array ParamBufferList.append((ParameterBuffer,LenOrIndBuf,sql_type)) - + self._last_param_types = param_types self._ParamBufferList = ParamBufferList - + def execute(self, query_string, params=None, many_mode=False, call_mode=False): """ Execute the query string, with optional parameters. If parameters are provided, the query would first be prepared, then executed with parameters; - If parameters are not provided, only th query sting, it would be executed directly + If parameters are not provided, only th query sting, it would be executed directly """ if not self.connection: self.close() - + self._free_stmt(SQL_CLOSE) if params: # If parameters exist, first prepare the query then executed with parameters - + if not isinstance(params, (tuple, list)): raise TypeError("Params must be in a list, tuple, or Row") - + if query_string != self.statement: - # if the query is not same as last query, then it is not prepared + # if the query is not same as last query, then it is not prepared self.prepare(query_string) - - + + param_types = list(map(get_type, params)) if call_mode: @@ -1462,8 +1462,8 @@ class Cursor: elif sum([p_type[0] != 'N' and p_type != self._last_param_types[i] for i,p_type in enumerate(param_types)]) > 0: self._free_stmt(SQL_RESET_PARAMS) self._BindParams(param_types) - - + + # With query prepared, now put parameters into buffers col_num = 0 for param_buffer, param_buffer_len, sql_type in self._ParamBufferList: @@ -1479,24 +1479,24 @@ class Cursor: else: c_char_buf = str(param_val) c_buf_len = len(c_char_buf) - + elif param_types[col_num][0] in ('s','S'): c_char_buf = param_val c_buf_len = len(c_char_buf) elif param_types[col_num][0] in ('u','U'): c_char_buf = UCS_buf(param_val) c_buf_len = len(c_char_buf) - + elif param_types[col_num][0] == 'dt': max_len = self.connection.type_size_dic[SQL_TYPE_TIMESTAMP][0] datetime_str = param_val.strftime('%Y-%m-%d %H:%M:%S.%f') c_char_buf = datetime_str[:max_len] if py_v3: c_char_buf = bytes(c_char_buf,'ascii') - + c_buf_len = len(c_char_buf) # print c_buf_len, c_char_buf - + elif param_types[col_num][0] == 'd': if SQL_TYPE_DATE in self.connection.type_size_dic: max_len = self.connection.type_size_dic[SQL_TYPE_DATE][0] @@ -1507,7 +1507,7 @@ class Cursor: c_char_buf = bytes(c_char_buf,'ascii') c_buf_len = len(c_char_buf) #print c_char_buf - + elif param_types[col_num][0] == 't': if SQL_TYPE_TIME in self.connection.type_size_dic: max_len = self.connection.type_size_dic[SQL_TYPE_TIME][0] @@ -1526,7 +1526,7 @@ class Cursor: if py_v3: c_char_buf = bytes(c_char_buf,'ascii') #print c_buf_len, c_char_buf - + elif param_types[col_num][0] == 'b': if param_val == True: c_char_buf = '1' @@ -1535,7 +1535,7 @@ class Cursor: if py_v3: c_char_buf = bytes(c_char_buf,'ascii') c_buf_len = 1 - + elif param_types[col_num][0] == 'D': #Decimal sign = param_val.as_tuple()[0] == 0 and '+' or '-' digit_string = ''.join([str(x) for x in param_val.as_tuple()[1]]) @@ -1555,18 +1555,18 @@ class Cursor: else: c_char_buf = v c_buf_len = len(c_char_buf) - + elif param_types[col_num][0] == 'bi': c_char_buf = str_8b(param_val) c_buf_len = len(c_char_buf) - + else: c_char_buf = param_val - - + + if param_types[col_num][0] == 'bi': param_buffer.raw = str_8b(param_val) - + else: #print (type(param_val),param_buffer, param_buffer.value) param_buffer.value = c_char_buf @@ -1576,37 +1576,37 @@ class Cursor: param_buffer_len.value = SQL_NTS else: param_buffer_len.value = c_buf_len - + col_num += 1 ret = SQLExecute(self.stmt_h) if ret != SQL_SUCCESS: #print param_valparam_buffer, param_buffer.value check_success(self, ret) - + if not many_mode: self._NumOfRows() self._UpdateDesc() #self._BindCols() - + else: self.execdirect(query_string) return self - - + + def _SQLExecute(self): if not self.connection: self.close() ret = SQLExecute(self.stmt_h) if ret != SQL_SUCCESS: - check_success(self, ret) - - + check_success(self, ret) + + def execdirect(self, query_string): """Execute a query directly""" if not self.connection: self.close() - + self._free_stmt() self._last_param_types = None self.statement = None @@ -1621,25 +1621,25 @@ class Cursor: self._UpdateDesc() #self._BindCols() return self - - + + def callproc(self, procname, args): if not self.connection: self.close() raise Warning('', 'Still not fully implemented') self._pram_io_list = [row[4] for row in self.procedurecolumns(procedure = procname).fetchall() if row[4] not in (SQL_RESULT_COL, SQL_RETURN_VALUE)] - + print('pram_io_list: '+str(self._pram_io_list)) - - + + call_escape = '{CALL '+procname if args: call_escape += '(' + ','.join(['?' for params in args]) + ')' call_escape += '}' self.execute(call_escape, args, call_mode = True) - + result = [] for buf, buf_len, sql_type in self._ParamBufferList: @@ -1649,20 +1649,20 @@ class Cursor: result.append(self.connection.output_converter[sql_type](buf.value)) return result - - + + def executemany(self, query_string, params_list = [None]): if not self.connection: self.close() - + for params in params_list: self.execute(query_string, params, many_mode = True) self._NumOfRows() self.rowcount = -1 self._UpdateDesc() #self._BindCols() - - + + def _CreateColBuf(self): if not self.connection: @@ -1672,60 +1672,60 @@ class Cursor: self._ColBufferList = [] bind_data = True for col_num in range(NOC): - col_name = self.description[col_num][0] - col_size = self.description[col_num][2] - col_sql_data_type = self._ColTypeCodeList[col_num] + col_name = self.description[col_num][0] + col_size = self.description[col_num][2] + col_sql_data_type = self._ColTypeCodeList[col_num] target_type = SQL_data_type_dict[col_sql_data_type][2] - dynamic_length = SQL_data_type_dict[col_sql_data_type][5] + dynamic_length = SQL_data_type_dict[col_sql_data_type][5] # set default size base on the column's sql data type - total_buf_len = SQL_data_type_dict[col_sql_data_type][4] - + total_buf_len = SQL_data_type_dict[col_sql_data_type][4] + # over-write if there's pre-set size value for "large columns" - if total_buf_len > 20500: + if total_buf_len > 20500: total_buf_len = self._outputsize.get(None,total_buf_len) - # over-write if there's pre-set size value for the "col_num" column + # over-write if there's pre-set size value for the "col_num" column total_buf_len = self._outputsize.get(col_num, total_buf_len) # if the size of the buffer is very long, do not bind - # because a large buffer decrease performance, and sometimes you only get a NULL value. + # because a large buffer decrease performance, and sometimes you only get a NULL value. # in that case use sqlgetdata instead. if col_size >= 1024: - dynamic_length = True + dynamic_length = True alloc_buffer = SQL_data_type_dict[col_sql_data_type][3](total_buf_len) used_buf_len = c_ssize_t() - + force_unicode = self.connection.unicode_results - + if force_unicode and col_sql_data_type in (SQL_CHAR,SQL_VARCHAR,SQL_LONGVARCHAR): target_type = SQL_C_WCHAR alloc_buffer = create_buffer_u(total_buf_len) - + buf_cvt_func = self.connection.output_converter[self._ColTypeCodeList[col_num]] - + if bind_data: if dynamic_length: bind_data = False - self._ColBufferList.append([col_name, target_type, used_buf_len, ADDR(used_buf_len), alloc_buffer, ADDR(alloc_buffer), total_buf_len, buf_cvt_func, bind_data]) - + self._ColBufferList.append([col_name, target_type, used_buf_len, ADDR(used_buf_len), alloc_buffer, ADDR(alloc_buffer), total_buf_len, buf_cvt_func, bind_data]) + if bind_data: ret = ODBC_API.SQLBindCol(self.stmt_h, col_num + 1, target_type, ADDR(alloc_buffer), total_buf_len, ADDR(used_buf_len)) if ret != SQL_SUCCESS: check_success(self, ret) - + def _UpdateDesc(self): - "Get the information of (name, type_code, display_size, internal_size, col_precision, scale, null_ok)" + "Get the information of (name, type_code, display_size, internal_size, col_precision, scale, null_ok)" if not self.connection: self.close() - + force_unicode = self.connection.unicode_results if force_unicode: Cname = create_buffer_u(1024) else: Cname = create_buffer(1024) - + Cname_ptr = c_short() Ctype_code = c_short() Csize = ctypes.c_size_t() @@ -1736,34 +1736,34 @@ class Cursor: self._ColTypeCodeList = [] NOC = self._NumOfCols() for col in range(1, NOC+1): - - ret = ODBC_API.SQLColAttribute(self.stmt_h, col, SQL_DESC_DISPLAY_SIZE, ADDR(create_buffer(10)), + + ret = ODBC_API.SQLColAttribute(self.stmt_h, col, SQL_DESC_DISPLAY_SIZE, ADDR(create_buffer(10)), 10, ADDR(c_short()),ADDR(Cdisp_size)) if ret != SQL_SUCCESS: check_success(self, ret) - + if force_unicode: - + ret = ODBC_API.SQLDescribeColW(self.stmt_h, col, Cname, len(Cname), ADDR(Cname_ptr),\ ADDR(Ctype_code),ADDR(Csize),ADDR(CDecimalDigits), ADDR(Cnull_ok)) if ret != SQL_SUCCESS: check_success(self, ret) else: - + ret = ODBC_API.SQLDescribeCol(self.stmt_h, col, Cname, len(Cname), ADDR(Cname_ptr),\ ADDR(Ctype_code),ADDR(Csize),ADDR(CDecimalDigits), ADDR(Cnull_ok)) if ret != SQL_SUCCESS: check_success(self, ret) - + col_name = Cname.value if lowercase: col_name = col_name.lower() - #(name, type_code, display_size, + #(name, type_code, display_size, ColDescr.append((col_name, SQL_data_type_dict.get(Ctype_code.value,(Ctype_code.value,))[0],Cdisp_size.value,\ Csize.value, Csize.value,CDecimalDigits.value,Cnull_ok.value == 1 and True or False)) self._ColTypeCodeList.append(Ctype_code.value) - + if len(ColDescr) > 0: self.description = ColDescr # Create the row type before fetching. @@ -1771,26 +1771,26 @@ class Cursor: else: self.description = None self._CreateColBuf() - - + + def _NumOfRows(self): """Get the number of rows""" if not self.connection: self.close() - + NOR = c_ssize_t() ret = SQLRowCount(self.stmt_h, ADDR(NOR)) if ret != SQL_SUCCESS: check_success(self, ret) self.rowcount = NOR.value - return self.rowcount + return self.rowcount + - def _NumOfCols(self): """Get the number of cols""" if not self.connection: self.close() - + NOC = c_short() ret = SQLNumResultCols(self.stmt_h, ADDR(NOC)) if ret != SQL_SUCCESS: @@ -1801,7 +1801,7 @@ class Cursor: def fetchall(self): if not self.connection: self.close() - + rows = [] while True: row = self.fetchone() @@ -1814,11 +1814,11 @@ class Cursor: def fetchmany(self, num = None): if not self.connection: self.close() - + if num is None: num = self.arraysize rows = [] - + while len(rows) < num: row = self.fetchone() if row is None: @@ -1830,12 +1830,12 @@ class Cursor: def fetchone(self): if not self.connection: self.close() - + ret = SQLFetch(self.stmt_h) - - if ret in (SQL_SUCCESS,SQL_SUCCESS_WITH_INFO): + + if ret in (SQL_SUCCESS,SQL_SUCCESS_WITH_INFO): '''Bind buffers for the record set columns''' - + value_list = [] col_num = 1 for col_name, target_type, used_buf_len, ADDR_used_buf_len, alloc_buffer, ADDR_alloc_buffer, total_buf_len, buf_cvt_func, bind_data in self._ColBufferList: @@ -1844,10 +1844,10 @@ class Cursor: if bind_data: ret = SQL_SUCCESS else: - ret = SQLGetData(self.stmt_h, col_num, target_type, ADDR_alloc_buffer, total_buf_len, ADDR_used_buf_len) + ret = SQLGetData(self.stmt_h, col_num, target_type, ADDR_alloc_buffer, total_buf_len, ADDR_used_buf_len) if ret == SQL_SUCCESS: if used_buf_len.value == SQL_NULL_DATA: - value_list.append(None) + value_list.append(None) else: if raw_data_parts == []: # Means no previous data, no need to combine @@ -1865,21 +1865,21 @@ class Cursor: raw_data_parts.append(from_buffer_u(alloc_buffer)) else: raw_data_parts.append(alloc_buffer.value) - break - + break + elif ret == SQL_SUCCESS_WITH_INFO: # Means the data is only partial if target_type == SQL_C_BINARY: raw_data_parts.append(alloc_buffer.raw) else: - raw_data_parts.append(alloc_buffer.value) - + raw_data_parts.append(alloc_buffer.value) + elif ret == SQL_NO_DATA: # Means all data has been transmitted break else: - check_success(self, ret) - + check_success(self, ret) + if raw_data_parts != []: if py_v3: if target_type != SQL_C_BINARY: @@ -1893,47 +1893,47 @@ class Cursor: col_num += 1 return self._row_type(value_list) - + else: if ret == SQL_NO_DATA_FOUND: - + return None else: check_success(self, ret) - + def __next__(self): return self.next() - - def next(self): + + def next(self): row = self.fetchone() if row is None: raise(StopIteration) return row - + def __iter__(self): return self - + def skip(self, count = 0): if not self.connection: self.close() - + for i in range(count): ret = ODBC_API.SQLFetchScroll(self.stmt_h, SQL_FETCH_NEXT, 0) if ret != SQL_SUCCESS: check_success(self, ret) - return None - - - + return None + + + def nextset(self): if not self.connection: self.close() - + ret = ODBC_API.SQLMoreResults(self.stmt_h) if ret not in (SQL_SUCCESS, SQL_NO_DATA): check_success(self, ret) - + if ret == SQL_NO_DATA: self._free_stmt() return False @@ -1942,15 +1942,15 @@ class Cursor: self._UpdateDesc() #self._BindCols() return True - - + + def _free_stmt(self, free_type = None): if not self.connection: self.close() - + if not self.connection.connected: raise ProgrammingError('HY000','Attempt to use a closed connection.') - + #self.description = None #self.rowcount = -1 if free_type in (SQL_CLOSE, None): @@ -1961,17 +1961,17 @@ class Cursor: ret = ODBC_API.SQLFreeStmt(self.stmt_h, SQL_UNBIND) if ret != SQL_SUCCESS: check_success(self, ret) - if free_type in (SQL_RESET_PARAMS, None): + if free_type in (SQL_RESET_PARAMS, None): ret = ODBC_API.SQLFreeStmt(self.stmt_h, SQL_RESET_PARAMS) if ret != SQL_SUCCESS: check_success(self, ret) - - - + + + def getTypeInfo(self, sqlType = None): if not self.connection: self.close() - + if sqlType is None: type = SQL_ALL_TYPES else: @@ -1982,89 +1982,89 @@ class Cursor: self._UpdateDesc() #self._BindCols() return self.fetchone() - - + + def tables(self, table=None, catalog=None, schema=None, tableType=None): - """Return a list with all tables""" + """Return a list with all tables""" if not self.connection: self.close() - + l_catalog = l_schema = l_table = l_tableType = 0 - + if unicode in [type(x) for x in (table, catalog, schema,tableType)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLTablesW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLTables - - - + + + if catalog is not None: l_catalog = len(catalog) - catalog = string_p(catalog) + catalog = string_p(catalog) - if schema is not None: + if schema is not None: l_schema = len(schema) schema = string_p(schema) - + if table is not None: l_table = len(table) table = string_p(table) - - if tableType is not None: + + if tableType is not None: l_tableType = len(tableType) tableType = string_p(tableType) - + self._free_stmt() self._last_param_types = None self.statement = None ret = API_f(self.stmt_h, catalog, l_catalog, - schema, l_schema, + schema, l_schema, table, l_table, tableType, l_tableType) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() #self._BindCols() return self - - + + def columns(self, table=None, catalog=None, schema=None, column=None): - """Return a list with all columns""" + """Return a list with all columns""" if not self.connection: self.close() - + l_catalog = l_schema = l_table = l_column = 0 - + if unicode in [type(x) for x in (table, catalog, schema,column)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLColumnsW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLColumns - - - - if catalog is not None: + + + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) if schema is not None: l_schema = len(schema) schema = string_p(schema) - if table is not None: + if table is not None: l_table = len(table) table = string_p(table) - if column is not None: + if column is not None: l_column = len(column) column = string_p(column) - + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, schema, l_schema, @@ -2076,87 +2076,87 @@ class Cursor: self._UpdateDesc() #self._BindCols() return self - - + + def primaryKeys(self, table=None, catalog=None, schema=None): if not self.connection: self.close() - + l_catalog = l_schema = l_table = 0 - + if unicode in [type(x) for x in (table, catalog, schema)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLPrimaryKeysW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLPrimaryKeys - - - - if catalog is not None: + + + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) - - if schema is not None: + + if schema is not None: l_schema = len(schema) schema = string_p(schema) - - if table is not None: + + if table is not None: l_table = len(table) table = string_p(table) - + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, schema, l_schema, table, l_table) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() #self._BindCols() return self - - + + def foreignKeys(self, table=None, catalog=None, schema=None, foreignTable=None, foreignCatalog=None, foreignSchema=None): if not self.connection: self.close() - + l_catalog = l_schema = l_table = l_foreignTable = l_foreignCatalog = l_foreignSchema = 0 - + if unicode in [type(x) for x in (table, catalog, schema,foreignTable,foreignCatalog,foreignSchema)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLForeignKeysW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLForeignKeys - - if catalog is not None: + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) - if schema is not None: + if schema is not None: l_schema = len(schema) schema = string_p(schema) - if table is not None: + if table is not None: l_table = len(table) table = string_p(table) - if foreignTable is not None: + if foreignTable is not None: l_foreignTable = len(foreignTable) foreignTable = string_p(foreignTable) - if foreignCatalog is not None: + if foreignCatalog is not None: l_foreignCatalog = len(foreignCatalog) foreignCatalog = string_p(foreignCatalog) - if foreignSchema is not None: + if foreignSchema is not None: l_foreignSchema = len(foreignSchema) foreignSchema = string_p(foreignSchema) - + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, schema, l_schema, @@ -2165,17 +2165,17 @@ class Cursor: foreignSchema, l_foreignSchema, foreignTable, l_foreignTable) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() #self._BindCols() return self - - + + def procedurecolumns(self, procedure=None, catalog=None, schema=None, column=None): if not self.connection: self.close() - + l_catalog = l_schema = l_procedure = l_column = 0 if unicode in [type(x) for x in (procedure, catalog, schema,column)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) @@ -2183,74 +2183,74 @@ class Cursor: else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLProcedureColumns - - - if catalog is not None: + + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) - if schema is not None: + if schema is not None: l_schema = len(schema) schema = string_p(schema) - if procedure is not None: + if procedure is not None: l_procedure = len(procedure) procedure = string_p(procedure) - if column is not None: + if column is not None: l_column = len(column) column = string_p(column) - - + + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, schema, l_schema, procedure, l_procedure, column, l_column) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() return self - - + + def procedures(self, procedure=None, catalog=None, schema=None): if not self.connection: self.close() - + l_catalog = l_schema = l_procedure = 0 - + if unicode in [type(x) for x in (procedure, catalog, schema)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLProceduresW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLProcedures - - - - if catalog is not None: + + + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) - if schema is not None: + if schema is not None: l_schema = len(schema) schema = string_p(schema) - if procedure is not None: + if procedure is not None: l_procedure = len(procedure) procedure = string_p(procedure) - - + + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, schema, l_schema, procedure, l_procedure) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() return self @@ -2259,27 +2259,27 @@ class Cursor: def statistics(self, table, catalog=None, schema=None, unique=False, quick=True): if not self.connection: self.close() - + l_table = l_catalog = l_schema = 0 - + if unicode in [type(x) for x in (table, catalog, schema)]: string_p = lambda x:wchar_pointer(UCS_buf(x)) API_f = ODBC_API.SQLStatisticsW else: string_p = ctypes.c_char_p API_f = ODBC_API.SQLStatistics - - - if catalog is not None: + + + if catalog is not None: l_catalog = len(catalog) catalog = string_p(catalog) - if schema is not None: + if schema is not None: l_schema = len(schema) schema = string_p(schema) - if table is not None: + if table is not None: l_table = len(table) table = string_p(table) - + if unique: Unique = SQL_INDEX_UNIQUE else: @@ -2288,23 +2288,23 @@ class Cursor: Reserved = SQL_QUICK else: Reserved = SQL_ENSURE - + self._free_stmt() self._last_param_types = None self.statement = None - + ret = API_f(self.stmt_h, catalog, l_catalog, - schema, l_schema, + schema, l_schema, table, l_table, Unique, Reserved) check_success(self, ret) - + self._NumOfRows() self._UpdateDesc() #self._BindCols() return self - + def commit(self): if not self.connection: @@ -2315,12 +2315,12 @@ class Cursor: if not self.connection: self.close() self.connection.rollback() - + def setoutputsize(self, size, column = None): if not self.connection: self.close() self._outputsize[column] = size - + def setinputsizes(self, sizes): if not self.connection: self.close() @@ -2331,7 +2331,7 @@ class Cursor: """ Call SQLCloseCursor API to free the statement handle""" # ret = ODBC_API.SQLCloseCursor(self.stmt_h) # check_success(self, ret) -# +# if self.connection.connected: ret = ODBC_API.SQLFreeStmt(self.stmt_h, SQL_CLOSE) check_success(self, ret) @@ -2344,32 +2344,32 @@ class Cursor: ret = ODBC_API.SQLFreeHandle(SQL_HANDLE_STMT, self.stmt_h) check_success(self, ret) - - + + self.closed = True - - - def __del__(self): + + + def __del__(self): if not self.closed: self.close() - + def __exit__(self, type, value, traceback): if not self.connection: self.close() - + if value: self.rollback() else: self.commit() - + self.close() - - + + def __enter__(self): return self - -# This class implement a odbc connection. + +# This class implement a odbc connection. # # @@ -2390,7 +2390,7 @@ class Connection: connectString = connectString + key + '=' + value + ';' self.connectString = connectString - + self.clear_output_converters() try: @@ -2400,23 +2400,23 @@ class Connection: AllocateEnv() finally: lock.release() - + # Allocate an DBC handle self.dbc_h under the environment shared_env_h # This DBC handle is actually the basis of a "connection" - # The handle of self.dbc_h will be used to connect to a certain source + # The handle of self.dbc_h will be used to connect to a certain source # in the self.connect and self.ConnectByDSN method - + ret = ODBC_API.SQLAllocHandle(SQL_HANDLE_DBC, shared_env_h, ADDR(self.dbc_h)) check_success(self, ret) - + self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly) - - - + + + def connect(self, connectString = '', autocommit = False, ansi = False, timeout = 0, unicode_results = use_unicode, readonly = False): """Connect to odbc, using connect strings and set the connection's attributes like autocommit and timeout by calling SQLSetConnectAttr - """ + """ # Before we establish the connection by the connection string # Set the connection's attribute of "timeout" (Actully LOGIN_TIMEOUT) @@ -2431,9 +2431,9 @@ class Connection: # Convert the connetsytring to encoded string - # so it can be converted to a ctypes c_char array object + # so it can be converted to a ctypes c_char array object + - self.ansi = ansi if not ansi: c_connectString = wchar_pointer(UCS_buf(self.connectString)) @@ -2459,43 +2459,43 @@ class Connection: else: ret = odbc_func(self.dbc_h, 0, c_connectString, len(self.connectString), None, 0, None, SQL_DRIVER_NOPROMPT) check_success(self, ret) - - - # Set the connection's attribute of "autocommit" + + + # Set the connection's attribute of "autocommit" # self.autocommit = autocommit - + if self.autocommit == True: ret = ODBC_API.SQLSetConnectAttr(self.dbc_h, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON, SQL_IS_UINTEGER) else: ret = ODBC_API.SQLSetConnectAttr(self.dbc_h, SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF, SQL_IS_UINTEGER) check_success(self, ret) - - # Set the connection's attribute of "readonly" + + # Set the connection's attribute of "readonly" # self.readonly = readonly - + ret = ODBC_API.SQLSetConnectAttr(self.dbc_h, SQL_ATTR_ACCESS_MODE, self.readonly and SQL_MODE_READ_ONLY or SQL_MODE_READ_WRITE, SQL_IS_UINTEGER) check_success(self, ret) - + self.unicode_results = unicode_results self.connected = 1 self.update_db_special_info() - + def clear_output_converters(self): self.output_converter = {} for sqltype, profile in SQL_data_type_dict.items(): self.output_converter[sqltype] = profile[1] - - + + def add_output_converter(self, sqltype, func): self.output_converter[sqltype] = func - + def settimeout(self, timeout): ret = ODBC_API.SQLSetConnectAttr(self.dbc_h, SQL_ATTR_CONNECTION_TIMEOUT, timeout, SQL_IS_UINTEGER); check_success(self, ret) self.timeout = timeout - + def ConnectByDSN(self, dsn, user, passwd = ''): """Connect to odbc, we need dsn, user and optionally password""" @@ -2504,21 +2504,21 @@ class Connection: self.passwd = passwd sn = create_buffer(dsn) - un = create_buffer(user) + un = create_buffer(user) pw = create_buffer(passwd) - + ret = ODBC_API.SQLConnect(self.dbc_h, sn, len(sn), un, len(un), pw, len(pw)) check_success(self, ret) self.update_db_special_info() self.connected = 1 - - - def cursor(self, row_type_callable=None): + + + def cursor(self, row_type_callable=None): #self.settimeout(self.timeout) if not self.connected: raise ProgrammingError('HY000','Attempt to use a closed connection.') - cur = Cursor(self, row_type_callable=row_type_callable) + cur = Cursor(self, row_type_callable=row_type_callable) # self._cursors.append(cur) return cur @@ -2538,7 +2538,7 @@ class Connection: except: pass cur.close() - + self.support_SQLDescribeParam = False try: driver_name = self.getinfo(SQL_DRIVER_NAME) @@ -2546,11 +2546,11 @@ class Connection: self.support_SQLDescribeParam = True except: pass - + def commit(self): if not self.connected: raise ProgrammingError('HY000','Attempt to use a closed connection.') - + ret = SQLEndTran(SQL_HANDLE_DBC, self.dbc_h, SQL_COMMIT) if ret != SQL_SUCCESS: check_success(self, ret) @@ -2561,14 +2561,14 @@ class Connection: ret = SQLEndTran(SQL_HANDLE_DBC, self.dbc_h, SQL_ROLLBACK) if ret != SQL_SUCCESS: check_success(self, ret) - - - + + + def getinfo(self,infotype): if infotype not in list(aInfoTypes.keys()): - raise ProgrammingError('HY000','Invalid getinfo value: '+str(infotype)) - - + raise ProgrammingError('HY000','Invalid getinfo value: '+str(infotype)) + + if aInfoTypes[infotype] == 'GI_UINTEGER': total_buf_len = 1000 alloc_buffer = ctypes.c_ulong() @@ -2577,7 +2577,7 @@ class Connection: ADDR(used_buf_len)) check_success(self, ret) result = alloc_buffer.value - + elif aInfoTypes[infotype] == 'GI_USMALLINT': total_buf_len = 1000 alloc_buffer = ctypes.c_ushort() @@ -2607,25 +2607,25 @@ class Connection: result = True else: result = False - + return result - + def __exit__(self, type, value, traceback): if value: self.rollback() else: self.commit() - + if self.connected: self.close() - + def __enter__(self): return self def __del__(self): if self.connected: self.close() - + def close(self): if not self.connected: raise ProgrammingError('HY000','Attempt to close a closed connection.') @@ -2633,7 +2633,7 @@ class Connection: # if not cur is None: # if not cur.closed: # cur.close() - + if self.connected: #if DEBUG:print 'disconnect' if not self.autocommit: @@ -2648,7 +2648,7 @@ class Connection: # ret = ODBC_API.SQLFreeHandle(SQL_HANDLE_ENV, shared_env_h) # check_success(shared_env_h, ret) self.connected = 0 - + odbc = Connection connect = odbc ''' @@ -2665,7 +2665,7 @@ def drivers(): AllocateEnv() finally: lock.release() - + DriverDescription = create_buffer_u(1000) BufferLength1 = c_short(1000) DescriptionLength = c_short() @@ -2683,14 +2683,14 @@ def drivers(): if Direction == SQL_FETCH_FIRST: Direction = SQL_FETCH_NEXT return DriverList - + def win_create_mdb(mdb_path, sort_order = "General\0\0"): if sys.platform not in ('win32','cli'): raise Exception('This function is available for use in Windows only.') - + mdb_driver = [d for d in drivers() if 'Microsoft Access Driver (*.mdb' in d] if mdb_driver == []: raise Exception('Access Driver is not found.') @@ -2706,17 +2706,17 @@ def win_create_mdb(mdb_path, sort_order = "General\0\0"): else: c_Path = "CREATE_DB=" + mdb_path + " " + sort_order ODBC_ADD_SYS_DSN = 1 - - + + ret = ctypes.windll.ODBCCP32.SQLConfigDataSource(None,ODBC_ADD_SYS_DSN,driver_name, c_Path) if not ret: raise Exception('Failed to create Access mdb file - "%s". Please check file path, permission and Access driver readiness.' %mdb_path) - - + + def win_connect_mdb(mdb_path): if sys.platform not in ('win32','cli'): raise Exception('This function is available for use in Windows only.') - + mdb_driver = [d for d in drivers() if 'Microsoft Access Driver (*.mdb' in d] if mdb_driver == []: raise Exception('Access Driver is not found.') @@ -2724,20 +2724,20 @@ def win_connect_mdb(mdb_path): driver_name = mdb_driver[0] return connect('Driver={'+driver_name+"};DBQ="+mdb_path, unicode_results = use_unicode, readonly = False) - - - + + + def win_compact_mdb(mdb_path, compacted_mdb_path, sort_order = "General\0\0"): if sys.platform not in ('win32','cli'): raise Exception('This function is available for use in Windows only.') - - + + mdb_driver = [d for d in drivers() if 'Microsoft Access Driver (*.mdb' in d] if mdb_driver == []: raise Exception('Access Driver is not found.') else: driver_name = mdb_driver[0].encode('mbcs') - + #COMPACT_DB= ctypes.windll.ODBCCP32.SQLConfigDataSource.argtypes = [ctypes.c_void_p,ctypes.c_ushort,ctypes.c_char_p,ctypes.c_char_p] #driver_name = "Microsoft Access Driver (*.mdb)" @@ -2751,7 +2751,7 @@ def win_compact_mdb(mdb_path, compacted_mdb_path, sort_order = "General\0\0"): ret = ctypes.windll.ODBCCP32.SQLConfigDataSource(None,ODBC_ADD_SYS_DSN,driver_name, c_Path) if not ret: raise Exception('Failed to compact Access mdb file - "%s". Please check file path, permission and Access driver readiness.' %compacted_mdb_path) - + def dataSources(): """Return a list with [name, descrition]""" diff --git a/gluon/contrib/stripe.py b/gluon/contrib/stripe.py index b9602ace..a0f6ce6d 100644 --- a/gluon/contrib/stripe.py +++ b/gluon/contrib/stripe.py @@ -37,7 +37,7 @@ def pay(): elif form.errors: redirect(URL('pay_error')) return dict(form=form) - + """ URL_CHARGE = 'https://%s:@api.stripe.com/v1/charges' @@ -114,7 +114,7 @@ class StripeForm(object): def process(self): from gluon import current - request = current.request + request = current.request if request.post_vars: if self.signature == request.post_vars.signature: self.response = Stripe(self.sk).charge( @@ -127,7 +127,7 @@ class StripeForm(object): return self self.errors = True return self - + def xml(self): from gluon.template import render if self.accepted: @@ -135,8 +135,8 @@ class StripeForm(object): elif self.errors: return "There was an processing error" else: - context = dict(amount=self.amount, - signature=self.signature, pk=self.pk, + context = dict(amount=self.amount, + signature=self.signature, pk=self.pk, currency_symbol=self.currency_symbol, security_notice=self.security_notice, disclosure_notice=self.disclosure_notice) @@ -145,14 +145,14 @@ class StripeForm(object): TEMPLATE = """ -