[22] | 1 | from Globals import InitializeClass |
---|
| 2 | from Products.CPSSchemas.Widget import CPSWidgetType |
---|
| 3 | from Products.CPSSchemas.BasicWidgets import CPSStringWidget, CPSEmailWidget |
---|
| 4 | from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget |
---|
| 5 | from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry |
---|
| 6 | from DateTime.DateTime import DateTime |
---|
| 7 | from AccessControl import getSecurityManager |
---|
| 8 | |
---|
| 9 | from re import compile |
---|
| 10 | |
---|
| 11 | from zLOG import LOG, DEBUG |
---|
| 12 | |
---|
| 13 | class StudentIdWidget(CPSStringWidget): |
---|
| 14 | """ StudentId Widget""" |
---|
| 15 | meta_type = "StudentId Widget" |
---|
| 16 | |
---|
| 17 | def validate(self, datastructure, **kw): |
---|
| 18 | """Validate datastructure and update datamodel.""" |
---|
| 19 | |
---|
| 20 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 21 | if not valid: |
---|
| 22 | return 0 |
---|
| 23 | else: |
---|
| 24 | widget_id = self.getWidgetId() |
---|
| 25 | value = datastructure[widget_id] |
---|
| 26 | err = 0 |
---|
| 27 | if not (len(value) == 8 and value[:6].isdigit() and value[6:].isalpha()): |
---|
| 28 | err = 'invalid Student Id' |
---|
| 29 | |
---|
| 30 | if err: |
---|
| 31 | datastructure.setError(widget_id, err) |
---|
| 32 | else: |
---|
| 33 | datamodel = datastructure.getDataModel() |
---|
| 34 | datamodel[self.fields[0]] = value |
---|
| 35 | |
---|
| 36 | return not err |
---|
| 37 | |
---|
| 38 | InitializeClass(StudentIdWidget) |
---|
| 39 | |
---|
| 40 | class StudentIdWidgetType(CPSWidgetType): |
---|
| 41 | """Student Id String widget type.""" |
---|
| 42 | meta_type = "StudentId Widget Type" |
---|
| 43 | cls = StudentIdWidget |
---|
| 44 | |
---|
| 45 | class ScratchcardPinWidget(CPSStringWidget): |
---|
| 46 | """ ScratchcardPin Widget""" |
---|
| 47 | meta_type = "ScratchcardPin Widget" |
---|
| 48 | valid_pins = ['123456', |
---|
| 49 | '234567', |
---|
| 50 | '345678', |
---|
| 51 | '456789', |
---|
| 52 | ] |
---|
| 53 | |
---|
| 54 | def validate(self, datastructure, **kw): |
---|
| 55 | """Validate datastructure and update datamodel.""" |
---|
| 56 | |
---|
| 57 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 58 | if not valid: |
---|
| 59 | return 0 |
---|
| 60 | else: |
---|
| 61 | widget_id = self.getWidgetId() |
---|
| 62 | value = datastructure[widget_id] |
---|
| 63 | err = 0 |
---|
| 64 | if not value in valid_pins: |
---|
| 65 | err = 'invalid Pin' |
---|
| 66 | if err: |
---|
| 67 | datastructure.setError(widget_id, err) |
---|
| 68 | else: |
---|
| 69 | datamodel = datastructure.getDataModel() |
---|
| 70 | datamodel[self.fields[0]] = value |
---|
| 71 | return not err |
---|
| 72 | |
---|
| 73 | InitializeClass(ScratchcardPinWidget) |
---|
| 74 | |
---|
| 75 | class ScratchcardPinWidgetType(CPSWidgetType): |
---|
| 76 | """Student Id String widget type.""" |
---|
| 77 | meta_type = "Scratchcard Pin Widget Type" |
---|
| 78 | cls = ScratchcardPinWidget |
---|
| 79 | ########### |
---|
| 80 | |
---|
| 81 | WidgetTypeRegistry.register(StudentIdWidgetType) |
---|
| 82 | WidgetTypeRegistry.register(ScratchcardPinWidgetType) |
---|
| 83 | |
---|
| 84 | ##class MyProjectWeekDateTimeWidget(CPSDateTimeWidget): |
---|
| 85 | ## """ """ |
---|
| 86 | ## meta_type = "MyProject Week Date Time Widget" |
---|
| 87 | ## |
---|
| 88 | ## def validate(self, datastructure, **kw): |
---|
| 89 | ## """Validate datastructure and update datamodel.""" |
---|
| 90 | ## |
---|
| 91 | ## valid = CPSDateTimeWidget.validate(self, datastructure, **kw) |
---|
| 92 | ## if not valid: |
---|
| 93 | ## return 0 |
---|
| 94 | ## else: |
---|
| 95 | ## widget_id = self.getWidgetId() |
---|
| 96 | ## value = datastructure[widget_id] |
---|
| 97 | ## err = 0 |
---|
| 98 | ## if value.dow() in (6, 0): |
---|
| 99 | ## err = 'Cette date ne correspond pas à un jour de semaine' |
---|
| 100 | ## |
---|
| 101 | ## if err: |
---|
| 102 | ## datastructure.setError(widget_id, err) |
---|
| 103 | ## |
---|
| 104 | ## return not err |
---|
| 105 | ## |
---|
| 106 | ##InitializeClass(MyProjectWeekDateTimeWidget) |
---|
| 107 | |
---|
| 108 | ##class MyProjectWeekDateTimeWidgetType(CPSWidgetType): |
---|
| 109 | ## """MyProject Week DateTime widget type.""" |
---|
| 110 | ## meta_type = "MyProject Week Date Time Widget Type" |
---|
| 111 | ## cls = MyProjectWeekDateTimeWidget |
---|
| 112 | ## |
---|
| 113 | ##InitializeClass(MyProjectWeekDateTimeWidgetType) |
---|
| 114 | ##class MyProjectRestrictedStringWidget(CPSStringWidget): |
---|
| 115 | ## """ """ |
---|
| 116 | ## meta_type = "MyProject Restricted String Widget" |
---|
| 117 | ## |
---|
| 118 | ## def validate(self, datastructure, **kw): |
---|
| 119 | ## """Validate datastructure and update datamodel.""" |
---|
| 120 | ## |
---|
| 121 | ## valid = CPSStringWidget.validate(self, datastructure, **kw) |
---|
| 122 | ## if not valid: |
---|
| 123 | ## return 0 |
---|
| 124 | ## else: |
---|
| 125 | ## widget_id = self.getWidgetId() |
---|
| 126 | ## value = datastructure[widget_id] |
---|
| 127 | ## err = 0 |
---|
| 128 | ## if not (len(value) == 5 and value.isupper() and value.isalpha()): |
---|
| 129 | ## err = 'Cette chaîne ne correspond pas au format imposé' |
---|
| 130 | ## |
---|
| 131 | ## if err: |
---|
| 132 | ## datastructure.setError(widget_id, err) |
---|
| 133 | ## else: |
---|
| 134 | ## datamodel = datastructure.getDataModel() |
---|
| 135 | ## datamodel[self.fields[0]] = value |
---|
| 136 | ## |
---|
| 137 | ## return not err |
---|
| 138 | ## |
---|
| 139 | ##InitializeClass(MyProjectRestrictedStringWidget) |
---|
| 140 | ## |
---|
| 141 | ##class MyProjectRestrictedStringWidgetType(CPSWidgetType): |
---|
| 142 | ## """MyProject Restricted String widget type.""" |
---|
| 143 | ## meta_type = "MyProject Restricted String Widget Type" |
---|
| 144 | ## cls = MyProjectRestrictedStringWidget |
---|
| 145 | ## |
---|
| 146 | ##InitializeClass(MyProjectRestrictedStringWidgetType) |
---|
| 147 | ## |
---|
| 148 | ## |
---|
| 149 | ##class MyProjectRestrictedEmailWidget(CPSEmailWidget): |
---|
| 150 | ## """ """ |
---|
| 151 | ## meta_type = "MyProject Restricted Email Widget" |
---|
| 152 | ## |
---|
| 153 | ## email_myproject_pat = compile(r"^([-\w_+])+\.([-\w_+])+@myproject\.fr$") |
---|
| 154 | ## |
---|
| 155 | ## def validate(self, datastructure, **kw): |
---|
| 156 | ## """Validate datastructure and update datamodel.""" |
---|
| 157 | ## |
---|
| 158 | ## valid = CPSEmailWidget.validate(self, datastructure, **kw) |
---|
| 159 | ## if not valid: |
---|
| 160 | ## return 0 |
---|
| 161 | ## else: |
---|
| 162 | ## widget_id = self.getWidgetId() |
---|
| 163 | ## err, v = self._extractValue(datastructure[widget_id]) |
---|
| 164 | ## LOG('MyProject: MyProjectWidgets.py', DEBUG, "Valeur de v : %s" % v) |
---|
| 165 | ## if not err and kw.get('layout_mode') != 'search' and \ |
---|
| 166 | ## v and not self.email_myproject_pat.match(v): |
---|
| 167 | ## err = 'cpsschemas_err_email' |
---|
| 168 | ## |
---|
| 169 | ## if err: |
---|
| 170 | ## datastructure.setError(widget_id, err) |
---|
| 171 | ## else: |
---|
| 172 | ## datamodel = datastructure.getDataModel() |
---|
| 173 | ## datamodel[self.fields[0]] = v |
---|
| 174 | ## |
---|
| 175 | ## return not err |
---|
| 176 | ## |
---|
| 177 | ## def render(self, mode, datastructure, **kw): |
---|
| 178 | ## """Render in mode from datastructure.""" |
---|
| 179 | ## return CPSStringWidget.render(self, mode, datastructure, **kw) |
---|
| 180 | ## |
---|
| 181 | ##InitializeClass(MyProjectRestrictedEmailWidget) |
---|
| 182 | ## |
---|
| 183 | ##class MyProjectRestrictedEmailWidgetType(CPSWidgetType): |
---|
| 184 | ## """MyProject Restricted String widget type.""" |
---|
| 185 | ## meta_type = "MyProject Restricted Email Widget Type" |
---|
| 186 | ## cls = MyProjectRestrictedEmailWidget |
---|
| 187 | ## |
---|
| 188 | ##InitializeClass(MyProjectRestrictedEmailWidgetType) |
---|
| 189 | ## |
---|
| 190 | ##class MyProjectVisitTitleWidget(CPSStringWidget): |
---|
| 191 | ## """ """ |
---|
| 192 | ## meta_type = "MyProject Visit Title Widget" |
---|
| 193 | ## |
---|
| 194 | ## def validate(self, datastructure, **kw): |
---|
| 195 | ## """Validate datastructure and update datamodel.""" |
---|
| 196 | ## |
---|
| 197 | ## widget_id = self.getWidgetId() |
---|
| 198 | ## err = 0 |
---|
| 199 | ## curr_date = DateTime() |
---|
| 200 | ## date = "%d.%d.%d" % (curr_date.day(), curr_date.month(), curr_date.year()) |
---|
| 201 | ## user = getSecurityManager().getUser() |
---|
| 202 | ## value = 'Demande de visite du %s par %s' % (date, |
---|
| 203 | ## self.portal_directories.members.getEntry(user.getId()).get('fullname')) |
---|
| 204 | ## datamodel = datastructure.getDataModel() |
---|
| 205 | ## datamodel[self.fields[0]] = value |
---|
| 206 | ## |
---|
| 207 | ## return not err |
---|
| 208 | ## |
---|
| 209 | ## def render(self, mode, datastructure, **kw): |
---|
| 210 | ## return '' |
---|
| 211 | ## |
---|
| 212 | ##InitializeClass(MyProjectVisitTitleWidget) |
---|
| 213 | ## |
---|
| 214 | ##class MyProjectVisitTitleWidgetType(CPSWidgetType): |
---|
| 215 | ## """MyProject Visit Title widget type.""" |
---|
| 216 | ## meta_type = "MyProject Visit Title Widget Type" |
---|
| 217 | ## cls = MyProjectVisitTitleWidget |
---|
| 218 | ## |
---|
| 219 | ##InitializeClass(MyProjectVisitTitleWidgetType) |
---|
| 220 | |
---|
| 221 | ##################################################################################### |
---|
| 222 | |
---|
| 223 | ##WidgetTypeRegistry.register(MyProjectWeekDateTimeWidgetType) |
---|
| 224 | ##WidgetTypeRegistry.register(MyProjectRestrictedStringWidgetType) |
---|
| 225 | ##WidgetTypeRegistry.register(MyProjectRestrictedEmailWidgetType) |
---|
| 226 | ##WidgetTypeRegistry.register(MyProjectVisitTitleWidgetType) |
---|
| 227 | ## |
---|