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