1- import operator
21import random
32import uuid
4- from functools import reduce
53from typing import Optional
64from zoneinfo import ZoneInfo
75
86from django .utils import timezone
97
108from baserow .core .formula .argument_types import (
11- AddableBaserowRuntimeFormulaArgumentType ,
129 AnyBaserowRuntimeFormulaArgumentType ,
1310 BooleanBaserowRuntimeFormulaArgumentType ,
1411 DateTimeBaserowRuntimeFormulaArgumentType ,
1512 DictBaserowRuntimeFormulaArgumentType ,
1613 NumberBaserowRuntimeFormulaArgumentType ,
17- SubtractableBaserowRuntimeFormulaArgumentType ,
1814 TextBaserowRuntimeFormulaArgumentType ,
1915 TimezoneBaserowRuntimeFormulaArgumentType ,
2016)
@@ -28,7 +24,7 @@ class RuntimeConcat(RuntimeFormulaFunction):
2824 type = "concat"
2925
3026 def validate_type_of_args (self , args ) -> Optional [FormulaArg ]:
31- arg_type = AddableBaserowRuntimeFormulaArgumentType ()
27+ arg_type = TextBaserowRuntimeFormulaArgumentType ()
3228 return next (
3329 (arg for arg in args if not arg_type .test (arg )),
3430 None ,
@@ -52,61 +48,47 @@ def execute(self, context: FormulaContext, args: FormulaArgs):
5248class RuntimeAdd (RuntimeFormulaFunction ):
5349 type = "add"
5450
55- def validate_type_of_args (self , args ) -> Optional [FormulaArg ]:
56- arg_type = AddableBaserowRuntimeFormulaArgumentType ()
57- return next (
58- (arg for arg in args if not arg_type .test (arg )),
59- None ,
60- )
61-
62- def validate_number_of_args (self , args ):
63- return len (args ) >= 1
51+ args = [
52+ NumberBaserowRuntimeFormulaArgumentType (),
53+ NumberBaserowRuntimeFormulaArgumentType (),
54+ ]
6455
6556 def execute (self , context : FormulaContext , args : FormulaArgs ):
66- return reduce ( operator . add , args )
57+ return args [ 0 ] + args [ 1 ]
6758
6859
6960class RuntimeMinus (RuntimeFormulaFunction ):
7061 type = "minus"
7162
72- def validate_type_of_args (self , args ) -> Optional [FormulaArg ]:
73- arg_type = SubtractableBaserowRuntimeFormulaArgumentType ()
74- return next (
75- (arg for arg in args if not arg_type .test (arg )),
76- None ,
77- )
78-
79- def validate_number_of_args (self , args ):
80- return len (args ) > 1
63+ args = [
64+ NumberBaserowRuntimeFormulaArgumentType (),
65+ NumberBaserowRuntimeFormulaArgumentType (),
66+ ]
8167
8268 def execute (self , context : FormulaContext , args : FormulaArgs ):
83- return reduce ( operator . sub , args )
69+ return args [ 0 ] - args [ 1 ]
8470
8571
8672class RuntimeMultiply (RuntimeFormulaFunction ):
8773 type = "multiply"
74+
8875 args = [
8976 NumberBaserowRuntimeFormulaArgumentType (),
9077 NumberBaserowRuntimeFormulaArgumentType (),
9178 ]
9279
93- def validate_number_of_args (self , args ):
94- return len (args ) == 2
95-
9680 def execute (self , context : FormulaContext , args : FormulaArgs ):
9781 return args [0 ] * args [1 ]
9882
9983
10084class RuntimeDivide (RuntimeFormulaFunction ):
10185 type = "divide"
86+
10287 args = [
10388 NumberBaserowRuntimeFormulaArgumentType (),
10489 NumberBaserowRuntimeFormulaArgumentType (),
10590 ]
10691
107- def validate_number_of_args (self , args ):
108- return len (args ) == 2
109-
11092 def execute (self , context : FormulaContext , args : FormulaArgs ):
11193 return args [0 ] / args [1 ]
11294
@@ -118,9 +100,6 @@ class RuntimeEqual(RuntimeFormulaFunction):
118100 AnyBaserowRuntimeFormulaArgumentType (),
119101 ]
120102
121- def validate_number_of_args (self , args ):
122- return len (args ) == 2
123-
124103 def execute (self , context : FormulaContext , args : FormulaArgs ):
125104 return args [0 ] == args [1 ]
126105
@@ -132,65 +111,50 @@ class RuntimeNotEqual(RuntimeFormulaFunction):
132111 AnyBaserowRuntimeFormulaArgumentType (),
133112 ]
134113
135- def validate_number_of_args (self , args ):
136- return len (args ) == 2
137-
138114 def execute (self , context : FormulaContext , args : FormulaArgs ):
139115 return args [0 ] != args [1 ]
140116
141117
142118class RuntimeGreaterThan (RuntimeFormulaFunction ):
143119 type = "greater_than"
144120 args = [
145- NumberBaserowRuntimeFormulaArgumentType (),
146- NumberBaserowRuntimeFormulaArgumentType (),
121+ AnyBaserowRuntimeFormulaArgumentType (),
122+ AnyBaserowRuntimeFormulaArgumentType (),
147123 ]
148124
149- def validate_number_of_args (self , args ):
150- return len (args ) == 2
151-
152125 def execute (self , context : FormulaContext , args : FormulaArgs ):
153126 return args [0 ] > args [1 ]
154127
155128
156129class RuntimeLessThan (RuntimeFormulaFunction ):
157130 type = "less_than"
158131 args = [
159- NumberBaserowRuntimeFormulaArgumentType (),
160- NumberBaserowRuntimeFormulaArgumentType (),
132+ AnyBaserowRuntimeFormulaArgumentType (),
133+ AnyBaserowRuntimeFormulaArgumentType (),
161134 ]
162135
163- def validate_number_of_args (self , args ):
164- return len (args ) == 2
165-
166136 def execute (self , context : FormulaContext , args : FormulaArgs ):
167137 return args [0 ] < args [1 ]
168138
169139
170140class RuntimeGreaterThanOrEqual (RuntimeFormulaFunction ):
171141 type = "greater_than_or_equal"
172142 args = [
173- NumberBaserowRuntimeFormulaArgumentType (),
174- NumberBaserowRuntimeFormulaArgumentType (),
143+ AnyBaserowRuntimeFormulaArgumentType (),
144+ AnyBaserowRuntimeFormulaArgumentType (),
175145 ]
176146
177- def validate_number_of_args (self , args ):
178- return len (args ) == 2
179-
180147 def execute (self , context : FormulaContext , args : FormulaArgs ):
181148 return args [0 ] >= args [1 ]
182149
183150
184151class RuntimeLessThanOrEqual (RuntimeFormulaFunction ):
185152 type = "less_than_or_equal"
186153 args = [
187- NumberBaserowRuntimeFormulaArgumentType (),
188- NumberBaserowRuntimeFormulaArgumentType (),
154+ AnyBaserowRuntimeFormulaArgumentType (),
155+ AnyBaserowRuntimeFormulaArgumentType (),
189156 ]
190157
191- def validate_number_of_args (self , args ):
192- return len (args ) == 2
193-
194158 def execute (self , context : FormulaContext , args : FormulaArgs ):
195159 return args [0 ] <= args [1 ]
196160
@@ -227,7 +191,7 @@ class RuntimeRound(RuntimeFormulaFunction):
227191
228192 args = [
229193 NumberBaserowRuntimeFormulaArgumentType (),
230- NumberBaserowRuntimeFormulaArgumentType (),
194+ NumberBaserowRuntimeFormulaArgumentType (optional = True , cast_to_int = True ),
231195 ]
232196
233197 def execute (self , context : FormulaContext , args : FormulaArgs ):
@@ -272,7 +236,7 @@ def execute(self, context: FormulaContext, args: FormulaArgs):
272236 datetime_obj = args [0 ]
273237 moment_format = args [1 ]
274238
275- if ( len (args ) ) == 2 :
239+ if len (args ) == 2 :
276240 timezone_name = context .get_timezone_name ()
277241 else :
278242 timezone_name = args [2 ]
@@ -350,13 +314,17 @@ def execute(self, context: FormulaContext, args: FormulaArgs):
350314class RuntimeNow (RuntimeFormulaFunction ):
351315 type = "now"
352316
317+ args = []
318+
353319 def execute (self , context : FormulaContext , args : FormulaArgs ):
354320 return timezone .now ()
355321
356322
357323class RuntimeToday (RuntimeFormulaFunction ):
358324 type = "today"
359325
326+ args = []
327+
360328 def execute (self , context : FormulaContext , args : FormulaArgs ):
361329 return timezone .localdate ()
362330
@@ -400,26 +368,53 @@ def execute(self, context: FormulaContext, args: FormulaArgs):
400368class RuntimeRandomBool (RuntimeFormulaFunction ):
401369 type = "random_bool"
402370
371+ args = []
372+
403373 def execute (self , context : FormulaContext , args : FormulaArgs ):
404374 return random .choice ([True , False ]) # nosec: B311
405375
406376
407377class RuntimeGenerateUUID (RuntimeFormulaFunction ):
408378 type = "generate_uuid"
409379
380+ args = []
381+
410382 def execute (self , context : FormulaContext , args : FormulaArgs ):
411383 return str (uuid .uuid4 ())
412384
413385
414386class RuntimeIf (RuntimeFormulaFunction ):
415387 type = "if"
416388
417- def validate_type_of_args (self , args ) -> Optional [FormulaArg ]:
418- arg_type = BooleanBaserowRuntimeFormulaArgumentType ()
419- if not arg_type .test (args [0 ]):
420- return args [0 ]
421-
422- return None
389+ args = [
390+ BooleanBaserowRuntimeFormulaArgumentType (),
391+ AnyBaserowRuntimeFormulaArgumentType (),
392+ AnyBaserowRuntimeFormulaArgumentType (),
393+ ]
423394
424395 def execute (self , context : FormulaContext , args : FormulaArgs ):
425396 return args [1 ] if args [0 ] else args [2 ]
397+
398+
399+ class RuntimeAnd (RuntimeFormulaFunction ):
400+ type = "and"
401+
402+ args = [
403+ BooleanBaserowRuntimeFormulaArgumentType (),
404+ BooleanBaserowRuntimeFormulaArgumentType (),
405+ ]
406+
407+ def execute (self , context : FormulaContext , args : FormulaArgs ):
408+ return args [0 ] and args [1 ]
409+
410+
411+ class RuntimeOr (RuntimeFormulaFunction ):
412+ type = "or"
413+
414+ args = [
415+ BooleanBaserowRuntimeFormulaArgumentType (),
416+ BooleanBaserowRuntimeFormulaArgumentType (),
417+ ]
418+
419+ def execute (self , context : FormulaContext , args : FormulaArgs ):
420+ return args [0 ] or args [1 ]
0 commit comments