from Globals import InitializeClass from Products.CPSSchemas.Widget import CPSWidgetType from Products.CPSSchemas.BasicWidgets import CPSStringWidget, CPSEmailWidget from Products.CPSSchemas.ExtendedWidgets import CPSDateTimeWidget from Products.CPSSchemas.WidgetTypesTool import WidgetTypeRegistry from AccessControl import getSecurityManager from re import compile from zLOG import LOG, DEBUG class MyProjectWeekDateTimeWidget(CPSDateTimeWidget): """ """ meta_type = "MyProject Week Date Time Widget" def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSDateTimeWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id] err = 0 if value.dow() in (6, 0): err = 'Cette date ne correspond pas à un jour de semaine' if err: datastructure.setError(widget_id, err) return not err InitializeClass(MyProjectWeekDateTimeWidget) class MyProjectWeekDateTimeWidgetType(CPSWidgetType): """MyProject Week DateTime widget type.""" meta_type = "MyProject Week Date Time Widget Type" cls = MyProjectWeekDateTimeWidget InitializeClass(MyProjectWeekDateTimeWidgetType) class MyProjectRestrictedStringWidget(CPSStringWidget): """ """ meta_type = "MyProject Restricted String Widget" def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSStringWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() value = datastructure[widget_id] err = 0 if not (len(value) == 5 and value.isupper() and value.isalpha()): err = 'Cette chaîne ne correspond pas au format imposé' if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err InitializeClass(MyProjectRestrictedStringWidget) class MyProjectRestrictedStringWidgetType(CPSWidgetType): """MyProject Restricted String widget type.""" meta_type = "MyProject Restricted String Widget Type" cls = MyProjectRestrictedStringWidget InitializeClass(MyProjectRestrictedStringWidgetType) class MyProjectRestrictedEmailWidget(CPSEmailWidget): """ """ meta_type = "MyProject Restricted Email Widget" email_myproject_pat = compile(r"^([-\w_+])+\.([-\w_+])+@myproject\.fr$") def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" valid = CPSEmailWidget.validate(self, datastructure, **kw) if not valid: return 0 else: widget_id = self.getWidgetId() err, v = self._extractValue(datastructure[widget_id]) LOG('MyProject: MyProjectWidgets.py', DEBUG, "Valeur de v : %s" % v) if not err and kw.get('layout_mode') != 'search' and \ v and not self.email_myproject_pat.match(v): err = 'cpsschemas_err_email' if err: datastructure.setError(widget_id, err) else: datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = v return not err def render(self, mode, datastructure, **kw): """Render in mode from datastructure.""" return CPSStringWidget.render(self, mode, datastructure, **kw) InitializeClass(MyProjectRestrictedEmailWidget) class MyProjectRestrictedEmailWidgetType(CPSWidgetType): """MyProject Restricted String widget type.""" meta_type = "MyProject Restricted Email Widget Type" cls = MyProjectRestrictedEmailWidget InitializeClass(MyProjectRestrictedEmailWidgetType) class MyProjectVisitTitleWidget(CPSStringWidget): """ """ meta_type = "MyProject Visit Title Widget" def validate(self, datastructure, **kw): """Validate datastructure and update datamodel.""" widget_id = self.getWidgetId() err = 0 curr_date = DateTime() date = "%d.%d.%d" % (curr_date.day(), curr_date.month(), curr_date.year()) user = getSecurityManager().getUser() value = 'Demande de visite du %s par %s' % (date, self.portal_directories.members.getEntry(user.getId()).get('fullname')) datamodel = datastructure.getDataModel() datamodel[self.fields[0]] = value return not err def render(self, mode, datastructure, **kw): return '' InitializeClass(MyProjectVisitTitleWidget) class MyProjectVisitTitleWidgetType(CPSWidgetType): """MyProject Visit Title widget type.""" meta_type = "MyProject Visit Title Widget Type" cls = MyProjectVisitTitleWidget InitializeClass(MyProjectVisitTitleWidgetType) ##################################################################################### WidgetTypeRegistry.register(MyProjectWeekDateTimeWidgetType) WidgetTypeRegistry.register(MyProjectRestrictedStringWidgetType) WidgetTypeRegistry.register(MyProjectRestrictedEmailWidgetType) WidgetTypeRegistry.register(MyProjectVisitTitleWidgetType)