| 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 AccessControl import getSecurityManager |
|---|
| 7 | |
|---|
| 8 | from re import compile |
|---|
| 9 | |
|---|
| 10 | from zLOG import LOG, DEBUG |
|---|
| 11 | |
|---|
| 12 | class MyProjectWeekDateTimeWidget(CPSDateTimeWidget): |
|---|
| 13 | """ """ |
|---|
| 14 | meta_type = "MyProject Week Date Time Widget" |
|---|
| 15 | |
|---|
| 16 | def validate(self, datastructure, **kw): |
|---|
| 17 | """Validate datastructure and update datamodel.""" |
|---|
| 18 | |
|---|
| 19 | valid = CPSDateTimeWidget.validate(self, datastructure, **kw) |
|---|
| 20 | if not valid: |
|---|
| 21 | return 0 |
|---|
| 22 | else: |
|---|
| 23 | widget_id = self.getWidgetId() |
|---|
| 24 | value = datastructure[widget_id] |
|---|
| 25 | err = 0 |
|---|
| 26 | if value.dow() in (6, 0): |
|---|
| 27 | err = 'Cette date ne correspond pas à un jour de semaine' |
|---|
| 28 | |
|---|
| 29 | if err: |
|---|
| 30 | datastructure.setError(widget_id, err) |
|---|
| 31 | |
|---|
| 32 | return not err |
|---|
| 33 | |
|---|
| 34 | InitializeClass(MyProjectWeekDateTimeWidget) |
|---|
| 35 | |
|---|
| 36 | class MyProjectWeekDateTimeWidgetType(CPSWidgetType): |
|---|
| 37 | """MyProject Week DateTime widget type.""" |
|---|
| 38 | meta_type = "MyProject Week Date Time Widget Type" |
|---|
| 39 | cls = MyProjectWeekDateTimeWidget |
|---|
| 40 | |
|---|
| 41 | InitializeClass(MyProjectWeekDateTimeWidgetType) |
|---|
| 42 | |
|---|
| 43 | class MyProjectRestrictedStringWidget(CPSStringWidget): |
|---|
| 44 | """ """ |
|---|
| 45 | meta_type = "MyProject Restricted String Widget" |
|---|
| 46 | |
|---|
| 47 | def validate(self, datastructure, **kw): |
|---|
| 48 | """Validate datastructure and update datamodel.""" |
|---|
| 49 | |
|---|
| 50 | valid = CPSStringWidget.validate(self, datastructure, **kw) |
|---|
| 51 | if not valid: |
|---|
| 52 | return 0 |
|---|
| 53 | else: |
|---|
| 54 | widget_id = self.getWidgetId() |
|---|
| 55 | value = datastructure[widget_id] |
|---|
| 56 | err = 0 |
|---|
| 57 | if not (len(value) == 5 and value.isupper() and value.isalpha()): |
|---|
| 58 | err = 'Cette chaîne ne correspond pas au format imposé' |
|---|
| 59 | |
|---|
| 60 | if err: |
|---|
| 61 | datastructure.setError(widget_id, err) |
|---|
| 62 | else: |
|---|
| 63 | datamodel = datastructure.getDataModel() |
|---|
| 64 | datamodel[self.fields[0]] = value |
|---|
| 65 | |
|---|
| 66 | return not err |
|---|
| 67 | |
|---|
| 68 | InitializeClass(MyProjectRestrictedStringWidget) |
|---|
| 69 | |
|---|
| 70 | class MyProjectRestrictedStringWidgetType(CPSWidgetType): |
|---|
| 71 | """MyProject Restricted String widget type.""" |
|---|
| 72 | meta_type = "MyProject Restricted String Widget Type" |
|---|
| 73 | cls = MyProjectRestrictedStringWidget |
|---|
| 74 | |
|---|
| 75 | InitializeClass(MyProjectRestrictedStringWidgetType) |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | class MyProjectRestrictedEmailWidget(CPSEmailWidget): |
|---|
| 79 | """ """ |
|---|
| 80 | meta_type = "MyProject Restricted Email Widget" |
|---|
| 81 | |
|---|
| 82 | email_myproject_pat = compile(r"^([-\w_+])+\.([-\w_+])+@myproject\.fr$") |
|---|
| 83 | |
|---|
| 84 | def validate(self, datastructure, **kw): |
|---|
| 85 | """Validate datastructure and update datamodel.""" |
|---|
| 86 | |
|---|
| 87 | valid = CPSEmailWidget.validate(self, datastructure, **kw) |
|---|
| 88 | if not valid: |
|---|
| 89 | return 0 |
|---|
| 90 | else: |
|---|
| 91 | widget_id = self.getWidgetId() |
|---|
| 92 | err, v = self._extractValue(datastructure[widget_id]) |
|---|
| 93 | LOG('MyProject: MyProjectWidgets.py', DEBUG, "Valeur de v : %s" % v) |
|---|
| 94 | if not err and kw.get('layout_mode') != 'search' and \ |
|---|
| 95 | v and not self.email_myproject_pat.match(v): |
|---|
| 96 | err = 'cpsschemas_err_email' |
|---|
| 97 | |
|---|
| 98 | if err: |
|---|
| 99 | datastructure.setError(widget_id, err) |
|---|
| 100 | else: |
|---|
| 101 | datamodel = datastructure.getDataModel() |
|---|
| 102 | datamodel[self.fields[0]] = v |
|---|
| 103 | |
|---|
| 104 | return not err |
|---|
| 105 | |
|---|
| 106 | def render(self, mode, datastructure, **kw): |
|---|
| 107 | """Render in mode from datastructure.""" |
|---|
| 108 | return CPSStringWidget.render(self, mode, datastructure, **kw) |
|---|
| 109 | |
|---|
| 110 | InitializeClass(MyProjectRestrictedEmailWidget) |
|---|
| 111 | |
|---|
| 112 | class MyProjectRestrictedEmailWidgetType(CPSWidgetType): |
|---|
| 113 | """MyProject Restricted String widget type.""" |
|---|
| 114 | meta_type = "MyProject Restricted Email Widget Type" |
|---|
| 115 | cls = MyProjectRestrictedEmailWidget |
|---|
| 116 | |
|---|
| 117 | InitializeClass(MyProjectRestrictedEmailWidgetType) |
|---|
| 118 | |
|---|
| 119 | class MyProjectVisitTitleWidget(CPSStringWidget): |
|---|
| 120 | """ """ |
|---|
| 121 | meta_type = "MyProject Visit Title Widget" |
|---|
| 122 | |
|---|
| 123 | def validate(self, datastructure, **kw): |
|---|
| 124 | """Validate datastructure and update datamodel.""" |
|---|
| 125 | |
|---|
| 126 | widget_id = self.getWidgetId() |
|---|
| 127 | err = 0 |
|---|
| 128 | curr_date = DateTime() |
|---|
| 129 | date = "%d.%d.%d" % (curr_date.day(), curr_date.month(), curr_date.year()) |
|---|
| 130 | user = getSecurityManager().getUser() |
|---|
| 131 | value = 'Demande de visite du %s par %s' % (date, |
|---|
| 132 | self.portal_directories.members.getEntry(user.getId()).get('fullname')) |
|---|
| 133 | datamodel = datastructure.getDataModel() |
|---|
| 134 | datamodel[self.fields[0]] = value |
|---|
| 135 | |
|---|
| 136 | return not err |
|---|
| 137 | |
|---|
| 138 | def render(self, mode, datastructure, **kw): |
|---|
| 139 | return '' |
|---|
| 140 | |
|---|
| 141 | InitializeClass(MyProjectVisitTitleWidget) |
|---|
| 142 | |
|---|
| 143 | class MyProjectVisitTitleWidgetType(CPSWidgetType): |
|---|
| 144 | """MyProject Visit Title widget type.""" |
|---|
| 145 | meta_type = "MyProject Visit Title Widget Type" |
|---|
| 146 | cls = MyProjectVisitTitleWidget |
|---|
| 147 | |
|---|
| 148 | InitializeClass(MyProjectVisitTitleWidgetType) |
|---|
| 149 | |
|---|
| 150 | ##################################################################################### |
|---|
| 151 | |
|---|
| 152 | WidgetTypeRegistry.register(MyProjectWeekDateTimeWidgetType) |
|---|
| 153 | WidgetTypeRegistry.register(MyProjectRestrictedStringWidgetType) |
|---|
| 154 | WidgetTypeRegistry.register(MyProjectRestrictedEmailWidgetType) |
|---|
| 155 | WidgetTypeRegistry.register(MyProjectVisitTitleWidgetType) |
|---|
| 156 | |
|---|