Calculated fields and button scripts
Calculated fields and button scripts use the Python programming language. The calculation or script is the implementation of a function whose signature is provided for you.
B.1. Field values
B.2. Related Records
B.2.1. Single related records
For relationships that specify a single record, you can get the value of a field in that record. For instance,
B.3. Testing for empty values
How you test for empty values depends on the type of field:
B.3.1. Non-text fields
Non-text fields may be empty, indicating that the user has not entered any value in the field. For instance, Glom does not assume that an empty value in a numeric field should mean 0.
You can test whether a field is empty by using Python's None. For instance:
if(record["contact_id"] == None): return "No Contact" else: return record.related["contacts"]["name_full"]
You might also test whether there are any related records. For instance:
if(record.related["contacts"] == None): return "No Contact" else: return record.related["contacts"]["name_full"]
B.3.2. Text fields
For text fields, you should check for zero-length strings. It is not possible in Glom to distinguish between zero-length strings and the absence of any string, because there is no advantage to doing so. For instance:
if(record["name_full"] == ""): return "No Name" else: return record["name_full"]
B.4. Using the full pygda API
pygda is a python API for the libgda API. The record's connection attribute provides a gda.connection that can be used to access the current database directly. This allows you to run any SQL query, for instance to read data from the database with a SELECT, or to change values in the database with an UPDATE. Note that the connection is already open so you can avoid the difficult work of specifying the connection details.
The record's table_name attribute also provides the name of the current table.
This example reads all data from the current table and prints the values to the terminal:
# Use the current database's connection # to get all the data for the current table. # # record.connection is an already-open gda.connection object, # saving us the bother of opening the connection, # or even knowing the name of the database. query = "SELECT * FROM %s" % record.table_name data_model = gda.gda_execute_select_command(record.connection, query) rows = data_model.get_n_rows() columns = data_model.get_n_columns() print " Number of columns: ", columns for i in range(columns): print " column ", i; print " name=", data_model.get_column_title(i) # Find out whether it's the primary key: field = data_model.describe_column(i) if field.get_primary_key(): print " (primary key)" print "\n"; print " Number of rows: ", rows for row_index in range(rows): print " row ", row_index; for col_index in range(columns): print " value=", data_model.get_value_at(col_index, row_index).get() print "\n";
- Copyright © 2004 Murray Cumming
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation Licence (GFDL), Version 1.1 or any later version published by the Free Software Foundation with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. You can find a copy of the GFDL at this link or in the file COPYING-DOCS distributed with this manual.
This manual is part of a collection of GNOME manuals distributed under the GFDL. If you want to distribute this manual separately from the collection, you can do so by adding a copy of the licence to the manual, as described in section 6 of the licence.
Many of the names used by companies to distinguish their products and services are claimed as trademarks. Where those names appear in any GNOME documentation, and the members of the GNOME Documentation Project are made aware of those trademarks, then the names are in capital letters or initial capital letters.
DOCUMENT AND MODIFIED VERSIONS OF THE DOCUMENT ARE PROVIDED UNDER THE TERMS OF THE GNU FREE DOCUMENTATION LICENSE WITH THE FURTHER UNDERSTANDING THAT:
- DOCUMENT IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE DOCUMENT OR MODIFIED VERSION OF THE DOCUMENT IS FREE OF DEFECTS MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY, ACCURACY, AND PERFORMANCE OF THE DOCUMENT OR MODIFIED VERSION OF THE DOCUMENT IS WITH YOU. SHOULD ANY DOCUMENT OR MODIFIED VERSION PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL WRITER, AUTHOR OR ANY CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY DOCUMENT OR MODIFIED VERSION OF THE DOCUMENT IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER; AND
- UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE AUTHOR, INITIAL WRITER, ANY CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE DOCUMENT OR MODIFIED VERSION OF THE DOCUMENT, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO ANY PERSON FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER DAMAGES OR LOSSES ARISING OUT OF OR RELATING TO USE OF THE DOCUMENT AND MODIFIED VERSIONS OF THE DOCUMENT, EVEN IF SUCH PARTY SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF SUCH DAMAGES.
To report a bug or make a suggestion regarding the Glom or this manual can be submitted to the GNOME Bugzilla , under the Glom product. Please search bugzilla before submitting your bug to ensure that yours hasn't already been reported.