gristlabs_grist-core/sandbox/grist/usercode.py

69 lines
1.9 KiB
Python
Raw Normal View History

"""
usercode.py isn't a real module, but an example of a module produced by gencode.py from the
user-defined document schema.
It is the same code that's produced from the test schema in test_gencode.py. In fact, it is used
as part of that test.
User-defined Tables (i.e. classes that derive from grist.Table) automatically get some additional
members:
Record - a class derived from grist.Record, with a property for each table column.
RecordSet - a class derived from grist.Record, with a property for each table column.
RecordSet.Record - a reference to the Record class above
======================================================================
import grist
from functions import * # global uppercase functions
import datetime, math, re # modules commonly needed in formulas
@grist.UserTable
class Students:
firstName = grist.Text()
lastName = grist.Text()
school = grist.Reference('Schools')
def fullName(rec, table):
return rec.firstName + ' ' + rec.lastName
def fullNameLen(rec, table):
return len(rec.fullName)
def schoolShort(rec, table):
return rec.school.name.split(' ')[0]
def schoolRegion(rec, table):
addr = rec.school.address
return addr.state if addr.country == 'US' else addr.region
@grist.formulaType(grist.Reference('Schools'))
def school2(rec, table):
return Schools.lookupFirst(name=rec.school.name)
@grist.UserTable
class Schools:
name = grist.Text()
address = grist.Reference('Address')
@grist.UserTable
class Address:
city = grist.Text()
state = grist.Text()
(core) Implement trigger formulas (generalizing default formulas) Summary: Trigger formulas can be calculated for new records, or for new records and updates to certain fields, or all fields. They do not recalculate on open, and they MAY be set directly by the user, including for data-cleaning. - Column metadata now includes recalcWhen and recalcDeps fields. - Trigger formulas are NOT recalculated on open or on schema changes. - When recalcWhen is "never", formula isn't calculated even for new records. - When recalcWhen is "allupdates", formula is calculated for new records and any manual (non-formula) updates to the record. - When recalcWhen is "", formula is calculated for new records, and changes to recalcDeps fields (which may be formula fields or column itself). - A column whose recalcDeps includes itself is a "data-cleaning" column; a value set by the user will still trigger the formula. - All trigger-formulas receive a "value" argument (to support the case above). Small changes - Update RefLists (used for recalcDeps) when target rows are deleted. - Add RecordList.__contains__ (for `rec in refList` or `id in refList` checks) - Clarify that Calculate action has replaced load_done() in practice, and use it in tests too, to better match reality. Left for later: - UI for setting recalcWhen / recalcDeps. - Implementation of actions such as "Recalculate for all cells". - Allowing trigger-formulas access to the current user's info. Test Plan: Added a comprehensive python-side test for various trigger combinations Reviewers: paulfitz, alexmojaki Reviewed By: paulfitz Differential Revision: https://phab.getgrist.com/D2872
2021-06-25 20:34:20 +00:00
def _default_country(rec, table, value):
return 'US'
country = grist.Text()
def region(rec, table):
return {'US': 'North America', 'UK': 'Europe'}.get(rec.country, 'N/A')
def badSyntax(rec, table):
# for a in b
# 10
raise SyntaxError('invalid syntax on line 1 col 11')
======================================================================
"""