Abstract base class¶
-
class
metapensiero.sqlalchemy.proxy.base.
ProxiedBase
¶ Abstract base for the proxy thingie.
-
__call__
(session, *conditions, **args)¶ Apply filter conditions, execute the query and return results.
Parameters: - session – an SQLAlchemy
Session
- conditions – a list of SQLAlchemy expressions
- args – a dictionary
Return type: depending on the arguments, either a dictionary, a list of tuples or a JSON string
The first argument is the SQLAlchemy session, mandatory. All remaining arguments are eventually used to filter and change the query. Unused arguments remains accessible when the query is finally called.
Some keyword arguments have special meaning:
- start
- This is the start of the interested range of records
- limit
- This is the maximum number of returned records
- result
When
False
orNone
(or the strings"False"
,"None"
and"false"
) means that no result set will be returned (reasonably either metadata or count are given); whenTrue
(or the strings"True"
and"true"
), most of the other options are ignored and only the native Python results is returned; otherwise it’s a string, the name of the slot of the returned dictionary containing the results set.True
by default.- success
If result is not
True
, this is a string used as the name of the slot containing execution status, that will be a boolean flag,True
to indicate successfull execution,False
otherwise.By default it’s
"success"
.- message
If result is not
True
, this is a string used as the name of the slot containing execution message, that will either"Ok"
when everything went good, otherwise a string with a possible reason of the failure.By default it’s
"message"
.- count
- If result is not
True
, this may be a string used as the name of the slot containing the total number of records that satisfy the conditions, ignoring the eventual range specified with start and limit. WhenFalse
orNone
(the default) the count won’t be computed. - metadata
If result is not
True
, this may be the name of the slot containing a description of the result. SeegetMetadata()
for details.None
, the default, disables the feature.- asdict
- When you want to deal with plain Python dictionaries, one for each row, instead of
the standard SQLAlchemy resultset, set this to
True
.False
by default.
All these keywords are pulled (that is, removed) from the execution context of the query.
- session – an SQLAlchemy
-
filterQueryWithArgs
(session, conditions, args)¶ Apply filter conditions to the query.
Parameters: - session – an SQLAlchemy
Session
- conditions – a list of SQLAlchemy expressions
- args – a dictionary
Return type: either a core SQL statement or an ORM query
- session – an SQLAlchemy
-
getColumns
(query)¶ Return the columns of the given query.
-
getCount
(session, query)¶ Execute a query to get the actual count of matching records.
-
getMetadata
(query, countslot, resultslot, successslot)¶ Description of the result.
Parameters: - query – an SQLAlchemy
Query
- countslot – a string or
None
- resultslot – a string or
None
- successslot – a string or
None
Return type: a dictionary
For each selected field in the query, this method builds a dictionary containing the following keys:
- name
- the name of the field
- type
- the type of the field
- label
- the localized header for the field
- hint
- a longer description of the field, also localized
- length
- the size in characters, for String columns
- decimals
- the scale of the number, for Numeric columns
These dictionaries are collected in a list, called
fields
.The information about each field is extracted by two sources:
- self.metadata, a dictionary keyed on field names: each slot may be either a dictionary or a callable accepting the field name as parameter and returning the actual dictionary
- field column info, either a dictionary or a callable accepting the field name as parameter and returning the actual dictionary
Also the value of any single slot in the information dictionary may be either a scalar value or a callable accepting two parameters, the name of the field and the name of the slot, returning the actual scalar information, for example:
def birthdate_info(name): return { 'min': date(1980, 1, 1), 'max': lambda fname, iname: date.today() } persons = Table('persons', metadata, Column('id', Integer, primary_key=True), Column('firstname', String, info=dict(label="First name", hint="The first name of the person")), Column('lastname', String), Column('birthdate', Date, info=birthdate_info), Column('timestamp', DateTime), Column('smart', Boolean, default=True), Column('somevalue', Integer), Column('title', Title), Column('WeirdFN', String, key='goodfn'), )
The value coming from proxy’s metadata takes precedence over the one from the column’s info.
Both dictionaries can contain any arbitrary extra slots that the caller may use for whatever reason.
With the exception of name, everything can be overridden by self.metadata.
Given the table above and the following proxy:
pc = persons.c query = select([pc.id, pc.firstname, pc.lastname, pc.birthdate]) meta = dict(lastname=dict(label='Last name', hint='Family name of the person', nullable=False)) proxy = ProxiedQuery(query, meta)
the call
proxy(Session(), result=False, limit=0, metadata='meta')
returns something like:{ 'message': 'Ok', 'meta': { 'fields': [{ 'align': 'right', 'hidden': True, 'hint': '', 'label': 'Id', 'name': 'id', 'nullable': False, 'readonly': True, 'type': 'integer' }, { 'hint': 'The first name of the person', 'label': 'First name', 'length': 10, 'name': 'firstname', 'nullable': True, 'type': 'string' }, { 'hint': 'Family name of the person', 'label': 'Last name', 'length': 10, 'name': 'lastname', 'nullable': False, 'type': 'string' }, { 'hint': '', 'label': 'Birthdate', 'max': datetime.date(2017, 1, 11), 'min': datetime.date(1980, 1, 1), 'name': 'birthdate', 'nullable': True, 'type': 'date' }, { 'hint': '', 'label': 'Smart', 'name': 'smart', 'default': True, 'type': 'boolean' } ... ], 'primary_key': 'id', 'success_slot': 'success' }, 'success': True }
- query – an SQLAlchemy
-
getResult
(session, query, asdict)¶ Execute the query in the given session, returning the result.
Parameters: - session – an SQLAlchemy
Session
- query – an SQLAlchemy
Query
- asdict – a boolean
If asdict is
False
then this should return either a ResultProxy (for SQL selects) or a list of entities (when the query is at the ORM level), otherwise they should be translated into a list of dictionaries.- session – an SQLAlchemy
-
prepareQueryFromConditionsAndArgs
(session, conditions, args)¶ Extract special meaning keyword arguments and return them, with a filtered query.
Parameters: - session – an SQLAlchemy
Session
- conditions – a list of SQLAlchemy expressions
- args – a dictionary
Return type: a tuple of 12 values
- session – an SQLAlchemy
-
translate
(msg)¶ Stub implementation of l10n basic function, to be overridden.
-