Coverage for gws-app/gws/base/model/field.py: 0%

117 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1from typing import Optional, cast 

2 

3import gws 

4 

5 

6class Props(gws.Props): 

7 attributeType: gws.AttributeType 

8 geometryType: gws.GeometryType 

9 name: str 

10 title: str 

11 type: str 

12 widget: gws.ext.props.modelWidget 

13 uid: str 

14 relatedModelUids: list[str] 

15 

16 

17class Config(gws.ConfigWithAccess): 

18 name: str 

19 title: Optional[str] 

20 

21 isPrimaryKey: Optional[bool] 

22 isRequired: Optional[bool] 

23 isUnique: Optional[bool] 

24 isAuto: Optional[bool] 

25 

26 values: Optional[list[gws.ext.config.modelValue]] 

27 validators: Optional[list[gws.ext.config.modelValidator]] 

28 

29 widget: Optional[gws.ext.config.modelWidget] 

30 

31 

32## 

33 

34class Object(gws.ModelField): 

35 notEmptyValidator: gws.ModelValidator 

36 formatValidator: gws.ModelValidator 

37 

38 def configure(self): 

39 self.model = self.cfg('_defaultModel') 

40 self.name = self.cfg('name') 

41 self.title = self.cfg('title', default=self.name) 

42 

43 self.values = [] 

44 self.validators = [] 

45 self.widget = None 

46 

47 self.configure_flags() 

48 self.configure_values() 

49 self.configure_validators() 

50 self.configure_widget() 

51 

52 def configure_flags(self): 

53 col = self.describe() 

54 

55 p = self.cfg('isPrimaryKey') 

56 if p is not None: 

57 self.isPrimaryKey = p 

58 else: 

59 self.isPrimaryKey = col and col.isPrimaryKey 

60 

61 p = self.cfg('isRequired') 

62 if p is not None: 

63 self.isRequired = p 

64 else: 

65 self.isRequired = col and not col.isNullable 

66 

67 p = self.cfg('isAuto') 

68 if p is not None: 

69 self.isAuto = p 

70 else: 

71 self.isAuto = col and col.isAutoincrement 

72 

73 p = self.cfg('isUnique') 

74 if p is not None: 

75 self.isUnique = p 

76 else: 

77 self.isUnique = False 

78 

79 def configure_values(self): 

80 p = self.cfg('values') 

81 if p: 

82 self.values = self.create_children(gws.ext.object.modelValue, p) 

83 return True 

84 

85 def configure_validators(self): 

86 vd_not_empty = None 

87 vd_format = None 

88 

89 for p in self.cfg('validators', default=[]): 

90 vd = self.create_validator(p) 

91 if vd.extType == 'notEmpty': 

92 vd_not_empty = vd 

93 elif vd.extType == 'format': 

94 vd_format = vd 

95 else: 

96 self.validators.append(vd) 

97 

98 self.notEmptyValidator = vd_not_empty or self.root.create_shared( 

99 gws.ext.object.modelValidator, 

100 type='notEmpty', 

101 uid='gws.base.model.field.default_validator_notEmpty', 

102 forCreate=True, 

103 forUpdate=True, 

104 ) 

105 

106 self.formatValidator = vd_format or self.root.create_shared( 

107 gws.ext.object.modelValidator, 

108 type='format', 

109 uid='gws.base.model.field.default_validator_format', 

110 forCreate=True, 

111 forUpdate=True, 

112 ) 

113 

114 return True 

115 

116 def create_validator(self, cfg): 

117 return self.create_child(gws.ext.object.modelValidator, cfg) 

118 

119 def configure_widget(self): 

120 p = self.cfg('widget') 

121 if p: 

122 self.widget = self.create_child(gws.ext.object.modelWidget, p) 

123 return True 

124 

125 ## 

126 

127 def props(self, user): 

128 wp = None 

129 if self.widget: 

130 wp = gws.props_of(self.widget, user, self) 

131 if not user.can_write(self): 

132 wp.readOnly = True 

133 

134 return Props( 

135 attributeType=self.attributeType, 

136 name=self.name, 

137 title=self.title, 

138 type=self.extType, 

139 widget=wp, 

140 uid=self.uid, 

141 relatedModelUids=[ 

142 m.uid 

143 for m in self.related_models() 

144 if user.can_read(m) 

145 ], 

146 ) 

147 

148 ## 

149 

150 def do_validate(self, feature, mc): 

151 

152 # apply the 'notEmpty' validator and exit immediately if it fails 

153 # (no error message if field is not required) 

154 

155 ok = self.notEmptyValidator.validate(self, feature, mc) 

156 if not ok: 

157 if self.isRequired: 

158 feature.errors.append(gws.ModelValidationError( 

159 fieldName=self.name, 

160 message=self.notEmptyValidator.message, 

161 )) 

162 return 

163 

164 # apply the 'format' validator 

165 

166 ok = self.formatValidator.validate(self, feature, mc) 

167 if not ok: 

168 feature.errors.append(gws.ModelValidationError( 

169 fieldName=self.name, 

170 message=self.formatValidator.message, 

171 )) 

172 return 

173 

174 # apply others 

175 

176 for vd in self.validators: 

177 if mc.op in vd.ops: 

178 ok = vd.validate(self, feature, mc) 

179 if not ok: 

180 feature.errors.append(gws.ModelValidationError( 

181 fieldName=self.name, 

182 message=vd.message, 

183 )) 

184 return 

185 

186 def related_models(self): 

187 return [] 

188 

189 def find_relatable_features(self, search, mc): 

190 return [] 

191 

192 def raw_to_python(self, feature, value, mc): 

193 return value 

194 

195 def prop_to_python(self, feature, value, mc): 

196 return value 

197 

198 def python_to_raw(self, feature, value, mc): 

199 return value 

200 

201 def python_to_prop(self, feature, value, mc): 

202 return value 

203 

204 ## 

205 

206 def describe(self): 

207 desc = self.model.describe() 

208 if desc: 

209 return desc.columnMap.get(self.name)