Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Deal with values of type. 

2 

3* Basic types 

4* Values from other tables 

5* Customizations 

6""" 

7 

8from config import Config as C, Names as N 

9from control.utils import cap1 

10from control.typ.master import Master 

11from control.typ.value import Value 

12from control.typ.text import Text, Url, Email, Markdown 

13from control.typ.numeric import Int, Decimal, Money 

14from control.typ.bool import Bool2, Bool3 

15from control.typ.datetime import Datetime 

16from control.typ.cust.country import Country 

17from control.typ.cust.criteria import Criteria 

18from control.typ.cust.criteriaEntry import CriteriaEntry 

19from control.typ.cust.decision import Decision 

20from control.typ.cust.reviewEntry import ReviewEntry 

21from control.typ.cust.score import Score 

22from control.typ.cust.typeContribution import TypeContribution 

23from control.typ.cust.user import User 

24 

25 

26ALL_TYPES = dict( 

27 text=Text, 

28 url=Url, 

29 email=Email, 

30 markdown=Markdown, 

31 int=Int, 

32 decimal=Decimal, 

33 money=Money, 

34 bool2=Bool2, 

35 bool3=Bool3, 

36 datetime=Datetime, 

37 country=Country, 

38 criteria=Criteria, 

39 criteriaEntry=CriteriaEntry, 

40 decision=Decision, 

41 reviewEntry=ReviewEntry, 

42 score=Score, 

43 typeContribution=TypeContribution, 

44 user=User, 

45) 

46ALL_TYPE_SET = set(ALL_TYPES) 

47 

48 

49CT = C.tables 

50CW = C.web 

51 

52SCALAR_TYPES = set(CT.scalarTypes) 

53SYSTEM_TABLES = CT.systemTables 

54VALUE_TABLES = CT.valueTables 

55USER_TABLES = CT.userTables 

56USER_ENTRY_TABLES = CT.userEntryTables 

57 

58 

59class Types: 

60 """Provides access to all data types. 

61 

62 There are kinds of data types: 

63 

64 * scalar types, such as 

65 `control.typ.numeric.Int`, 

66 `control.typ.datetime.Datetime`, ... 

67 * master types: 

68 values are ids of master records (e.g. criteria, assessment), 

69 see `control.typ.master.Master` 

70 * value types 

71 values are ids in value tables, 

72 see `control.typ.master.Master` 

73 

74 (see the tables.yaml configuration file). 

75 

76 For each type a class is defined and for each type class a singleton 

77 is created and registered as attribute in Types. 

78 

79 By means of these type objects, all operations on data types can be performed 

80 throughout the application. 

81 """ 

82 

83 def __init__(self, context): 

84 """## Initialization 

85 

86 Creates type singletons for all data types. 

87 

88 Some types define operations that need access to 

89 `control.db.Db`, or `control.auth.Auth`. 

90 The objects for these types will be passed the type singletons that need it, 

91 so that they can in turn pass that to their methods. 

92 

93 The type singletons will be stored under an attribute named 

94 after the type, but starting with a lowercase letter. 

95 

96 Parameters 

97 ---------- 

98 context: object 

99 See below. 

100 

101 It holds the `control.db.Db`, or `control.auth.Auth` singletons. 

102 """ 

103 

104 self.context = context 

105 """*object* A `control.context.Context` singleton. 

106 """ 

107 

108 done = set() 

109 

110 for (tp, TypeClass) in ALL_TYPES.items(): 

111 self.make(tp, TypeClass) 

112 done.add(tp) 

113 

114 for tp in VALUE_TABLES + USER_TABLES + USER_ENTRY_TABLES + SYSTEM_TABLES: 

115 if tp in done: 

116 continue 

117 TypeName = cap1(tp) 

118 Base = Value if tp in VALUE_TABLES else Master 

119 TypeClass = type(TypeName, (Base,), {}) 

120 self.make(tp, TypeClass) 

121 

122 def make(self, tp, TypeClass): 

123 """Create a type singleton and register it. 

124 

125 An singleton of the given TypeClass is created with the right attributes. 

126 That singleton will be registered as an attribute in the Types class. 

127 

128 Parameters 

129 ---------- 

130 tp: string 

131 The name under which the type singleton will be registered. 

132 TypeClass: class 

133 """ 

134 

135 context = self.context 

136 

137 atts = [] 

138 if TypeClass.needsContext: 

139 atts.append(context) 

140 

141 typeObj = TypeClass(*atts) 

142 self.register(typeObj, tp) 

143 

144 def register(self, typeObj, tp): 

145 """Register a type singleton. 

146 

147 The type singleton itself will also receive its name in a `name` attribute. 

148 

149 Parameters 

150 ---------- 

151 typeObj: object 

152 The type singleton. 

153 tp: string 

154 The name under which the type singleton must be registered. 

155 """ 

156 

157 setattr(typeObj, N.name, tp) 

158 setattr(typeObj, N.types, self) 

159 setattr(self, tp, typeObj)